Logo
~Sockets~
~Examples~
~Contact~


config.cpp

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