The Pedigree Project  0.1
user/applications/config/main.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 <iostream>
21 #include <string>
22 
23 #include "pedigree/native/config/Config.h"
24 
25 void usage()
26 {
27  std::cout << "config: Query and update the Pedigree configuration manager."
28  << std::endl
29  << "usage: config <sql>" << std::endl;
30 }
31 
32 int main(int argc, char **argv)
33 {
34  if (argc != 2)
35  {
36  usage();
37  return 1;
38  }
39 
40  const char *sql = argv[1];
41 
42  Config::Result *pResult = Config::query(sql);
43 
44  // Check for query fail
45  if (!pResult)
46  {
47  std::cerr << "Unable to query" << std::endl;
48  return 0;
49  }
50 
51  // Check for query error
52  if (!pResult->succeeded())
53  {
54  std::cerr << "error: " << pResult->errorMessage() << std::endl;
55  delete pResult;
56  return 1;
57  }
58 
59  // Is this an empty set?
60  if (!pResult->rows())
61  {
62  std::cout << "Ø" << std::endl;
63  delete pResult;
64  return 0;
65  }
66 
67  size_t cols = pResult->cols();
68  size_t *col_lens = new size_t[cols];
69 
70  // Print the column names
71  for (size_t col = 0; col < cols; col++)
72  {
73  std::string colName = pResult->getColumnName(col);
74  std::cout << " " << colName;
75  col_lens[col] = colName.length();
76  while (col_lens[col] < 15)
77  {
78  std::cout << " ";
79  col_lens[col]++;
80  }
81  std::cout << " |";
82  }
83  std::cout << std::endl;
84 
85  // Print delimiter row
86  for (size_t col = 0; col < cols; col++)
87  {
88  std::cout << "-";
89  for (size_t i = 0; i < col_lens[col]; i++)
90  std::cout << "-";
91  std::cout << "-+";
92  }
93  std::cout << std::endl;
94 
95  // Print the results
96  for (size_t row = 0; row < pResult->rows(); row++)
97  {
98  for (size_t col = 0; col < cols; col++)
99  {
100  std::string value = pResult->getStr(row, col);
101  std::cout << " " << value << "\t";
102  for (size_t i = value.length(); i < col_lens[col]; i++)
103  std::cout << " ";
104  std::cout << " |";
105  }
106  std::cout << std::endl;
107  }
108 
109  // Cleanup
110  delete[] col_lens;
111  delete pResult;
112 
113  return 0;
114 }
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.
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.
Result * query(const char *sql)
bool succeeded()
Returns true if the result is valid, false if there was an error.
std::string errorMessage(size_t buffSz=256)
Returns the error message.