📄 libconf.c
字号:
* This function does no checking wheter or not * the item already exists. * */item* create_new_item( conf_file* cf, char* iName ){ item* nItem; if( cf == NULL ) return NULL; if( cf->currGroup == NULL ) return NULL; nItem = (item*)malloc( sizeof( item ) ); if( nItem == NULL ) return NULL; nItem->key = strdup( iName ); if( nItem->key == NULL ) { free( nItem ); return NULL; } nItem->value = NULL; nItem->next = cf->currGroup->items; cf->currGroup->items = nItem; return nItem;}/* * Function: del_group * * Description: * This function removes group with name gName and * all items stored in it from the configuration file. * * Arguments: * - config_file* cf The configuration file * - char* gName Name of group to remove * * Returns: * - Bool True On success * - Bool False On error (when gName could not be found) * * Remarks: * If gName == NULL, the current group will be removed. * */Bool del_group( conf_file* cf, char* gName ){ grp* ptr, *ptr1; if( cf == NULL ) return False; if( gName != NULL ) if( goto_group( cf, gName ) == NULL ) return False; ptr = ptr1 = cf->groups; while( ptr && compare( cf, ptr->grpName, gName ) != 0 ) { ptr1 = ptr; ptr = ptr->next; } if( ptr == NULL ) return False; if( ptr->comment != NULL ) free( ptr->comment ); ptr->comment = NULL; while( goto_first_item( cf ) != NULL ) { if( del_item( cf, NULL, item_key( goto_first_item( cf ) ) ) == False ) break; } free( ptr->grpName ); if( ptr != ptr1 ) ptr1->next = ptr->next; else cf->groups = ptr->next; if( goto_group( cf, group_name( ptr1->next ) ) == NULL ) if( goto_group( cf, group_name( ptr1 ) ) == NULL ) goto_first_group( cf ); free( ptr ); return True;} /* * Function: del_item * * Description: * This function removes an item from the configuration file. * * Arguments: * - conf_file* cf The configuration file * - char* gName The group from which to remove * - char* iName The item to remove * * Returns: * - Bool True On success * - Bool False On error (The item could not be found ). * * Remarks: * If gName == NULL, iName will be searched in current group * If iName == NULL, Current item will be removed * */Bool del_item( conf_file* cf, char* gName, char* iName ){ item* ptr, *ptr1; clear_error( cf ); if( cf == NULL ) return False; if( cf->currGroup == NULL ) return False; if( gName != NULL ) if( goto_group( cf, gName ) == NULL ) return False; ptr = ptr1 = cf->currGroup->items; while( ptr && compare( cf, ptr->key, iName ) != 0 ) { ptr1 = ptr; ptr = ptr->next; } if( ptr == NULL ) return False; free( ptr->key ); free( ptr->value ); if( ptr != ptr1 ) { if( ptr->comment != NULL ) add_comment( &ptr1->comment, ptr->comment ); ptr1->next = ptr->next; } else { if( ptr->comment != NULL ) add_comment( &cf->currGroup->comment, ptr->comment ); cf->currGroup->items = ptr->next; } if( ptr->comment != NULL ) free( ptr->comment ); if( goto_item( cf, item_key( ptr1->next ) ) == NULL ) if( goto_item( cf, item_key( ptr1 ) ) == NULL ) goto_first_item( cf ); free( ptr ); return True;}/* Misc. *//* * Function: readLine * * Description: * This function reads a line from the file f, * and removes spaces at the beginning and the end of the line. * The line is stored in global buffer buf * * Arguments: * - FILE* f The file to read from * * Returns: * - Bool True On success * - Bool False On error * */ static Bool readLine( FILE* f ){ buf[0] = EOF; if( fgets( buf, MAXBUF, f ) == NULL ) return False; buf[Strlen(buf) - 1 ] = '\0'; strip_spaces( buf ); return True;}/* * Function: is_comment * * Description: * This functions determines if a line * is a comment (first no white character is '#') * * Arguments: * char* s The line to test * * Returns: * Bool True Yes, the line is comment * Bool False No, the line is not a comment * */static Bool is_comment( char* s ){ if( s && (s[0] == '#' || strlen( s ) == 0 ) ) return True; return False;}/* * Function: is_group * * Description: * This function determines if a line is a group * ( first non white character '[' last character ']' ) * * Arguments: * - char* s The line to check * * Returns: * - Bool True Yes, the line is a group * - Bool false No, the line is not a group * */static Bool is_group( char* s ){ int n; n = strlen( s ); if( s && n && s[0] == '[' && s[n - 1] == ']' ) return True; return False;}/* * Function: is_item * * Description: * This function determines if a line is an item. * A line that is not a group or comment, and has * an equal sign ('=') is an item. * * Arguments: * - char* s The line to check * * Returns: * - Bool True Yes, the line is an item * - Bool False No, the line is not an item * */static Bool is_item( char* s ){ if( !is_comment( s ) && !is_group( s ) && strchr( s, '=' ) != NULL ) return True; return False;}/* * Function: strip_spaces * * Description: * This function removes all white-spaces at * the beginning and the end of the string s/ * * Arguments: * char* s The string we strip * * Returns: * char* s Pointer to stripped string * */static char* strip_spaces( char* s ){ int count; int i; char* ptr; count = 0; ptr = s; while( *ptr && isspace( *ptr ) ) { ptr++; count++; } if( count > 0 ) for( i = 0; i < Strlen( s ) - count + 1; i++ ) s[i] = s[i+count]; if( Strlen( s ) == 0 ) return s; ptr = s + Strlen( s ) - 1; while( ptr > s && isspace( *ptr ) ) *ptr-- = '\0'; return s;} /* * Function: compare * * Description: * This function compares two strings according * to case_significant in the configuration file cf * * Arguments: * - conf_file* cf The configuration file * - char* s1 The first string * - char* s2 The second string * * Returns: * - int 0 s1 == s2 * - int >0 s1 > s2 * - int <0 s1 < s2 * * Remarks: * when cf->case_significant == TRUE, strcmp is used * otherwise, strcasecmp is used * */static int compare( conf_file* cf, char* s1, char* s2 ){ if( cf == NULL || cf->case_significant ) return strcmp( s1, s2 ); else return strcasecmp( s1, s2 ); }/* * Function: case_significant * * Description: * This function sets cf->case_significant = True * Wich means that all string compares are case * sensitive * * Arguments: * - conf_file* cf The configuration file * * Returns: * - None * */void case_significant( conf_file* cf ){ if( cf != NULL ) cf->case_significant = True;}/* * Function: case_insignificant * * Description: * This function sets cf->case_significant = False * Wich means that all string compares are not case * sensitive * * Arguments: * - conf_file* cf The configuration file * * Returns: * - None * */void case_insignificant( conf_file* cf ){ if( cf != NULL ) cf->case_significant = False;}/* * Function: Strlen * * Description: * On my computer, strcmp(NULL) returns 3. * Since few functions test for NULL pointers * before calling strcmp, I wrote a new strlen * function that returns 0 * * Arguments: * - char* s The string which length we count * * Returns: * - int l The length of the string * */static int Strlen( char *s ){ int count = 0; if( s != NULL ) count = strlen( s ); return count;}/* * Function: clear_error * * Description: * This function sets cf->err to noErr. * * Arguments: * - conf_file* cf The configuration file * * returns: * - None * */ static void clear_error( conf_file* cf ){ if( cf != NULL ) cf->err = noErr;}/* * Function: set_error * * Description: * This function sets cf->err to err * * Arguments: * - conf_file* cf The configuration file * - confErr err The error to set * * Returns: * - None * */static void set_error( conf_file* cf, confErr err ){ if( cf != NULL ) cf->err = err;}/* * Function: get_error * * Description: * This function returns the error value * of configuration file cf * * Arguments: * - conf_file* cf The configuration file * * Returns: * - confErr err The error * */confErr get_error( conf_file* cf ){ if( cf == NULL ) return noErr; return cf->err;}static void add_comment( char**ptr, char* comment ){ if( *ptr == NULL ) *ptr = strdup( comment ); else { *ptr = (char*) realloc( *ptr, strlen(*ptr) + strlen( comment ) + 2 ); strcat( *ptr, "\n" ); strcat( *ptr, comment ); }}/* * EOF libconf/libconf.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -