/*

INI class 0.2

03-05-2001 

Developed by Roland Snijder (roly@home.nl).

Only for non-commercial use.

irc.quakenet.org@#cns

********************************************

INI FILE STRUCTURE:

[sectioname]

itemname=value

;comment

Seggestion: enclose your ini file by 

;end

for stabler reading

USAGE/HELP:

->put this source somewhere in your application, or include it with a header file.

->This class needs the CString class to operate properly. (CString is a part of MFC)

READ INI:

INI::ReadIni(CString file, CString section, CString item)

[Example: CString DialogWidth=INI::ReadIni("settings.ini","dialog1","width");

WRITE INI:

INI::WriteIni(CString file, CString section, CString item, CString value)

[Example: INI::WriteIni("settings.ini","dialog1","width","800");

NOTE: I haven't checked my spell/grammer in any of these comments..

*/

class INI

    {

    public:

     INI(); //contructor

     ~INI(); //destructor

     //Function for reading the ini file, returning a CString. 

     static CString ReadIni(CString file, CString section, CString item)

         {

         CString output; //final output

         bool SectionOk=false; //boolean activated when the section is found

         bool ItemOk=false; //boolean activated when the item is found

         bool comment_ok=false; //boolean activated when comment is found

         int comment_X; //reserved for the beginning character of a found comment line

         int comment_U; //reserved for the end character 'the' a found comment line, for calcualting the length

         int X; //reserved for the beginning character of a found item line

         int U; //reserved for the end character 'the' a found item line, for calcualting the length

        

        

         //Loading the file. I gues the char array of 64000 bytes is not really efficient, however.

         CFile InFile(file, CFile::modeRead);

         DWORD leng=InFile.GetLength();

         char whole[64000];

         InFile.Read(whole,leng);

         InFile.Close();

         CString CSwhole(whole); //Convert char[] to CString

         //the ini files are read by looping trough the entire file 

         //and edit the string with sub-strings.

         for(int comment_index=0; comment_index

             {

             //first remove all the comments

             if (CSwhole.Mid(comment_index,1)==";")

                 {

                 comment_X=comment_index; //save the beginning of the comment line

                 comment_ok=true;

                 }

                 if (comment_ok)

                     {

                     //try to find the end of the comment line

                     //wich is seperated with a return or the end of the string.

                     if (CSwhole.Mid(comment_index,1)=="\n")

                         {

                         comment_U=comment_index-comment_X;

                         CSwhole.Delete(comment_X,comment_U);

                         comment_ok=false;

                         comment_index=0;

                         }

                         else if (comment_index==leng-1)

                             {

                             comment_U=comment_index-comment_X;

                             CSwhole.Delete(comment_X,comment_U);

                             comment_ok=false;

                             }

                             }

                             }

                             //Now loop trough the string again and try to find the 'value', 

                             //wich got it's input by one of the funtion's parameters.

                             for(int I=0; I

                                 {

                                 //first locate the section

                                 if (CSwhole.Mid(I,section.GetLength()+2)=="["+section+"]")

                                     {

                                     SectionOk=true;

                                     }

                                     if (SectionOk) 

                                         {

                                         //locate the item and the location where the value can be read

                                         if (CSwhole.Mid(I,item.GetLength()+1)==item+"=")

                                             {

                                             X=I+item.GetLength()+1;

                                             ItemOk=true;

                                             SectionOk=false;

                                             }

                                             }

                                             if (ItemOk) 

                                                 {

                                                 //try to find the end of the item line

                                                 //wich is seperated with a return or the end of the string.

                                                 //now that we have the position en length of the 'value' we can return it.

                                                 if (CSwhole.Mid(I,1)=="\n")

                                                     {

                                                     U=I-X-1;

                                                     output=CSwhole.Mid(X,U);

                                                     ItemOk=false;

                                                     }

                                                     else if (I==leng-1)

                                                         {

                                                         U=I-X;

                                                         output=CSwhole.Mid(X,U);

                                                         }

                                                         }

                                                         }

                                                         return output;

                                                         }

                                                         //Function for writing the ini file

                                                         static void WriteIni(CString file, CString section, CString item, CString value)

                                                             {

                                                             //variable structure is the same as above

                                                             bool rem_ok=false;

                                                             bool SectionOK=false;

                                                             int rem_X;

                                                             int rem_U;

                                                            

                                                             //Loading the file.

                                                             CFile InFile(file, CFile::modeRead);

                                                             DWORD leng=InFile.GetLength();

                                                             char whole[64000];

                                                             InFile.Read(whole,leng);

                                                             InFile.Close();

                                                            

                                                             CString CSwhole(whole,leng); //Convert char[] to CString

                                                             //loop trough the entire string 

                                                             for(int rem_index=0; rem_index

                                                                 {

                                                                 //First locate the section

                                                                 if (CSwhole.Mid(rem_index,section.GetLength()+2)=="["+section+"]")

                                                                     {

                                                                     SectionOK=true;

                                                                     }

                                                                     if (SectionOK)

                                                                         {

                                                                         //now locate the item and the first character of the old value,

                                                                         //or no value.

                                                                         if (CSwhole.Mid(rem_index,item.GetLength()+1)==item+"=")

                                                                             {

                                                                             rem_X=rem_index+item.GetLength()+1;

                                                                             rem_ok=true;

                                                                             SectionOK=false;

                                                                             }

                                                                             }

                                                                             if (rem_ok)

                                                                                 {

                                                                                 //try to find the end of the value line

                                                                                 //wich is seperated with a return or the end of the string.

                                                                                 //No we can remove the old value en insert the new value.

                                                                                 if (CSwhole.Mid(rem_index,1)=="\n")

                                                                                     {

                                                                                     rem_U=rem_index-rem_X;

                                                                                     CSwhole.Delete(rem_X,rem_U-1); //delete old value

                                                                                     CSwhole.Insert(rem_X,value); //insert new value

                                                                                     rem_ok=false;

                                                                                     }

                                                                                     else if (rem_index==leng-1)

                                                                                         {

                                                                                         rem_U=rem_index-rem_X+1;

                                                                                         CSwhole.Delete(rem_X,rem_U);

                                                                                         CSwhole.Insert(rem_X,value);

                                                                                         rem_ok=false;

                                                                                         }

                                                                                         }

                                                                                         }

                                                                                         int lengout=CSwhole.GetLength(); //get the new generated string length.

                                                                                         //save the new generated string.

                                                                                         CFile OutFile(file, CFile::modeCreate | CFile::modeWrite);

                                                                                         OutFile.Write(CSwhole, lengout);

                                                                                         OutFile.Close();

                                                                                         }

                                                                                    };