123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- /*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include <cutils/config_utils.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <cutils/misc.h>
- cnode* config_node(const char *name, const char *value)
- {
- cnode* node = static_cast<cnode*>(calloc(sizeof(cnode), 1));
- if(node) {
- node->name = name ? name : "";
- node->value = value ? value : "";
- }
- return node;
- }
- cnode* config_find(cnode *root, const char *name)
- {
- cnode *node, *match = NULL;
- /* we walk the whole list, as we need to return the last (newest) entry */
- for(node = root->first_child; node; node = node->next)
- if(!strcmp(node->name, name))
- match = node;
- return match;
- }
- static cnode* _config_create(cnode *root, const char *name)
- {
- cnode *node;
- node = config_node(name, NULL);
- if(root->last_child)
- root->last_child->next = node;
- else
- root->first_child = node;
- root->last_child = node;
- return node;
- }
- int config_bool(cnode *root, const char *name, int _default)
- {
- cnode *node;
-
- node = config_find(root, name);
- if(!node)
- return _default;
- switch(node->value[0]) {
- case 'y':
- case 'Y':
- case '1':
- return 1;
- default:
- return 0;
- }
- }
- const char* config_str(cnode *root, const char *name, const char *_default)
- {
- cnode *node;
- node = config_find(root, name);
- if(!node)
- return _default;
- return node->value;
- }
- void config_set(cnode *root, const char *name, const char *value)
- {
- cnode *node;
- node = config_find(root, name);
- if(node)
- node->value = value;
- else {
- node = _config_create(root, name);
- node->value = value;
- }
- }
- #define T_EOF 0
- #define T_TEXT 1
- #define T_DOT 2
- #define T_OBRACE 3
- #define T_CBRACE 4
- typedef struct
- {
- char *data;
- char *text;
- int len;
- char next;
- } cstate;
- static int _lex(cstate *cs, int value)
- {
- char c;
- char *s;
- char *data;
- data = cs->data;
- if(cs->next != 0) {
- c = cs->next;
- cs->next = 0;
- goto got_c;
- }
- restart:
- for(;;) {
- c = *data++;
- got_c:
- if(isspace(c))
- continue;
- switch(c) {
- case 0:
- return T_EOF;
- case '#':
- for(;;) {
- switch(*data) {
- case 0:
- cs->data = data;
- return T_EOF;
- case '\n':
- cs->data = data + 1;
- goto restart;
- default:
- data++;
- }
- }
- break;
-
- case '.':
- cs->data = data;
- return T_DOT;
- case '{':
- cs->data = data;
- return T_OBRACE;
- case '}':
- cs->data = data;
- return T_CBRACE;
- default:
- s = data - 1;
- if(value) {
- for(;;) {
- if(*data == 0) {
- cs->data = data;
- break;
- }
- if(*data == '\n') {
- cs->data = data + 1;
- *data-- = 0;
- break;
- }
- data++;
- }
- /* strip trailing whitespace */
- while(data > s){
- if(!isspace(*data)) break;
- *data-- = 0;
- }
- goto got_text;
- } else {
- for(;;) {
- if(isspace(*data)) {
- *data = 0;
- cs->data = data + 1;
- goto got_text;
- }
- switch(*data) {
- case 0:
- cs->data = data;
- goto got_text;
- case '.':
- case '{':
- case '}':
- cs->next = *data;
- *data = 0;
- cs->data = data + 1;
- goto got_text;
- default:
- data++;
- }
- }
- }
- }
- }
- got_text:
- cs->text = s;
- return T_TEXT;
- }
- #if 0
- char *TOKENNAMES[] = { "EOF", "TEXT", "DOT", "OBRACE", "CBRACE" };
- static int lex(cstate *cs, int value)
- {
- int tok = _lex(cs, value);
- printf("TOKEN(%d) %s %s\n", value, TOKENNAMES[tok],
- tok == T_TEXT ? cs->text : "");
- return tok;
- }
- #else
- #define lex(cs,v) _lex(cs,v)
- #endif
- static int parse_expr(cstate *cs, cnode *node);
- static int parse_block(cstate *cs, cnode *node)
- {
- for(;;){
- switch(lex(cs, 0)){
- case T_TEXT:
- if(parse_expr(cs, node)) return -1;
- continue;
- case T_CBRACE:
- return 0;
- default:
- return -1;
- }
- }
- }
- static int parse_expr(cstate *cs, cnode *root)
- {
- cnode *node;
- /* last token was T_TEXT */
- node = config_find(root, cs->text);
- if(!node || *node->value)
- node = _config_create(root, cs->text);
- for(;;) {
- switch(lex(cs, 1)) {
- case T_DOT:
- if(lex(cs, 0) != T_TEXT)
- return -1;
- node = _config_create(node, cs->text);
- continue;
- case T_TEXT:
- node->value = cs->text;
- return 0;
- case T_OBRACE:
- return parse_block(cs, node);
- default:
- return -1;
- }
- }
- }
- void config_load(cnode *root, char *data)
- {
- if(data != 0) {
- cstate cs;
- cs.data = data;
- cs.next = 0;
- for(;;) {
- switch(lex(&cs, 0)) {
- case T_TEXT:
- if(parse_expr(&cs, root))
- return;
- break;
- default:
- return;
- }
- }
- }
- }
- void config_load_file(cnode *root, const char *fn)
- {
- char* data = static_cast<char*>(load_file(fn, nullptr));
- config_load(root, data);
- // TODO: deliberate leak :-/
- }
- void config_free(cnode *root)
- {
- cnode *cur = root->first_child;
- while (cur) {
- cnode *prev = cur;
- config_free(cur);
- cur = cur->next;
- free(prev);
- }
- }
|