File: setup_config.cpp

package info (click to toggle)
xstow 1.0.2-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,064 kB
  • ctags: 1,073
  • sloc: cpp: 7,264; sh: 1,216; makefile: 196; awk: 26
file content (269 lines) | stat: -rw-r--r-- 4,901 bytes parent folder | download | duplicates (3)
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
/*
 * $Log: setup_config.cpp,v $
 * Revision 1.2  2005/07/04 21:59:42  martin
 * added logging to all files
 *
 */
#include "setup_config.h"
#include "string_utils.h"
#include "leoini.h"
#include "cppdir.h"
#include "debug.h"

#undef OUT

#define OUT( level ) DEBUG_OUT( level, MODULE_SETUP )

#ifdef CAN_USE_INI

bool read_ini_value( Leo::Ini &ini, const std::string &section, const std::string &key, std::string &value )
{
  if( !ini )
    return false;

  Leo::Ini::Element el( Leo::Ini::Element::TYPE::KEY, section, key );

  if( ini.read( el ) )
    {
      value = strip( el.value );
      return true;
    }

  return false;
}

bool read_ini_value( Leo::Ini &ini, const std::string &section, const std::string &key, vec_string &values )
{

  if( !ini )
    return false;

  Leo::Ini::Element el( Leo::Ini::Element::TYPE::SECTION, section );

  std::string k = toupper( key );

  if( ini.read( el ) )
    {
      bool found = false;

      for( Leo::Ini::Element::element_list_it it = el.elements.begin();
	   it != el.elements.end(); ++it )
	if( toupper( it->key ) == k )
	  {
	    found = true;
	    values.push_back( strip( it->value ) );
	  }

      return found;
    }

  return false;
}

#endif

bool DirChecker::check( const std::string &s )
{
  CppDir::File file( s );

  if( !file )
    {
      ERROR( "Warning: %s is not a valid file\n", s );
      return false;
    }

  if( file.get_type() != CppDir::EFILE::DIR )
    {
      ERROR( "Warning: %s is not a valid directory\n", s );
      return false;
    }

  if( must_be_link )
    if( !file.is_link() )
      {
	ERROR( "Warning: %s is not a valid link to a valid directory\n", s );
	return false;
      }
  
  return true;
}

std::string DirChecker::strip( const std::string &s )
{
  if( s.size() > 1 )
    if( s[ s.size() - 1 ] == '/' )
      return s.substr( 0, s.size() - 1 );

  return s;
}

std::string VecDirChecker::strip( const std::string &s )
{
  return checker.strip( s );
}


bool VecDirChecker::check( const vec_string &v )
{
  for( unsigned int i = 0; i < v.size(); ++i )
    {
      if( !checker.check( v[i] ) )
	return false;
    }

  return true;
}

bool VecDirChecker::check( const std::string &s )
{
  return checker.check( s );    
}

bool BoolValue::string2data( const std::string &s )
{
  return s2x<bool>( s );
}

void VecStringValue::operator+=( const std::string &s )
{
  if( match_only_once )
    if( already_have( s ) )
      return;

  if( checker )
    {
      if( checker->check( s ) )
	data.push_back( checker->strip( s ) );
    }
  else
    data.push_back( s );
}

void VecStringValue::operator+=( const vec_string &v )
{
  for( unsigned int i = 0; i < v.size(); ++i )
    operator+=( v[i] );
}


vec_string VecStringValue::string2data( const std::string &s )
{
  return split_simple( s );
}

#ifdef CAN_USE_INI

void VecStringValue::read_ini( Leo::Ini &ini )
{
  if( !ini_section.empty() && !ini_key.empty() )
    {
      vec_string value;
      
      if( read_ini_value( ini, ini_section, ini_key, value ) )
	operator+=( value );
    }
}

#endif

bool VecStringValue::already_have( const std::string &s )
{
  for( unsigned int i = 0; i < data.size(); ++i )
    if( data[i] == s )
      return true;

  return false;
}


void RegExVecStringValue::operator+=( const std::string &s )
{
#ifdef HAVE_REGEX_H

  if( match_only_once )
    if( already_have( s ) )
      return;

  regex_t re;

  if( regcomp( &re, s.c_str(), REG_EXTENDED ) == 0 )
    {
      data.push_back( s );
      regs.push_back( re );
    }
  else
    ERROR( "Warning: regular expression %s is not valid\n", s );

#else

  ERROR << "Warning: XStow was not compiled with regular expression support\n";

#endif
}


#ifdef CAN_USE_INI

void Value<std::string>::read_ini( Leo::Ini &ini )
{
  if( !ini_section.empty() && !ini_key.empty() )
    {
      std::string value;

      read_ini_value( ini, ini_section, ini_key, value );	

      if( checker )
	if( checker->check( value ) )
	  data = checker->strip( value );
    }
}

#endif

void Value<std::string>::operator()( const std::string &data_ )
{
  if( checker )
    {
      if( checker->check( data_ ) )
	data = checker->strip( data_ );
    }
  else
    data = data_;
}

std::ostream& operator<<( std::ostream& out, const vec_string &v )
{
  for( unsigned int i = 0; i < v.size(); ++i )
    out << ' ' << v[i] << '\n';
  
  return out;
}

#ifdef CAN_USE_INI

void Section::read_ini( Leo::Ini &ini )
{
  for( unsigned int i = 0; i < values.size(); ++i )
    values[i]->read_ini( ini );
}

void Section::add( IniValue *value )
{
  if( value )
    if( value->ini_section.empty() )
      if( !ini_section.empty() )
	value->ini_section = ini_section;
  
  values.push_back( value );
}

bool Section::check_ini( Leo::Ini &ini, const std::string &key )
{
    for( unsigned int i = 0; i < values.size(); ++i )
	if( values[i]->ini_key == key )
	    return true;

    return false;
}

#endif