Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / inifilereader.h @ 636cbdd5

History | View | Annotate | Download (3.44 KB)

1
//==========================================================================
2
//  INIFILEREADER.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17

    
18
#ifndef INIFILEREADER_H_
19
#define INIFILEREADER_H_
20

    
21
#include <string>
22
#include <set>
23
#include <vector>
24
#include "envirdefs.h"
25
#include "cconfigreader.h"
26

    
27
NAMESPACE_BEGIN
28

    
29
/**
30
 * Low-level inifile reading, including the resolution of includes.
31
 * Inifile contents is presented as key-value pairs grouped into sections.
32
 * This class does not try to make sense of section/key names.
33
 */
34
class ENVIR_API InifileReader : public cConfigurationReader
35
{
36
  protected:
37
    class KeyValue1 : public KeyValue {
38
      private:
39
        const std::string *basedir;  // points into basedirs[]
40
        const std::string *filename; // points into filenames[]
41
        int lineNumber;
42
        std::string key;
43
        std::string value; //XXX stringpool it?
44

    
45
      public:
46
        KeyValue1(const std::string *bdir, const std::string *fname, int li, const char *k, const char *v) {
47
            basedir = bdir; filename = fname; lineNumber = li; key = k; value = v;
48
        }
49

    
50
        // virtual functions implementing the KeyValue interface
51
        virtual const char *getKey() const   {return key.c_str();}
52
        virtual const char *getValue() const {return value.c_str();}
53
        virtual const char *getBaseDirectory() const  {return basedir->c_str();}
54
        virtual const char *getFileName() const  {return filename->c_str();}
55
        virtual int getLineNumber() const  {return lineNumber;}
56
    };
57

    
58
    std::string rootfilename;  // name of "root" ini file read
59
    std::string defaultbasedir; // the default base directory
60

    
61
    typedef std::set<std::string> StringSet;
62
    StringSet filenames; // stores ini file names (absolute paths)
63
    StringSet basedirs;  // stores base directory names (absolute paths)
64

    
65
    struct Section {
66
        std::string name;
67
        std::vector<KeyValue1> entries;
68
    };
69
    std::vector<Section> sections;
70

    
71
  protected:
72
    void internalReadFile(const char *filename, Section *currentSection);
73
    const Section& getSection(int sectionId) const;
74
    const Section& getOrCreateSection(const char *sectionName);
75
    static const char *findEndContent(const char *line, const char *filename, int lineNumber);
76
    static bool readLineInto(std::string& line, FILE *file);
77
    static std::string trim(const char *start, const char *end);
78

    
79
  public:
80
    InifileReader();
81
    virtual ~InifileReader();
82
    virtual void readFile(const char *filename);
83

    
84
    /** @name Methods implementing cConfigurationReader */
85
    //@{
86
    virtual void initializeFrom(cConfiguration *bootConfig);
87
    virtual const char *getFileName() const;
88
    virtual const char *getDefaultBaseDirectory() const;
89
    virtual int getNumSections() const;
90
    virtual const char *getSectionName(int sectionId) const;
91
    virtual int getNumEntries(int sectionId) const;
92
    virtual const KeyValue& getEntry(int sectionId, int entryId) const;
93
    virtual void dump() const;
94
    //@}
95
};
96

    
97
NAMESPACE_END
98

    
99

    
100
#endif
101

    
102