📄 libconf.c
字号:
/* * $Id: libconf.c,v 1.10 1998/02/17 09:51:55 mdejonge Exp $ * * $Source: /home/mdejonge/CVS/projects/modem/libconf/libconf.c,v $ * $Revision: 1.10 $ * Author: Merijn de Jonge * Email: mdejonge@wins.uva.nl * * * * This file is part of the modem communication package. * Copyright (C) 1996-1998 Merijn de Jonge * * 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., 675 Mass Ave, Cambridge, MA 02139, USA. * * */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <string.h>#include <stdlib.h>#include <assert.h>#include <ctype.h>#include "libconf.h"#define MAXBUF 512static char buf[MAXBUF]; /* Global buffer */static Bool readLine( FILE* f );static void add_comment( char**, char* );static Bool is_comment( char* s );static Bool is_group( char* s );static Bool is_item( char* s );static char* strip_spaces( char* s );static Bool read_groups( FILE* f, conf_file* cf );static Bool read_items( FILE* f, conf_file* cf, grp* group );static int compare( conf_file* cf, char* s1, char* s2 );static int Strlen( char *s );static void clear_error( conf_file* cf );static void set_error( conf_file* cf, confErr err );/* Open Close Write configuration */ /* * Function: open_conf * * Description: * This function opens the configuration file 'file' * * Arguments: * - const char* file The configuration file to open * * Returns: * - conf_file* f The opened configuration file * - conf_file* NULL On error * * Remarks: * This function opens the configuration file and stores all * groups and items in datastructure. After reading the * file, the 'cursor' is located at the first item in the * first group. * */conf_file* open_conf( const char* file ){ FILE* f; conf_file* cf; cf = (conf_file*)malloc( sizeof( conf_file) ); if( cf == NULL ) return NULL; clear_error( cf ); cf->fileName = strdup( file ); if( cf->fileName == NULL ) { free( cf ); return NULL; } case_significant( cf ); cf->currGroup = NULL; cf->currItem = NULL; cf->groups = NULL; cf->comment = NULL; f = fopen( file, "r" ); if( f == NULL ) { free( cf->fileName ); free( cf ); return NULL; } if( read_groups( f, cf ) == False ) { close_conf( cf ); return NULL; } fclose( f ); return cf;}/* * Function: close_conf * * Description: * This function closes the configuration file * * Arguments: * - conf_file* cf The configuration file to close * * Returns: * - Bool True On success * - Bool False On error or cf == NULL * * Remarks: * This funcion also free's the memory used to store * items and groups, and the memory * used to store the conf_file structure. * */Bool close_conf( conf_file* cf ){ if( cf == NULL ) return False; while( !conf_empty( cf ) ) del_group( cf, group_name( current_group( cf ) ) ); if( cf->comment != NULL ) free( cf->comment ); cf->comment = NULL; cf->currGroup = NULL; cf->currItem= NULL; free( cf->fileName ); free( cf ); return True;}/* * Function: write_conf * * Description: * This function writes all groups and items * stored in memory to the config_file. * * Arguments: * - conf_file* cf The configuration file to write * * Returns: * - Bool True On success * - Bool False On error * * Remarks: * This function only writes the data stored in memory, * which means that comments, additional whitespaces and * newlines are lost! * */Bool write_conf( conf_file* cf ){ FILE* f; grp* gPtr; item* iPtr; if( cf == NULL ) return False; f = fopen( cf->fileName, "w" ); if( f == NULL ) return False; if( cf->comment != NULL ) fprintf( f, "%s\n", cf->comment ); gPtr = goto_first_group( cf ); while( gPtr != NULL ) { fprintf( f, "[%s]\n", group_name( current_group( cf ) ) ); if( gPtr->comment != NULL ) fprintf( f, "%s\n", gPtr->comment ); iPtr = goto_first_item( cf ); while( iPtr != NULL ) { fprintf( f, " %s = %s\n", item_key( current_item( cf ) ), item_value( current_item( cf ) ) ); if( iPtr->comment != NULL ) fprintf( f, "%s\n", iPtr->comment ); iPtr = goto_next_item( cf ); } gPtr = goto_next_group( cf ); } fclose( f ); return True;}/* Init conf: read groups and items */ /* * Function: read_groups * * Description: * This function reads all groups and items * from configuration file, and stores them in memory * * Arguments: * - FILE* f The Config. file to read from * - conf_file* cf The structure to store data in * * Returns: * - Bool True On success * - Bool False On error * */static Bool read_groups( FILE* f, conf_file* cf ){ grp* tmp; char* ptr; assert( f != NULL && cf != NULL ); fseek( f, SEEK_SET, 0 ); tmp = NULL; if( readLine( f ) == False ) return False; while( True ) { if( is_group( buf ) ) { if( cf->groups == NULL ) { cf->groups = ( grp*) malloc( sizeof( grp ) ); if( cf->groups == NULL ) return False; tmp = cf->groups; } else { tmp->next = (grp*)malloc( sizeof( grp ) ); if( tmp->next == NULL ) return False; tmp = tmp->next; } tmp->next = NULL; tmp->items = NULL; tmp->comment = NULL; ptr = strchr( buf, ']' ); *ptr = '\0'; tmp->grpName = strdup( buf + 1 ); if( tmp->grpName == NULL ) return False; if( read_items( f, cf, tmp ) == False ) return False; } else { if( is_comment( buf ) ) { if( cf->groups == NULL ) add_comment( &cf->comment, buf ); else add_comment( &tmp->comment, buf ); } if( readLine( f ) == False ) break; } } goto_first_group( cf ); return True;}/* * Function: read_items * * Description: * This function reads all items of current * group, and stores them in memory * * Arguments: * - FILE* f Config. file to read from * - conf_file* cf Structure to store data in * - grp* group Group where items should be stored * * Returns: * - Bool True On success, all items are stored * - Bool False On error * */static Bool read_items( FILE* f, conf_file* cf, grp* group ){ item* tmp; char* ptr; assert( f != NULL && cf != NULL && group != NULL ); tmp = NULL; while( readLine( f ) == True ) { if( is_group( buf ) ) return True; if( is_item( buf ) ) { if( tmp == NULL ) { group->items = (item*)malloc(sizeof( item ) ); if( group->items == NULL ) return False; tmp = group->items; } else { assert( tmp != NULL ); tmp->next = (item*)malloc( sizeof( item ) ); if( tmp->next == NULL ) return False; tmp = tmp->next; } tmp->next = NULL; tmp->comment = NULL; ptr = strchr( buf, '=' ); if( ptr == NULL ) return False; *ptr = '\0'; tmp->value = strdup( strip_spaces( ptr + 1 ) ); if( tmp->value == NULL ) return False; tmp->key = strdup( strip_spaces( buf ) ); if( tmp->key == NULL ) return False; } else if( is_comment( buf ) ) { if( tmp == NULL ) add_comment( &group->comment, buf ); else add_comment( &tmp->comment, buf ); } } return True;}/* Goto: group/item *//* Query: current group/item, group/conf empty *//* * Function: conf_empty * * Description: * This function checks if configuration file is empty * * Arguments: * - conf_file* cf The configuration file we check * * Returns: * - Bool True Config. file is empty * - Bool False Config. file is not empty * * Remarks: * When cf == NULL we also return True * */Bool conf_empty( conf_file* cf ){ if( cf == NULL ) return True; return ( cf->groups == NULL );}/* * Function: group_empty * * Description: * This functions checks if current group * has one or more items stored * * Arguments: * - conf_file* cf Config. file we check * * Returns: * - Bool True Group is empty * - Bool False Group is not empty * * Remarks * If cf == NULL, or no groups are stored, * we also return True * */Bool group_empty( conf_file* cf ){ if( cf == NULL ) return True; if( cf->currGroup == NULL ) return True; return cf->currGroup->items == NULL;}/* * Function: current_group * * Description: * This functions returns a pointer to * the current group * * Arguments: * - conf_file* cf The configuration file we're talking about * * Returns: * - grp* cg Pointer to current group * - grp* NULL On error (Means, cf = empty ) * */grp* current_group( conf_file* cf ){ if( cf == NULL ) return NULL; return cf->currGroup;}/* * Function: current_item * * Description: * This function returns pointer to * current item in current group * * Arguments: * - conf_file* cf The configuration file we're using * * Returns: * - item* ci Pointer to current item in current group * - item* NULL On error, which means current group is empty * or configuration file is empty * */item* current_item( conf_file* cf ){ if( cf == NULL ) return NULL; return cf->currItem;}/* * Function: goto_group * * Description: * This function changes the current group to gName * * Arguments: * - conf_file* cf The configuration file * - char* gName The group name * * Returns: * - grp* ng Pointer to the new group * - grp* NULL On error, gName does not exists * */grp* goto_group( conf_file* cf, char* gName ){ grp* tmp; if( cf == NULL || gName == NULL )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -