webperimental: killstack decides stack protects.
[freeciv.git] / common / server_settings.c
blobfc90838ea7253ca9a2565a1298c803225c427ba3
1 /***********************************************************************
2 Freeciv - Copyright (C) 2017 - Freeciv Development Team
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 /* utility */
19 #include "astring.h"
21 /* common */
22 #include "fc_interface.h"
24 #include "server_settings.h"
26 /***************************************************************************
27 Returns the server setting with the specified name.
28 ***************************************************************************/
29 server_setting_id server_setting_by_name(const char *name)
31 fc_assert_ret_val(fc_funcs, SERVER_SETTING_NONE);
32 fc_assert_ret_val(fc_funcs->server_setting_by_name, SERVER_SETTING_NONE);
34 return fc_funcs->server_setting_by_name(name);
37 /***************************************************************************
38 Returns the name of the server setting with the specified id.
39 ***************************************************************************/
40 const char *server_setting_name_get(server_setting_id id)
42 fc_assert_ret_val(fc_funcs, NULL);
43 fc_assert_ret_val(fc_funcs->server_setting_name_get, NULL);
45 return fc_funcs->server_setting_name_get(id);
48 /***************************************************************************
49 Returns the type of the server setting with the specified id.
50 ***************************************************************************/
51 enum sset_type server_setting_type_get(server_setting_id id)
53 fc_assert_ret_val(fc_funcs, sset_type_invalid());
54 fc_assert_ret_val(fc_funcs->server_setting_type_get, sset_type_invalid());
56 return fc_funcs->server_setting_type_get(id);
59 /***************************************************************************
60 Returns TRUE iff a server setting with the specified id exists.
61 ***************************************************************************/
62 bool server_setting_exists(server_setting_id id)
64 return sset_type_is_valid(server_setting_type_get(id));
67 /***************************************************************************
68 Returns the value of the server setting with the specified id.
69 ***************************************************************************/
70 bool server_setting_value_bool_get(server_setting_id id)
72 fc_assert_ret_val(fc_funcs, FALSE);
73 fc_assert_ret_val(fc_funcs->server_setting_val_bool_get, FALSE);
74 fc_assert_ret_val(server_setting_type_get(id) == SST_BOOL, FALSE);
76 return fc_funcs->server_setting_val_bool_get(id);
79 /***************************************************************************
80 Returns a server setting - value pair from its setting and value;
81 ***************************************************************************/
82 ssetv ssetv_from_values(server_setting_id setting, int value)
84 /* Only Boolean and TRUE can be supported unless setting value encoding
85 * is implemented. */
86 if (value != TRUE) {
87 fc_assert(value == TRUE);
88 return SSETV_NONE;
91 /* Only Boolean settings can be supported unless the setting value is
92 * encoded with the setting id. */
93 if (server_setting_type_get((server_setting_id)setting) != SST_BOOL) {
94 fc_assert(server_setting_type_get((server_setting_id)setting)
95 == SST_BOOL);
96 return SSETV_NONE;
99 return (ssetv)setting;
102 /***************************************************************************
103 Returns the server setting of the setting - value pair.
104 ***************************************************************************/
105 server_setting_id ssetv_setting_get(ssetv enc)
107 /* Only Boolean settings can be supported unless the setting value is
108 * encoded with the setting id. */
109 fc_assert(server_setting_type_get((server_setting_id)enc) == SST_BOOL);
111 return (server_setting_id)enc;
114 /***************************************************************************
115 Returns the server setting value of the setting - value pair.
116 ***************************************************************************/
117 int ssetv_value_get(ssetv enc)
119 /* Only Boolean settings can be supported unless the setting value is
120 * encoded with the setting id. */
121 fc_assert(server_setting_type_get((server_setting_id)enc) == SST_BOOL);
123 return TRUE;
126 /***************************************************************************
127 Returns the server setting - value pair encoded in the string.
128 ***************************************************************************/
129 ssetv ssetv_by_rule_name(const char *name)
131 ssetv val = (ssetv)server_setting_by_name(name);
133 /* Only Boolean settings can be supported unless the setting value is
134 * encoded with the setting id. */
135 if (server_setting_type_get((server_setting_id)val) != SST_BOOL) {
136 return SSETV_NONE;
139 return val;
142 /***************************************************************************
143 Returns the server setting - value pair encoded as a string.
144 ***************************************************************************/
145 const char *ssetv_rule_name(ssetv val)
147 /* Only Boolean settings can be supported unless the setting value is
148 * encoded with the setting id. */
149 fc_assert(server_setting_type_get((server_setting_id)val) == SST_BOOL);
151 return server_setting_name_get((server_setting_id)val);
154 /***************************************************************************
155 Returns the server setting - value pair formated in a user readable way.
156 ***************************************************************************/
157 const char *ssetv_human_readable(ssetv val, bool present)
159 static struct astring out = ASTRING_INIT;
161 /* Only Boolean settings can be supported unless the setting value is
162 * encoded with the setting id. */
163 fc_assert(server_setting_type_get((server_setting_id)val) == SST_BOOL);
165 /* TRANS: the first %s is a server setting, the second %s is it's value.
166 * Example: killstack is enabled */
167 astr_set(&out, _("%s is %s"),
168 server_setting_name_get((server_setting_id)val),
169 present ? _("enabled") : _("disabled"));
171 return astr_str(&out);
174 /***************************************************************************
175 Returns if the server setting currently has the value in the pair.
176 ***************************************************************************/
177 bool ssetv_setting_has_value(ssetv val)
179 /* Only boolean settings can be supported unless the setting value is
180 * encoded with the setting id. */
181 fc_assert_ret_val(
182 server_setting_type_get((server_setting_id)val) == SST_BOOL, FALSE);
184 return server_setting_value_bool_get((server_setting_id)val);