1 /**
2  **	File ......... config.cpp
3  **	Published ....  2004-04-18
4  **	Author ....... grymse@alhem.net
5 **/
6 /*
7 Copyright (C) 2004  Anders Hedstrom
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23 #ifdef _WIN32
24 #pragma warning(disable:4786)
25 #endif
26 #include <stdio.h>
27 #include <cstring>
28 #include "Parse.h"
29 
30 #include "config.h"
31 
32 #define C slask[strlen(slask) - 1]
33 
34 
35 	config_m config;
36 static	std::string config_filename;
37 
38 
read_config(const std::string & filename)39 static void read_config(const std::string& filename)
40 {
41 	FILE *fil = fopen(filename.c_str(),"rt");
42 	config_filename = filename;
43 	if (fil)
44 	{
45 		char slask[1000];
46 		std::string key;
47 		std::string value;
48 		fgets(slask,1000,fil);
49 		while (!feof(fil))
50 		{
51 			while (strlen(slask) && (C == 13 || C == 10))
52 				C = 0;
53 			Parse pa(slask);
54 			pa.getword(key);
55 			pa.getrest(value);
56 			config[key] = value;
57 			//
58 			fgets(slask,1000,fil);
59 		}
60 		fclose(fil);
61 	}
62 }
63 
64 
write_config()65 void write_config() //const std::string& filename)
66 {
67 	FILE *fil = fopen(config_filename.c_str(),"wt");
68 	if (fil)
69 	{
70 		for (config_m::iterator it = config.begin(); it != config.end(); it++)
71 		{
72 			std::string key = (*it).first;
73 			std::string value = (*it).second;
74 			if (key[0] != '.')
75 				fprintf(fil,"%s %s\n",key.c_str(),value.c_str());
76 		}
77 		fclose(fil);
78 	}
79 }
80 
81 
parse_config(int argc,char * argv[],const std::string & filename)82 void parse_config(int argc,char *argv[],const std::string& filename)
83 {
84 	read_config( filename );
85 	for (int i = 1; i < argc; i++)
86 		if (argv[i][0] == '-' && i < argc - 1)
87 		{
88 			config[argv[i] + 1] = argv[i + 1];
89 			i++;
90 		}
91 	write_config(); // filename );
92 }
93 
94 
95