summaryrefslogtreecommitdiff
path: root/src/configfile.cc
blob: 7fe095ab03f470080ba2dcb08a8f274abb66718f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            configfile.cc
 *
 *  Thu May 14 14:51:39 CEST 2015
 *  Copyright 2015 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "configfile.h"

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/types.h>

#ifdef WIN32
#include <direct.h>
#include <windows.h>
#include <Shlobj.h>
#include <Shlwapi.h>
#else
#endif

#include <hugin.hpp>

#ifdef WIN32
#define SEP "\\"
#else
#define SEP "/"
#endif

#define CONFIGDIRNAME ".drumgizmo"

/**
 * Return the path containing the config files.
 */
static std::string getConfigPath()
{
#ifdef WIN32
	std::string configpath;
	TCHAR szPath[256];
	if(SUCCEEDED(SHGetFolderPath(
	       NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, szPath)))
	{
		configpath = szPath;
	}
#else
	std::string configpath = getenv("HOME");
#endif
	configpath += SEP;
	configpath += CONFIGDIRNAME;

	return configpath;
}

/**
 * Calling this makes sure that the config path exists
 */
static bool createConfigPath()
{
	std::string configpath = getConfigPath();

	struct stat st;
	if(stat(configpath.c_str(), &st) == 0)
	{
		DEBUG(configfile, "No configuration exists, creating directory '%s'\n",
		    configpath.c_str());
#ifdef WIN32
		if(mkdir(configpath.c_str()) < 0)
		{
#else
		if(mkdir(configpath.c_str(), 0755) < 0)
		{
#endif
			DEBUG(configfile, "Could not create config directory\n");
		}

		return false;
	}

	return true;
}

ConfigFile::ConfigFile(std::string const& filename)
	: filename(filename)
	, fp(nullptr)
{
}

ConfigFile::~ConfigFile()
{
	if (fp != nullptr)
	{
		DEBUG(configfile, "File has not been closed by the client...\n";
	}
}

bool ConfigFile::load()
{
	DEBUG(configfile, "Loading config file...\n");
	if(!open("r"))
	{
		return false;
	}

	values.clear();

	std::string line;
	while(true)
	{
		line = readLine();

		if(line == "")
			break;

		if(!parseLine(line))
		{
			return false;
		}
	}

	close();

	return true;
}

bool ConfigFile::save()
{
	DEBUG(configfile, "Saving configuration...\n");

	createConfigPath();

	if(!open("w"))
	{
		return false;
	}

	std::map<std::string, std::string>::iterator v = values.begin();
	for(; v != values.end(); ++v)
	{
		fprintf(fp, "%s:%s\n", v->first.c_str(), v->second.c_str());
	}

	close();

	return true;
}

std::string ConfigFile::getValue(const std::string& key) const
{
	auto i = values.find(key);
	if (i != values.end())
	{
		return i->second;
	}

	return "";
}

void ConfigFile::setValue(const std::string& key, const std::string& value)
{
	values[key] = value;
}

bool ConfigFile::open(std::string mode)
{
	if(fp)
	{
		close();
	}

	std::string configpath = getConfigPath();

	std::string configfile = configpath;
	configfile += SEP;
	configfile += filename;

	DEBUG(configfile, "Opening config file '%s'\n", configfile.c_str());
	fp = fopen(configfile.c_str(), mode.c_str());

	if(!fp)
	{
		return false;
	}

	return true;
}

void ConfigFile::close()
{
	fclose(fp);
	fp = nullptr;
}

std::string ConfigFile::readLine()
{
	if(!fp)
	{
		return "";
	}
	
	std::string line;

	char buf[1024];
	while(!feof(fp))
	{
		char* s = fgets(buf, sizeof(buf), fp);
		if(s)
		{
			line += buf;
			if(buf[strlen(buf) - 1] == '\n')
				break;
		}
	}

	return line;
}

bool ConfigFile::parseLine(const std::string& line)
{
	std::string key;
	std::string value;
	enum
	{
		before_key,
		in_key,
		after_key,
		before_value,
		in_value,
		in_value_single_quoted,
		in_value_double_quoted,
		after_value,
	} state = before_key;

	for(std::size_t p = 0; p < line.size(); ++p)
	{
		switch(state)
		{
		case before_key:
			if(line[p] == '#')
			{
				// Comment: Ignore line.
				p = line.size();
				continue;
			}
			if(std::isspace(line[p]))
			{
				continue;
			}
			key += line[p];
			state = in_key;
			break;

		case in_key:
			if(std::isspace(line[p]))
			{
				state = after_key;
				continue;
			}
			if(line[p] == ':' || line[p] == '=')
			{
				state = before_value;
				continue;
			}
			key += line[p];
			break;

		case after_key:
			if(std::isspace(line[p]))
			{
				continue;
			}
			if(line[p] == ':' || line[p] == '=')
			{
				state = before_value;
				continue;
			}
			ERR(configfile,
			    "Bad symbol."
			    " Expecting only whitespace or key/value seperator: '%s'",
			    line.c_str());
			return false;

		case before_value:
			if(std::isspace(line[p]))
			{
				continue;
			}
			if(line[p] == '\'')
			{
				state = in_value_single_quoted;
				continue;
			}
			if(line[p] == '"')
			{
				state = in_value_double_quoted;
				continue;
			}
			value += line[p];
			state = in_value;
			break;

		case in_value:
			if(std::isspace(line[p]))
			{
				state = after_value;
				continue;
			}
			if(line[p] == '#')
			{
				// Comment: Ignore the rest of the line.
				p = line.size();
				state = after_value;
				continue;
			}
			value += line[p];
			break;

		case in_value_single_quoted:
			if(line[p] == '\'')
			{
				state = after_value;
				continue;
			}
			value += line[p];
			break;

		case in_value_double_quoted:
			if(line[p] == '"')
			{
				state = after_value;
				continue;
			}
			value += line[p];
			break;

		case after_value:
			if(std::isspace(line[p]))
			{
				continue;
			}
			if(line[p] == '#')
			{
				// Comment: Ignore the rest of the line.
				p = line.size();
				continue;
			}
			ERR(configfile,
			    "Bad symbol."
			    " Expecting only whitespace or key/value seperator: '%s'",
			    line.c_str());
			return false;
		}
	}

	if(state == before_key)
	{
		// Line did not contain any data (empty or comment)
		return true;
	}

	// If state == in_value_XXX_quoted here, the string was not terminated.
	if(state != after_value && state != in_value)
	{
		ERR(configfile, "Malformed line: '%s'", line.c_str());
		return false;
	}

	DEBUG(configfile, "key['%s'] value['%s']\n", key.c_str(), value.c_str());

	if(key != "")
	{
		values[key] = value;
	}

	return true;
}