summaryrefslogtreecommitdiffstats
path: root/var.c
blob: 9cd070a36ab212a64685b80bfd2bb66791e65e8a (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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* var.c - a typeless variable class
 *
 * This class is used to represent variable values, which may have any type.
 * Among others, it will be used inside rsyslog's expression system, but
 * also internally at any place where a typeless variable is needed.
 *
 * Module begun 2008-02-20 by Rainer Gerhards, with some code taken
 * from the obj.c/.h files.
 *
 * Copyright 2007, 2008 Rainer Gerhards and Adiscon GmbH.
 *
 * This file is part of rsyslog.
 *
 * Rsyslog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rsyslog 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with Rsyslog.  If not, see <http://www.gnu.org/licenses/>.
 *
 * A copy of the GPL can be found in the file "COPYING" in this distribution.
 */

#include "config.h"
#include <stdlib.h>
#include <assert.h>

#include "rsyslog.h"
#include "obj.h"
#include "srUtils.h"
#include "var.h"

/* static data */
DEFobjStaticHelpers


/* Standard-Constructor
 */
BEGINobjConstruct(var) /* be sure to specify the object type also in END macro! */
ENDobjConstruct(var)


/* ConstructionFinalizer
 * rgerhards, 2008-01-09
 */
rsRetVal varConstructFinalize(var_t __attribute__((unused)) *pThis)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, var);

	RETiRet;
}


/* destructor for the var object */
BEGINobjDestruct(var) /* be sure to specify the object type also in END and CODESTART macros! */
CODESTARTobjDestruct(var)
	if(pThis->pcsName != NULL)
		d_free(pThis->pcsName);
	if(pThis->varType == VARTYPE_STR) {
		if(pThis->val.pStr != NULL)
			d_free(pThis->val.pStr);
	}

ENDobjDestruct(var)


/* DebugPrint support for the var object */
BEGINobjDebugPrint(var) /* be sure to specify the object type also in END and CODESTART macros! */
CODESTARTobjDebugPrint(var)
	switch(pThis->varType) {
		case VARTYPE_STR:
			dbgoprint((obj_t*) pThis, "type: cstr, val '%s'\n", rsCStrGetSzStr(pThis->val.pStr));
			break;
		case VARTYPE_NUMBER:
			dbgoprint((obj_t*) pThis, "type: number, val %lld\n", pThis->val.num);
			break;
		default:
			dbgoprint((obj_t*) pThis, "type %d currently not suppored in debug output\n", pThis->varType);
			break;
	}
ENDobjDebugPrint(var)


/* duplicates a var instance
 * rgerhards, 2008-02-25
 */
static rsRetVal
Duplicate(var_t *pThis, var_t **ppNew)
{
	DEFiRet;
	var_t *pNew = NULL;
	cstr_t *pstr;

	ISOBJ_TYPE_assert(pThis, var);
	assert(ppNew != NULL);

	CHKiRet(varConstruct(&pNew));
	CHKiRet(varConstructFinalize(pNew));

	/* we have the object, now copy value */
	pNew->varType = pThis->varType;
	if(pThis->varType == VARTYPE_NUMBER) {
		pNew->val.num = pThis->val.num;
	} else if(pThis->varType == VARTYPE_STR) {
		CHKiRet(rsCStrConstructFromCStr(&pstr, pThis->val.pStr));
		pNew->val.pStr = pstr;
	}

	*ppNew = pNew;

finalize_it:
	if(iRet != RS_RET_OK && pNew != NULL)
		varDestruct(&pNew);

	RETiRet;
}


/* free the current values (destructs objects if necessary)
 */
static rsRetVal
varUnsetValues(var_t *pThis)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, var);
	if(pThis->varType == VARTYPE_STR)
		rsCStrDestruct(&pThis->val.pStr);

	pThis->varType = VARTYPE_NONE;

	RETiRet;
}


/* set a string value 
 * The caller hands over the string and must n longer use it after this method
 * has been called.
 */
static rsRetVal
varSetString(var_t *pThis, cstr_t *pStr)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, var);

	CHKiRet(varUnsetValues(pThis));
	pThis->varType = VARTYPE_STR;
	pThis->val.pStr = pStr;

finalize_it:
	RETiRet;
}


/* set an int64 value */
static rsRetVal
varSetNumber(var_t *pThis, number_t iVal)
{
	DEFiRet;

	ISOBJ_TYPE_assert(pThis, var);

	CHKiRet(varUnsetValues(pThis));
	pThis->varType = VARTYPE_NUMBER;
	pThis->val.num = iVal;

finalize_it:
	RETiRet;
}


/* Change the provided object to be of type number.
 * rgerhards, 2008-02-22
 */
rsRetVal
ConvToNumber(var_t *pThis)
{
	DEFiRet;
	number_t n;

	if(pThis->varType == VARTYPE_NUMBER) {
		FINALIZE;
	} else if(pThis->varType == VARTYPE_STR) {
		iRet = rsCStrConvertToNumber(pThis->val.pStr, &n);
		if(iRet == RS_RET_NOT_A_NUMBER) {
			n = 0; /* TODO: isn't it better to pass the error? */
		} else if (iRet != RS_RET_OK) {
			FINALIZE;
		}
	
		pThis->val.num = n;
		pThis->varType = VARTYPE_NUMBER;
	}

finalize_it:
	RETiRet;
}


/* convert the provided var to type string. This is always possible
 * (except, of course, for things like out of memory...)
 * TODO: finish implementation!!!!!!!!!
 * rgerhards, 2008-02-24
 */
rsRetVal
ConvToString(var_t *pThis)
{
	DEFiRet;
	uchar szNumBuf[64];

	if(pThis->varType == VARTYPE_STR) {
		FINALIZE;
	} else if(pThis->varType == VARTYPE_NUMBER) {
		CHKiRet(srUtilItoA((char*)szNumBuf, sizeof(szNumBuf)/sizeof(uchar), pThis->val.num));
		CHKiRet(rsCStrConstructFromszStr(&pThis->val.pStr, szNumBuf));
		pThis->varType = VARTYPE_STR;
	}

finalize_it:
	RETiRet;
}


/* convert (if necessary) the value to a boolean. In essence, this means the
 * value must be a number, but in case of a string special logic is used as
 * some string-values may represent a boolean (e.g. "true").
 * rgerhards, 2008-02-25
 */
rsRetVal
ConvToBool(var_t *pThis)
{
	DEFiRet;
	number_t n;

	if(pThis->varType == VARTYPE_NUMBER) {
		FINALIZE;
	} else if(pThis->varType == VARTYPE_STR) {
		CHKiRet(rsCStrConvertToBool(pThis->val.pStr, &n));
		pThis->val.num = n;
		pThis->varType = VARTYPE_NUMBER;
	}

finalize_it:
	RETiRet;
}


/* This function is used to prepare two var_t objects for a common operation,
 * e.g before they are added, compared. The function looks at
 * the data types of both operands and finds the best data type suitable for
 * the operation (in respect to current types). Then, it converts those
 * operands that need conversion. Please note that the passed-in var objects
 * *are* modified and returned as new type. So do call this function only if
 * you actually need the conversion.
 *
 * This is how the common data type is selected. Note that op1 and op2 are
 * just the two operands, their order is irrelevant (this would just take up
 * more table space - so string/number is the same thing as number/string).
 *
 * Common Types:
 * op1		op2	operation data type
 * string	string	string
 * string	number	number if op1 can be converted to number, string else
 * date		string  date if op1 can be converted to date, string else
 * number	number  number
 * date		number	string (maybe we can do better?)
 * date		date	date
 * none         n/a     error
 *
 * If a boolean value is required, we need to have a number inside the
 * operand. If it is not, conversion rules to number apply. Once we
 * have a number, things get easy: 0 is false, anything else is true.
 * Please note that due to this conversion rules, "0" becomes false
 * while "-4712" becomes true. Using a date as boolen is not a good
 * idea. Depending on the ultimate conversion rules, it may always
 * become true or false. As such, using dates as booleans is
 * prohibited and the result defined to be undefined.
 *
 * rgerhards, 2008-02-22
 */
static rsRetVal
ConvForOperation(var_t *pThis, var_t *pOther)
{
	DEFiRet;

	if(pThis->varType == VARTYPE_NONE || pOther->varType == VARTYPE_NONE)
		ABORT_FINALIZE(RS_RET_INVALID_VAR);

	switch(pThis->varType) {
		case VARTYPE_NONE:
			ABORT_FINALIZE(RS_RET_INVALID_VAR);
			break;
		case VARTYPE_STR:
			switch(pOther->varType) {
				case VARTYPE_NONE:
					ABORT_FINALIZE(RS_RET_INVALID_VAR);
					break;
				case VARTYPE_STR:
					FINALIZE; /* two strings, we are all set */
					break;
				case VARTYPE_NUMBER:
					/* check if we can convert pThis to a number, if so use number format. */
					iRet = ConvToNumber(pThis);
					if(iRet != RS_RET_NOT_A_NUMBER) {
						CHKiRet(ConvToString(pOther));
					} else {
						FINALIZE; /* OK or error */
					}
					break;
				case VARTYPE_SYSLOGTIME:
					ABORT_FINALIZE(RS_RET_NOT_IMPLEMENTED);
					break;
			}
			break;
		case VARTYPE_NUMBER:
			switch(pOther->varType) {
				case VARTYPE_NONE:
					ABORT_FINALIZE(RS_RET_INVALID_VAR);
					break;
				case VARTYPE_STR:
					iRet = ConvToNumber(pOther);
					if(iRet != RS_RET_NOT_A_NUMBER) {
						CHKiRet(ConvToString(pThis));
					} else {
						FINALIZE; /* OK or error */
					}
					break;
				case VARTYPE_NUMBER:
					FINALIZE; /* two numbers, so we are all set */
					break;
				case VARTYPE_SYSLOGTIME:
					ABORT_FINALIZE(RS_RET_NOT_IMPLEMENTED);
					break;
			}
			break;
		case VARTYPE_SYSLOGTIME:
			ABORT_FINALIZE(RS_RET_NOT_IMPLEMENTED);
			break;
	}

finalize_it:
	RETiRet;
}


/* queryInterface function
 * rgerhards, 2008-02-21
 */
BEGINobjQueryInterface(var)
CODESTARTobjQueryInterface(var)
	if(pIf->ifVersion != varCURR_IF_VERSION) { /* check for current version, increment on each change */
		ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
	}

	/* ok, we have the right interface, so let's fill it
	 * Please note that we may also do some backwards-compatibility
	 * work here (if we can support an older interface version - that,
	 * of course, also affects the "if" above).
	 */
	pIf->oID = OBJvar;

	pIf->Construct = varConstruct;
	pIf->ConstructFinalize = varConstructFinalize;
	pIf->Destruct = varDestruct;
	pIf->DebugPrint = varDebugPrint;
	pIf->SetNumber = varSetNumber;
	pIf->SetString = varSetString;
	pIf->ConvForOperation = ConvForOperation;
	pIf->ConvToNumber = ConvToNumber;
	pIf->ConvToBool = ConvToBool;
	pIf->ConvToString = ConvToString;
	pIf->Duplicate = Duplicate;
finalize_it:
ENDobjQueryInterface(var)


/* Initialize the var class. Must be called as the very first method
 * before anything else is called inside this class.
 * rgerhards, 2008-02-19
 */
BEGINObjClassInit(var, 1) /* class, version */
	OBJSetMethodHandler(objMethod_DEBUGPRINT, varDebugPrint);
	OBJSetMethodHandler(objMethod_CONSTRUCTION_FINALIZER, varConstructFinalize);
ENDObjClassInit(var)

/* vi:set ai:
 */