~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to property.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: property.c 840 2007-09-09 12:17:42Z michael $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/property.c $
 
3
 *
 
4
 * dynamic properties
 
5
 *
 
6
 * Copyright (C) 2006 Michael Reinelt <michael@reinelt.co.at>
 
7
 * Copyright (C) 2006 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
8
 *
 
9
 * This file is part of LCD4Linux.
 
10
 *
 
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)
 
14
 * any later version.
 
15
 *
 
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.
 
20
 *
 
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.
 
24
 *
 
25
 */
 
26
 
 
27
 
 
28
/* 
 
29
 * exported functions:
 
30
 *
 
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
 
33
 *
 
34
 * void property_free (PROPERTY *prop)
 
35
 *   frees all property allocations
 
36
 *
 
37
 * int property_eval(PROPERTY * prop)
 
38
 *   evaluates a property; returns 1 if value has changed
 
39
 *
 
40
 * double P2N(PROPERTY * prop)
 
41
 *   returns a (already evaluated) property as number
 
42
 *
 
43
 * char *P2S(PROPERTY * prop)
 
44
 *   returns a (already evaluated) property as string
 
45
 *
 
46
 */
 
47
 
 
48
 
 
49
#include "config.h"
 
50
 
 
51
#include <stdlib.h>
 
52
#include <stdio.h>
 
53
#include <string.h>
 
54
 
 
55
#include "debug.h"
 
56
#include "cfg.h"
 
57
#include "evaluator.h"
 
58
#include "property.h"
 
59
 
 
60
#ifdef WITH_DMALLOC
 
61
#include <dmalloc.h>
 
62
#endif
 
63
 
 
64
 
 
65
void property_load(const char *section, const char *name, const char *defval, PROPERTY * prop)
 
66
{
 
67
    char *expression;
 
68
 
 
69
    /* initialize structure */
 
70
    prop->valid = 0;
 
71
    prop->name = NULL;
 
72
    prop->expression = NULL;
 
73
    prop->compiled = NULL;
 
74
    DelResult(&prop->result);
 
75
 
 
76
    /* remember the name */
 
77
    prop->name = strdup(name);
 
78
 
 
79
    /* load expression from config, but do not evaluate it */
 
80
    expression = cfg_get_raw(section, name, NULL);
 
81
 
 
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;
 
86
    } else {
 
87
        prop->valid = 1;
 
88
        prop->expression = expression;
 
89
    }
 
90
 
 
91
    /* pre-compile the expression */
 
92
    Compile(prop->expression, &prop->compiled);
 
93
 
 
94
}
 
95
 
 
96
 
 
97
int property_valid(PROPERTY * prop)
 
98
{
 
99
    return prop->valid;
 
100
}
 
101
 
 
102
 
 
103
int property_eval(PROPERTY * prop)
 
104
{
 
105
    RESULT old;
 
106
    int update;
 
107
 
 
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;
 
113
 
 
114
    DelResult(&prop->result);
 
115
    Eval(prop->compiled, &prop->result);
 
116
 
 
117
    /* check if property value has changed */
 
118
    update = 1;
 
119
    if (prop->result.type & R_NUMBER && old.type & R_NUMBER && prop->result.number == old.number) {
 
120
        update = 0;
 
121
    }
 
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) {
 
124
            update = 0;
 
125
        } else if (prop->result.string != NULL && old.string != NULL && strcmp(prop->result.string, old.string) == 0) {
 
126
            update = 0;
 
127
        }
 
128
    }
 
129
 
 
130
    if (old.string)
 
131
        free(old.string);
 
132
 
 
133
    return update;
 
134
}
 
135
 
 
136
 
 
137
double P2N(PROPERTY * prop)
 
138
{
 
139
    if (prop == NULL) {
 
140
        error("Property: internal error: NULL property");
 
141
        return 0.0;
 
142
    }
 
143
    return R2N(&prop->result);
 
144
}
 
145
 
 
146
 
 
147
char *P2S(PROPERTY * prop)
 
148
{
 
149
    if (prop == NULL) {
 
150
        error("Property: internal error: NULL property");
 
151
        return NULL;
 
152
    }
 
153
    return R2S(&prop->result);
 
154
}
 
155
 
 
156
void property_free(PROPERTY * prop)
 
157
{
 
158
    if (prop->name != NULL) {
 
159
        free(prop->name);
 
160
        prop->name = NULL;
 
161
    }
 
162
 
 
163
    if (prop->expression != NULL) {
 
164
        /* do *not* free expression */
 
165
        prop->expression = NULL;
 
166
    }
 
167
 
 
168
    if (prop->compiled != NULL) {
 
169
        free(prop->compiled);
 
170
        prop->compiled = NULL;
 
171
    }
 
172
 
 
173
    DelResult(&prop->result);
 
174
}