OpenClonk
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

◆ mape_config_file_destroy()

void mape_config_file_destroy ( MapeConfigFile file)

Definition at line 56 of file configfile.c.

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

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

Referenced by mape_window_destroy().

Here is the caller graph for this function:

◆ mape_config_file_entry_get_key()

const gchar* mape_config_file_entry_get_key ( MapeConfigFileEntry entry)

Definition at line 169 of file configfile.c.

170 {
171  return entry->key;
172 }

References MapeConfigFileEntry_::key.

◆ mape_config_file_entry_get_value()

const gchar* mape_config_file_entry_get_value ( MapeConfigFileEntry entry)

Definition at line 174 of file configfile.c.

175 {
176  return entry->value;
177 }

References MapeConfigFileEntry_::value.

◆ mape_config_file_get_entry()

MapeConfigFileEntry* mape_config_file_get_entry ( MapeConfigFile file,
gsize  index 
)

Definition at line 125 of file configfile.c.

127 {
128  g_assert(index < file->entry_count);
129  return &file->entries[index];
130 }

References MapeConfigFile_::entries.

◆ mape_config_file_get_entry_by_key()

MapeConfigFileEntry* mape_config_file_get_entry_by_key ( MapeConfigFile file,
const gchar *  key 
)

Definition at line 132 of file configfile.c.

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 }

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

Referenced by mape_config_file_set_entry().

Here is the caller graph for this function:

◆ mape_config_file_get_entry_count()

gsize mape_config_file_get_entry_count ( MapeConfigFile file)

Definition at line 120 of file configfile.c.

121 {
122  return file->entry_count;
123 }

References MapeConfigFile_::entry_count.

◆ mape_config_file_new()

MapeConfigFile* mape_config_file_new ( const gchar *  filename)

Definition at line 21 of file configfile.c.

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 }
void mape_config_file_set_entry(MapeConfigFile *file, const gchar *key, const gchar *value)
Definition: configfile.c:143

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

Referenced by mape_window_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mape_config_file_serialise()

gboolean mape_config_file_serialise ( MapeConfigFile file,
GError **  error 
)

Definition at line 70 of file configfile.c.

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 }

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

Referenced by mape_window_destroy().

Here is the caller graph for this function:

◆ mape_config_file_set_entry()

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

Definition at line 143 of file configfile.c.

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 }
MapeConfigFileEntry * mape_config_file_get_entry_by_key(MapeConfigFile *file, const gchar *key)
Definition: configfile.c:132
Definition: configfile.h:22

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().

Here is the call graph for this function:
Here is the caller graph for this function: