-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
82 lines (64 loc) · 2.19 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// #############################################################################
#include "util.h"
// #############################################################################
gchar *mousepad_util_get_save_location (const gchar *relpath, gboolean create_parents)
{
gchar *filename, *dirname;
g_return_val_if_fail (g_get_user_config_dir () != NULL, NULL);
/* create the full filename */
filename = g_build_filename (g_get_user_config_dir (), relpath, NULL);
/* test if the file exists */
if (G_UNLIKELY (g_file_test (filename, G_FILE_TEST_EXISTS) == FALSE))
{
if (create_parents)
{
/* get the directory name */
dirname = g_path_get_dirname (filename);
/* make the directory with parents */
if (g_mkdir_with_parents (dirname, 0700) == -1)
{
/* show warning to the user */
g_critical (_("Unable to create base directory \"%s\". "
"Saving to file \"%s\" will be aborted."), dirname, filename);
/* don't return a filename, to avoid problems */
g_free (filename);
filename = NULL;
}
/* cleanup */
g_free (dirname);
}
else
{
/* cleanup */
g_free (filename);
filename = NULL;
}
}
return filename;
}
// #############################################################################
void mousepad_util_save_key_file (GKeyFile *keyfile, const gchar *filename)
{
gchar *contents;
gsize length;
GError *error = NULL;
/* get the contents of the key file */
contents = g_key_file_to_data (keyfile, &length, &error);
if (G_LIKELY (error == NULL))
{
/* write the contents to the file */
if (G_UNLIKELY (g_file_set_contents (filename, contents, length, &error) == FALSE))
goto print_error;
}
else
{
print_error:
/* print error */
g_critical (_("Failed to store the preferences to \"%s\": %s"), filename, error->message);
/* cleanup */
g_error_free (error);
}
/* cleanup */
g_free (contents);
}
// #############################################################################