📄 libconf.c
字号:
return NULL; if( cf->currGroup != NULL ) if( compare( cf, group_name( cf->currGroup ), gName ) == 0 ) { goto_first_item( cf ); return cf->currGroup; } tmp = cf->groups; while( tmp != NULL ) { if( compare( cf, group_name( tmp ), gName ) == 0 ) { cf->currGroup = tmp; goto_first_item( cf ); return tmp; } tmp = tmp->next; } return NULL;}/* * Function: goto_item * * Description: * This function changes the current item to iName * in the current group * * Arguments: * - conf_file* cf The configuration file * - char* iName The name of the item * * Returns: * - item* ni The new current item * - item* NULL On error, item does not exists * */item* goto_item( conf_file* cf, char* iName ){ item* tmp; if( cf == NULL || iName == NULL ) return NULL; assert( cf->currGroup != NULL ); if( cf->currItem != NULL ) if( compare( cf, item_key( cf->currItem) , iName ) == 0 ) return cf->currItem; tmp = cf->currGroup->items; while( tmp != NULL ) { if( compare( cf, item_key( tmp ), iName ) == 0 ) { cf->currItem = tmp; return tmp; } tmp = tmp->next; } return NULL;}/* * Function: goto_first_group * * Description: * This functions changes current group * to the first group in the configuration file * * Arguments: * - conf_file* cf The configuration file * * Returns: * - grp* ng Pointer to the new current group * - grp* NULL On error, cf = empty * */ grp* goto_first_group( conf_file* cf ){ if( cf == NULL ) return NULL; cf->currGroup = cf->groups; goto_first_item( cf ); return cf->groups;}/* * Function: goto_next_group * * Description: * This function changes the current group * to the next group. * * Arguments: * - conf_file* cf The configuration file * * Returns: * - grp* ng The new current group * - grp* NULL On error, cf is empty * or no more groups * */grp* goto_next_group( conf_file* cf ){ if( cf == NULL || cf->currGroup == NULL ) return NULL; if( cf->currGroup->next == NULL ) return NULL; cf->currGroup = cf->currGroup->next; goto_first_item( cf ); return cf->currGroup;}/* * Function: goto_first_item * * Description: * This function changes the current item * to the first item in the current group * * Arguments: * - conf_file* cf The configuration file * * Returns: * - item* ni Pointer to new current item * - item* NULL On error, cf or group is empty * */item* goto_first_item( conf_file* cf ){ if( cf == NULL || cf->currGroup == NULL ) return NULL; cf->currItem = cf->currGroup->items; return cf->currItem;}/* * Function: goto_next_item * * Description: * This function changes the current item * to the next one. * * Arguments: * - conf_file* cf The configuration file * * Returns: * - item* ni Pointer to new current item * - item* NULL On error, cf is empty * or no more items * */item* goto_next_item( conf_file* cf ){ if( cf== NULL ) return NULL; if( cf->currItem == NULL ) return NULL; cf->currItem = cf->currItem->next; return cf->currItem;}/* Get: groupName, itemKey, itemValue *//* int, string */ /* * Function: group_name * * Description: * This function returns the name of group 'group' * * Arguments: * - grp* group Pointer to group * * Returns: * - char* gName Name of the group * - char* NULL On error * */char* group_name( grp* group ){ if( group == NULL ) return NULL; return group->grpName;}/* * Function: item_key * * Description: * This function returns the item name * of the item * * Arguments: * - item* i Pointer to item structure * * Returns: * - char* in Name of item * - char* NULL On error * */char* item_key( item* i ){ if( i == NULL ) return NULL; return i->key;}/* * Function: item_value * * Description: * This function returns the value of the item * * Arguments: * - char* iv Value of item * - char* NULL On error * * Remarks: * This function only returns string, no * integers. * */char* item_value( item* i ){ if( i == NULL ) return NULL; return i->value;}/* * Function: get_str * * Description: * This function returns the string value that belongs * to iName in the group 'gName'. * * Arguments: * - conf_file* cf The configuration file * - char* gName Name of group in which to search * - char* iName Name of item to search for * - char* default Default value to return * * Returns: * - char* value Value that was found * - char* default On error, or when item could not be found * * Remarks: * When gName == NULL, iName is searched in current group * When iName == NULL, Value of current item is returned * * When default is returned (on error, or when item could * not be found), err will be set to indicate the error. * Otherwise err is set to noErr. * */char* get_str( conf_file* cf, char* gName, char* iName, char* Default ){ clear_error( cf ); if( cf == NULL ) { set_error( cf, notFound ); return Default; } if( gName != NULL ) if( goto_group( cf, gName ) == NULL ) { set_error( cf, notFound ); return Default; } if( iName != NULL ) if( goto_item( cf, iName ) == NULL ) { set_error( cf, notFound ); return Default; } return item_value( cf->currItem );}/* * Function: get_int * * Description: * This function returns the integer value that belongs * to iName in the group 'gName'. * Arguments: * - conf_file* cf The configuration file * - char* gName Name of group in which to search * - char* iName Name of item to search fot * - int default Default value to return * * Returns: * - int value Value that was found * - int default On error, or when item could not be found * * Remarks: * This function simply calls get_str( cg, gName, iName, NULL ) * And next, converts the returned string to an integer using * strtol. * See get_str for more details. * Numbers in the configuration file can be in base 10 (default) * base 8 (prefex 0) and base 16 (prefix 0x), just like * the C language. * */int get_int( conf_file* cf, char* gName, char* iName, int Default ) { char* ptr; ptr = get_str( cf, gName, iName, NULL ); if( ptr == NULL ) return Default; return strtol( ptr, NULL, 0 );}/* Set: string, int *//* * Function: set_str * * Description: * This function sets the string value of iName in * gName to value. If gName does not exists it is created * If iName does not exists it is created. * * Arguments: * - conf_file* cf The configuration file * - char* gName The group name to store value in * - char* iName The item name which value sould be set * - char* value The value to set * * Returns: * - Bool True On success * - Bool False On error * * Remarks: * If gName == NULL, iName will be searched in current group * If iName == NULL, value of current item will be set * * When needed, a new group or item is created. * */Bool set_str( conf_file* cf, char* gName, char* iName, char* value ){ if( cf == NULL ) return False; if( gName != NULL ) { if( goto_group( cf, gName ) == NULL ) { if( create_new_group( cf, gName ) == NULL ) return False; goto_group( cf, gName ); } } if( iName != NULL ) { if( goto_item( cf, iName ) == NULL ) { if( create_new_item( cf, iName ) == NULL ) return False; goto_item( cf, iName ); } } if( value != NULL ) { if( cf->currItem->value != NULL ) free( cf->currItem->value ); cf->currItem->value = strdup( value ); if( cf->currItem->value == NULL ) return False; } return True;}/* * Function: set_int * * Description: * This function sets the integer value of iName in * gName to value. If gName does not exists it is created * If iName does not exists it is created. * * Arguments: * - conf_file* cf The configuration file * - char* gName The group name to store value in * - char* iName The item name which value sould be set * - int value The value to set * * Returns: * - Bool True On success * - Bool False On error * * Remarks: * This function simply prints the value to a string, and * calls set_str( cf, gName, iName, buf ) * * see set_str for more details * */Bool set_int( conf_file* cf, char* gName, char* iName, int value ){ char buf[20]; sprintf( buf, "%d", value ); return set_str( cf, gName, iName, buf );}/* Create/Delete Group/Item *//* * Function: create_new_group * * Description: * This function creates a new group with name gName * in front of all other groups. * * Arguments: * - conf_file* cf The configuration file * - char* gName The group to create * * Returns: * - grp* ng Pointer to new created group * - grp* NULL On error * * Remarks: * This function does no checking wheter or not * the group already exists. * */grp* create_new_group( conf_file* cf, char* gName ){ grp* nGrp; if( cf == NULL ) return NULL; nGrp = (grp*)malloc( sizeof( grp ) ); if( nGrp == NULL ) return NULL; nGrp->grpName = strdup( gName ); if( nGrp->grpName == NULL ) { free( nGrp ); return NULL; } nGrp->items = NULL; nGrp->next = cf->groups; cf->groups = nGrp; return nGrp;}/* * Function: create_new_item * * Description * This function creates a new item with name iName * in the current group in front of all other items * * Arguments: * - conf_file* cf The configuration file * - char* iName The item to create * * Returns: * - item* ni The new created item * - item* NULL On error * * Remarks:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -