📄 libconfig.c
字号:
/* voifax general configuration interface library * Copyright (C) 2004 Simone Freddio & Andrea Emanuelli * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <stdio.h>#include <string.h>#include "libconfig.h"#include "common.h"/* Variabili Locali */char libconfig_currentfile[256];/* prototipi locali */int libconfig_addhash(char *section, char *key, char *val);int libconfig_addhash(char *secname, char *key, char *val){ char module[] = "libconfig_addhash"; int secnum = 0, itemnum = 0; /* recupera la prima sezione libera oppure il numero di sezione se gia' esistente*/ while (strcmp(sections[secnum].section_name, secname) != 0 && sections[secnum].used == 1 && secnum < MAXSECTIONS) secnum++; if (secnum == MAXSECTIONS) { TRC(TRC_ERR, module, "maximum sections reached!!!"); return false; } if (sections[secnum].used == 0) { sections[secnum].used = 1; strcpy(sections[secnum].section_name, secname); } while (strcmp(sections[secnum].items[itemnum].key, key) !=0 && sections[secnum].items[itemnum].used == 1 && itemnum < MAXHASHNUMBER) itemnum++; if (sections[secnum].items[itemnum].used == 1) { TRC(TRC_WARN, module, "key duplicated!!! ignoring line..."); return false; } sections[secnum].items[itemnum].used = 1; strcpy(sections[secnum].items[itemnum].key, key); strcpy(sections[secnum].items[itemnum].val, val); return true;}void libconfig_freehash(void){ memset(libconfig_currentfile, 0, sizeof(libconfig_currentfile)); memset(sections, 0, sizeof(sections));}int libconfig_init(char *filename){ char module[] = "libconfig_init"; FILE *conffd; char buffer[MAXHASHSIZE * 3]; int argc; char args[3][MAXHASHSIZE]; if (strcmp(filename, libconfig_currentfile) != 0) { libconfig_freehash(); conffd = fopen(filename, "r"); if (conffd == NULL) { TRC(TRC_ERR, module, "Impossibile trovare il file di configurazione"); return false; } while (!feof(conffd)) { fgets(buffer, MAXHASHSIZE*3, conffd); if (buffer[0]!='#' && strlen(buffer)>3) { argc = sscanf(buffer, "%s %s %s", args[0], args[1], args[2]); if (argc==3) libconfig_addhash(args[0], args[1], args[2]); else TRC(TRC_WARN, module, "Attention! a row has bad format!! Ignoring line..."); } } fclose(conffd); strcpy(libconfig_currentfile, filename); } return true;}char *libconfig_getvalue(char *secname, char *key, char *defval){ int secnum = 0, itemnum = 0; while (sections[secnum].used == 1 && strcmp(sections[secnum].section_name, secname) != 0 && secnum < MAXSECTIONS) secnum++; if (secnum == MAXSECTIONS) return defval; if (sections[secnum].used == 0) return defval; while (sections[secnum].items[itemnum].used == 1 && strcmp(sections[secnum].items[itemnum].key, key) != 0 && itemnum < MAXHASHNUMBER) itemnum++; if (itemnum == MAXHASHNUMBER) return defval; if (sections[secnum].items[itemnum].used == 0) return defval; return §ions[secnum].items[itemnum].val[0];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -