- Timestamp:
- 2015-02-20T22:50:54Z (9 years ago)
- Branches:
- master
- Children:
- 0b9daac, 3d45471, 7733b8c
- Parents:
- af359b4
- git-author:
- Indent <please@…> (19-02-15 05:47:20)
- git-committer:
- dequis <dx@…> (20-02-15 22:50:54)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
set.c
raf359b4 r5ebff60 1 1 /********************************************************************\ 2 2 * BitlBee -- An IRC to other IM-networks gateway * 3 3 * * … … 30 30 char *SET_INVALID = "nee"; 31 31 32 set_t *set_add( set_t **head, const char *key, const char *def, set_eval eval, void *data)33 { 34 set_t *s = set_find( head, key);35 32 set_t *set_add(set_t **head, const char *key, const char *def, set_eval eval, void *data) 33 { 34 set_t *s = set_find(head, key); 35 36 36 /* Possibly the setting already exists. If it doesn't exist yet, 37 37 we create it. If it does, we'll just change the default. */ 38 if( !s ) 39 { 40 if( ( s = *head ) ) 41 { 38 if (!s) { 39 if ((s = *head)) { 42 40 /* Sorted insertion. Special-case insertion at the start. */ 43 if( strcmp( key, s->key ) < 0 ) 44 { 45 s = g_new0( set_t, 1 ); 41 if (strcmp(key, s->key) < 0) { 42 s = g_new0(set_t, 1); 46 43 s->next = *head; 47 44 *head = s; 48 } 49 else 50 { 51 while( s->next && strcmp( key, s->next->key ) > 0 ) 45 } else { 46 while (s->next && strcmp(key, s->next->key) > 0) { 52 47 s = s->next; 48 } 53 49 set_t *last_next = s->next; 54 s->next = g_new0( set_t, 1);50 s->next = g_new0(set_t, 1); 55 51 s = s->next; 56 52 s->next = last_next; 57 53 } 58 } 59 else 60 { 61 s = *head = g_new0( set_t, 1 ); 62 } 63 s->key = g_strdup( key ); 64 } 65 66 if( s->def ) 67 { 68 g_free( s->def ); 54 } else { 55 s = *head = g_new0(set_t, 1); 56 } 57 s->key = g_strdup(key); 58 } 59 60 if (s->def) { 61 g_free(s->def); 69 62 s->def = NULL; 70 63 } 71 if( def ) s->def = g_strdup( def ); 72 64 if (def) { 65 s->def = g_strdup(def); 66 } 67 73 68 s->eval = eval; 74 69 s->data = data; 75 70 76 71 return s; 77 72 } 78 73 79 set_t *set_find( set_t **head, const char *key)74 set_t *set_find(set_t **head, const char *key) 80 75 { 81 76 set_t *s = *head; 82 83 while( s ) 84 { 85 if( g_strcasecmp( s->key, key ) == 0 || 86 ( s->old_key && g_strcasecmp( s->old_key, key ) == 0 ) ) 77 78 while (s) { 79 if (g_strcasecmp(s->key, key) == 0 || 80 (s->old_key && g_strcasecmp(s->old_key, key) == 0)) { 87 81 break; 82 } 88 83 s = s->next; 89 84 } 90 85 91 86 return s; 92 87 } 93 88 94 char *set_getstr( set_t **head, const char *key)95 { 96 set_t *s = set_find( head, key);97 98 if ( !s || ( !s->value && !s->def ) )89 char *set_getstr(set_t **head, const char *key) 90 { 91 set_t *s = set_find(head, key); 92 93 if (!s || (!s->value && !s->def)) { 99 94 return NULL; 100 101 return set_value( s ); 102 } 103 104 int set_getint( set_t **head, const char *key ) 105 { 106 char *s = set_getstr( head, key ); 95 } 96 97 return set_value(s); 98 } 99 100 int set_getint(set_t **head, const char *key) 101 { 102 char *s = set_getstr(head, key); 107 103 int i = 0; 108 109 if( !s ) 110 return 0; 111 112 if( sscanf( s, "%d", &i ) != 1 ) 113 return 0; 114 104 105 if (!s) { 106 return 0; 107 } 108 109 if (sscanf(s, "%d", &i) != 1) { 110 return 0; 111 } 112 115 113 return i; 116 114 } 117 115 118 int set_getbool( set_t **head, const char *key ) 119 { 120 char *s = set_getstr( head, key ); 121 122 if( !s ) 123 return 0; 124 125 return bool2int( s ); 126 } 127 128 int set_isvisible( set_t *set ) 116 int set_getbool(set_t **head, const char *key) 117 { 118 char *s = set_getstr(head, key); 119 120 if (!s) { 121 return 0; 122 } 123 124 return bool2int(s); 125 } 126 127 int set_isvisible(set_t *set) 129 128 { 130 129 /* the default value is not stored in value, only in def */ 131 return !( ( set->flags & SET_HIDDEN) ||132 ( ( set->flags & SET_HIDDEN_DEFAULT) &&133 ( set->value == NULL ) ));134 } 135 136 int set_setstr( set_t **head, const char *key, char *value)137 { 138 set_t *s = set_find( head, key);130 return !((set->flags & SET_HIDDEN) || 131 ((set->flags & SET_HIDDEN_DEFAULT) && 132 (set->value == NULL))); 133 } 134 135 int set_setstr(set_t **head, const char *key, char *value) 136 { 137 set_t *s = set_find(head, key); 139 138 char *nv = value; 140 141 if ( !s )139 140 if (!s) { 142 141 /* 143 142 Used to do this, but it never really made sense. … … 145 144 */ 146 145 return 0; 147 148 if( value == NULL && ( s->flags & SET_NULL_OK ) == 0 ) 149 return 0; 150 146 } 147 148 if (value == NULL && (s->flags & SET_NULL_OK) == 0) { 149 return 0; 150 } 151 151 152 /* Call the evaluator. For invalid values, evaluators should now 152 153 return SET_INVALID, but previously this was NULL. Try to handle 153 154 that too if NULL is not an allowed value for this setting. */ 154 if ( s->eval && ( ( nv = s->eval( s, value )) == SET_INVALID ||155 ( ( s->flags & SET_NULL_OK ) == 0 && nv == NULL ) ) )156 return 0; 157 158 if( s->value ) 159 {160 g_free( s->value);155 if (s->eval && ((nv = s->eval(s, value)) == SET_INVALID || 156 ((s->flags & SET_NULL_OK) == 0 && nv == NULL))) { 157 return 0; 158 } 159 160 if (s->value) { 161 g_free(s->value); 161 162 s->value = NULL; 162 163 } 163 164 164 165 /* If there's a default setting and it's equal to what we're trying to 165 166 set, stick with s->value = NULL. Otherwise, remember the setting. */ 166 if( !s->def || ( strcmp( nv, s->def ) != 0 ) ) 167 s->value = g_strdup( nv ); 168 169 if( nv != value ) 170 g_free( nv ); 171 167 if (!s->def || (strcmp(nv, s->def) != 0)) { 168 s->value = g_strdup(nv); 169 } 170 171 if (nv != value) { 172 g_free(nv); 173 } 174 172 175 return 1; 173 176 } 174 177 175 int set_setint( set_t **head, const char *key, int value ) 176 { 177 char *s = g_strdup_printf( "%d", value ); 178 int retval = set_setstr( head, key, s ); 179 g_free( s ); 178 int set_setint(set_t **head, const char *key, int value) 179 { 180 char *s = g_strdup_printf("%d", value); 181 int retval = set_setstr(head, key, s); 182 183 g_free(s); 180 184 return retval; 181 185 } 182 186 183 void set_del( set_t **head, const char *key)187 void set_del(set_t **head, const char *key) 184 188 { 185 189 set_t *s = *head, *t = NULL; 186 187 while( s ) 188 { 189 if( g_strcasecmp( s->key, key ) == 0 ) 190 191 while (s) { 192 if (g_strcasecmp(s->key, key) == 0) { 190 193 break; 191 s = (t=s)->next;192 }193 if( s )194 {195 if ( t )194 } 195 s = (t = s)->next; 196 } 197 if (s) { 198 if (t) { 196 199 t->next = s->next; 197 else200 } else { 198 201 *head = s->next; 199 200 g_free( s->key ); 201 g_free( s->old_key ); 202 g_free( s->value ); 203 g_free( s->def ); 204 g_free( s ); 205 } 206 } 207 208 int set_reset( set_t **head, const char *key ) 202 } 203 204 g_free(s->key); 205 g_free(s->old_key); 206 g_free(s->value); 207 g_free(s->def); 208 g_free(s); 209 } 210 } 211 212 int set_reset(set_t **head, const char *key) 209 213 { 210 214 set_t *s; 211 212 s = set_find( head, key ); 213 if( s ) 214 return set_setstr( head, key, s->def ); 215 215 216 s = set_find(head, key); 217 if (s) { 218 return set_setstr(head, key, s->def); 219 } 220 216 221 return 0; 217 222 } 218 223 219 char *set_eval_int( set_t *set, char *value)224 char *set_eval_int(set_t *set, char *value) 220 225 { 221 226 char *s = value; 222 227 223 228 /* Allow a minus at the first position. */ 224 if( *s == '-' ) 225 s ++; 226 227 for( ; *s; s ++ ) 228 if( !g_ascii_isdigit( *s ) ) 229 if (*s == '-') { 230 s++; 231 } 232 233 for (; *s; s++) { 234 if (!g_ascii_isdigit(*s)) { 229 235 return SET_INVALID; 230 236 } 237 } 238 231 239 return value; 232 240 } 233 241 234 char *set_eval_bool( set_t *set, char *value)235 { 236 return is_bool( value) ? value : SET_INVALID;237 } 238 239 char *set_eval_list( set_t *set, char *value)242 char *set_eval_bool(set_t *set, char *value) 243 { 244 return is_bool(value) ? value : SET_INVALID; 245 } 246 247 char *set_eval_list(set_t *set, char *value) 240 248 { 241 249 GSList *options = set->eval_data, *opt; 242 243 for ( opt = options; opt; opt = opt->next )244 if ( strcmp( value, opt->data ) == 0 )250 251 for (opt = options; opt; opt = opt->next) { 252 if (strcmp(value, opt->data) == 0) { 245 253 return value; 246 254 } 255 } 256 247 257 /* TODO: It'd be nice to show the user a list of allowed values, 248 258 but we don't have enough context here to do that. May 249 259 want to fix that. */ 250 260 251 261 return NULL; 252 262 } 253 263 254 char *set_eval_to_char( set_t *set, char *value ) 255 { 256 char *s = g_new( char, 3 ); 257 258 if( *value == ' ' ) 259 strcpy( s, " " ); 260 else 261 sprintf( s, "%c ", *value ); 262 264 char *set_eval_to_char(set_t *set, char *value) 265 { 266 char *s = g_new(char, 3); 267 268 if (*value == ' ') { 269 strcpy(s, " "); 270 } else { 271 sprintf(s, "%c ", *value); 272 } 273 263 274 return s; 264 275 } 265 276 266 char *set_eval_oauth( set_t *set, char *value)277 char *set_eval_oauth(set_t *set, char *value) 267 278 { 268 279 account_t *acc = set->data; 269 270 if ( bool2int( value ) && strcmp( acc->pass, PASSWORD_PENDING ) == 0 )280 281 if (bool2int(value) && strcmp(acc->pass, PASSWORD_PENDING) == 0) { 271 282 *acc->pass = '\0'; 272 273 return set_eval_bool( set, value ); 274 } 283 } 284 285 return set_eval_bool(set, value); 286 }
Note: See TracChangeset
for help on using the changeset viewer.