📄 inifile.c
字号:
return FALSE; if( strnicmp(f_inifile.sz_filebuffer+i, psz_section, strlen(psz_section)) == 0 ) { // found section name, we try to seek ']' i += strlen(psz_section); while( i < f_inifile.i_filesize && (f_inifile.sz_filebuffer[i]==' ' || f_inifile.sz_filebuffer[i]=='\t') ) i++; if( i >= f_inifile.i_filesize ) return FALSE; if( f_inifile.sz_filebuffer[i] == ']' ) { // ']' found, so we: // 1. save section name to sz_lastsection memset( f_inifile.sz_lastsection, 0, sizeof(f_inifile.sz_lastsection) ); sprintf( f_inifile.sz_lastsection, "%s", psz_section ); // 2. get start and end position of section content i++; IniSearchContentStart( i ); IniSearchContentEnd( f_inifile.i_sc_startpos ); DBGPRINT(( "\nSearchSection -- section content start at %d, end at %d\n", f_inifile.i_sc_startpos, f_inifile.i_sc_endpos )); f_inifile.b_sectionfound = TRUE; return TRUE; } else { // no matching ']' b_skip = TRUE; } } else { // section name not match b_skip = TRUE; } break; default: // other b_skip = TRUE; break; } if ( b_skip ) { // ignore the line and forward while( i<f_inifile.i_filesize && f_inifile.sz_filebuffer[i] != '\n' ) i++; if( i >= f_inifile.i_filesize ) return FALSE; i++; // Jump to the next line } } return FALSE;}/******************************************************************************* * desc: search start position of section content *------------------------------------------------------------------------------ * param: const long i_position -- next position to ']'<section name end mark> *------------------------------------------------------------------------------ * return: TRUE -- found*******************************************************************************/int IniSearchContentStart( const long i_position ){ long i = 0; long i_temp = 0; i = i_position; // we ignore the rest of sectio name line while( i < f_inifile.i_filesize && f_inifile.sz_filebuffer[i] != '\n' ) i++; if( f_inifile.sz_filebuffer[i] == '\n' ) i++; // if reach end of file, we append some \n if( i >= f_inifile.i_filesize ) { for( i_temp=0; i_temp<2; i_temp++ ) { f_inifile.sz_filebuffer[i+i_temp] = '\n'; } f_inifile.i_sc_startpos = i + 1; f_inifile.i_filesize += 2; f_inifile.b_bufferchanged = TRUE; return TRUE; } // not reach end of file f_inifile.i_sc_startpos = i; // if it's '['(which means no enough \n between setciotns), we insert some \n if( f_inifile.sz_filebuffer[i] == '[' ) { for( i_temp=f_inifile.i_filesize; i_temp>=f_inifile.i_sc_startpos; i_temp-- ) { f_inifile.sz_filebuffer[i_temp+3] = f_inifile.sz_filebuffer[i_temp]; } for( i_temp=0; i_temp<3; i_temp++ ) { f_inifile.sz_filebuffer[f_inifile.i_sc_startpos+i_temp] = '\n'; } f_inifile.b_bufferchanged = TRUE; f_inifile.i_filesize += 3; } return TRUE;}/******************************************************************************* * desc: search end position of section content *------------------------------------------------------------------------------ * param: const long i_startpos -- start position of section content *------------------------------------------------------------------------------ * return: TRUE -- found ******************************************************************************/int IniSearchContentEnd( const long i_startpos ){ long i = 0; long i_temp = 0; i = i_startpos; // try to serach position of next '[' while( i < f_inifile.i_filesize ) { // skip space, tab and \n while( i < f_inifile.i_filesize && (f_inifile.sz_filebuffer[i]==' ' || f_inifile.sz_filebuffer[i]=='\t' || f_inifile.sz_filebuffer[i]=='\n') ) i++; //========================================================================== // 1. found '[', we try to find a position before '[' //========================================================================== if( f_inifile.sz_filebuffer[i] == '[' ) { //DBGPRINT(( "SearchContentEnd -- position of next [ = %ld\n", i )); // skip \n backword i_temp = i; while( i > i_startpos+1 && f_inifile.sz_filebuffer[i-1] == '\n' ) i--; f_inifile.i_sc_endpos = i; // if no enough \n between setciotns, we insert some \n if( f_inifile.i_sc_endpos == i_temp ) { for( i=f_inifile.i_filesize; i>=i_temp; i-- ) { f_inifile.sz_filebuffer[i+2] = f_inifile.sz_filebuffer[i]; } for( i=0; i<2; i++ ) { f_inifile.sz_filebuffer[i_temp+i] = '\n'; } f_inifile.i_filesize += 2; f_inifile.b_bufferchanged = TRUE; } return TRUE; } else { // ignore the line and forward while( i < f_inifile.i_filesize && f_inifile.sz_filebuffer[i] != '\n' ) i++; if( f_inifile.sz_filebuffer[i] == '\n' ) i++; } //========================================================================== // 2. if reach end of file //========================================================================== if( i == f_inifile.i_filesize ) { // skip \n backword while( i > i_startpos+1 && f_inifile.sz_filebuffer[i-1] == '\n' ) i--; f_inifile.i_sc_endpos = i; // we append some \n if not enough if( i >= f_inifile.i_filesize-1 ) { i = f_inifile.i_filesize; for( i_temp=0; i_temp<2; i_temp++ ) { f_inifile.sz_filebuffer[i_temp+i] = '\n'; } f_inifile.i_filesize += 2; f_inifile.b_bufferchanged = TRUE; } return TRUE; } } return TRUE;}#if 0//==============================================================================// test code//==============================================================================int main(int argc, char **argv){ char sz_value[256]; int i_status = 0; long l_status = 0; double d_status = 0; int b_status; DBGPRINT(( "\nMain -- ------------------DEMO.C-------------------------\n" )); // 1. open ini file i_status = IniOpenFile( "test.ini" ); if( i_status != 0 ) return -1; // 如果不确定该Ini文件是否为Unix类型文件,调用转换函数进行转换 //if (ToUnixStyle() != 0) DBGPRINT(( "Fail to convert to unix style file.\n" ));#if 1 // 2. serach section b_status = IniSearchSection( "sectionA" ); if( b_status == TRUE ) DBGPRINT(( "Main -- sectionA found, position = %ld\n", f_inifile.i_sc_startpos )); else DBGPRINT(( "Main -- sectionA not found.\n" ));#endif#if 1 // 3. get/set value of string type memset( sz_value, 0, sizeof(sz_value) ); b_status = IniGetString( "sectionA", "AID", sz_value ); DBGPRINT(( "Main -- sectionA--AID = %s\n", sz_value )); DBGPRINT(( "\nMain -- to Set String" )); b_status = IniSetString( "sectionC", "PID", "ABCD1234561234567890" ); DBGPRINT(( "Main -- PID set to: %s\n", "ABCD1234561234567890" )); memset( sz_value, 0, sizeof(sz_value) ); b_status = IniGetString( "sectionC", "PID", sz_value ); DBGPRINT(( "Main -- sectionC--PID = %s\n", sz_value )); memset( sz_value, 0, sizeof(sz_value) ); b_status = IniGetString( "sectionC", "AID", sz_value ); DBGPRINT(( "Main -- sectionC--AID = %s\n", sz_value ));#endif#if 0 // 4. get/set value of integer type i_status = IniGetInteger( "sectionA", "NNN", 99 ); DBGPRINT(( "Main -- sectionA--NNN = %d\n", i_status )); i_status = IniGetInteger( "sectionA", "NUM", 99 ); DBGPRINT(( "Main -- sectionA--NUM = %d\n", i_status )); DBGPRINT(( "\nMain -- to SetInteger\n" ); b_status = IniSetInteger( "sectionA", "NUM", 12345 ); DBGPRINT(( "Main -- NUM set to: %d\n", 12345 )); i_status = IniGetInteger( "sectionA", "NUM", 999 ); DBGPRINT(( "Main -- sectionA--NUM = %d\n", i_status ));#endif#if 0 // 5. get/set value of long type l_status = IniGetLong( "sectionA", "NNN", 99 ); DBGPRINT(( "Main -- sectionA--NNN = %ld\n", l_status )); l_status = IniGetLong( "sectionA", "NUM", 99 ); DBGPRINT(( "Main -- sectionA--NUM = %ld\n", l_status )); DBGPRINT(( "\nMain -- to SetInteger\n" )); b_status = IniSetLong( "sectionA", "NUM", 12345678 ); DBGPRINT(( "Main -- NUM set to: %ld\n", 12345678 )); l_status = IniGetLong( "sectionA", "NUM", 999 ); DBGPRINT(( "Main -- sectionA--NUM = %ld\n", l_status ));#endif#if 0 // 6. get/set value of double type d_status = IniGetDouble( "sectionA", "NNN", 99 ); DBGPRINT(( "Main -- sectionA--NNN = %g\n", d_status )); d_status = IniGetDouble( "sectionA", "NUM", 99 ); DBGPRINT(( "Main -- sectionA--NUM = %g\n", d_status )); DBGPRINT(( "\nMain -- to Set Number" )); b_status = IniSetDouble( "sectionA", "NUM", 12345678.123 ); DBGPRINT(( "Main -- NUM set to: %g\n", 12345678.123 )); d_status = IniGetDouble( "sectionA", "NUM", 999 ); DBGPRINT(( "Main -- sectionA--NUM = %g\n", d_status ));#endif#if 1 // 7. get/set value of int type b_status = IniGetBool( "sectionA", "BOOLA", FALSE ); DBGPRINT(( "Main -- sectionA--BOOLA = %d\n", (int)b_status )); b_status = IniGetBool( "sectionA", "BOOLB", FALSE ); DBGPRINT(( "Main -- sectionA--BOOLB = %d\n", (int)b_status )); DBGPRINT(( "\nMain -- to Set Bool" )); b_status = IniSetBool( "sectionA", "BOOLB", FALSE ); DBGPRINT(( "Main -- BOOLB set to: %d\n", (int)FALSE )); b_status = IniGetBool( "sectionA", "BOOLB", FALSE ); DBGPRINT(( "Main -- sectionA--BOOLB = %d\n", (int)b_status ));#endif // 9. close file IniCloseFile(); DBGPRINT(( "Main -- ---------------------E N D-------------------------\n\n" )); return 0;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -