121 lines
3.7 KiB
C
121 lines
3.7 KiB
C
#include <parse.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
int parse(char *content, Tokendata **data) {
|
|
int i, current_token_pos = 0;
|
|
bool token_started = FALSE;
|
|
char c;
|
|
char current_token[TOKEN_SZ];
|
|
Token current_token_type;
|
|
int token_num = 0;
|
|
for(i=0;i<(int)strlen(content);i++){
|
|
c = content[i];
|
|
if(!token_started && c != ' '){
|
|
token_started = TRUE;
|
|
switch(c){
|
|
case '\'':
|
|
current_token_type = T_CHAR;
|
|
break;
|
|
case '"':
|
|
current_token_type = T_STRING;
|
|
break;
|
|
case '-':
|
|
current_token_type = T_GROUP;
|
|
break;
|
|
default:
|
|
current_token_type = T_NAME;
|
|
}
|
|
if(c >= 0x30 && c <= 0x39){
|
|
current_token_type = T_INT;
|
|
}
|
|
}
|
|
if(token_started){
|
|
if(c != ' ' || (current_token_type == T_STRING ||
|
|
current_token_type == T_CHAR)){
|
|
/* TODO : the following condition should be changed, to allow
|
|
'"', '\'' and '-' in chars, strings, names, groups ... */
|
|
if(current_token_pos < TOKEN_SZ && (c != '"' && c != '\'' &&
|
|
c != '=' && c != '-' && c != '\n')){
|
|
current_token[current_token_pos] = c;
|
|
current_token_pos++;
|
|
}else if(current_token_pos >= TOKEN_SZ){
|
|
puts("[config] Too big token !");
|
|
return -2;
|
|
}
|
|
}
|
|
if(((c == '=' && current_token_type == T_NAME) ||
|
|
c == '\n') && strlen(current_token) > 0){
|
|
(*data) = realloc((*data), (token_num+1)*sizeof(Tokendata));
|
|
if(!(*data)){
|
|
puts("[config] More memory needed !");
|
|
return -1;
|
|
}
|
|
(*data)[token_num].token = current_token_type;
|
|
strcpy((*data)[token_num].content, current_token);
|
|
memset(current_token, '\0', TOKEN_SZ);
|
|
current_token_pos = 0;
|
|
token_started = FALSE;
|
|
token_num++;
|
|
}
|
|
}
|
|
}
|
|
if(current_token_pos > 0){
|
|
(*data) = realloc((*data), (token_num+1)*sizeof(Tokendata));
|
|
if(!(*data)){
|
|
puts("[config] More memory needed !");
|
|
return -1;
|
|
}
|
|
(*data)[token_num].token = current_token_type;
|
|
strcpy((*data)[token_num].content, current_token);
|
|
}
|
|
return token_num;
|
|
}
|
|
|
|
int search_token(Token token, char *content, Tokendata *tokens,
|
|
int token_d_sz) {
|
|
int i;
|
|
for(i=0;i<token_d_sz;i++){
|
|
if(tokens[i].token == token && !strcmp(tokens[i].content, content)){
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int get_value_from_name(Token value_type, char *value, Tokendata *tokens,
|
|
int token_d_sz) {
|
|
int i;
|
|
i=search_token(T_NAME, value, tokens, token_d_sz);
|
|
if(i<0){
|
|
printf("[config] Can't get \"%s\" value !\n", value);
|
|
exit(-2);
|
|
}
|
|
if(i+1 >= token_d_sz){
|
|
printf("[config] Can't get \"%s\" value, value not existing ! !\n",
|
|
value);
|
|
exit(-2);
|
|
}
|
|
if(tokens[i+1].token != value_type){
|
|
printf("[config] Bad type for \"%s\" value !\n", value);
|
|
exit(-2);
|
|
}
|
|
return i+1;
|
|
}
|
|
|
|
int get_value_from_name_noerror(Token value_type, char *value,
|
|
Tokendata *tokens, int token_d_sz) {
|
|
int i;
|
|
i=search_token(T_NAME, value, tokens, token_d_sz);
|
|
if(i<0 || i+1 >= token_d_sz){
|
|
return -1;
|
|
}
|
|
if(tokens[i+1].token != value_type){
|
|
return -1;
|
|
}
|
|
return i+1;
|
|
}
|