blob: dfb78ca45b49b5683f18f3c93bbbee61e5876251 (
plain)
1 /**
2 * A class to help with parsing standard config files
3 *
4 * Copyright (C) 2014 Aaron Ball <nullspoon@iohq.net>
5 *
6 * Noteless is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Noteless is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with noteless. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "config.h"
20
21 /**
22 * Constructor for the config class. Reads config file into a vector (one line
23 * per index), then parses each line into a vector size 2x.
24 *
25 * @param const char* p Path to the config file to be read in
26 */
27 config::config( const char* p ) {
28 path = p;
29
30 vector<string> lines;
31 ifstream fs( path );
32
33 // Verify the file handle opened sucessfully
34 if( ! fs.is_open() ) {
35 cout << "Could not open file at " << path << "." << endl;
36 exit( 1 );
37 }
38 // Continue
39 string line;
40 while( getline( fs, line ) ) {
41 parse_line( line );
42 }
43 fs.close();
44 }
45
46 /**
47 * Parses one line of the config file. Strips illegal characters first, then
48 * sets key to the text on the left of the equals sign and value to the text on
49 * the right.
50 *
51 * string line - Config file line to be parsed and added to the config object
52 *
53 * return int Success or failure
54 */
55 int config::parse_line( string line ) {
56 // The two vars that will be appended to the key value vector
57 string key;
58 string value;
59 int split;
60
61 // Clean spaces and other illegal chars
62 string clean_line;
63 for( int i = 0; i < line.length(); i++ ) {
64 if( line[i] == ' ' ) {
65 // Ignore, it's a space
66 continue;
67 } else {
68 clean_line += line[i];
69 }
70 }
71
72 // Line was empty, skip 'er
73 if( clean_line.length() == 0 ) { return 0; }
74
75 // One character at a time
76 for( int i = 0; i < clean_line.length(); i++ ) {
77 if( clean_line[i] == '=' ) {
78 // Set the key
79 key = clean_line.substr( 0, i );
80 split = i;
81 break;
82 }
83 }
84 // Set the value
85 value = clean_line.substr( split + 1, clean_line.length() );
86
87 // Append to class global values
88 values.push_back( key );
89 values.push_back( value );
90
91 // Success!
92 return 0;
93 }
94
95 bool config::isset( string key ) {
96 for( int i = 0; i < values.size(); i+=2 ) {
97 if( values[i] == key ) {
98 return true;
99 }
100 }
101 return false;
102 }
103
104 /**
105 * Returns the value for the specified key
106 *
107 * string key - The key to return the value for
108 *
109 * return string The value of the specified key
110 */
111 string config::get( string key ) {
112 // Hop skip and jump
113 // Evens are keys, odds are values (even + 1)
114 for( int i = 0; i < values.size(); i+=2 ) {
115 if( values[i] == key ) {
116 return values[i+1];
117 }
118 }
119 return "Unknown";
120 }
|