[Code] A class for the wxFileConfig

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
lfjking
Experienced Solver
Experienced Solver
Posts: 96
Joined: Mon Nov 14, 2016 1:35 pm

[Code] A class for the wxFileConfig

Postby lfjking » Sun Dec 11, 2016 7:09 am

Thanks for the help from the forum.

I have encountered a lot of problems in the use of wxFileConfig.
But I solved it.
Now I've posted the class I made.
So as not to repeat these mistakes

//Using wxFileConfig, it should be seen as a path rather than a MAP

Code: Select all

//USConfig.h
#ifndef USCONFIG_H
#define USCONFIG_H

#include <wx/fileconf.h>


class USConfig : public wxFileConfig
{
    public:
        /** Default constructor */
        USConfig(const wxString& localFile);
        /** Default destructor */
        virtual ~USConfig();

        void SetSection(const wxString& sec);

        bool SetItem(const wxString& key, const wxString& value);
        bool SetItem(const wxString& key, long value);

        wxString GetItem(const wxString& key);
        long GetItem(const wxString& key, long defult );

        void AllSection(wxArrayString &setionlist);
        void AllKey(wxArrayString &keylist);

    protected:

    private:
        wxString mSection;//
};

#endif // USCONFIG_H




Code: Select all

//USConfig.cpp
#include "USConfig.h"

USConfig::USConfig(const wxString& localFile)
    :wxFileConfig(wxEmptyString, wxEmptyString, localFile, wxEmptyString, wxCONFIG_USE_RELATIVE_PATH)
{
    //ctor
    mSection.Clear();
}

USConfig::~USConfig()
{
    //dtor
}

void USConfig::SetSection(const wxString& sec)
{
    if(sec.IsEmpty()){return;}
    mSection = sec;
    wxString sSec = wxString::Format(_T("/%s"),mSection);
    SetPath(sSec);
}

bool USConfig::SetItem(const wxString& key, const wxString& value)
{
    wxString skey = wxString::Format(_T("/%s/%s"),mSection, key);
    return Write(skey, value);
}

bool USConfig::SetItem(const wxString& key, long value)
{
    wxString skey = wxString::Format(_T("/%s/%s"),mSection, key);
    return Write(skey, value);
}

wxString USConfig::GetItem(const wxString& key)
{
    wxString skey = wxString::Format(_T("/%s/%s"),mSection, key);
    return Read(skey);
}

long USConfig::GetItem(const wxString& key, long defult)
{
    wxString skey = wxString::Format(_T("/%s/%s"),mSection, key);
    return Read(skey, defult);
}

void USConfig::AllSection(wxArrayString& setionlist)
{
    wxString ret;
    long sid;
    setionlist.Clear();
    SetPath(_T("/"));//Must return to the root or else it will not be found
    bool sok = GetFirstGroup(ret, sid);
    while(sok)
    {
        setionlist.Add(ret);
        sok = GetNextGroup(ret, sid);
    }
}

void USConfig::AllKey(wxArrayString& keylist)
{
    wxString ret;
    long sid;
    keylist.Clear();
    SetPath(_T("/"));//Must return to the root or else it will not be found
    bool sok = GetFirstEntry(ret, sid);
    while(sok)
    {
        keylist.Add(ret);
        sok = GetNextEntry(ret, sid);
    }
}




For example: I need to define two ports on different devices, and can't be the same as other devices.

Code: Select all


      wxString GDevice(wxT("adbcdgdad");
      long GPortA = 0, GPortB = 0;
      USConfig config(_T("Config.ini"));
      config.SetSection(GDevice);
      GPortA = config.GetItem(_T("ProtA"), 0);
      GPortB = config.GetItem(_T("ProtB"), 0);
           if(GPortA == 0 || GPortB == 0)
      {
         int PortA = (int)((1000) + (rand()%100 + 1));//Port int 1000- 1199
         int PortB = PortA + 1;
         wxString str;
         long nIndex;
         wxArrayString sectiongroup;
         config.AllSection(sectiongroup);
         if(!sectiongroup.IsEmpty())
         {
            size_t nCount = sectiongroup.GetCount();
            for(size_t n = 0; n < nCount; n++)
            {
               config.SetSection(sectiongroup[n]);
               long porta = config.GetItem(_T("ProtA"), 0);
               long portb = config.GetItem(_T("ProtB"), 0);
               if(PortA == porta || PortB == porta || PortA == portb || PortB == portb)
               {
                  //some other , take again
                  PortA = (int)((1000) + (rand()%100 + 1));////Port int 1000- 1199
                  PortB = PortA + 1;
                  n = -1;//for will +1,  Cmp form head;
               }
            }
         }
         ///save
         config.SetSection(GDevice);//must be do it
         GPortA = PortA;
         GPortB = PortB;
         config.Write(_T("ProtA"), GPortA);
         config.Write(_T("ProtB"), GPortB);
      }



Some people can optimize the USConfig is very much better and better :D :D :D :D :D

Return to “The Code Dump”

Who is online

Users browsing this forum: No registered users and 2 guests