📄 pub_file.c
字号:
/* * Program Name : pub_File.c * Description : 文件操作相关函数 * Create Date : 2004-08-31 * Author : shilyu *functionnum 006 */#include "./../inc/pub.h"#define STR_BUF_LEN 1024/* 001 *读配置文件(去掉空格) */int GetFileString ( FileName , Section, Key , KeyValue)char *FileName ;char *Section ;char *Key ;char *KeyValue ;{ FILE *fp ; char buf [ STR_BUF_LEN ] ; char key [ STR_BUF_LEN ] ; char tmp_keyvalue [ STR_BUF_LEN ] ; int i ; /* Open file */ if ( ( fp = fopen ( FileName , "r" ) ) == NULL ) { #ifdef DEBUG fprintf ( stderr, "Can't open file:%s\n" , FileName ) ;#endif return ( -1 ) ; } /* Find section */ while ( 1 ) { memset ( buf , 0 , STR_BUF_LEN ) ; if ( feof ( fp ) || fgets ( buf , STR_BUF_LEN , fp ) == ( char * ) NULL ) { fclose ( fp ) ; fprintf ( stderr , "Can't find section:%s\n" , Section ) ; return ( -1 ) ; } if ( buf [ 0 ] = '[' && strncmp ( Section , buf + 1 , strlen ( Section ) ) == 0 && buf [ strlen ( Section ) + 1 ] == ']' ) break ; } /* Find Key */ while ( 1 ) { memset ( buf , 0 , STR_BUF_LEN ) ; if ( feof ( fp ) || fgets ( buf , STR_BUF_LEN , fp ) == ( char * ) NULL || buf [ 0 ] == '[' ) { fclose ( fp ) ; fprintf ( stderr , "Can't find key:%s\n" , Key ) ; return ( -1 ) ; } /* Check '=' */ for ( i = 0 ; i < strlen ( buf ) && buf [i] != '=' ; i++ ) ; if ( i < strlen ( buf ) ) { memset ( key , 0x00 , STR_BUF_LEN ) ; strncpy ( key , buf , i ) ; StrTrim ( key , 'A' ) ; if ( strcmp ( key , Key ) == 0 ) break ; /* while */ } } /* Return */ fclose ( fp ) ; StrTrim ( buf , 'R' ) ; strcpy ( tmp_keyvalue , buf + i + 1 ) ; if ( tmp_keyvalue [ strlen ( tmp_keyvalue ) - 1 ] = '\n' ) tmp_keyvalue [ strlen ( tmp_keyvalue ) - 1 ] = 0x00 ; StrTrim ( tmp_keyvalue , 'A' ) ; strcpy ( KeyValue , tmp_keyvalue ) ; return ( 0 ) ;}/* 002 * 从文件当前位置开始找到节在文件中的起始位置 * 返回行数 */int FindSection ( fp , Section ) FILE *fp ;char *Section ;{ char buf [ STR_BUF_LEN ] ; int i = 0 ; /* Find section */ while ( 1 ) { memset ( buf , 0 , STR_BUF_LEN ) ; if ( feof ( fp ) ) {#ifdef DEBUG fprintf ( stderr , "feof Can't find section:%s\n" , Section ) ;#endif return ( -1 ) ; } if( fgets ( buf , STR_BUF_LEN , fp ) == ( char * ) NULL ) {#ifdef DEBUG fprintf ( stderr , "is null Can't find section:%s\n" , Section ) ;#endif return ( -1 ) ; } i++ ; if ( buf [ 0 ] = '[' && strncmp ( Section , buf + 1 , strlen ( Section ) ) == 0 && buf [ strlen ( Section ) + 1 ] == ']' ) break ; } return ( i ) ;}/* 003 * 计算某一起始节到下一节之间的行数 */int GetLinesBetweenSection ( fp , StartSection , EndSection )FILE *fp ;char *StartSection ;char *EndSection ;{ int line1 , line2 , line ; rewind ( fp ) ; line1 = FindSection ( fp , EndSection ) ; if ( line1 < 0 ) return ( 0 ) ; rewind ( fp ) ; line2 = FindSection ( fp , StartSection ) ; if ( line2 < 0 ) return ( 0 ) ; line = line1 - line2 ; return ( ( line > 0 ) ? ( line - 1 ) : 0 ) ;}/* 004 * 获得某一起始节到下一节之间的最宽列的宽度 */int GetMaxWidthBetweenSection ( fp , StartSection , EndSection )FILE *fp ;char *StartSection ;char *EndSection ;{ int MaxCol , /* 最大列 */ Lines , /* 两节之间的行数 */ i ; /* 循环控制变量 */ char rbuf [ STR_BUF_LEN ] ; /* 初始化最大列数 */ MaxCol = 0 ; /* 取得两节之间的行数 */ rewind ( fp ) ; Lines = GetLinesBetweenSection ( fp , StartSection , EndSection ) ; /* 计算两节之间最宽列数 */ if ( Lines != 0 ) { rewind ( fp ) ; FindSection ( fp , StartSection ) ; /*将文件指针定位到第一个节点处*/ for ( i = 0 ; i < Lines ; i++ ) { fgets ( rbuf , STR_BUF_LEN , fp ) ; if ( strlen ( rbuf ) > MaxCol ) MaxCol = strlen ( rbuf ) ; } } return ( MaxCol ) ;}/* 005 *读配置文件(去掉空格) */int GetFileStringNoTail ( FileName , Section, Key , KeyValue)char *FileName ;char *Section ;char *Key ;char *KeyValue ;{ FILE *fp ; char buf [ STR_BUF_LEN ] , key [ STR_BUF_LEN ] , tmp_keyvalue [ STR_BUF_LEN ] ; int i ; /* Open file */ if ( ( fp = fopen ( FileName , "r" ) ) == NULL ) {#ifdef DEBUG fprintf ( stderr, "Can't open file:%s\n" , FileName ) ;#endif return ( -1 ) ; } /* Find section */ while ( 1 ) { memset ( buf , 0 , STR_BUF_LEN ) ; if ( feof ( fp ) || fgets ( buf , STR_BUF_LEN , fp ) == ( char * ) NULL ) { fclose ( fp ) ; fprintf ( stderr , "Can't find section:%s\n" , Section ) ; return ( -1 ) ; } if ( buf [ 0 ] = '[' && strncmp ( Section , buf + 1 , strlen ( Section ) ) == 0 && buf [ strlen ( Section ) + 1 ] == ']' ) break ; } /* Find Key */ while ( 1 ) { memset ( buf , 0 , STR_BUF_LEN ) ; if ( feof ( fp ) || fgets ( buf , STR_BUF_LEN , fp ) == ( char * ) NULL || buf [ 0 ] == '[' ) { fclose ( fp ) ; fprintf ( stderr , "Can't find key:%s\n" , Key ) ; return ( -1 ) ; } /* Check '=' */ for ( i = 0 ; i < strlen ( buf ) && buf [i] != '=' ; i++ ) ; if ( i < strlen ( buf ) ) { memset ( key , 0x00 , STR_BUF_LEN ) ; strncpy ( key , buf , i ) ; StrTrim ( key , 'A' ) ; if ( strcmp ( key , Key ) == 0 ) break ; /* while */ } } /* Return */ fclose ( fp ) ; /* StrTrim ( buf , 'R' ) ; */ strcpy ( tmp_keyvalue , buf + i + 1 ) ; if ( tmp_keyvalue [ strlen ( tmp_keyvalue ) - 1 ] = '\n' ) tmp_keyvalue [ strlen ( tmp_keyvalue ) - 1 ] = 0x00 ; /* StrTrim ( tmp_keyvalue , 'A' ) ; */ memcpy ( KeyValue , tmp_keyvalue, strlen( tmp_keyvalue ) ) ; return ( 0 ) ;}/* 006 *改变文件的权限 */void FileChmod ( char *FileName, mode_t Mode_t ) /* mode_t == usigned int */{ chmod ( FileName , Mode_t ) ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -