⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inifile.c

📁 Unix/Linux下的INI文件读取、设置函数
💻 C
📖 第 1 页 / 共 3 页
字号:
      f_inifile.i_filesize  -= i_temp;      for( i=f_inifile.i_filesize; i<f_inifile.i_filesize+i_temp; i++ ) {        DBGPRINT(( "%c", f_inifile.sz_filebuffer[i] ));        //sz_filebuffer[i] = '\0';        memset( f_inifile.sz_filebuffer+i, 0, 1 );        DBGPRINT(( "N%c", f_inifile.sz_filebuffer[i] ));      }      DBGPRINT(( "\n" ));#endif      // 2. replace the following chars with spaces      i_temp  = i_oldvaluelen - i_newvaluelen;      for( i=0; i<i_temp; i++ ) {        f_inifile.sz_filebuffer[f_inifile.i_value_endpos-i] = ' ';      }    }    else {      // same length ***********************************************************      // just replace old value      strncpy( f_inifile.sz_filebuffer+f_inifile.i_value_startpos, psz_value, strlen(psz_value) );    }    //DBGPRINT(( "SetString -- after  update, file size = %d\n", f_inifile.i_filesize ));    f_inifile.b_bufferchanged = TRUE;  }  else {    //==========================================================================    // key not found, we add key value by inserting a new line    //==========================================================================    // 1. make new line    memset( sz_value, 0, sizeof(sz_value) );    sprintf( sz_value, "\n%s = %s\n", psz_key, psz_value );    i_temp	= strlen( sz_value );    // 2. move buffer for new line    for( i=f_inifile.i_filesize; i>=f_inifile.i_sc_endpos; i-- ) {      f_inifile.sz_filebuffer[i+i_temp] = f_inifile.sz_filebuffer[i];    }    // 3. copy new line to buffer    strncpy( f_inifile.sz_filebuffer+f_inifile.i_sc_endpos, sz_value, strlen(sz_value) );    f_inifile.i_filesize += i_temp;    f_inifile.b_bufferchanged = TRUE;  }  // search end position of content again  IniSearchContentEnd( f_inifile.i_sc_startpos );	return  TRUE;}/******************************************************************************* *   desc: get a interger value by key *------------------------------------------------------------------------------ *  param: const char * psz_section     -- section name *         const char * psz_key         -- key name *         int          i_default       -- default value *------------------------------------------------------------------------------ * return: key value or default value*******************************************************************************/int IniGetInteger( const char * psz_section, const char * psz_key,                   int i_default ){  char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE];  //memset( sz_buffer, 0, sizeof(sz_buffer) );  if( IniGetString(psz_section, psz_key, sz_buffer) ) {    DBGPRINT(( "GetInteger -- key value is: %s\n", sz_buffer ));    if( strlen(sz_buffer) > 2 ) {      // maybe a hex value      if( sz_buffer[0] == '0'  && ( sz_buffer[1]=='x' || sz_buffer[1]=='X' ) ) {        return  (int)( strtol(sz_buffer, (char **)NULL, 16) );      }    }    return  atoi( sz_buffer );  }  return  i_default;}/******************************************************************************* *   desc: set a interger value *------------------------------------------------------------------------------ *  param: const char * psz_section     -- section name *         const char * psz_key         -- key name *         const int    i_value         -- key value *------------------------------------------------------------------------------ * return: TRUE*******************************************************************************/int IniSetInteger( const char * psz_section, const char * psz_key,                    const int i_value ){  char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE];  DBGPRINT(( "SetInteger -- key value is: %d\n", i_value ));  memset( sz_buffer, 0, sizeof(sz_buffer) );  sprintf( sz_buffer, "%d", i_value );  DBGPRINT(( "SetInteger -- value buffer is: %s\n", sz_buffer ));  IniSetString( psz_section, psz_key, sz_buffer );  return  TRUE;}/******************************************************************************* *   desc: get a long value by key *------------------------------------------------------------------------------ *  param: const char * psz_section     -- section name *         const char * psz_key         -- key name *         long         i_default       -- default value *------------------------------------------------------------------------------ * return: key value or default value*******************************************************************************/long IniGetLong( const char * psz_section, const char * psz_key,                 long i_default ){  char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE];  //memset( sz_buffer, 0, sizeof(sz_buffer) );  if( IniGetString(psz_section, psz_key, sz_buffer) ) {    if( strlen(sz_buffer) > 2 ) {      // maybe a hex value      if( sz_buffer[0] == '0'  && ( sz_buffer[1]=='x' || sz_buffer[1]=='X' ) ) {        return  ( strtol(sz_buffer, (char **)NULL, 16) );      }    }    return  atol( sz_buffer );  }  return  i_default;}/******************************************************************************* *   desc: set a long value *------------------------------------------------------------------------------ *  param: const char * psz_section     -- section name *         const char * psz_key         -- key name *         const long   i_value         -- key value *------------------------------------------------------------------------------ * return: TRUE*******************************************************************************/int IniSetLong( const char * psz_section, const char * psz_key,                 const long i_value ){  char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE];  memset( sz_buffer, 0, sizeof(sz_buffer) );  sprintf( sz_buffer, "%ld", i_value );  IniSetString( psz_section, psz_key, sz_buffer );  return  TRUE;}/******************************************************************************* *   desc: get a double value by key *------------------------------------------------------------------------------ *  param: const char * psz_section     -- section name *         const char * psz_key         -- key name *         double       i_default       -- default value *------------------------------------------------------------------------------ * return: key value or default value*******************************************************************************/double IniGetDouble( const char * psz_section, const char * psz_key,                     double i_default ){  char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE];  //memset( sz_buffer, 0, sizeof(sz_buffer) );  if( IniGetString(psz_section, psz_key, sz_buffer) ) {    return  atof( sz_buffer );  }  return  i_default;}/******************************************************************************* *   desc: set a double value *------------------------------------------------------------------------------ *  param: const char * psz_section     -- section name *         const char * psz_key         -- key name *         const double i_value         -- key value *------------------------------------------------------------------------------ * return: TRUE*******************************************************************************/int IniSetDouble( const char * psz_section, const char * psz_key,                   const double i_value ){  char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE];  memset( sz_buffer, 0, sizeof(sz_buffer) );  sprintf( sz_buffer, "%g", i_value );  IniSetString( psz_section, psz_key, sz_buffer );  return  TRUE;}/******************************************************************************* *   desc: get a bool value by key *------------------------------------------------------------------------------ *  param: const char * psz_section     -- section name *         const char * psz_key         -- key name *         bool         b_default       -- default value *------------------------------------------------------------------------------ * return: key value or default value*******************************************************************************/int IniGetBool( const char * psz_section, const char * psz_key,                 int b_default ){  char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE];  //memset( sz_buffer, 0, sizeof(sz_buffer) );  if( IniGetString(psz_section, psz_key, sz_buffer) ) {    DBGPRINT(( "GetBool -- key value is: %s\n", sz_buffer ));    if( strnicmp(sz_buffer, "y", strlen("y")) == 0      ||        strnicmp(sz_buffer, "yes", strlen("yes")) == 0  ||        strnicmp(sz_buffer, "true", strlen("true")) == 0  )    return  TRUE;    if( strnicmp(sz_buffer, "n", strlen("n")) == 0      ||        strnicmp(sz_buffer, "no", strlen("no")) == 0    ||        strnicmp(sz_buffer, "false", strlen("false")) == 0  )    return  FALSE;  }  return  b_default;}/******************************************************************************* *   desc: set a bool value *------------------------------------------------------------------------------ *  param: const char * psz_section     -- section name *         const char * psz_key         -- key name *         const bool   b_value         -- key value *------------------------------------------------------------------------------ * return: TRUE*******************************************************************************/int IniSetBool( const char * psz_section, const char * psz_key,                 const int b_value ){  char sz_buffer[M_MAX_INTVAL_BUFFER_SIZE];  memset( sz_buffer, 0, sizeof(sz_buffer) );  if( b_value )    sprintf( sz_buffer, "%s", "true" );  else    sprintf( sz_buffer, "%s", "false" );  IniSetString( psz_section, psz_key, sz_buffer );  return  TRUE;}/******************************************************************************* *   desc: search a section *------------------------------------------------------------------------------ *  param: const char * psz_section     -- section name *------------------------------------------------------------------------------ * return: TRUE   -- section found *         FALSE  -- section not found*******************************************************************************/int IniSearchSection( const char * psz_section ){  long i = 0;  int b_skip = FALSE;  f_inifile.b_sectionfound  = FALSE;  // file error  if( f_inifile.p_inifile == NULL )    return  FALSE;  // section name can't be null  if( strlen(psz_section) == 0 )    return	FALSE;  // same section name to the previous one  if( strnicmp(f_inifile.sz_lastsection, psz_section, strlen(psz_section)) == 0 ) {    f_inifile.b_sectionfound  = TRUE;    return  TRUE;  }  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++;    // return if reach end of file    if( i >= f_inifile.i_filesize )      return  FALSE;    b_skip = FALSE;    switch( f_inifile.sz_filebuffer[i] ) {    case  '#':            // comment      b_skip = TRUE;      break;    case  '[':            // section begin mark -- [      i++;      while( i < f_inifile.i_filesize && (f_inifile.sz_filebuffer[i]==' ' ||             f_inifile.sz_filebuffer[i]=='\t') )        i++;      if( i >= f_inifile.i_filesize )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -