📄 lcfg_static.c
字号:
/* This file is an autogenerated single-file version of liblcfg. * The revision used to create this file is 997. It is recommended * that you update this file on a regulary basis from the original * liblcfg distribution package. * * The most recent version of liblcfg is available at * <http://liblcfg.mwcollect.org> */#include <assert.h>#include <ctype.h>#include <errno.h>#include <fcntl.h>#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>#include "lcfg_static.h"/*** begin file include/lcfg/lcfg_string.h ***//* * liblcfg - lightweight configuration file library * Copyright (C) 2007 Paul Baecher * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * $Id: lcfg_string.h 995 2007-05-07 01:49:42Z dp $ * */#ifndef LCFG_STRING_H#define LCFG_STRING_Hstruct lcfg_string * lcfg_string_new();struct lcfg_string * lcfg_string_new_copy(struct lcfg_string *);int lcfg_string_set(struct lcfg_string *, const char *);int lcfg_string_cat_char(struct lcfg_string *, char);int lcfg_string_cat_cstr(struct lcfg_string *, const char *);int lcfg_string_cat_uint(struct lcfg_string *, unsigned int);int lcfg_string_find(struct lcfg_string *, char);int lcfg_string_rfind(struct lcfg_string *, char);void lcfg_string_trunc(struct lcfg_string *, unsigned int);inline const char * lcfg_string_cstr(struct lcfg_string *);inline unsigned int lcfg_string_len(struct lcfg_string *);void lcfg_string_delete(struct lcfg_string *);#endif/*** end file include/lcfg/lcfg_string.h ***//*** begin file include/lcfg/lcfg_token.h ***//* * liblcfg - lightweight configuration file library * Copyright (C) 2007 Paul Baecher * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * $Id: lcfg_token.h 995 2007-05-07 01:49:42Z dp $ * */#ifndef LCFG_TOKEN_H#define LCFG_TOKEN_Henum lcfg_token_type { lcfg_null_token = 0, lcfg_identifier, lcfg_equals, lcfg_string, lcfg_sbracket_open, lcfg_sbracket_close, lcfg_comma, lcfg_brace_open, lcfg_brace_close};extern const char *lcfg_token_map[];struct lcfg_token{ enum lcfg_token_type type; struct lcfg_string *string; short line; short col;};#endif/*** end file include/lcfg/lcfg_token.h ***//*** begin file include/lcfg/lcfg_scanner.h ***//* * liblcfg - lightweight configuration file library * Copyright (C) 2007 Paul Baecher * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * $Id: lcfg_scanner.h 995 2007-05-07 01:49:42Z dp $ * */#ifndef LCFG_SCANNER_H#define LCFG_SCANNER_Hstruct lcfg_scanner;struct lcfg_token;struct lcfg_scanner * lcfg_scanner_new(struct lcfg *, int fd);enum lcfg_status lcfg_scanner_init(struct lcfg_scanner *);enum lcfg_status lcfg_scanner_next_token(struct lcfg_scanner *, struct lcfg_token *);int lcfg_scanner_has_next(struct lcfg_scanner *);void lcfg_scanner_delete(struct lcfg_scanner *);#endif/*** end file include/lcfg/lcfg_scanner.h ***//*** begin file include/lcfg/lcfg_parser.h ***//* * liblcfg - lightweight configuration file library * Copyright (C) 2007 Paul Baecher * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * $Id: lcfg_parser.h 995 2007-05-07 01:49:42Z dp $ * */#ifndef LCFG_PARSER_H#define LCFG_PARSER_Hstruct lcfg_parser;struct lcfg_parser * lcfg_parser_new(struct lcfg *, const char *);enum lcfg_status lcfg_parser_run(struct lcfg_parser *);enum lcfg_status lcfg_parser_accept(struct lcfg_parser *, lcfg_visitor_function, void *);void lcfg_parser_delete(struct lcfg_parser *);enum lcfg_status lcfg_parser_get(struct lcfg_parser *, const char *, void **, size_t *);#endif/*** end file include/lcfg/lcfg_parser.h ***//*** begin file src/lcfg_string.c ***//* * liblcfg - lightweight configuration file library * Copyright (C) 2007 Paul Baecher * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * $Id: lcfg_string.c 995 2007-05-07 01:49:42Z dp $ * */struct lcfg_string{ char *str; unsigned int size; unsigned int capacity;};int lcfg_string_set(struct lcfg_string *s, const char *cstr){ lcfg_string_trunc(s, 0); return lcfg_string_cat_cstr(s, cstr);}/* make sure new_size bytes fit into the string */inline static void lcfg_string_grow(struct lcfg_string *s, unsigned int new_size){ /* always allocate one byte more than needed * to make _cstr() working in any case without realloc. */ while( (new_size + 1) > s->capacity ) { s->capacity *= 2; s->str = realloc(s->str, s->capacity); }}struct lcfg_string *lcfg_string_new(){ struct lcfg_string *s = malloc(sizeof(struct lcfg_string)); assert(s); s->capacity = 8; s->size = 0; s->str = malloc(s->capacity); assert(s->str); return s;}struct lcfg_string *lcfg_string_new_copy(struct lcfg_string *s){ struct lcfg_string *s_new = malloc(sizeof(struct lcfg_string)); assert(s_new); s_new->capacity = s->capacity; s_new->size = s->size; s_new->str = malloc(s_new->capacity); memcpy(s_new->str, s->str, s_new->size); return s_new;}int lcfg_string_cat_uint(struct lcfg_string *s, unsigned int i){ unsigned int size_needed = 1; unsigned int ii = i; char c; while( ii > 10 ) { size_needed++; ii /= 10; } lcfg_string_grow(s, s->size + size_needed); ii = size_needed - 1; do { c = '0' + i % 10; s->str[s->size + ii--] = c; i /= 10; } while( i != 0 ); s->size += size_needed; return s->size;}int lcfg_string_find(struct lcfg_string *s, char c){ int i; for( i = 0; i < s->size; i++ ) { if( s->str[i] == c ) { return i; } } return -1;}int lcfg_string_rfind(struct lcfg_string *s, char c){ int i; for( i = s->size - 1; i >= 0; i-- ) { if( s->str[i] == c ) { return i; } } return -1;}void lcfg_string_trunc(struct lcfg_string *s, unsigned int max_size){ if( max_size < s->size ) { s->size = max_size; }}int lcfg_string_cat_cstr(struct lcfg_string *s, const char *cstr){ size_t len = strlen(cstr); lcfg_string_grow(s, s->size + len); memcpy(s->str + s->size, cstr, len); s->size += len; return s->size;}int lcfg_string_cat_char(struct lcfg_string *s, char c){ lcfg_string_grow(s, s->size + 1); s->str[s->size++] = c; return s->size;}inline const char *lcfg_string_cstr(struct lcfg_string *s){ s->str[s->size] = '\0'; return s->str;}inline unsigned int lcfg_string_len(struct lcfg_string *s){ return s->size;}void lcfg_string_delete(struct lcfg_string *s){ free(s->str); free(s);}/*** end file src/lcfg_string.c ***//*** begin file src/lcfg_token.c ***//* * liblcfg - lightweight configuration file library * Copyright (C) 2007 Paul Baecher * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * $Id: lcfg_token.c 997 2007-05-07 02:11:53Z dp $ * */const char *lcfg_token_map[] = { "null_token", "T_IDENTIFIER", "`='", "T_STRING", "`['", "`]'", "`,'", "`{'", "`}'" };/*** end file src/lcfg_token.c ***//*** begin file src/lcfg_scanner.c ***//* * liblcfg - lightweight configuration file library * Copyright (C) 2007 Paul Baecher * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * $Id: lcfg_scanner.c 995 2007-05-07 01:49:42Z dp $ * */#define BUFFER_SIZE 0xffstruct lcfg_scanner{ struct lcfg *lcfg; int fd; char buffer[BUFFER_SIZE]; int offset; int size; int eof; short line; short col; struct lcfg_token prepared_token; int token_eof;};static enum lcfg_status lcfg_scanner_buffer_fill(struct lcfg_scanner *s){ if( (s->size = read(s->fd, s->buffer, BUFFER_SIZE)) < 0 ) { lcfg_error_set(s->lcfg, "read(): %s", strerror(errno)); return lcfg_status_error; } else if( s->size == 0 ) { s->eof = !0; } else { s->offset = 0; } return lcfg_status_ok;}static inline int lcfg_scanner_char_eof(struct lcfg_scanner *s){ if( s->eof ) { return !0; } else { if( s->size == 0 || s->offset == BUFFER_SIZE ) { lcfg_scanner_buffer_fill(s); } if( s->size < BUFFER_SIZE && s->offset == s->size ) { s->eof = !0; } return s->eof; }}static enum lcfg_status lcfg_scanner_char_read(struct lcfg_scanner *s, char *c){ if( lcfg_scanner_char_eof(s) ) { lcfg_error_set(s->lcfg, "%s", "cannot read beyond eof"); return lcfg_status_error; } *c = s->buffer[s->offset++]; return lcfg_status_ok;}static enum lcfg_status lcfg_scanner_char_peek(struct lcfg_scanner *s, char *c){ if( lcfg_scanner_char_eof(s) ) { lcfg_error_set(s->lcfg, "%s", "cannot peek beyond eof"); return lcfg_status_error; } *c = s->buffer[s->offset]; return lcfg_status_ok;}/* the beautiful lowlevel fsm */static enum lcfg_status lcfg_scanner_token_read(struct lcfg_scanner *s){ enum scanner_state { start = 0, comm_start, in_oneline, in_multiline, multiline_end, in_identifier, in_str, in_esc, esc_hex_exp_first, esc_hex_exp_second, invalid }; enum scanner_state state = start; char c = '\0'; char hex[3]; s->prepared_token.type = lcfg_null_token; while( !lcfg_scanner_char_eof(s) ) { int consume = !0; lcfg_scanner_char_peek(s, &c); switch( state ) { case start: switch( c ) { case ' ': case '\t': case '\r': case '\n': break; case '=': s->prepared_token.type = lcfg_equals; break; case '[': s->prepared_token.type = lcfg_sbracket_open; break; case ']': s->prepared_token.type = lcfg_sbracket_close; break; case '{': s->prepared_token.type = lcfg_brace_open; break; case '}': s->prepared_token.type = lcfg_brace_close; break; case ',': s->prepared_token.type = lcfg_comma; break; case '/': state = comm_start; break; case '"': state = in_str; lcfg_string_trunc(s->prepared_token.string, 0); break; default: if( isalpha(c) ) { lcfg_string_trunc(s->prepared_token.string, 0); lcfg_string_cat_char(s->prepared_token.string, c); state = in_identifier; } else { lcfg_error_set(s->lcfg, "parse error: invalid input character `%c' (0x%02x) near line %d, col %d", isprint(c) ? c : '.', c, s->line, s->col); state = invalid; } } break; case comm_start: if( c == '/' ) { state = in_oneline; } else if( c == '*' ) { state = in_multiline; } else { lcfg_error_set(s->lcfg, "parse error: invalid input character `%c' (0x%02x) near line %d, col %d", isprint(c) ? c : '.', c, s->line, s->col); state = invalid; } break; case in_oneline: if( c == '\n' ) { state = start; } break; case in_multiline: if( c == '*' )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -