blob: 2d8ea6dd4d34615c896eff5b54a459929765a985 [file] [log] [blame]
Jon Ashburn47e92892014-12-22 12:04:40 -07001/**************************************************************************
2 *
3 * Copyright 2014 Lunarg, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25#include <fstream>
26#include <string>
27#include <map>
28#include <string.h>
29#include "layers_config.h"
30
31#define MAX_CHARS_PER_LINE 4096
32
33class ConfigFile
34{
35public:
36 ConfigFile();
37 ~ConfigFile();
38
39 const char *getOption(const std::string &_option);
40private:
41 bool m_fileIsParsed;
42 std::map<std::string, std::string> m_valueMap;
43
44 void parseFile(const char *filename);
45};
46
47static ConfigFile g_configFileObj;
48const char *getLayerOption(const char *_option)
49{
50 std::string option(_option);
51 return g_configFileObj.getOption(_option);
52}
53
54ConfigFile::ConfigFile() : m_fileIsParsed(false)
55{
56}
57
58ConfigFile::~ConfigFile()
59{
60}
61
62const char *ConfigFile::getOption(const std::string &_option)
63{
64 std::map<std::string, std::string>::const_iterator it;
65 if (!m_fileIsParsed)
66 {
67 parseFile("xgl_layer_settings.txt");
68 }
69
70 if ((it = m_valueMap.find(_option)) == m_valueMap.end())
71 return NULL;
72 else
73 return it->second.c_str();
74}
75
76void ConfigFile::parseFile(const char *filename)
77{
78 std::ifstream file;
79 char buf[MAX_CHARS_PER_LINE];
80
81 m_fileIsParsed = true;
82 m_valueMap.clear();
83
84 file.open(filename);
85 if (!file.good())
86 return;
87
88 // read tokens from the file and form option, value pairs
89 file.getline(buf, MAX_CHARS_PER_LINE);
90 while (!file.eof())
91 {
92 char option[512];
93 char value[512];
94
95 char *pComment;
96
97 //discard any comments delimited by '#' in the line
98 pComment = strchr(buf, '#');
99 if (pComment)
100 *pComment = '\0';
101
102 if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2)
103 {
104 std::string optStr(option);
105 std::string valStr(value);
106 m_valueMap[optStr] = valStr;
107 }
108 file.getline(buf, MAX_CHARS_PER_LINE);
109 }
110}
111