Bug #453

Improve .ini files

Added by Miroslav Fidler over 11 years ago. Updated over 11 years ago.

Status:ApprovedStart date:03/08/2013
Priority:NormalDue date:
Assignee:Tomas Ebenlendr% Done:

0%

Category:CoreSpent time:-
Target version:-

Description

Prikladam svoji predstavu o nacitani ini souboru v uppsrc/Core/Ini.cpp,
ustrizek nahrazuje funkce LoadIniFile a LoadIniStream.

Pri zapnutem flagLOOSEINI se to chova jako drive. Pri vypnutem flagLOOSEINI to:
ignoruje radky ktere pred prvnim alfanumerickym (nebo podtrzitkem) maji neco co neni mezera.
To zapne podporu pro bezne komentare v ini souborech (';' a '#').
Zaroven pridavam @include, ktery se chova jako opravdovy include, na rozdil od LINK, ktery
/prepina/ vstup. (Navic jsem pridal @end, ktery umoznuje zakomentovat vse az do konce souboru)

static void LoadIniStream(Stream &sin, VectorMap<String, String>& ret);
static void LoadIniFile(const char *filename, VectorMap<String, String>& ret) {
        FileIn in(filename);
        if(in) LoadIniStream(in, ret);
}

static void LoadIniStream(Stream &sin, VectorMap<String, String>& key){
        Stream *in = &sin;
        FileIn fin;
        int c;
        if((c = in->Get()) < 0) return;
        for(;;) {
                String k, v;
#ifndef flagLOOSEINI
                while (IsSpace(c))
                        if((c = in->Get()) < 0) return;
                if (c == '@') {
                        k.Cat(c);
                        if((c = in->Get()) < 0) return;
                }
#endif
                for(;;) {
                        if(IsAlNum(c) || c == '_')
                                k.Cat(c);
                        else
                                break;
                        if((c = in->Get()) < 0) return;
                }
                for(;;) {
#ifdef flagLOOSEINI
                        if(c != '=' && c != ' ') break;
#else
                        if(c != '=' && c != ' ' && c != '\t') break;
#endif
                        if((c = in->Get()) < 0) return;
                }
                for(;;) {
                        if(c < ' ') break;
                        v.Cat(c);
                        if((c = in->Get()) < 0) break;
                }
#ifndef flagLOOSEINI
                if(k.StartsWith("@")) {
                        if (ToLower(k) == "@end")
                                return;
                        if (ToLower(k) == "@include")
                                LoadIniFile(v, key);
                } else
#endif
                if(!k.IsEmpty())
                        key.Add(k, v);
                if(k == "LINK") {
                        if(in == &fin)
                                fin.Close();
                        if(!fin.Open(v) || (c = in->Get()) < 0) return;
                        in = &fin;
                }
                else
                        for(;;) {
#ifdef flagLOOSEINI
                                if(IsAlNum(c) || c == '_') break;
#else
                                if(c == '\r' || c == '\n') break;
#endif
                                if((c = in->Get()) < 0) return;
                        }
        }
}

VectorMap<String, String> LoadIniStream(Stream &sin) {
        VectorMap<String, String> ret;
        LoadIniStream(sin, ret);
        return ret;
}

VectorMap<String, String> LoadIniFile(const char *filename) {
        VectorMap<String, String> ret;
        LoadIniFile(filename, ret);
        return ret;
}

History

#1 Updated by Miroslav Fidler over 11 years ago

In the end I have decided to refactor the code a little bit more...

#2 Updated by Miroslav Fidler over 11 years ago

  • Status changed from New to Ready for CR
  • Assignee changed from Miroslav Fidler to Tomas Ebenlendr

#3 Updated by Miroslav Fidler over 11 years ago

testing package: upptst/LoadIniStream

#4 Updated by Miroslav Fidler over 11 years ago

  • Status changed from Ready for CR to Approved

Also available in: Atom PDF