1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Moved directory creating functions to common.h

Removed duplicate function
This commit is contained in:
James Booth 2013-02-02 22:06:19 +00:00
parent 9d34c41227
commit d86a774953
3 changed files with 21 additions and 32 deletions

View File

@ -79,6 +79,19 @@ create_dir(char *name)
e = mkdir(name, S_IRWXU); e = mkdir(name, S_IRWXU);
} }
void
mkdir_recursive(const char *dir)
{
int i;
for (i = 1; i <= strlen(dir); i++) {
if (dir[i] == '/' || dir[i] == '\0') {
gchar *next_dir = g_strndup(dir, i);
create_dir(next_dir);
g_free(next_dir);
}
}
}
char * char *
str_replace(const char *string, const char *substr, str_replace(const char *string, const char *substr,
const char *replacement) const char *replacement)

View File

@ -67,6 +67,7 @@ typedef enum {
gchar* p_utf8_substring(const gchar *str, glong start_pos, glong end_pos); gchar* p_utf8_substring(const gchar *str, glong start_pos, glong end_pos);
void p_slist_free_full(GSList *items, GDestroyNotify free_func); void p_slist_free_full(GSList *items, GDestroyNotify free_func);
void create_dir(char *name); void create_dir(char *name);
void mkdir_recursive(const char *dir);
char * str_replace(const char *string, const char *substr, char * str_replace(const char *string, const char *substr,
const char *replacement); const char *replacement);
int str_contains(char str[], int size, char ch); int str_contains(char str[], int size, char ch);

View File

@ -27,13 +27,13 @@
#include <glib.h> #include <glib.h>
#include "common.h"
static void _files_create_config_directory(void); static void _files_create_config_directory(void);
static void _files_create_data_directory(void); static void _files_create_data_directory(void);
static void _files_create_chatlog_directory(void); static void _files_create_chatlog_directory(void);
static void _files_create_log_directory(void); static void _files_create_log_directory(void);
static void _files_create_themes_directory(void); static void _files_create_themes_directory(void);
static void _create_dir(char *name);
static void _mkdir_recursive(const char *dir);
static gchar* _xdg_get_config_home(void); static gchar* _xdg_get_config_home(void);
static gchar* _xdg_get_data_home(void); static gchar* _xdg_get_data_home(void);
@ -118,7 +118,7 @@ _files_create_config_directory(void)
gchar *xdg_config = _xdg_get_config_home(); gchar *xdg_config = _xdg_get_config_home();
GString *prof_conf_dir = g_string_new(xdg_config); GString *prof_conf_dir = g_string_new(xdg_config);
g_string_append(prof_conf_dir, "/profanity"); g_string_append(prof_conf_dir, "/profanity");
_mkdir_recursive(prof_conf_dir->str); mkdir_recursive(prof_conf_dir->str);
g_free(xdg_config); g_free(xdg_config);
g_string_free(prof_conf_dir, TRUE); g_string_free(prof_conf_dir, TRUE);
} }
@ -129,7 +129,7 @@ _files_create_data_directory(void)
gchar *xdg_data = _xdg_get_data_home(); gchar *xdg_data = _xdg_get_data_home();
GString *prof_data_dir = g_string_new(xdg_data); GString *prof_data_dir = g_string_new(xdg_data);
g_string_append(prof_data_dir, "/profanity"); g_string_append(prof_data_dir, "/profanity");
_mkdir_recursive(prof_data_dir->str); mkdir_recursive(prof_data_dir->str);
g_free(xdg_data); g_free(xdg_data);
g_string_free(prof_data_dir, TRUE); g_string_free(prof_data_dir, TRUE);
} }
@ -140,7 +140,7 @@ _files_create_chatlog_directory(void)
gchar *xdg_data = _xdg_get_data_home(); gchar *xdg_data = _xdg_get_data_home();
GString *chatlogs_dir = g_string_new(xdg_data); GString *chatlogs_dir = g_string_new(xdg_data);
g_string_append(chatlogs_dir, "/profanity/chatlogs"); g_string_append(chatlogs_dir, "/profanity/chatlogs");
_mkdir_recursive(chatlogs_dir->str); mkdir_recursive(chatlogs_dir->str);
g_free(xdg_data); g_free(xdg_data);
g_string_free(chatlogs_dir, TRUE); g_string_free(chatlogs_dir, TRUE);
} }
@ -151,7 +151,7 @@ _files_create_log_directory(void)
gchar *xdg_data = _xdg_get_data_home(); gchar *xdg_data = _xdg_get_data_home();
GString *chatlogs_dir = g_string_new(xdg_data); GString *chatlogs_dir = g_string_new(xdg_data);
g_string_append(chatlogs_dir, "/profanity/logs"); g_string_append(chatlogs_dir, "/profanity/logs");
_mkdir_recursive(chatlogs_dir->str); mkdir_recursive(chatlogs_dir->str);
g_free(xdg_data); g_free(xdg_data);
g_string_free(chatlogs_dir, TRUE); g_string_free(chatlogs_dir, TRUE);
} }
@ -162,36 +162,11 @@ _files_create_themes_directory(void)
gchar *xdg_config = _xdg_get_config_home(); gchar *xdg_config = _xdg_get_config_home();
GString *themes_dir = g_string_new(xdg_config); GString *themes_dir = g_string_new(xdg_config);
g_string_append(themes_dir, "/profanity/themes"); g_string_append(themes_dir, "/profanity/themes");
_mkdir_recursive(themes_dir->str); mkdir_recursive(themes_dir->str);
g_free(xdg_config); g_free(xdg_config);
g_string_free(themes_dir, TRUE); g_string_free(themes_dir, TRUE);
} }
static void
_create_dir(char *name)
{
int e;
struct stat sb;
e = stat(name, &sb);
if (e != 0)
if (errno == ENOENT)
e = mkdir(name, S_IRWXU);
}
static void
_mkdir_recursive(const char *dir)
{
int i;
for (i = 1; i <= strlen(dir); i++) {
if (dir[i] == '/' || dir[i] == '\0') {
gchar *next_dir = g_strndup(dir, i);
_create_dir(next_dir);
g_free(next_dir);
}
}
}
static gchar * static gchar *
_xdg_get_config_home(void) _xdg_get_config_home(void)
{ {