Google
Web alhem.net
Main Page | Alphabetical List | Class List | File List | Class Members | File Members

config.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2004  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #ifdef _WIN32
00024 #pragma warning(disable:4786)
00025 #endif
00026 #include <stdio.h>
00027 #include "Parse.h"
00028 
00029 #include "config.h"
00030 
00031 #define C slask[strlen(slask) - 1]
00032 
00033 
00034         config_m config;
00035 static  std::string config_filename;
00036 
00037 
00038 static void read_config(const std::string& filename)
00039 {
00040         FILE *fil = fopen(filename.c_str(),"rt");
00041         config_filename = filename;
00042         if (fil)
00043         {
00044                 char slask[1000];
00045                 std::string key;
00046                 std::string value;
00047                 fgets(slask,1000,fil);
00048                 while (!feof(fil))
00049                 {
00050                         while (strlen(slask) && (C == 13 || C == 10))
00051                                 C = 0;
00052                         Parse pa(slask);
00053                         pa.getword(key);
00054                         pa.getrest(value);
00055                         config[key] = value;
00056                         //
00057                         fgets(slask,1000,fil);
00058                 }
00059                 fclose(fil);
00060         }
00061 }
00062 
00063 
00064 void write_config() //const std::string& filename)
00065 {
00066         FILE *fil = fopen(config_filename.c_str(),"wt");
00067         if (fil)
00068         {
00069                 for (config_m::iterator it = config.begin(); it != config.end(); it++)
00070                 {
00071                         std::string key = (*it).first;
00072                         std::string value = (*it).second;
00073                         if (key[0] != '.')
00074                                 fprintf(fil,"%s %s\n",key.c_str(),value.c_str());
00075                 }
00076                 fclose(fil);
00077         }
00078 }
00079 
00080 
00081 void parse_config(int argc,char *argv[],const std::string& filename)
00082 {
00083         read_config( filename );
00084         for (int i = 1; i < argc; i++)
00085                 if (argv[i][0] == '-' && i < argc - 1)
00086                 {
00087                         config[argv[i] + 1] = argv[i + 1];
00088                         i++;
00089                 }
00090         write_config(); // filename );
00091 }
00092 
00093 

Generated on Tue Oct 3 23:47:05 2006 for The Minder by doxygen 1.3.6