2
* Copyright (C) 1999 Craig Knudsen
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19
* Helps you keep track of time spent on different tasks.
22
* Craig Knudsen, cknudsen@cknudsen.com, http://www.cknudsen.com/
25
* http://www.cknudsen.com/gtimer/
30
****************************************************************************/
34
#include <sys/types.h>
44
#ifdef GTIMER_MEMDEBUG
45
#include "memdebug/memdebug.h"
48
#define CONFIG_DEFAULTS
57
static char *attrnames[MAX_ATTR]; /* attribute names */
58
static char *attrvalues[MAX_ATTR]; /* attribute values */
59
static int num_attr = 0; /* no. of attributes in above arrays */
60
static int modified = 0; /* modified since read from file */
62
static char *my_strtok (
72
** Read all the attributes in the specified file.
74
int configReadAttributes ( path )
78
char *text = NULL, *ptr;
84
for ( loop = 0; loop < num_attr; loop++ ) {
85
free ( attrnames[loop] );
86
free ( attrvalues[loop] );
90
for ( loop = 0; default_config[loop]; loop += 2 ) {
91
attrnames[num_attr] = (char *) malloc
92
( strlen ( default_config[loop] ) + 1 );
93
strcpy ( attrnames[num_attr], default_config[loop] );
94
attrvalues[num_attr] = (char *) malloc
95
( strlen ( default_config[loop+1] ) + 1 );
96
strcpy ( attrvalues[num_attr], default_config[loop+1] );
100
if ( stat ( path, &buf ) != 0 )
102
fd = open ( path, O_RDONLY );
104
text = (char *) malloc ( buf.st_size + 1 );
105
read ( fd, text, buf.st_size );
106
text[buf.st_size] = '\0';
108
ptr = my_strtok ( text, "\n" );
110
for ( old = -1, loop = 0; loop < num_attr; loop++ ) {
111
if ( strcmp ( attrnames[loop], ptr ) == 0 ) {
117
ptr = my_strtok ( NULL, "\n" );
120
free ( attrvalues[old] );
121
attrvalues[old] = (char *) malloc ( strlen ( ptr ) + 1 );
122
strcpy ( attrvalues[old], ptr );
125
attrnames[num_attr] = (char *) malloc ( strlen ( ptr ) + 1 );
126
strcpy ( attrnames[num_attr], ptr );
127
ptr = my_strtok ( NULL, "\n" );
130
attrvalues[num_attr] = (char *) malloc ( strlen ( ptr ) + 1 );
131
strcpy ( attrvalues[num_attr], ptr );
134
ptr = my_strtok ( NULL, "\n" );
151
** Get the value for a specified attribute.
153
int configGetAttribute ( attribute, value )
159
for ( loop = 0; loop < num_attr; loop++ ) {
160
if ( strcmp ( attrnames[loop], attribute ) == 0 ) {
161
*value = attrvalues[loop];
172
** Get a value in int form.
174
int configGetAttributeInt ( attribute, value )
181
ret = configGetAttribute ( attribute, &ptr );
183
*value = atoi ( ptr );
191
** Set the value for a specified attribute.
193
int configSetAttribute ( attribute, value )
201
for ( loop = 0; loop < num_attr; loop++ ) {
202
if ( strcmp ( attrnames[loop], attribute ) == 0 ) {
203
free ( attrvalues[loop] );
204
attrvalues[loop] = (char *) malloc ( strlen ( value ) + 1 );
205
strcpy ( attrvalues[loop], value );
211
** Attribute does not exits. Add it.
213
attrnames[num_attr] = (char *) malloc ( strlen ( attribute ) + 1 );
214
strcpy ( attrnames[num_attr], attribute );
215
attrvalues[num_attr] = (char *) malloc ( strlen ( value ) + 1 );
216
strcpy ( attrvalues[num_attr], value );
225
** Set the value for a specified attribute.
227
int configSetAttributeInt ( attribute, value )
232
sprintf ( temp, "%d", value );
233
return ( configSetAttribute ( attribute, temp ) );
240
** Save the attributes in memory to the specified file.
242
int configSaveAttributes ( attrfile )
248
fp = fopen ( attrfile, "w" );
253
for ( loop = 0; loop < num_attr; loop++ ) {
254
fprintf ( fp, "%s\n%s\n", attrnames[loop], attrvalues[loop] );
266
** Tell the caller if changes have been made since last read/save.
268
int configModified ()
276
** Clear out all values.
282
for ( loop = 0; loop < num_attr; loop++ ) {
283
free ( attrnames[loop] );
284
free ( attrvalues[loop] );
291
static char *my_strtok ( ptr1, tok )
306
for ( p = ptr1; *p != '\0'; p++ ) {
307
if ( strncmp ( p, tok, strlen ( tok ) ) == 0 ) {
309
last = p + strlen ( tok );