File: layout.c

package info (click to toggle)
xpmumon 1.1.0
  • links: PTS
  • area: main
  • in suites: woody
  • size: 160 kB
  • ctags: 306
  • sloc: ansic: 1,787; makefile: 42
file content (215 lines) | stat: -rw-r--r-- 4,338 bytes parent folder | download
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
/*
 *  Layout resource parsing for xpmumon.
 *
 *  Copyright (c) 2002  Brendan O'Dea <bod@debian.org>
 *
 *  This 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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.
 *
 *  http://www.gnu.org/copyleft/gpl.txt
 */

#include <X11/Intrinsic.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "misc.h"
#include "carp.h"
#include "layout.h"

RCS_Id("$Id: layout.c,v 1.1 2002/01/20 07:14:25 bod Exp $")

static struct types {
    char const *name;
    enum type type;
    enum style default_style;
} types[] = {
    { "current", type_current, style_meter },
    { "voltage", type_voltage, style_meter },
    { "power",   type_power,   style_meter },
    { "charge",  type_charge,  style_hgauge },
    { "time",    type_time,    style_basic },
    { 0 },
};

static struct styles {
    char const *name;
    enum style style;
} styles[] = {
    { "meter",  style_meter },
    { "hgauge", style_hgauge },
    { "vgauge", style_vgauge },
    { "basic",  style_basic },
    { 0 },
};

static struct types *lookup_type(char const *str)
{
    struct types *t = types;

    while (t->name)
	if (!strcmp(t->name, str))
	    return t;
	else
	    t++;

    return 0;
}

static struct styles *lookup_style(char const *str)
{
    struct styles *s = styles;

    while (s->name)
	if (!strcmp(s->name, str))
	    return s;
	else
	    s++;

    return 0;
}

static char *strip(char *s)
{
    while (isspace(*s))
	s++;

    {
	char *p = s + strlen(s) - 1;
	while (p >= s && isspace(*p))
	    *p-- = 0;
    }

    return s;
}

struct doodads **parse_layout(char const *layout, int batteries)
{
    struct doodads **ret = 0;
    int row = 0;

    if (!*layout)
	croak(2, 0, "layout required");

    /* calculate the number of rows */
    {
	char const *p = layout;
	while (p)
	{
	    row++;
	    if ((p = strpbrk(p, ";\n")))
		p++;
	}
    }

    ret = (struct doodads **) XtMalloc((row + 1) * sizeof(struct doodads *));
    ret[row] = 0;

    {
	char *tmp = XtNewString(layout);
	char *next_row = 0;
	row = 0;

	do {
	    int col = 0;
	    char *next_col = 0;

	    next_row = strpbrk(tmp, ";\n");
	    if (next_row)
		*next_row++ = 0;

	    /* calculate the number of cols */
	    {
		char const *p = tmp;
		while (p)
		{
		    col++;
		    if ((p = strchr(p, ',')))
			p++;
		}
	    }

	    ret[row] = (struct doodads *)
		XtMalloc((col + 1) * sizeof(struct doodads));

	    ret[row][col].name = 0;
	    col = 0;

	    do {
		int battery = -1;
		int style = -1;
		struct types *t;
		char *p;
		int len;

		next_col = strchr(tmp, ',');
		if (next_col)
		    *next_col++ = 0;

		tmp = strip(tmp);
		len = strlen(tmp);
		if (tmp[len - 1] == ')' && (p = strchr(tmp, '(')))
		{
		    struct styles *sp = 0;

		    tmp[len - 1] = *p++ = 0;
		    p = strip(p);
		    if (!(sp = lookup_style(p)))
			croak(2, 0, "invalid style specification `%s' (%d,%d)",
			    p, col, row);

		    style = sp->style;
		    tmp = strip(tmp);
		}

		if ((p = strchr(tmp, '_')))
		{
		    char *e;

		    *p++ = 0;
		    battery = strtol(p, &e, 10);
		    if (*e || battery < 0 || battery >= batteries)
			croak(2, 0, "invalid battery specification `%s' "
			    "(%d,%d)", p, col, row);
		}

		if (!(t = lookup_type(tmp)))
		    croak(2, 0, "invalid type specification `%s' (%d,%d)",
			tmp, col, row);

		ret[row][col].name = t->name;
		ret[row][col].w = None;
		ret[row][col].style = style < 0 ? t->default_style : style;
		ret[row][col].type = t->type;
		ret[row][col].battery = battery;
		ret[row][col].value = 0;
		ret[row][col].string_value[0] = 0;

		if (battery != -1)
		{
		    char *name;
		    /* re-append the battery number */
		    sprintf(tmp + strlen(tmp), "_%d", battery);
		    name = XtMalloc(strlen(tmp) + 1);
		    ret[row][col].name = strcpy(name, tmp);
		}

		col++;
	    } while ((tmp = next_col));

	    row++;
	} while ((tmp = next_row));

	XtFree(tmp);
    }

    return ret;
}