Click here to Skip to main content
16,012,468 members
Articles / Desktop Programming / MFC
Article

CIniEx - Class with Extended INI file support

Rate me:
Please Sign up or sign in to vote.
2.60/5 (5 votes)
16 Apr 2001 81.9K   2.9K   47   9
Class CIniEx carries out an extended set of INI file functions in memory

Introduction

The CIniEx class contains the following methods to work with INI files:

  • add sections, delete sections, update sections
  • add key, delete key, update key, get key data
  • clear data
  • read INI file, save INI file

This class carries out all of the above-mentioned functions in memory.

It also allows you to save comments within an INI file. Comments must precede the section or key that they belong to.

Class CIniEx supports formats of Wine INI files Editor. Additional ability of Wine Ini files Editor is KeyType Section, which keeps for a key one of the following types: character, Boolean, integer, filename, directory, list, color, font, link. But you can work without additional ability of Wine Ini files Editor.

The CIniEx class header is as follows:

#define CINIEX_CHARACTER    0
#define CINIEX_BOOLEAN      1     
#define CINIEX_INTEGER      2
#define CINIEX_FILENAME     3
#define CINIEX_DIRECTORY    4
#define CINIEX_LIST         5
#define CINIEX_COLOR        6
#define CINIEX_FONT         7
#define CINIEX_LINK         8

#define CINIEX_TYPE_MAX          9

#define CINIEX_EMPTY         9


typedef struct
{
    DWORD  UpdateFlag;   // 0 - normal
                         // 1 - updated
                         // 2 - deleted
    CString Comment;
    CString Key;
    CString Value;

} KEY_STRUCT;

class CSection  :  public CObject
{
    public: 
     DWORD    UpdateFlag;

     CString  Comment;
     CString  Name;
     CArray <KEY_STRUCT,KEY_STRUCT> key;  
};

typedef struct
{
   CString Section;
   CString Key;
} LINK_STRUCT;

//--------------------------------
typedef struct
{
  int index;
  CString Type;
} KEYTYPE_STRUCT;

/////////////////////////////////////////////////////////////////////////////
class CIniEx
{
// Construction
public:
	CIniEx();	// standard constructor
  ~CIniEx();


  BOOL  ReadFile(char* s);
  void ShowSetOfSections();
  int  GetIndexForKey(char* Section,char* Key);
  BOOL GetKeyData(char* Section,char* Key,char* Value,
                  char* Comment,char* Type);
  int  GetIndexForSection(char* Section);
  void ClearMemory();

  BOOL  GetDataFromKeyTypeSection(char* Section,
                                  char* Key, 
                                  KEYTYPE_STRUCT* pkt);
  BOOL  GetDataFromKeyTypeSection(char* Key, KEYTYPE_STRUCT* pkt);
  char* GetKeyTypeString(DWORD type);
  BOOL  AddKeyToKeyTypeSection(char* Section,
                              char* Key,
                              char* Type);
  BOOL  AddKeyToKeyTypeSection(char* Section,
                              char* Key,
                              DWORD Type);
  int  DeleteIndexFromKeyTypeSection(char* Key);
  SaveFile(char* s);
  BOOL AddNewSection(char* section,char* comment,int* pindex);
  BOOL AddKeyTypeSection(int* pindex);
  BOOL DeleteSection(char* section);
  BOOL AddNewKey(char* section,
                 char* key,
                 char* value,
                 char* comment,
                 char* type, 
                 int*  pindex);
  BOOL AddNewKey(char* section,
                 char* key,
                 char* value,
                 char* comment,
                 DWORD type, 
                 int*  pindex);

  BOOL DeleteKey(char* section,char* key);
  BOOL UpdateSection(char* sectionOldName,
                     char* sectionNewName,
                     char* comment);
  BOOL UpdateKey(char* section,
                 char* keyOld,
                 char* keyNew,
                 char* value,
                 char* comment,
                 char* type);
  BOOL UpdateKey(char* section,
                 char* keyOld,
                 char* keyNew,
                 char* value,
                 char* comment,
                 DWORD type);

  CObArray  SetOfSections;

  char FullName[FILENAME_MAX];
};

CIniExDemo is included in the demo project ZIP file as a sample of how to use the CIniEx class.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIni Management Pin
SnapConfig3-Jan-08 8:03
SnapConfig3-Jan-08 8:03 
GeneralBug &amp; Fix for the use of the function ReadFile(...) Pin
alanlive24-Jun-05 0:01
alanlive24-Jun-05 0:01 
GeneralCProfile.cpp File Pin
WilliamD31-Aug-04 21:37
WilliamD31-Aug-04 21:37 
GeneralThe Zip File CIniEx_demo is corrupted Pin
Hector T25-Mar-03 4:58
Hector T25-Mar-03 4:58 
GeneralUsing CIniEx without MFC (dll included) Pin
PacoGM27-Jan-03 12:12
PacoGM27-Jan-03 12:12 
GeneralKeys are length limited Pin
11-Oct-01 6:51
suss11-Oct-01 6:51 
GeneralRe: Keys are length limited Pin
alanlive24-Jun-05 12:20
alanlive24-Jun-05 12:20 
Questioncan not find CProfile.cpp Pin
xia5-Jun-00 5:09
xia5-Jun-00 5:09 
AnswerRe: can not find CProfile.cpp Pin
Oscar Kogosov7-Jun-00 17:54
Oscar Kogosov7-Jun-00 17:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.