Example #1
0
// CompareNoCase
// it's amazing what features std::string lacks.  This function simply
// does a lowercase compare against the two strings, returning 0 if they
// match.
int CompareNoCase(t_Str str1, t_Str str2)
{
#ifdef WIN32
   return stricmp(str1.c_str(), str2.c_str());
#else
   return strcasecmp(str1.c_str(), str2.c_str());
#endif
}
Example #2
0
// SetFileName
// Set's the m_szFileName member variable. For use when creating the CDataFile
// object by hand (-vs- loading it from a file
void CDataFile::SetFileName(t_Str szFileName)
{
   if (m_szFileName.size() != 0 && CompareNoCase(szFileName, m_szFileName) != 0)
      {
         m_bDirty = true;

         Report(E_WARN, "[CDataFile::SetFileName] The filename has changed from <%s> to <%s>.",
                m_szFileName.c_str(), szFileName.c_str());
      }

   m_szFileName = szFileName;
}
Example #3
0
// CreateSection
// Given a section name, this function first checks to see if the given section
// allready exists in the list or not, if not, it creates the new section and
// assigns it the comment given in szComment.  The function returns true if
// sucessfully created, or false otherwise.
bool CDataFile::CreateSection(t_Str szSection, t_Str szComment)
{
   t_Section* pSection = GetSection(szSection);

   if ( pSection )
      {
         Report(E_INFO, "[CDataFile::CreateSection] Section <%s> allready exists. Aborting.", szSection.c_str());
         return false;
      }

   t_Section section;

   section.szName = szSection;
   section.szComment = szComment;
   m_Sections.push_back(section);

   m_bDirty = true;

   return true;
}
Example #4
0
// Load
// Attempts to load in the text file. If successful it will populate the
// Section list with the key/value pairs found in the file. Note that comments
// are saved so that they can be rewritten to the file later.
bool CDataFile::Load(t_Str szFileName)
{
   // We dont want to create a new file here.  If it doesn't exist, just
   // return false and report the failure.
   std::fstream File(szFileName.c_str(), std::ios_base::in);
   // |std::ios_base::nocreate
   if ( File.is_open() )
      {
         bool bDone = false;
         bool bAutoKey = (m_Flags & AUTOCREATE_KEYS) == AUTOCREATE_KEYS;
         bool bAutoSec = (m_Flags & AUTOCREATE_SECTIONS) == AUTOCREATE_SECTIONS;

         t_Str szLine;
         t_Str szComment;
         char buffer[MAX_BUFFER_LEN];
         t_Section* pSection = GetSection("");

         // These need to be set, we'll restore the original values later.
         m_Flags |= AUTOCREATE_KEYS;
         m_Flags |= AUTOCREATE_SECTIONS;

         while ( !bDone )
            {
               memset(buffer, 0, MAX_BUFFER_LEN);
               File.getline(buffer, MAX_BUFFER_LEN);

               szLine = buffer;
               Trim(szLine);

               bDone = ( File.eof() || File.bad() || File.fail() );

               if ( szLine.find_first_of(CommentIndicators) == 0 )
                  {
                     szComment += "\n";
                     szComment += szLine;
                  }
               else
                  if ( szLine.find_first_of('[') == 0 ) // new section
                     {
                        szLine.erase( 0, 1 );
                        szLine.erase( szLine.find_last_of(']'), 1 );

                        CreateSection(szLine, szComment);
                        pSection = GetSection(szLine);
                        szComment = t_Str("");
                     }
                  else
                     if ( szLine.size() > 0 ) // we have a key, add this key/value pair
                        {
                           t_Str szKey = GetNextWord(szLine);
                           t_Str szValue = szLine;

                           if ( szKey.size() > 0 && szValue.size() > 0 )
                              {
                                 SetValue(szKey, szValue, szComment, pSection->szName);
                                 szComment = t_Str("");
                              }
                        }
            }

         // Restore the original flag values.
         if ( !bAutoKey )
            m_Flags &= ~AUTOCREATE_KEYS;

         if ( !bAutoSec )
            m_Flags &= ~AUTOCREATE_SECTIONS;
      }
   else
      {
         Report(E_INFO, "[CDataFile::Load] Unable to open file. Does it exist?");
         return false;
      }

   File.close();

   return true;
}