OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
configfile.c File Reference
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "mape/configfile.h"
Include dependency graph for configfile.c:

Go to the source code of this file.

Functions

MapeConfigFilemape_config_file_new (const gchar *filename)
 
void mape_config_file_destroy (MapeConfigFile *file)
 
gboolean mape_config_file_serialise (MapeConfigFile *file, GError **error)
 
gsize mape_config_file_get_entry_count (MapeConfigFile *file)
 
MapeConfigFileEntrymape_config_file_get_entry (MapeConfigFile *file, gsize index)
 
MapeConfigFileEntrymape_config_file_get_entry_by_key (MapeConfigFile *file, const gchar *key)
 
void mape_config_file_set_entry (MapeConfigFile *file, const gchar *key, const gchar *value)
 
const gchar * mape_config_file_entry_get_key (MapeConfigFileEntry *entry)
 
const gchar * mape_config_file_entry_get_value (MapeConfigFileEntry *entry)
 

Function Documentation

void mape_config_file_destroy ( MapeConfigFile file)

Definition at line 56 of file configfile.c.

References MapeConfigFile_::entries, MapeConfigFile_::entry_count, MapeConfigFile_::file_path, MapeConfigFileEntry_::key, and MapeConfigFileEntry_::value.

Referenced by mape_window_destroy(), and mape_window_new().

57 {
58  gsize i;
59  for(i = 0; i < file->entry_count; ++ i)
60  {
61  g_free(file->entries[i].key);
62  g_free(file->entries[i].value);
63  }
64 
65  g_free(file->entries);
66  g_free(file->file_path);
67  free(file);
68 }
gsize entry_count
Definition: configfile.h:31
MapeConfigFileEntry * entries
Definition: configfile.h:30
gchar * file_path
Definition: configfile.h:28

Here is the caller graph for this function:

const gchar* mape_config_file_entry_get_key ( MapeConfigFileEntry entry)

Definition at line 169 of file configfile.c.

References MapeConfigFileEntry_::key.

170 {
171  return entry->key;
172 }
const gchar* mape_config_file_entry_get_value ( MapeConfigFileEntry entry)

Definition at line 174 of file configfile.c.

References MapeConfigFileEntry_::value.

Referenced by mape_window_new().

175 {
176  return entry->value;
177 }

Here is the caller graph for this function:

MapeConfigFileEntry* mape_config_file_get_entry ( MapeConfigFile file,
gsize  index 
)

Definition at line 125 of file configfile.c.

References MapeConfigFile_::entries.

127 {
128  g_assert(index < file->entry_count);
129  return &file->entries[index];
130 }
MapeConfigFileEntry * entries
Definition: configfile.h:30
MapeConfigFileEntry* mape_config_file_get_entry_by_key ( MapeConfigFile file,
const gchar *  key 
)

Definition at line 132 of file configfile.c.

References MapeConfigFile_::entries, MapeConfigFile_::entry_count, and MapeConfigFileEntry_::key.

Referenced by mape_config_file_set_entry(), and mape_window_new().

134 {
135  gsize i;
136  for(i = 0; i < file->entry_count; ++ i)
137  if(g_strcasecmp(file->entries[i].key, key) == 0)
138  return &file->entries[i];
139 
140  return NULL;
141 }
gsize entry_count
Definition: configfile.h:31
MapeConfigFileEntry * entries
Definition: configfile.h:30

Here is the caller graph for this function:

gsize mape_config_file_get_entry_count ( MapeConfigFile file)

Definition at line 120 of file configfile.c.

References MapeConfigFile_::entry_count.

121 {
122  return file->entry_count;
123 }
gsize entry_count
Definition: configfile.h:31
MapeConfigFile* mape_config_file_new ( const gchar *  filename)

Definition at line 21 of file configfile.c.

References MapeConfigFile_::entries, MapeConfigFile_::entry_count, MapeConfigFile_::file_path, and mape_config_file_set_entry().

Referenced by mape_window_new().

22 {
23  /* If filename does not exist, we return an empty config file. */
24  MapeConfigFile* file;
25  gchar* contents;
26  gchar** lines;
27  gchar** cur_line;
28  gchar* sep;
29  gsize length;
30 
31  file = malloc(sizeof(MapeConfigFile) );
32  file->file_path = g_strdup(filename);
33  file->entries = NULL;
34  file->entry_count = 0;
35 
36  if(g_file_get_contents(filename, &contents, &length, NULL) == FALSE)
37  return file;
38 
39  lines = g_strsplit(contents, "\n", 0);
40  g_free(contents);
41 
42  for(cur_line = lines; *cur_line != NULL; ++ cur_line)
43  {
44  sep = strchr(*cur_line, '=');
45  if(sep == NULL) continue;
46 
47  *sep = '\0';
48  mape_config_file_set_entry(file, *cur_line, sep + 1);
49  *sep = '=';
50  }
51 
52  g_strfreev(lines);
53  return file;
54 }
gsize entry_count
Definition: configfile.h:31
MapeConfigFileEntry * entries
Definition: configfile.h:30
void mape_config_file_set_entry(MapeConfigFile *file, const gchar *key, const gchar *value)
Definition: configfile.c:143
gchar * file_path
Definition: configfile.h:28

Here is the call graph for this function:

Here is the caller graph for this function:

gboolean mape_config_file_serialise ( MapeConfigFile file,
GError **  error 
)

Definition at line 70 of file configfile.c.

References MapeConfigFile_::entries, MapeConfigFile_::entry_count, MapeConfigFile_::file_path, MapeConfigFileEntry_::key, and MapeConfigFileEntry_::value.

Referenced by mape_window_destroy().

72 {
73  gchar* dir;
74  gchar* content;
75  gchar* temp;
76  gsize i;
77 
78  int dir_result;
79  gboolean cont_result;
80 
81  dir = g_dirname(file->file_path);
82  dir_result = g_mkdir_with_parents(dir, 0755);
83 
84  g_free(dir);
85  if(dir_result == -1)
86  {
87  g_set_error(
88  error,
89  g_quark_from_static_string("MAPE_CONFIG_FILE_ERROR"),
90  errno,
91  "%s",
92  g_strerror(errno)
93  );
94 
95  return FALSE;
96  }
97 
98  content = g_strdup("");
99  for(i = 0; i < file->entry_count; ++ i)
100  {
101  temp = g_strconcat(
102  content,
103  file->entries[i].key,
104  "=",
105  file->entries[i].value,
106  "\n",
107  NULL
108  );
109 
110  g_free(content);
111  content = temp;
112  }
113 
114  cont_result = g_file_set_contents(file->file_path, content, -1, error);
115  g_free(content);
116 
117  return(cont_result);
118 }
gsize entry_count
Definition: configfile.h:31
MapeConfigFileEntry * entries
Definition: configfile.h:30
gchar * file_path
Definition: configfile.h:28

Here is the caller graph for this function:

void mape_config_file_set_entry ( MapeConfigFile file,
const gchar *  key,
const gchar *  value 
)

Definition at line 143 of file configfile.c.

References MapeConfigFile_::entries, MapeConfigFile_::entry_count, MapeConfigFileEntry_::key, mape_config_file_get_entry_by_key(), and MapeConfigFileEntry_::value.

Referenced by mape_config_file_new(), and mape_window_new().

146 {
147  MapeConfigFileEntry* entry;
148  entry = mape_config_file_get_entry_by_key(file, key);
149 
150  if(entry != NULL)
151  {
152  g_free(entry->value);
153  entry->value = g_strdup(value);
154  }
155  else
156  {
157  ++ file->entry_count;
158  file->entries = realloc(
159  file->entries,
160  sizeof(MapeConfigFileEntry) * file->entry_count
161  );
162 
163  entry = &file->entries[file->entry_count - 1];
164  entry->key = g_strdup(key);
165  entry->value = g_strdup(value);
166  }
167 }
gsize entry_count
Definition: configfile.h:31
MapeConfigFileEntry * entries
Definition: configfile.h:30
MapeConfigFileEntry * mape_config_file_get_entry_by_key(MapeConfigFile *file, const gchar *key)
Definition: configfile.c:132
Definition: configfile.h:22

Here is the call graph for this function:

Here is the caller graph for this function: