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