The Pedigree Project  0.1
system/config/Config.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "Config.h"
21 #include "pedigree/kernel/LockGuard.h"
22 #include "pedigree/kernel/Log.h"
23 #include "pedigree/kernel/process/Mutex.h"
24 #include "pedigree/kernel/utilities/utility.h"
25 
26 static Mutex g_sqlLock(false);
27 
28 Config Config::m_Instance;
29 
30 extern "C" void free(void *);
31 
32 Config::Result::Result(
33  char **ppResult, size_t rows, size_t cols, char *error, int ret)
34  : m_ppResult(ppResult), m_Rows(rows), m_Cols(cols), m_pError(error),
35  m_Ret(ret)
36 {
37 }
38 
39 Config::Result::~Result()
40 {
41  LockGuard<Mutex> guard(g_sqlLock);
42 
43  if (m_ppResult)
44  sqlite3_free_table(m_ppResult);
45 
46  free(m_pError);
47 }
48 
50 {
51  char *str = m_ppResult[n];
52  if (str)
53  return String(str);
54  return String("");
55 }
56 
57 String Config::Result::getStr(size_t row, size_t n)
58 {
59  char *str = m_ppResult[(row + 1) * m_Cols + n];
60  if (str)
61  return String(str);
62  return String("");
63 }
64 
65 size_t Config::Result::getNum(size_t row, size_t n)
66 {
67  if (m_ppResult[(row + 1) * m_Cols + n] == 0)
68  return 0;
69  return StringToUnsignedLong(m_ppResult[(row + 1) * m_Cols + n], 0, 10);
70 }
71 
72 bool Config::Result::getBool(size_t row, size_t n)
73 {
74  const char *s = m_ppResult[(row + 1) * m_Cols + n];
75  if (s == 0)
76  return false;
77  if (!StringCompare(s, "true") || !StringCompare(s, "True") ||
78  !StringCompare(s, "1"))
79  return true;
80  else
81  return false;
82 }
83 
84 String Config::Result::getStr(size_t row, const char *str)
85 {
86  size_t n = lookupCol(str);
87  if (n == ~0UL)
88  return String("");
89  char *str2 = m_ppResult[(row + 1) * m_Cols + n];
90  if (str2)
91  return String(str2);
92  return String("");
93 }
94 
95 size_t Config::Result::getNum(size_t row, const char *str)
96 {
97  size_t n = lookupCol(str);
98  if (n == ~0UL)
99  return 0;
100  if (m_ppResult[(row + 1) * m_Cols + n] == 0)
101  return 0;
102  return StringToUnsignedLong(m_ppResult[(row + 1) * m_Cols + n], 0, 10);
103 }
104 
105 bool Config::Result::getBool(size_t row, const char *str)
106 {
107  size_t n = lookupCol(str);
108  if (n == ~0UL)
109  return false;
110  const char *s = m_ppResult[(row + 1) * m_Cols + n];
111  if (s == 0)
112  return false;
113  if (!StringCompare(s, "true") || !StringCompare(s, "True") ||
114  !StringCompare(s, "1"))
115  return true;
116  else
117  return false;
118 }
119 
120 size_t Config::Result::lookupCol(const char *str)
121 {
122  for (size_t i = 0; i < m_Cols; i++)
123  {
124  if (!StringCompare(str, m_ppResult[i]))
125  return i;
126  }
127  return ~0UL;
128 }
129 
130 Config::Config()
131 {
132 }
133 
134 Config::~Config()
135 {
136 }
137 
138 Config::Result *Config::query(const char *sql)
139 {
140  if (!*sql)
141  {
142  ERROR("Dud query string passed to Config::query");
143  return 0;
144  }
145 
146  LockGuard<Mutex> guard(g_sqlLock);
147 
148  char **result;
149  int rows, cols;
150  char *error;
151  int ret = sqlite3_get_table(g_pSqlite, sql, &result, &rows, &cols, &error);
152  return new Result(
153  result, static_cast<size_t>(rows), static_cast<size_t>(cols), error,
154  ret);
155 }
std::string getColumnName(size_t col, size_t buffSz=256)
Returns the name of the col&#39;th column.
Definition: Mutex.h:58
Definition: String.h:49
std::string getStr(size_t row, size_t col, size_t buffSz=256)
Returns the value in column &#39;col&#39; of the result, in string form.
size_t getNum(size_t row, size_t col)
Returns the value in column &#39;col&#39; of the result, in number form.
Result * query(const char *sql)
bool getBool(size_t row, size_t col)
Returns the value in column &#39;col&#39; of the result, in boolean form.
#define ERROR(text)
Definition: Log.h:82