File: Config.cpp

package info (click to toggle)
beid 3.5.2.dfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 147,240 kB
  • ctags: 34,507
  • sloc: cpp: 149,944; ansic: 41,577; java: 8,927; cs: 6,528; sh: 2,426; perl: 1,866; xml: 805; python: 463; makefile: 263; lex: 92
file content (366 lines) | stat: -rw-r--r-- 11,033 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* ****************************************************************************

 * eID Middleware Project.
 * Copyright (C) 2008-2009 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see
 * http://www.gnu.org/licenses/.

**************************************************************************** */
#include <iostream>
#include <stdlib.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "Config.h"
#include "Util.h"
#include "datafile.h"
#include "eidErrors.h"
#include "MWException.h"
#include "prefix.h"

// open the user config file and also the general config file
// search first in the user config file and then in the general one

// setters: set always in the user config file unless the admin is executing the programme
// ---> now the choice of the file is made via the location::USER and location::SYSTEM,
//      still one should check the permissions for the requested location
namespace eIDMW
{ 

  bool bIsInitialized = false;

  CDataFile CConfig::o_userDataFile;
  CDataFile CConfig::o_systemDataFile;
  CMutex CConfig::m_Mutex;

  std::wstring home_path;

  CConfig::CConfig(void)  {
  }

  CConfig::~CConfig(){
  }

  void CConfig::Init(){

    if (! bIsInitialized){
      const std::wstring system_datafilePath = utilStringWiden(STRINGIFY(EIDMW_PREFIX)) + L"/etc/";
      home_path = utilStringWiden(std::string(getenv("HOME")));
#ifdef __APPLE__
      const std::wstring user_datafilePath = L"Library/Preferences/";
#else
      const std::wstring user_datafilePath = L".config/";
      mkdir(utilStringNarrow(home_path + L"/" + user_datafilePath).c_str(), 0700);  //Create the directory if it doesn't exist
#endif

      const std::wstring userFile = home_path + L"/" + user_datafilePath + L"beid.conf";
      const std::wstring systemFile = system_datafilePath + L"beid.conf";

      o_userDataFile.SetFileName(userFile) ;
 
      o_systemDataFile.SetFileName(systemFile);
    
      bIsInitialized = true;
    }
  }

/**
 * Macro expansion:
 *  - $install -> $PREFIX (default: /usr/local)
 *  - $home    -> user home dir
 *  - $common  -> /tmp
 */
std::wstring ExpandSection(
	const std::wstring & czSectionOriginal
    ) 
{
    int                              iResult;
    basic_string <char>::size_type   iTotLenght = czSectionOriginal.length();
    basic_string <char>::size_type   iStrLenght;

    //--- Find if anything to expand
    if ((iTotLenght == 0)||(czSectionOriginal[0] != '$'))
    {
        //nothing to replace
        return(czSectionOriginal);
    }

    //--- check for EIDMW_CNF_MACRO_INSTALL
    iStrLenght = wcslen(EIDMW_CNF_MACRO_INSTALL);
    iResult = czSectionOriginal.compare(0, iStrLenght, EIDMW_CNF_MACRO_INSTALL);
    if (iResult == 0)
    {
        //replace EIDMW_CNF_MACRO_INSTALL
        std::wstring czSectionExpanded(utilStringWiden(STRINGIFY(EIDMW_PREFIX)));

        //add part after the $-macro
        czSectionExpanded.append(czSectionOriginal.substr(iStrLenght, iTotLenght-iStrLenght));//add part after the $-macro
        return(czSectionExpanded);
    }

    //--- check for EIDMW_CNF_MACRO_HOME
    iStrLenght = wcslen(EIDMW_CNF_MACRO_HOME);
    iResult = czSectionOriginal.compare(0, iStrLenght, EIDMW_CNF_MACRO_HOME);
    if (iResult == 0)
    {
        std::wstring czSectionExpanded(home_path);

        //add part after the $-macro
        czSectionExpanded.append(czSectionOriginal.substr(iStrLenght, iTotLenght-iStrLenght));//add part after the $-macro
        return(czSectionExpanded);
    }

    //--- check for EIDMW_CNF_MACRO_COMMON
    iStrLenght = wcslen(EIDMW_CNF_MACRO_COMMON);
    iResult = czSectionOriginal.compare(0, iStrLenght, EIDMW_CNF_MACRO_COMMON);
    if (iResult == 0)
    {
        std::wstring czSectionExpanded(L"/tmp");

        //add part after the $-macro
        czSectionExpanded.append(czSectionOriginal.substr(iStrLenght, iTotLenght-iStrLenght));//add part after the $-macro
        return(czSectionExpanded);
    }

    return(czSectionOriginal);
}

  std::wstring CConfig::GetStringInt(tLocation location, const std::wstring &csName, const std::wstring &csSection,bool bExpand)
  {
    CAutoMutex autoMutex(&m_Mutex);

    if (! bIsInitialized) Init();

    std::wstring csResult= L"";

     if(location == CConfig::SYSTEM)
        csResult = o_systemDataFile.GetString(csName,csSection);
    else
        csResult = o_userDataFile.GetString(csName,csSection);

    if( csResult  != L"" )
         return bExpand ? ExpandSection(csResult) : csResult;

   throw CMWEXCEPTION(EIDMW_CONF);
  }

 std::wstring CConfig::GetStringInt(const std::wstring &csName, const std::wstring &csSection,bool bExpand)
  {
	try
	{
		return GetStringInt(CConfig::USER,csName,csSection,bExpand);
	}
	catch(...)
	{
		return GetStringInt(CConfig::SYSTEM,csName,csSection,bExpand);
	}
  }

  // std::wstring CConfig::GetString(t_Str szKey, t_Str szSection)
  std::wstring CConfig::GetString(const Param_Str param) 
  {
	return(GetString(param.csParam, param.csSection, param.csDefault, true));
  }


  std::wstring CConfig::GetString(const std::wstring & csName,
				  const std::wstring & szSection) 
  {
    return GetStringInt(csName, szSection, true);
  }

  std::wstring CConfig::GetString(const std::wstring &csName, 
				  const std::wstring &czSection, 
				  const std::wstring &csDefaultValue,
				  bool bExpand)
  {
    try 
    {
      return GetStringInt(csName, czSection, bExpand);
    }
    catch(...)
    {
      return bExpand ? ExpandSection(csDefaultValue) : csDefaultValue;
    }
};

  std::wstring CConfig::GetString(tLocation location, const Param_Str param) 
  {
	return(GetString(location,param.csParam, param.csSection, param.csDefault));
  }


  std::wstring CConfig::GetString(tLocation location, 
				  const std::wstring & csName,
				  const std::wstring & szSection) 
  {
    return GetStringInt(location,csName, szSection, true);
  }

  std::wstring CConfig::GetString(tLocation location, 
				  const std::wstring &csName, 
				  const std::wstring &czSection, 
				  const std::wstring &csDefaultValue,
				  bool bExpand)
  {
    try 
    {
      return GetStringInt(location,csName, czSection,bExpand);
    }
    catch(...)
    {
      return bExpand ? ExpandSection(csDefaultValue) : csDefaultValue;
    }
  };

  long CConfig::GetLong(tLocation location, const Param_Num param) 
  {
	return(GetLong(location,param.csParam, param.csSection, param.lDefault));
  }

  long CConfig::GetLong(  tLocation location, const std::wstring &csName, const std::wstring &czSection) { 
    CAutoMutex autoMutex(&m_Mutex);

    if (! bIsInitialized) Init();

    long lResult = LONG_MIN;

     if(location == CConfig::SYSTEM)
        lResult = o_systemDataFile.GetLong(csName,czSection);
    else
        lResult = o_userDataFile.GetLong(csName,czSection) ;

    if(lResult != LONG_MIN)
      return lResult;

    throw CMWEXCEPTION(EIDMW_CONF);
  };


  long CConfig::GetLong(  tLocation location, const std::wstring &csName, const std::wstring &czSection, long lDefaultValue) { 

     try {
      return  GetLong(location,csName,czSection);
    } catch (...) {
      return lDefaultValue;
    }
  };

 long CConfig::GetLong(const Param_Num param) 
  {
	return(GetLong(param.csParam, param.csSection, param.lDefault));
  }

  long CConfig::GetLong(  const std::wstring &csName, const std::wstring &czSection) { 
 	try
	{
		return GetLong(CConfig::USER,csName,czSection);
	}
	catch(...)
	{
		return GetLong(CConfig::SYSTEM,csName,czSection);
	}
  };


  long CConfig::GetLong(  const std::wstring &csName, const std::wstring &czSection, long lDefaultValue) { 

     try {
      return  GetLong(csName,czSection);
    } catch (...) {
      return lDefaultValue;
    }
  };

void CConfig::SetString(tLocation location,const struct Param_Str param,const std::wstring &csValue)
{
	return(SetString(location,param.csParam,param.csSection,csValue));
}

  void CConfig::SetString(tLocation location, 
			  const std::wstring &csName, 
			  const std::wstring &czSection, 
			  const std::wstring &csValue) {
    CAutoMutex autoMutex(&m_Mutex);

    if (! bIsInitialized) Init();
        
    if (location == SYSTEM)
    {
      o_systemDataFile.SetValue(csName,csValue,L"",czSection);
      if( ! o_systemDataFile.Save() )
        throw CMWEXCEPTION(EIDMW_CONF);
    } else {
      o_userDataFile.SetValue(csName,csValue,L"",czSection);
      if( ! o_userDataFile.Save())
        throw CMWEXCEPTION(EIDMW_CONF);
    }
  };

void CConfig::SetLong(tLocation location,const struct Param_Num param,long lValue)
{
	return(SetLong(location,param.csParam,param.csSection,lValue));
}

  void CConfig::SetLong(  tLocation location, const std::wstring &csName, const std::wstring &czSection, long lValue) {
  CAutoMutex autoMutex(&m_Mutex);
   
  if (! bIsInitialized) Init();

    if (location == SYSTEM){
      o_systemDataFile.SetLong(csName,lValue,L"",czSection);
      if( ! o_systemDataFile.Save() )
         throw CMWEXCEPTION(EIDMW_CONF);
   } else {
      o_userDataFile.SetLong(csName,lValue,L"",czSection);
      if( ! o_userDataFile.Save() )
         throw CMWEXCEPTION(EIDMW_CONF);
   }
  };

void CConfig::DelString(tLocation location, const struct Param_Str param)
{
	return(DelString(location,param.csParam,param.csSection));
}

   void CConfig::DelString(tLocation location, const std::wstring &csName, const std::wstring &czSection) {
    CAutoMutex autoMutex(&m_Mutex);

    if (! bIsInitialized) Init();

    if (location == SYSTEM){
      if( ! o_systemDataFile.DeleteKey(csName,czSection) )
	throw CMWEXCEPTION(EIDMW_CONF);
      if( ! o_systemDataFile.Save() )
         throw CMWEXCEPTION(EIDMW_CONF);
  } else {
     if( !  o_userDataFile.DeleteKey(csName,czSection) )
	throw CMWEXCEPTION(EIDMW_CONF);
     if( ! o_userDataFile.Save() )
        throw CMWEXCEPTION(EIDMW_CONF);
    }
  };

void CConfig::DelLong(tLocation location, const struct Param_Num param) 
{
	return(DelLong(location,param.csParam,param.csSection));
}

void CConfig::DelLong(  tLocation location, const std::wstring &csName, const std::wstring &czSection) 
{
    DelString(location,csName,czSection);
};

} // namespace eidMW