The Pedigree Project  0.1
subsys/native/user/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 "pedigree/native/config/Config.h"
21 #include <string.h>
22 
25 extern "C" {
26 void pedigree_config_getcolname(
27  size_t resultIdx, size_t n, char *buf, size_t bufsz);
28 
29 void pedigree_config_getstr_n(
30  size_t resultIdx, size_t row, size_t n, char *buf, size_t bufsz);
31 
32 void pedigree_config_getstr_s(
33  size_t resultIdx, size_t row, const char *col, char *buf, size_t bufsz);
34 
35 int pedigree_config_getnum_n(size_t resultIdx, size_t row, size_t n);
36 
37 int pedigree_config_getnum_s(size_t resultIdx, size_t row, const char *col);
38 
39 int pedigree_config_getbool_n(size_t resultIdx, size_t row, size_t n);
40 
41 int pedigree_config_getbool_s(size_t resultIdx, size_t row, const char *col);
42 
43 int pedigree_config_query(const char *query);
44 
45 void pedigree_config_freeresult(size_t resultIdx);
46 
47 int pedigree_config_numcols(size_t resultIdx);
48 
49 int pedigree_config_numrows(size_t resultIdx);
50 
51 int pedigree_config_was_successful(size_t resultIdx);
52 
53 void pedigree_config_get_error_message(size_t resultIdx, char *buf, int buflen);
54 
55 char *pedigree_config_escape_string(const char *str);
56 } // extern "C"
57 
58 Config::Result::~Result()
59 {
60  pedigree_config_freeresult(m_nResultIdx);
61 }
62 
64 {
65  return !pedigree_config_was_successful(m_nResultIdx);
66 }
67 
68 std::string Config::Result::errorMessage(size_t buffSz)
69 {
70  // Allocate a buffer of the given (or default) size, zero it,
71  // get the string in it, then put the buffer contents into a nice string
72  // and return the nice string.
73  char *pBuffer = new char[buffSz];
74  memset(pBuffer, 0, buffSz);
75  pedigree_config_get_error_message(m_nResultIdx, pBuffer, buffSz);
76  std::string str(pBuffer);
77  delete[] pBuffer;
78  return str;
79 }
80 
82 {
83  return pedigree_config_numrows(m_nResultIdx);
84 }
85 
87 {
88  return pedigree_config_numcols(m_nResultIdx);
89 }
90 
91 std::string Config::Result::getColumnName(size_t col, size_t buffSz)
92 {
93  // Allocate a buffer of the given (or default) size, zero it,
94  // get the string in it, then put the buffer contents into a nice string
95  // and return the nice string.
96  char *pBuffer = new char[buffSz];
97  memset(pBuffer, 0, buffSz);
98  pedigree_config_getcolname(m_nResultIdx, col, pBuffer, buffSz);
99  std::string str(pBuffer);
100  delete[] pBuffer;
101  return str;
102 }
103 
104 std::string Config::Result::getStr(size_t row, size_t col, size_t buffSz)
105 {
106  // Allocate a buffer of the given (or default) size, zero it,
107  // get the string in it, then put the buffer contents into a nice string
108  // and return the nice string.
109  char *pBuffer = new char[buffSz];
110  memset(pBuffer, 0, buffSz);
111  pedigree_config_getstr_n(m_nResultIdx, row, col, pBuffer, buffSz);
112  std::string str(pBuffer);
113  delete[] pBuffer;
114  return str;
115 }
116 
117 size_t Config::Result::getNum(size_t row, size_t col)
118 {
119  return pedigree_config_getnum_n(m_nResultIdx, row, col);
120 }
121 
122 bool Config::Result::getBool(size_t row, size_t col)
123 {
124  return pedigree_config_getbool_n(m_nResultIdx, row, col);
125 }
126 
127 std::string Config::Result::getStr(size_t row, const char *col, size_t buffSz)
128 {
129  // Allocate a buffer of the given (or default) size, zero it,
130  // get the string in it, then put the buffer contents into a nice string
131  // and return the nice string.
132  char *pBuffer = new char[buffSz];
133  memset(pBuffer, 0, buffSz);
134  pedigree_config_getstr_s(m_nResultIdx, row, col, pBuffer, buffSz);
135  std::string str(pBuffer);
136  delete[] pBuffer;
137  return str;
138 }
139 
140 size_t Config::Result::getNum(size_t row, const char *col)
141 {
142  return pedigree_config_getnum_s(m_nResultIdx, row, col);
143 }
144 
145 bool Config::Result::getBool(size_t row, const char *col)
146 {
147  return pedigree_config_getbool_s(m_nResultIdx, row, col);
148 }
149 
150 Config::Result *Config::query(const char *sql)
151 {
152  // Check for null or empty queries
153  if (!sql || !*sql)
154  return 0;
155 
156  // Query the database
157  int resultIdx = pedigree_config_query(sql);
158 
159  // Check for query errors
160  if (resultIdx < 0)
161  return 0;
162 
163  // Return a new Result
164  return new Result(resultIdx);
165 }
size_t cols()
Returns the number of columns.
size_t rows()
Returns the number of rows.
std::string getColumnName(size_t col, size_t buffSz=256)
Returns the name of the col&#39;th column.
Definition: Result.h:36
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 succeeded()
Returns true if the result is valid, false if there was an error.
bool getBool(size_t row, size_t col)
Returns the value in column &#39;col&#39; of the result, in boolean form.