blob: de894d24463a056008706fd87642c5d5a498b91b [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>
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070029#include <xglLayer.h>
Jon Ashburn47e92892014-12-22 12:04:40 -070030#include "layers_config.h"
31
32#define MAX_CHARS_PER_LINE 4096
33
34class ConfigFile
35{
36public:
37 ConfigFile();
38 ~ConfigFile();
39
40 const char *getOption(const std::string &_option);
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070041 void setOption(const std::string &_option, const std::string &_val);
42
Jon Ashburn47e92892014-12-22 12:04:40 -070043private:
44 bool m_fileIsParsed;
45 std::map<std::string, std::string> m_valueMap;
46
47 void parseFile(const char *filename);
48};
49
50static ConfigFile g_configFileObj;
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070051
52static unsigned int convertStringEnumVal(const char *_enum)
53{
54 // only handles single enum values
55 if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_IGNORE"))
56 return XGL_DBG_LAYER_ACTION_IGNORE;
57 else if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_CALLBACK"))
58 return XGL_DBG_LAYER_ACTION_CALLBACK;
59 else if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_LOG_MSG"))
60 return XGL_DBG_LAYER_ACTION_LOG_MSG;
61 else if (!strcmp(_enum, "XGL_DBG_LAYER_ACTION_BREAK"))
62 return XGL_DBG_LAYER_ACTION_BREAK;
63 else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_INFO"))
64 return XGL_DBG_LAYER_LEVEL_INFO;
65 else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_WARN"))
66 return XGL_DBG_LAYER_LEVEL_WARN;
67 else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_PERF_WARN"))
68 return XGL_DBG_LAYER_LEVEL_PERF_WARN;
69 else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_ERROR"))
70 return XGL_DBG_LAYER_LEVEL_ERROR;
71 else if (!strcmp(_enum, "XGL_DBG_LAYER_LEVEL_NONE"))
72 return XGL_DBG_LAYER_LEVEL_NONE;
73 return 0;
74}
Jon Ashburn47e92892014-12-22 12:04:40 -070075const char *getLayerOption(const char *_option)
76{
Jon Ashburn47e92892014-12-22 12:04:40 -070077 return g_configFileObj.getOption(_option);
78}
79
Jon Ashburn0f1dbf12015-01-13 17:24:01 -070080void setLayerOptionEnum(const char *_option, const char *_valEnum)
81{
82 unsigned int val = convertStringEnumVal(_valEnum);
83 char strVal[24];
84 snprintf(strVal, 24, "%u", val);
85 g_configFileObj.setOption(_option, strVal);
86}
87
88void setLayerOption(const char *_option, const char *_val)
89{
90 g_configFileObj.setOption(_option, _val);
91}
92
Jon Ashburn47e92892014-12-22 12:04:40 -070093ConfigFile::ConfigFile() : m_fileIsParsed(false)
94{
95}
96
97ConfigFile::~ConfigFile()
98{
99}
100
101const char *ConfigFile::getOption(const std::string &_option)
102{
103 std::map<std::string, std::string>::const_iterator it;
104 if (!m_fileIsParsed)
105 {
106 parseFile("xgl_layer_settings.txt");
107 }
108
109 if ((it = m_valueMap.find(_option)) == m_valueMap.end())
110 return NULL;
111 else
112 return it->second.c_str();
113}
114
Jon Ashburn0f1dbf12015-01-13 17:24:01 -0700115void ConfigFile::setOption(const std::string &_option, const std::string &_val)
116{
117 if (!m_fileIsParsed)
118 {
119 parseFile("xgl_layer_settings.txt");
120 }
121
122 m_valueMap[_option] = _val;
123}
124
Jon Ashburn47e92892014-12-22 12:04:40 -0700125void ConfigFile::parseFile(const char *filename)
126{
127 std::ifstream file;
128 char buf[MAX_CHARS_PER_LINE];
129
130 m_fileIsParsed = true;
131 m_valueMap.clear();
132
133 file.open(filename);
134 if (!file.good())
135 return;
136
137 // read tokens from the file and form option, value pairs
138 file.getline(buf, MAX_CHARS_PER_LINE);
139 while (!file.eof())
140 {
141 char option[512];
142 char value[512];
143
144 char *pComment;
145
146 //discard any comments delimited by '#' in the line
147 pComment = strchr(buf, '#');
148 if (pComment)
149 *pComment = '\0';
150
151 if (sscanf(buf, " %511[^\n\t =] = %511[^\n \t]", option, value) == 2)
152 {
153 std::string optStr(option);
154 std::string valStr(value);
155 m_valueMap[optStr] = valStr;
156 }
157 file.getline(buf, MAX_CHARS_PER_LINE);
158 }
159}
160