Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/fileloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#define MC_LOCAL_MENU ".mc.menu"
#define MC_HINT "hints" PATH_SEP_STR "mc.hint"
#define MC_HELP "help" PATH_SEP_STR "mc.hlp"
#define GLOBAL_KEYDEF_FILE "mc.keydef"
#define GLOBAL_KEYMAP_FILE "mc.keymap"
#define CHARSETS_LIST "mc.charsets"
#define MC_MACRO_FILE "mc.macros"
Expand Down
7 changes: 7 additions & 0 deletions lib/mcconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ gboolean *mc_config_get_bool_list (mc_config_t *mc_config, const gchar *group, c
gsize *length);
int *mc_config_get_int_list (mc_config_t *mc_config, const gchar *group, const gchar *param,
gsize *length);
GPtrArray *mc_config_get_escape_sequence_list (mc_config_t *mc_config, const gchar *group,
const gchar *param);

/* mcconfig/set.c: */

Expand All @@ -83,6 +85,8 @@ void mc_config_set_bool_list (mc_config_t *mc_config, const gchar *group, const
gboolean value[], gsize length);
void mc_config_set_int_list (mc_config_t *mc_config, const gchar *group, const gchar *param,
int value[], gsize length);
void mc_config_set_escape_sequence_list (mc_config_t *mc_config, const gchar *group,
const gchar *param, const GPtrArray *value);

/* mcconfig/paths.c: */

Expand All @@ -96,6 +100,9 @@ const char *mc_config_get_path (void);
char *mc_config_get_full_path (const char *config_name);
vfs_path_t *mc_config_get_full_vpath (const char *config_name);

char *mc_config_get_full_config_name (const char *subdir, const char *config_file_name,
const char *suffix);

/* mcconfig/history.h */

/* read history to the mc_config, but don't save config to file */
Expand Down
48 changes: 48 additions & 0 deletions lib/mcconfig/get.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "lib/global.h"
#include "lib/strutil.h"
#include "lib/terminal.h" // encode_controls(), decode_controls()

#include "lib/mcconfig.h"

Expand Down Expand Up @@ -254,3 +255,50 @@ mc_config_get_int_list (mc_config_t *mc_config, const gchar *group, const gchar
}

/* --------------------------------------------------------------------------------------------- */

/*
* An interface matching the other mc_config_get_*_list methods.
*
* Gets space-separated values, each parsed according to decode_controls().
*
* Primarily useful for having escape sequences in easy-to-read format, including \e for the escape
* character without having to double-escape it, and without having to escape semicolons.
*
* @return array of GString
*/
GPtrArray *
mc_config_get_escape_sequence_list (mc_config_t *mc_config, const gchar *group, const gchar *param)
{
const gchar *separator_str = " ";
gchar *str;
gchar **encoded_list;
GPtrArray *decoded_list;

if (mc_config == NULL || group == NULL || param == NULL)
return NULL;

str = g_key_file_get_value (mc_config->handle, group, param, NULL);
if (str == NULL)
return NULL;

encoded_list = g_strsplit (str, separator_str, -1);
g_free (str);

decoded_list = g_ptr_array_new ();

// Decode each element. Skip empty strings or where decoding fails.
for (gchar **p = encoded_list; *p != NULL; p++)
{
GString *decoded;

decoded = decode_controls (*p);
if (decoded != NULL)
g_ptr_array_add (decoded_list, decoded);
}

g_strfreev (encoded_list);

return decoded_list;
}

/* --------------------------------------------------------------------------------------------- */
70 changes: 70 additions & 0 deletions lib/mcconfig/paths.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static const struct
{ &mc_config_str, MC_CONFIG_FILE },
{ &mc_config_str, MC_FHL_INI_FILE },
{ &mc_config_str, MC_HOTLIST_FILE },
{ &mc_config_str, GLOBAL_KEYDEF_FILE },
{ &mc_config_str, GLOBAL_KEYMAP_FILE },
{ &mc_config_str, MC_USERMENU_FILE },
{ &mc_config_str, EDIT_HOME_MENU },
Expand Down Expand Up @@ -308,3 +309,72 @@ mc_config_get_full_vpath (const char *config_name)
}

/* --------------------------------------------------------------------------------------------- */

/**
* Get name of config file.
*
* @param subdir If not NULL, config is searched in specified subdir.
* @param config_file_name If relative, file if searched in standard paths.
* @param suffix If file name does not end in this suffix then append it.
*
* @return newly allocated string with config name or NULL if file is not found.
*/

char *
mc_config_get_full_config_name (const char *subdir, const char *config_file_name,
const char *suffix)
{
char *lc_basename, *ret;
char *file_name;

if (config_file_name == NULL)
return NULL;

// check for suffix
if (suffix != NULL && g_str_has_suffix (config_file_name, suffix))
file_name = g_strdup (config_file_name);
else
file_name = g_strconcat (config_file_name, suffix, (char *) NULL);

canonicalize_pathname (file_name);

if (g_path_is_absolute (file_name))
return file_name;

lc_basename = g_path_get_basename (file_name);
g_free (file_name);

if (lc_basename == NULL)
return NULL;

if (subdir != NULL)
ret = g_build_filename (mc_config_get_path (), subdir, lc_basename, (char *) NULL);
else
ret = g_build_filename (mc_config_get_path (), lc_basename, (char *) NULL);

if (exist_file (ret))
{
g_free (lc_basename);
canonicalize_pathname (ret);
return ret;
}
g_free (ret);

if (subdir != NULL)
ret = g_build_filename (mc_global.share_data_dir, subdir, lc_basename, (char *) NULL);
else
ret = g_build_filename (mc_global.share_data_dir, lc_basename, (char *) NULL);

g_free (lc_basename);

if (exist_file (ret))
{
canonicalize_pathname (ret);
return ret;
}

g_free (ret);
return NULL;
}

/* --------------------------------------------------------------------------------------------- */
46 changes: 46 additions & 0 deletions lib/mcconfig/set.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "lib/global.h"
#include "lib/strutil.h"
#include "lib/terminal.h" // encode_controls(), decode_controls()

#include "lib/mcconfig.h"

Expand Down Expand Up @@ -156,3 +157,48 @@ mc_config_set_int_list (mc_config_t *mc_config, const gchar *group, const gchar
}

/* --------------------------------------------------------------------------------------------- */

/*
* An interface matching the other mc_config_set_*_list methods.
*
* Sets space-separated values, each stored according to encode_controls().
*
* Primarily useful for having escape sequences in easy-to-read format, including \e for the escape
* character without having to double-escape it, and without having to escape semicolons.
*/
void
mc_config_set_escape_sequence_list (mc_config_t *mc_config, const gchar *group, const gchar *param,
const GPtrArray *value)
{
const gchar separator = ' ';
GString *str;
gboolean add_separator = FALSE;

if (mc_config == NULL || group == NULL || param == NULL || value == NULL || value->len == 0)
return;

str = g_string_new ("");

for (guint i = 0; i < value->len; i++)
{
GString *p = (GString *) g_ptr_array_index (value, i);

if (p->len != 0)
{
GString *encoded;

encoded = encode_controls (p->str, p->len);
if (add_separator)
g_string_append_c (str, separator);
mc_g_string_concat (str, encoded);
g_string_free (encoded, TRUE);
add_separator = TRUE;
}
}

g_key_file_set_value (mc_config->handle, group, param, str->str);

g_string_free (str, TRUE);
}

/* --------------------------------------------------------------------------------------------- */
Loading