Example #1
0
bool CIniFile::ReadString(const char* ini_str)
{
  CString   line;
  CString   keyname, valuename, value;
  CString::size_type pLeft, pRight;

  Clear();
  CString _str = ini_str;
  while( getline( _str, line)) {
    // To be compatible with Win32, check for existence of '\r'.
    // Win32 files have the '\r' and Unix files don't at the end of a line.
    // Note that the '\r' will be written to INI files from
    // Unix so that the created INI file can be read under Win32
    // without change.
    //printf("str=%s \n line=%s\n",_str.c_str(),line.c_str());
    if ( line[line.length() - 1] == '\r')
      line = line.substr( 0, line.length() - 1);

    if ( line.length()) {
      // Check that the user hasn't openned a binary file by checking the first
      // character of each line!
      if ( !isprint( line.at(0))) {
//       printf( "Failing on char %d\n", line[0]);
//       f.close();
//        return false;
      };
      if (( pLeft = line.find_first_of(";#[=:")) != CString::npos) {
       switch ( line[pLeft]) {
       case '[':
         if ((pRight = line.find_last_of("]")) != CString::npos &&
              pRight > pLeft) {
           keyname = line.substr( pLeft + 1, pRight - pLeft - 1);
           AddKeyName( keyname);
//           printf("Section: %s\n",keyname.c_str());
         }
       break;

       case '=':
//       case ':':
         valuename = line.substr( 0, pLeft);
         value = line.substr( pLeft + 1);
         AddValue( keyname, valuename, value);
//         printf("%s=%s\n",valuename.c_str(),value.c_str());
       break;

       case ';':
       case '#':
         if ( !names.size())
            HeaderComment( line.substr( pLeft + 1));
          else
            KeyComment( keyname, line.substr( pLeft + 1));
       break;
       };//switch()
      }//if(( pLeft = line.find_first_of(";#[=")) != CString::npos)
    };//if ( line.length())
  };//while( getline( ini_str, line))

  if(minSize <= 0)
    return true;

  if ( names.size())
    return true;

  return false;
};
Example #2
0
bool CIniFile::ReadFile(const CString&  _path)
{
  // Normally you would use ifstream, but the SGI CC compiler has
  // a few bugs with ifstream. So ... fstream used.
  ifstream f;
  CString   line;
  CString   keyname, valuename, value;
  CString::size_type pLeft, pRight;

  //если файл испортили из-вне.
  //проверка испорченности файла: открытие
  f.open( _path.c_str());
  if ( f.fail()){
    printf("Warning: ini file:'%s' is NOT OPENED\n", _path.c_str());
    f.close();
    return false;
  };

  //проверка испорченности файла: проверяем размер
  if((minSize > 0) && (size_of_stream(f) < minSize)){
    printf("Warning: ini file:'%s' is suspiciously SMALL\n", _path.c_str());
    f.close();
    return false;
  };

  try{
  Clear();
  while( getline( f, line)) {
    // To be compatible with Win32, check for existence of '\r'.
    // Win32 files have the '\r' and Unix files don't at the end of a line.
    // Note that the '\r' will be written to INI files from
    // Unix so that the created INI file can be read under Win32
    // without change.
    //printf("str=%s \n line=%s\n",_str.c_str(),line.c_str());
    if ( line[line.length() - 1] == '\r')
      line = line.substr( 0, line.length() - 1);

    if ( line.length()) {
      // Check that the user hasn't openned a binary file by checking the first
      // character of each line!
//      if ( !isprint( line[0])) {
//       printf( "Failing on char %d\n", line[0]);
//       f.close();
//        return false;
//      };
      if (( pLeft = line.find_first_of(";#[=:")) != CString::npos) {
       switch ( line[pLeft]) {
       case '[':
         if ((pRight = line.find_last_of("]")) != CString::npos &&
              pRight > pLeft) {
           keyname = trim(line.substr( pLeft + 1, pRight - pLeft - 1));

           AddKeyName(keyname);
           //printf("Section: %s\n",keyname.c_str());
         }
       break;

       case '=':
         valuename = trim(line.substr( 0, pLeft));
         value = trim(line.substr( pLeft + 1));
         SetValue( keyname, valuename, value);
       break;
//
//       case ':':
//         valuename = line.substr( 0, pLeft);
//         value = line.substr( pLeft + 1);
//         AddValue( keyname, valuename, value);
//       break;

       case ';':
       case '#':
         if ( !names.size())
            HeaderComment( line.substr( pLeft + 1));
          else
            KeyComment( keyname, line.substr( pLeft + 1));
       break;
       };//switch()
      }//if(( pLeft = line.find_first_of(";#[=")) != CString::npos)
    };//if ( line.length())
  };//while( getline( ini_str, line))
  }catch(...){

  };
  f.close();

  //проверка испорченности файла: проверяем размер
  if(minSize<=0)
    return true;

  if(0 == names.size()){
    printf("Warning: ini file:'%s' is NOT contain any names\n", _path.c_str());
    return false;
  };

  return (CheckFile());
}