summaryrefslogtreecommitdiff
path: root/mg_filters.c
blob: e0457daf6f3ce3986297d40940759d8815ee0d25 (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
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
/*******************************************************************/
/*! \file   mg_filters.c
 *  \brief  
 ******************************************************************** 
 * \version $Revision: 1.3 $
 * \date    $Date$
 * \author  Ralf Klueber, Lars von Wedel, Andreas Kellner
 * \author  file owner: $Author$
 */
/*******************************************************************/

/* makes sure we dont parse the same declarations twice */
#include <stdio.h>
#include "mg_filters.h"
#include "mg_tools.h"


//-------------------------------------------------------------------
//                         mgFilter
//-------------------------------------------------------------------
mgFilter::mgFilter(const char* name)
{
  m_name = strdup(name);
}
mgFilter::~mgFilter()
{
  free(m_name);
}

const char* mgFilter::getName()
{
  return m_name;
}

mgFilter::filterType mgFilter::getType()
{
  return m_type;
}

//-------------------------------------------------------------------
//        mgFilterInt
//-------------------------------------------------------------------
mgFilterInt::mgFilterInt(const char *name, int value, int min, int max)
  : mgFilter(name)
{
  m_type = INT;
  m_intval = value;
  m_default_val = value;
  m_stored_val = value;
  m_max = max;
  m_min = min;
}
mgFilterInt::~mgFilterInt()
{
}

std::string mgFilterInt::getStrVal()
{
  char buffer[20];
  sprintf(buffer, "%d", m_intval);

  return (std::string)buffer;
}

int mgFilterInt::getIntVal()
{
  return (int) m_intval;
}

int mgFilterInt::getVal()
{
  return m_intval;
}

int mgFilterInt::getMin()
{
  return m_min;
}

int mgFilterInt::getMax()
{
  return m_max;
}

void mgFilterInt::store()
{
  m_stored_val = m_intval;
}
void mgFilterInt::restore()
{
  m_intval = m_stored_val;
}
void mgFilterInt::clear()
{
  m_stored_val = m_default_val;
  m_intval     =  m_default_val;
}

bool mgFilterInt::isSet()
{
  if(m_stored_val == m_default_val)
  {
    return false;
  }
  return true;
}

//-------------------------------------------------------------------
//       mgFilterString
//-------------------------------------------------------------------
mgFilterString::mgFilterString(const char *name, const char* value,
			       int maxlen, std::string allowedchar)
  : mgFilter(name)
{
  m_type = STRING;
  m_strval = strdup(value);
  m_default_val = strdup(value);
  m_stored_val  = strdup(value);
  m_allowedchar = allowedchar;
  m_maxlen = maxlen;
}
mgFilterString::~mgFilterString()
{
  if(m_strval)
    {
      free(m_strval);
    }
}
  
int mgFilterString::getMaxLength()
{
  return m_maxlen;
} 

std::string mgFilterString::getAllowedChars()
{
  return m_allowedchar;
}
std::string mgFilterString::getStrVal()
{
 
  return (std::string) m_strval;
}
void mgFilterString::store()
{
  if(m_stored_val) free(m_stored_val);
  m_stored_val = strdup(m_strval);
}
void mgFilterString::restore()
{
  if(m_strval) free(m_strval);
  m_strval = strdup(m_stored_val);
}
void mgFilterString::clear()
{
  if(m_stored_val) free(m_stored_val);
  if(m_strval) free(m_strval);

  m_stored_val = strdup(m_default_val);
  m_strval = strdup(m_default_val);
}

bool mgFilterString::isSet()
{
  if(strlen(m_stored_val) == 0)
  {
    return false;
  }
  return true;
}
//-------------------------------------------------------------------
//        mgFilterBool
//-------------------------------------------------------------------
mgFilterBool::mgFilterBool(const char *name, bool value,
			   std::string truestr, std::string falsestr)
  : mgFilter(name)
{
  m_type = BOOL;
  m_bval  = (int) value;
  m_default_val = value;
  m_stored_val  = value;
  m_truestr = truestr;
  m_falsestr = falsestr;
  
}

mgFilterBool::~mgFilterBool()
{
}

std::string mgFilterBool::getStrVal()
{
  if(m_bval)
    return "true";
  else
    return "false";
}

int mgFilterBool::getIntVal()
{
  return (int) m_bval;
}

std::string mgFilterBool::getTrueString()
{
  return m_truestr;
}

std::string mgFilterBool::getFalseString()
{
  return m_falsestr;
}

bool mgFilterBool::getVal()
{
  return (bool) m_bval;
}

void mgFilterBool::store()
{
  m_stored_val = (bool) m_bval;
}

void mgFilterBool::restore()
{
  m_bval = (int) m_stored_val;
}

void mgFilterBool::clear()
{
  m_stored_val = (int) m_default_val;
  m_bval       = (int) m_default_val;
}

bool mgFilterBool::isSet()
{
  if(m_stored_val == m_default_val )
  {
    return false;
  }
  return true;
}
//-------------------------------------------------------------------
//        mgFilterChoice
//-------------------------------------------------------------------
mgFilterChoice::mgFilterChoice(const char *name, int value, std::vector<std::string> *choices)
  : mgFilter(name)
{
  m_type = CHOICE;
  m_choices = *choices;
  m_selval = value;
  m_default_val = value;
  if( m_selval < 0 || m_selval >= (int) m_choices.size() )
  {
    mgError("mgFilterChoice::mgFilterChoice(..): Illegal index %d", m_selval);
  }
}
mgFilterChoice::~mgFilterChoice()
{
  m_choices.clear();
}

std::string mgFilterChoice::getStrVal()
{
  if( m_selval < 0 || m_selval >= (int) m_choices.size() )
  {
    mgError("mgFilterChoice::getStrVal(): Illegal index %d", m_selval);
  }
  return m_choices[m_selval];
}
std::vector<std::string> &mgFilterChoice::getChoices()
{
  return m_choices;
}
void mgFilterChoice::store()
{
  m_stored_val = m_selval;
  
}
void mgFilterChoice::restore()
{
  m_selval =  m_stored_val;
}
void mgFilterChoice::clear()
{
  m_stored_val =  m_default_val;
  m_selval     =  m_default_val;
}

bool mgFilterChoice::isSet()
{
  if(m_stored_val == m_default_val)
    {
      return false;
    }
  return true;
}