1
/* $Id: property.c 840 2007-09-09 12:17:42Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/property.c $
6
* Copyright (C) 2006 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2006 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* This file is part of LCD4Linux.
11
* LCD4Linux is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2, or (at your option)
16
* LCD4Linux is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31
* void property_load (const char *section, const char *name, const char *defval, PROPERTY *prop)
32
* initializes and loads a property from the config file and pre-compiles it
34
* void property_free (PROPERTY *prop)
35
* frees all property allocations
37
* int property_eval(PROPERTY * prop)
38
* evaluates a property; returns 1 if value has changed
40
* double P2N(PROPERTY * prop)
41
* returns a (already evaluated) property as number
43
* char *P2S(PROPERTY * prop)
44
* returns a (already evaluated) property as string
57
#include "evaluator.h"
65
void property_load(const char *section, const char *name, const char *defval, PROPERTY * prop)
69
/* initialize structure */
72
prop->expression = NULL;
73
prop->compiled = NULL;
74
DelResult(&prop->result);
76
/* remember the name */
77
prop->name = strdup(name);
79
/* load expression from config, but do not evaluate it */
80
expression = cfg_get_raw(section, name, NULL);
82
if (expression == NULL) {
83
if (defval != NULL && *defval != '\0')
84
debug("Notice: using default value <%s> for property '%s.%s'", defval, section, name);
85
prop->expression = (char *) defval;
88
prop->expression = expression;
91
/* pre-compile the expression */
92
Compile(prop->expression, &prop->compiled);
97
int property_valid(PROPERTY * prop)
103
int property_eval(PROPERTY * prop)
108
/* this is a bit ugly: we need to remember the old value */
109
old.type = prop->result.type;
110
old.size = prop->result.size;
111
old.number = prop->result.number;
112
old.string = prop->result.string != NULL ? strdup(prop->result.string) : NULL;
114
DelResult(&prop->result);
115
Eval(prop->compiled, &prop->result);
117
/* check if property value has changed */
119
if (prop->result.type & R_NUMBER && old.type & R_NUMBER && prop->result.number == old.number) {
122
if (prop->result.type & R_STRING && old.type & R_STRING && prop->result.size == old.size) {
123
if (prop->result.string == NULL && old.string == NULL) {
125
} else if (prop->result.string != NULL && old.string != NULL && strcmp(prop->result.string, old.string) == 0) {
137
double P2N(PROPERTY * prop)
140
error("Property: internal error: NULL property");
143
return R2N(&prop->result);
147
char *P2S(PROPERTY * prop)
150
error("Property: internal error: NULL property");
153
return R2S(&prop->result);
156
void property_free(PROPERTY * prop)
158
if (prop->name != NULL) {
163
if (prop->expression != NULL) {
164
/* do *not* free expression */
165
prop->expression = NULL;
168
if (prop->compiled != NULL) {
169
free(prop->compiled);
170
prop->compiled = NULL;
173
DelResult(&prop->result);