textfileparser.cpp 6.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "textfileparser.h"

#include <sstream>


namespace TUMVis {

    TextFileParser::TextFileParser(const std::string& url, bool caseSensitiveKeys, const std::string& delimiters, const std::string& whitespace /*= " \t"*/) 
        : _url(url)
        , _caseSensitiveKeys(caseSensitiveKeys)
        , _delimiters(delimiters)
        , _whitespace(whitespace)
    {
    }

schultezub's avatar
schultezub committed
16
    bool TextFileParser::hasKey(const std::string& key) const {
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
        return (_tokens.find(key) != _tokens.end());
    }

    const std::string& TextFileParser::getString(const std::string& key) const throw (tgt::CorruptedFileException) {
        std::map<std::string, std::string>::const_iterator it = (_caseSensitiveKeys ? _tokens.find(key) : _tokens.find(StringUtils::lowercase(key)));
        if (it == _tokens.end()) {
            throw tgt::CorruptedFileException("No token with key " + key + " found.", _url);
        }
        else {
            return it->second;
        }
    }

    bool TextFileParser::getBool(const std::string& key) const throw (tgt::CorruptedFileException) {
        std::string lc = StringUtils::lowercase(getString(key));

        if ((lc == "0") || (lc == "false"))
            return false;
        else if ((lc == "1") || (lc == "true"))
            return true;
        else
            throw tgt::CorruptedFileException("Error parsing key " + key + " to bool.", _url);
    }

    int TextFileParser::getInt(const std::string& key) const throw (tgt::CorruptedFileException) {
        std::string str = getString(key);
        try {
            return StringUtils::fromString<int>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to int: " + e.what(), _url);
        }        
    }

    tgt::ivec2 TextFileParser::getIvec2(const std::string& key) const  throw (tgt::CorruptedFileException){
        std::string str = getString(key);
        try {
            return StringUtils::fromString<tgt::ivec2>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to ivec2: " + e.what(), _url);
        }        
    }

    tgt::ivec3 TextFileParser::getIvec3(const std::string& key) const  throw (tgt::CorruptedFileException){
        std::string str = getString(key);
        try {
            return StringUtils::fromString<tgt::ivec3>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to ivec3: " + e.what(), _url);
        }        
    }

    tgt::ivec4 TextFileParser::getIvec4(const std::string& key) const  throw (tgt::CorruptedFileException){
        std::string str = getString(key);
        try {
            return StringUtils::fromString<tgt::ivec4>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to ivec4: " + e.what(), _url);
        }        
    }

schultezub's avatar
schultezub committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    size_t TextFileParser::getSizeT(const std::string& key) const throw (tgt::CorruptedFileException) {
        std::string str = getString(key);
        try {
            return StringUtils::fromString<size_t>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to size_t: " + e.what(), _url);
        }        
    }

    tgt::svec2 TextFileParser::getSvec2(const std::string& key) const  throw (tgt::CorruptedFileException){
        std::string str = getString(key);
        try {
            return StringUtils::fromString<tgt::svec2>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to svec2: " + e.what(), _url);
        }        
    }

    tgt::svec3 TextFileParser::getSvec3(const std::string& key) const  throw (tgt::CorruptedFileException){
        std::string str = getString(key);
        try {
            return StringUtils::fromString<tgt::svec3>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to svec3: " + e.what(), _url);
        }        
    }

    tgt::svec4 TextFileParser::getSvec4(const std::string& key) const  throw (tgt::CorruptedFileException){
        std::string str = getString(key);
        try {
            return StringUtils::fromString<tgt::svec4>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to svec4: " + e.what(), _url);
        }        
    }

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    float TextFileParser::getFloat(const std::string& key) const throw (tgt::CorruptedFileException) {
        std::string str = getString(key);
        try {
            return StringUtils::fromString<float>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to float: " + e.what(), _url);
        }        
    }

    tgt::vec2 TextFileParser::getVec2(const std::string& key) const throw (tgt::CorruptedFileException) {
        std::string str = getString(key);
        try {
            return StringUtils::fromString<tgt::vec2>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to vec2: " + e.what(), _url);
        }        
    }

    tgt::vec3 TextFileParser::getVec3(const std::string& key) const throw (tgt::CorruptedFileException) {
        std::string str = getString(key);
        try {
            return StringUtils::fromString<tgt::vec3>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to vec3: " + e.what(), _url);
        }        
    }

    tgt::vec4 TextFileParser::getVec4(const std::string& key) const throw (tgt::CorruptedFileException) {
        std::string str = getString(key);
        try {
            return StringUtils::fromString<tgt::vec4>(str);
        }
        catch (std::exception& e) {
            throw tgt::CorruptedFileException("Error parsing key " + key + " to vec4: " + e.what(), _url);
        }        
    }
}