config.cpp

changeset 69
6790dea720a8
parent 68
c637b172d565
child 78
c190fe218506
--- a/config.cpp	Mon Mar 25 15:20:56 2013 +0200
+++ b/config.cpp	Mon Mar 25 16:05:03 2013 +0200
@@ -17,53 +17,14 @@
  */
 
 #include <stdio.h>
-// #include <stdlib.h>
+#include <stdlib.h>
 #include <errno.h>
 #include <time.h>
 #include "str.h"
 #include "config.h"
-
-#ifdef CONFIG_WITH_QT
 #include <QDir>
-#endif // CONFIG_WITH_QT
-
-// =============================================================================
-// Define the configs
-#define CFG(TYPE, SECT, NAME, DESCR, DEFAULT) \
-	TYPE##config SECT##_##NAME (CFGSECTNAME (SECT), DESCR, \
-		DEFAULT, #NAME, #SECT "_" #NAME, #TYPE, #DEFAULT);
-
-#define SECT(...)
- #include "cfgdef.h"
-#undef CFG
-#undef SECT
 
-// =============================================================================
-config* config::pointers[] = {
-#define CFG(TYPE, SECT, NAME, DESCR, DEFAULT) &SECT##_##NAME,
-#define SECT(...)
- #include "cfgdef.h"
-#undef CFG
-#undef SECT
-};
-
-// =============================================================================
-const char* config::sections[] = {
-#define CFG(...)
-#define SECT(A,B) #A,
- #include "cfgdef.h"
-#undef CFG
-#undef SECT
-};
-
-// =============================================================================
-const char* config::sectionNames[] = {
-#define CFG(...)
-#define SECT(A,B) #B,
- #include "cfgdef.h"
-#undef CFG
-#undef SECT
-};
+std::vector<config*> g_pConfigPointers;
 
 // =============================================================================
 const char* g_WeekdayNames[7] = {
@@ -110,7 +71,6 @@
 	char linedata[MAX_INI_LINE];
 	char* line;
 	size_t ln = 0;
-	configsection_e section = NO_CONFIG_SECTION;
 	
 	if (!fp)
 		return false; // can't open for reading
@@ -126,32 +86,6 @@
 		if (*line == '\0' || line[0] == '#')
 			continue; // Empty line or comment.
 		
-		if (line[0] == '[') {
-			// Section
-			char* endbracket = strchr (line, ']');
-			
-			if (!endbracket) {
-				fprintf (stderr, "badly formed section: %s", line);
-				continue;
-			}
-			
-			str sectionName = str (line).substr (1, endbracket - line);
-			const configsection_e oldsection = section;
-			section = NO_CONFIG_SECTION;
-			
-			// Find the section
-			for (unsigned i = 0; i < NUM_ConfigSections && section == NO_CONFIG_SECTION; i++)
-				if (sectionName.compare (sectionNames[i]) == 0)
-					section = (configsection_e)i;
-			
-			if (section == NO_CONFIG_SECTION) {
-				fprintf (stderr, "unknown config section `%s`\n", sectionName.chars());
-				section = oldsection;
-			}
-			
-			continue;
-		}
-		
 		// Find the equals sign.
 		char* equals = strchr (line, '=');
 		if (!equals) {
@@ -161,17 +95,14 @@
 		
 		str entry = str (line).substr (0, equals - line);
 		
-		str configname;
-		configname.format ("%s_%s", sections[section], entry.chars ());
-		
 		// Find the config entry for this.
-		config* cfg = NULL;
-		for (size_t i = 0; i < NUM_CONFIG && !cfg; i++)
-			if (configname.compare (pointers[i]->fullname) == 0)
-				cfg = pointers[i];
+		config* cfg = nullptr;
+		for (config* i : g_pConfigPointers)
+			if (entry == i->name)
+				cfg = i;
 		
 		if (!cfg) {
-			fprintf (stderr, "unknown config `%s`\n", configname.chars());
+			fprintf (stderr, "unknown config `%s`\n", entry.chars());
 			continue;
 		}
 		
@@ -237,21 +168,14 @@
 	// are written properly.
 	setlocale (LC_NUMERIC, "C");
 	
-#ifdef APPNAME
-	#ifdef CONFIG_WITH_QT
-		// If the directory doesn't exist, create it now.
-		if (!QDir (dirpath().chars()).exists ()) {
-			fprintf (stderr, "Creating config path %s...\n", dirpath().chars());
-			if (!QDir ().mkpath (dirpath().chars())) {
-				fprintf (stderr, "Failed to create the directory. Configuration cannot be saved!\n");
-				return false; // Couldn't create directory
-			}
+	// If the directory doesn't exist, create it now.
+	if (!QDir (dirpath().chars()).exists ()) {
+		fprintf (stderr, "Creating config path %s...\n", dirpath().chars());
+		if (!QDir ().mkpath (dirpath().chars())) {
+			fprintf (stderr, "Failed to create the directory. Configuration cannot be saved!\n");
+			return false; // Couldn't create directory
 		}
-	#else
-		#warning Need QT to check for directories. Config will not be able
-		#warning to save properly if ~/.APPNAME/ does not exist.
-	#endif // CONFIG_WITH_QT
-#endif // CONFIG_DIRECTORY
+	}
 	
 	FILE* fp = fopen (filepath().chars(), "w");
 	printf ("writing cfg to %s\n", filepath().chars());
@@ -273,59 +197,40 @@
 		g_WeekdayNames[timeinfo->tm_wday], g_MonthNames[timeinfo->tm_mon],
 		timeinfo->tm_mday, daysuffix, timeinfo->tm_year + 1900,
 		timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
-	writef (fp, "\n");
 	
-	for (int i = 0; i < NUM_ConfigSections; i++) {
-		if (i > 0)
-			writef (fp, "\n");
-		
-		writef (fp, "[%s]\n", sectionNames[i]);
-		bool first = true;
-		
-		for (size_t j = 0; j < NUM_CONFIG; j++) {
-			config* cfg = pointers[j];
-			
-			if (cfg->sect != i)
-				continue;
-			
-			if (!first)
-				writef (fp, "\n");
+	for (config* cfg : g_pConfigPointers) {
+		str valstring;
+		switch (cfg->getType()) {
+		case CONFIG_int:
+			valstring.format ("%d", static_cast<intconfig*> (cfg)->value);
+			break;
+		case CONFIG_str:
+			valstring = static_cast<strconfig*> (cfg)->value;
+			break;
+		case CONFIG_float:
+			valstring.format ("%f", static_cast<floatconfig*> (cfg)->value);
 			
-			str valstring;
-			switch (cfg->getType()) {
-			case CONFIG_int:
-				valstring.format ("%d", static_cast<intconfig*> (cfg)->value);
-				break;
-			case CONFIG_str:
-				valstring = static_cast<strconfig*> (cfg)->value;
-				break;
-			case CONFIG_float:
-				valstring.format ("%f", static_cast<floatconfig*> (cfg)->value);
+			// Trim any trailing zeros
+			if (valstring.first (".") != -1) {
+				while (valstring[~valstring - 1] == '0')
+					valstring -= 1;
 				
-				// Trim any trailing zeros
-				if (valstring.first (".") != -1) {
-					while (valstring[~valstring - 1] == '0')
-						valstring -= 1;
-					
-					// But don't trim the only one out...
-					if (valstring[~valstring - 1] == '.')
-						valstring += '0';
-				}
-				
-				break;
-			case CONFIG_bool:
-				valstring = (static_cast<boolconfig*> (cfg)->value) ? "true" : "false";
-				break;
-			default:
-				break;
+				// But don't trim the only one out...
+				if (valstring[~valstring - 1] == '.')
+					valstring += '0';
 			}
 			
-			// Write the entry now.
-			writef (fp, "# %s, default: %s\n", g_ConfigTypeNames[cfg->getType()], cfg->defaultstring);
-			writef (fp, "# %s: %s\n", cfg->fullname, cfg->description);
-			writef (fp, "%s=%s\n", cfg->name, valstring.chars());
-			first = false;
+			break;
+		case CONFIG_bool:
+			valstring = (static_cast<boolconfig*> (cfg)->value) ? "true" : "false";
+			break;
+		default:
+			break;
 		}
+		
+		// Write the entry now.
+		writef (fp, "\n# [%s] default: %s\n", g_ConfigTypeNames[cfg->getType()], cfg->defaultstring);
+		writef (fp, "%s=%s\n", cfg->name, valstring.chars());
 	}
 	
 	fclose (fp);
@@ -335,35 +240,20 @@
 // =============================================================================
 void config::reset () {
 	for (size_t i = 0; i < NUM_CONFIG; i++)
-		pointers[i]->resetValue ();
+		g_pConfigPointers[i]->resetValue ();
 }
 
 // =============================================================================
 str config::filepath () {
-#ifdef APPNAME
 	str path;
-	path.format ("%s" APPNAME ".ini", dirpath().chars());
+	path.format ("%s" CONFIGFILE, dirpath().chars());
 	return path;
-#else // APPNAME
-	return "config.ini";
-#endif // APPNAME
 }
 
 // =============================================================================
 str config::dirpath () {
-#ifdef APPNAME
-	str path;
-	
-	#ifdef CONFIG_WITH_QT
-		path = (QDir::homePath ().toStdString().c_str());
-	#else // CONFIG_WITH_QT
-		path = "~";
-	#endif // CONFIG_WITH_QT
-	
+	str path = (QDir::homePath ().toStdString().c_str());
 	path += "/." APPNAME "/";
-#else
-	path = "./";
-#endif // APPNAME
 	
 	return path;
 }
\ No newline at end of file

mercurial