3 Reading and parse ini-files
5 Copyright (C) 2009-2025
6 Free Software Foundation, Inc.
9 Slava Zanko <slavazanko@gmail.com>, 2009.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "lib/global.h" /* <glib.h> */
34 #include "lib/fileloc.h"
35 #include "lib/util.h" /* exist_file() */
37 /*** global variables ****************************************************************************/
39 /*** file scope macro definitions ****************************************************************/
41 /*** file scope type declarations ****************************************************************/
43 /*** forward declarations (file scope functions) *************************************************/
45 /*** file scope variables ************************************************************************/
47 /* --------------------------------------------------------------------------------------------- */
48 /*** file scope functions ************************************************************************/
49 /* --------------------------------------------------------------------------------------------- */
52 mc_skin_get_list_from_dir (const gchar
*base_dir
, GPtrArray
*list
)
57 name
= g_build_filename (base_dir
, MC_SKINS_DIR
, (char *) NULL
);
58 dir
= g_dir_open (name
, 0, NULL
);
65 while ((cname
= g_dir_read_name (dir
)) != NULL
)
71 slen
= strlen (cname
);
72 sname
= g_strndup (cname
, slen
);
74 if (slen
> 4 && strcmp (sname
+ slen
- 4, ".ini") == 0)
75 sname
[slen
- 4] = '\0';
77 for (i
= 0; i
< list
->len
; i
++)
78 if (strcmp (sname
, g_ptr_array_index (list
, i
)) == 0)
84 g_ptr_array_add (list
, sname
);
91 /* --------------------------------------------------------------------------------------------- */
94 string_array_comparator (gconstpointer a
, gconstpointer b
)
96 return strcmp (*(char *const *) a
, *(char *const *) b
);
99 /* --------------------------------------------------------------------------------------------- */
102 mc_skin_ini_file_load_search_in_dir (mc_skin_t
*mc_skin
, const gchar
*base_dir
)
104 char *file_name
, *file_name2
;
106 file_name
= g_build_filename (base_dir
, MC_SKINS_DIR
, mc_skin
->name
, (char *) NULL
);
107 if (exist_file (file_name
))
109 mc_skin
->config
= mc_config_init (file_name
, TRUE
);
111 return (mc_skin
->config
!= NULL
);
115 file_name2
= g_strdup_printf ("%s.ini", mc_skin
->name
);
116 file_name
= g_build_filename (base_dir
, MC_SKINS_DIR
, file_name2
, (char *) NULL
);
119 if (exist_file (file_name
))
121 mc_skin
->config
= mc_config_init (file_name
, TRUE
);
123 return (mc_skin
->config
!= NULL
);
129 /* --------------------------------------------------------------------------------------------- */
130 /*** public functions ****************************************************************************/
131 /* --------------------------------------------------------------------------------------------- */
138 list
= g_ptr_array_new_with_free_func (g_free
);
139 mc_skin_get_list_from_dir (mc_config_get_data_path (), list
);
140 mc_skin_get_list_from_dir (mc_global
.sysconfig_dir
, list
);
141 mc_skin_get_list_from_dir (mc_global
.share_data_dir
, list
);
142 g_ptr_array_sort (list
, (GCompareFunc
) string_array_comparator
);
147 /* --------------------------------------------------------------------------------------------- */
150 mc_skin_ini_file_load (mc_skin_t
*mc_skin
)
154 file_name
= g_path_get_basename (mc_skin
->name
);
155 if (file_name
== NULL
)
158 if (strcmp (file_name
, mc_skin
->name
) != 0)
161 if (!g_path_is_absolute (mc_skin
->name
))
163 mc_skin
->config
= mc_config_init (mc_skin
->name
, TRUE
);
164 return (mc_skin
->config
!= NULL
);
168 /* ${XDG_DATA_HOME}/mc/skins/ */
169 if (mc_skin_ini_file_load_search_in_dir (mc_skin
, mc_config_get_data_path ()))
173 if (mc_skin_ini_file_load_search_in_dir (mc_skin
, mc_global
.sysconfig_dir
))
176 /* /usr/share/mc/skins/ */
177 return mc_skin_ini_file_load_search_in_dir (mc_skin
, mc_global
.share_data_dir
);
180 /* --------------------------------------------------------------------------------------------- */
183 mc_skin_ini_file_parse (mc_skin_t
*mc_skin
)
185 mc_skin
->description
=
186 mc_config_get_string (mc_skin
->config
, "skin", "description", "- no description -");
187 if (!mc_skin_color_parse_ini_file (mc_skin
))
190 mc_skin_lines_parse_ini_file (mc_skin
);
191 mc_skin
->have_256_colors
= mc_config_get_bool (mc_skin
->config
, "skin", "256colors", FALSE
);
192 mc_skin
->have_true_colors
= mc_config_get_bool (mc_skin
->config
, "skin", "truecolors", FALSE
);
197 /* --------------------------------------------------------------------------------------------- */
200 mc_skin_set_hardcoded_skin (mc_skin_t
*mc_skin
)
202 mc_skin
->config
= mc_config_init (NULL
, TRUE
);
204 mc_config_set_string (mc_skin
->config
, "skin", "description", "hardcoded skin");
206 mc_skin_hardcoded_ugly_lines (mc_skin
);
207 mc_skin_hardcoded_blackwhite_colors (mc_skin
);
210 /* --------------------------------------------------------------------------------------------- */