A bit more memory grind...

This commit is contained in:
2025-03-01 03:27:46 -05:00
parent 396b5fc2a6
commit 982684e36c
6 changed files with 55 additions and 36 deletions

View File

@ -275,13 +275,15 @@ const char *a_Dialog_select_file(const char *title,
*
* @return pointer to chosen filename, or NULL on Cancel.
*/
char *a_Dialog_open_file(const char *title,
const char *pattern, const char *fname)
std::optional< std::string >
a_Dialog_open_file(const std::string &title,
const std::optional< std::string > &pattern, const std::string &fname)
{
const char *fc_name;
fc_name = fl_file_chooser(title, pattern, fname);
return (fc_name) ? a_Misc_escape_chars(fc_name, "% #") : NULL;
const char *const pat= pattern.has_value() ? pattern.value().c_str() : nullptr;
fc_name = fl_file_chooser(title.c_str(), pat, fname.c_str());
if( not fc_name ) return std::nullopt;
return a_Misc_escape_chars(fc_name, "% #");
}
/**

View File

@ -18,8 +18,14 @@ const char *a_Dialog_save_file(const char *title,
const char *pattern, const char *fname);
const char *a_Dialog_select_file(const char *title,
const char *pattern, const char *fname);
char *a_Dialog_open_file(const char *title,
const char *pattern, const char *fname);
#ifdef __cplusplus
extern "C++" {
#include <optional>
#include <string>
std::optional< std::string > a_Dialog_open_file(const std::string &title,
const std::optional< std::string > &pattern, const std::string &fname);
}
#endif
void a_Dialog_text_window(const char *title, const char *txt);
#ifdef __cplusplus

View File

@ -370,7 +370,8 @@ static void setColors()
*/
static DilloUrl *makeStartUrl(char *str, bool local)
{
char *url_str, *p;
std::string url_str;
char *p;
DilloUrl *start_url;
/* Relative path to a local file? */
@ -380,14 +381,15 @@ static DilloUrl *makeStartUrl(char *str, bool local)
if (access(p, F_OK) == 0) {
/* absolute path may have non-URL characters */
url_str = a_Misc_escape_chars(p, "% #");
start_url = a_Url_new(url_str + 1, "file:/");
start_url = a_Url_new(url_str.c_str() + 1, "file:/");
} else {
/* Not a file, filter URL string */
url_str = a_Url_string_strip_delimiters(str);
start_url = a_Url_new(url_str, NULL);
char *const tmp= a_Url_string_strip_delimiters(str);
url_str = tmp;
dFree( tmp );
start_url = a_Url_new(url_str.c_str(), NULL);
}
dFree(p);
dFree(url_str);
if (local)
a_Url_set_flags(start_url, URL_FLAGS(start_url) | URL_SpamSafe);

View File

@ -23,27 +23,33 @@
* Escape characters as %XX sequences.
* Return value: New string.
*/
char *a_Misc_escape_chars(const char *str, const char *esc_set)
std::string
a_Misc_escape_chars( const std::string_view str,
const std::string_view esc_set )
{
static const char hex[] = "0123456789ABCDEF";
char *p = NULL;
Dstr *dstr;
int i;
using namespace std::literals::string_view_literals;
dstr = dStr_sized_new(64);
for (i = 0; str[i]; ++i) {
if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) {
dStr_append_c(dstr, '%');
dStr_append_c(dstr, hex[(str[i] >> 4) & 15]);
dStr_append_c(dstr, hex[str[i] & 15]);
} else {
dStr_append_c(dstr, str[i]);
}
}
p = dstr->str;
dStr_free(dstr, FALSE);
const auto hex= "0123456789ABCDEF"sv;
return p;
std::string rv;
rv.reserve( 64 );
for( const char ch: str )
{
if( ch <= 0x1F or ch == 0x7F or esc_set.contains( ch ) )
{
rv+= '%';
rv+= hex[ ( ch >> 4 ) & 0xF ];
rv+= hex[ ch & 0xF ];
}
else
{
rv+= ch;
}
}
return rv;
}
#define TAB_SIZE 8

View File

@ -4,7 +4,12 @@
#define d_isascii(c) (((c) & ~0x7f) == 0)
char *a_Misc_escape_chars(const char *str, const char *esc_set);
#ifdef __cplusplus
#include <string>
#include <string_view>
std::string a_Misc_escape_chars(std::string_view str, std::string_view esc_set);
#endif
int a_Misc_expand_tabs(char **start, char *end, char *buf, int buflen);
int a_Misc_get_content_type_from_data(void *Data, size_t Size,const char **PT);
int a_Misc_content_type_check(const char *EntryType, const char *DetectedType);

View File

@ -1152,16 +1152,14 @@ void a_UIcmd_tools(void *vbw, int x, int y)
*/
void a_UIcmd_open_file(void *vbw)
{
char *name;
DilloUrl *url;
name = a_Dialog_open_file("Flenser: Open File", NULL, "");
auto name = a_Dialog_open_file("Flenser: Open File", std::nullopt, "");
if (name) {
url = a_Url_new(name, "file:");
if (name.has_value()) {
url = a_Url_new(name.value().c_str(), "file:");
a_UIcmd_open_url((BrowserWindow*)vbw, url);
a_Url_free(url);
dFree(name);
}
}