summaryrefslogtreecommitdiffstats
path: root/lanbrowsing/lisa/configfile.cpp
blob: 5c1e4a650e3f99700be8152d1d4c7ab6475e43a6 (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
/*    configfile.cpp
 *
 *    Copyright (c) 1998, 2000, Alexander Neundorf
 *    neundorf@kde.org
 *
 *    You may distribute under the terms of the GNU General Public
 *    License as specified in the COPYING file.
 *
 *    This program 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 General Public License for more details.
 *
 */

#include "configfile.h"

#include <iostream>
#include <fstream>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef LISA_DEBUG
#undef LISA_DEBUG
#endif
#define LISA_DEBUG 0

#ifdef dcerr
#undef dcerr
#endif

#define dcerr if (LISA_DEBUG==1) std::cerr<<"Config::"


#define CFGBUFSIZE 16*1024

Config::Config(const MyString& name/*,String path*/)
{
   char buff[CFGBUFSIZE],c;
/*   String s,empty="#############################################################################################################################################################";
   String home=getenv("HOME");

   if (!home.empty()) home+=String("/")+name;
   if (fexists(home)==0)
   {
      home=path+"/"+name;
      if (fexists(home)==0) 
      {
         home=name;
         if (fexists(home)==0) return;
      };
   };*/
   std::ifstream inf(name.data());
   if (!inf)
   {
      std::cout<<"could not open file "<<name<<std::endl;
      return;
   }
   dcerr<<"Config(): opened file "<<name<<std::endl;
   //read the file
   char key[CFGBUFSIZE], value[CFGBUFSIZE];
   do
	{
      char* buffStart=buff;
		//inf.getline(buff,16*1024,'\n');
      int bufSize(CFGBUFSIZE);
      int lineBroken(0);
      do
      {
         lineBroken=0;
         inf.get(buffStart,bufSize,'\n');
         inf.get(c);
         int l=strlen(buffStart);
         if (l==0)
            break;
         if (buffStart[l-1]=='\\')
         {
            buffStart=buffStart+l-1;
            bufSize=bufSize+1-l;
            lineBroken=1;
         }
      } while ((lineBroken) && (!inf.eof()));
      //make it ignore comments
      char *theChar=strchr(buff,'#');
      if (theChar!=0)
         *theChar='\0';
      //now divide the line into key and value
      theChar=strchr(buff,'=');
      if (theChar!=0)
      {
         *theChar='\0';
         key[0]='\0';
         sscanf(buff,"%8000s",key);
         //do we have something valid ?
         if (key[0]!='\0')
         {
            //the char behind the = should be at least the terminating \0
            // so I can be sure to access valid memory here, IMO
            value[0]='\0';

            strncpy(value,theChar+1,CFGBUFSIZE);
            if (value[0]!='\0')
            {
               //here we can be sure that the list will only contain
               //strings which are at least one char long
               dcerr<<"Config(): adding "<<key<<std::endl;
               m_entries[key]=value;
            }
         }
      }
	}
   while (!inf.eof());
}

MyString Config::getEntry(const char *key, const char* defaultValue)
{
   if ((key==0) || (key[0]=='\0'))
      return defaultValue;
   if (m_entries.find(key)==m_entries.end())
      return defaultValue;
   return m_entries[key];
}

int Config::getEntry(const char *key, int defaultValue)
{
   char def[100];
   sprintf(def,"%d",defaultValue);
   MyString tmp=stripWhiteSpace(getEntry(key,def));
   int i(0);
   int ok=sscanf(tmp.c_str(),"%d",&i);
   if (ok==1) return i;
   return defaultValue;
}