1 /*
2 * mape - C4 Landscape.txt editor
3 *
4 * Copyright (c) 2005-2009, Armin Burgmeier
5 *
6 * Distributed under the terms of the ISC license; see accompanying file
7 * "COPYING" for details.
8 *
9 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10 * See accompanying file "TRADEMARK" for details.
11 *
12 * To redistribute this file separately, substitute the full license texts
13 * for the above references.
14 */
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include "mape/configfile.h"
19 #include "mape/preferences.h"
20
mape_preferences_read_int(MapeConfigFile * file,const gchar * key,gint default_value)21 static gint mape_preferences_read_int(MapeConfigFile* file,
22 const gchar* key,
23 gint default_value)
24 {
25 MapeConfigFileEntry* entry;
26 entry = mape_config_file_get_entry_by_key(file, key );
27
28 if(entry == NULL) return default_value;
29 return strtol(mape_config_file_entry_get_value(entry), NULL, 0);
30 }
31
mape_preferences_read_ranged_int(MapeConfigFile * file,const gchar * key,gint lower_bound,gint upper_bound,gint default_value)32 static gint mape_preferences_read_ranged_int(MapeConfigFile* file,
33 const gchar* key,
34 gint lower_bound,
35 gint upper_bound,
36 gint default_value)
37 {
38 gint value;
39 value = mape_preferences_read_int(file, key, default_value);
40
41 if(value < lower_bound) value = lower_bound;
42 if(value > upper_bound) value = upper_bound;
43
44 return value;
45 }
46
mape_preferences_read_boolean(MapeConfigFile * file,const gchar * key,gboolean default_value)47 static gboolean mape_preferences_read_boolean(MapeConfigFile* file,
48 const gchar* key,
49 gboolean default_value)
50 {
51 const gchar* value;
52 MapeConfigFileEntry* entry;
53
54 entry = mape_config_file_get_entry_by_key(file, key);
55 if(entry == NULL) return default_value;
56
57 value = mape_config_file_entry_get_value(entry);
58 if(g_strcasecmp(value, "0") == 0 || g_strcasecmp(value, "off") == 0 ||
59 g_strcasecmp(value, "false") == 0) return FALSE;
60
61 return TRUE;
62 }
63
mape_preferences_write_int(MapeConfigFile * file,const gchar * key,gint value)64 static void mape_preferences_write_int(MapeConfigFile* file,
65 const gchar* key,
66 gint value)
67 {
68 gchar buf[16];
69 sprintf(buf, "%d", value);
70
71 mape_config_file_set_entry(file, key, buf);
72 }
73
mape_preferences_write_boolean(MapeConfigFile * file,const gchar * key,gboolean value)74 static void mape_preferences_write_boolean(MapeConfigFile* file,
75 const gchar* key,
76 gboolean value)
77 {
78 const gchar* text = "true";
79 if(value == FALSE) text = "false";
80
81 mape_config_file_set_entry(file, key, text);
82 }
83
mape_preferences_from_config(MapePreferences * preferences,MapeConfigFile * config)84 void mape_preferences_from_config(MapePreferences* preferences,
85 MapeConfigFile* config)
86 {
87 preferences->tab_width = mape_preferences_read_ranged_int(
88 config, "tab_width", 1, 8, 2);
89 preferences->tab_to_spaces = mape_preferences_read_boolean(
90 config, "tab_to_spaces", TRUE);
91 preferences->auto_indentation = mape_preferences_read_boolean(
92 config, "auto_indentation", TRUE);
93 preferences->text_wrapping = mape_preferences_read_boolean(
94 config, "text_wrapping", TRUE);
95 preferences->line_numbers = mape_preferences_read_boolean(
96 config, "line_numbers", TRUE);
97 preferences->highlight_line = mape_preferences_read_boolean(
98 config, "highlight_line", TRUE);
99 preferences->bracket_matching = mape_preferences_read_boolean(
100 config, "bracket_matching", TRUE);
101 preferences->fixed_seed = mape_preferences_read_boolean(
102 config, "fixed_seed", FALSE);
103 preferences->random_seed = mape_preferences_read_ranged_int(
104 config, "random_seed", 0, (1u << 31u) - 1, rand() );
105 preferences->map_width = mape_preferences_read_ranged_int(
106 config, "map_width", 50, 500, 150);
107 preferences->map_height = mape_preferences_read_ranged_int(
108 config, "map_height", 50, 500, 150);
109 preferences->map_zoom = mape_preferences_read_ranged_int(
110 config, "map_zoom", 20, 500, 100) / 100.;
111 }
112
mape_preferences_to_config(MapePreferences * preferences,MapeConfigFile * config)113 void mape_preferences_to_config(MapePreferences* preferences,
114 MapeConfigFile* config)
115 {
116 mape_preferences_write_int(
117 config, "tab_width", preferences->tab_width);
118 mape_preferences_write_boolean(
119 config, "tab_to_spaces", preferences->tab_to_spaces);
120 mape_preferences_write_boolean(
121 config, "auto_indentation", preferences->auto_indentation);
122 mape_preferences_write_boolean(
123 config, "text_wrapping", preferences->text_wrapping);
124 mape_preferences_write_boolean(
125 config, "line_numbers", preferences->line_numbers);
126 mape_preferences_write_boolean(
127 config, "highlight_line", preferences->highlight_line);
128 mape_preferences_write_boolean(
129 config, "bracket_matching", preferences->bracket_matching);
130 mape_preferences_write_boolean(
131 config, "fixed_seed", preferences->fixed_seed);
132 mape_preferences_write_int(
133 config, "random_seed", preferences->random_seed);
134 mape_preferences_write_int(
135 config, "map_width", preferences->map_width);
136 mape_preferences_write_int(
137 config, "map_height", preferences->map_height);
138 mape_preferences_write_int(
139 config, "map_zoom", (int)(preferences->map_zoom * 100 + 0.5));
140 }
141