~ubuntu-branches/ubuntu/precise/gtimer/precise-proposed

« back to all changes in this revision

Viewing changes to config.c

  • Committer: Bazaar Package Importer
  • Author(s): Taylor LeMasurier-Wren
  • Date: 2011-08-06 03:07:30 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110806030730-9wtxfpgsf1h4ulac
Tags: 2.0.0-1
* Update watch file
* Update debhelper compatibilty to V7 (significantly clean up packaging)
* New Upstream Release (Closes: #575666: RFP: GTimer -- GTK-based tool for
  timing tasks)
* Initial release. (Closes: #636822: ITP: gtimer -- GTK-based X11 task
  timer)
* Switch to dpkg-source 3.0 (quilt) format

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 1999 Craig Knudsen
 
3
 * 
 
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.
 
8
 * 
 
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.
 
13
 * 
 
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
 
17
 *
 
18
 * Description:
 
19
 *      Helps you keep track of time spent on different tasks.
 
20
 *
 
21
 * Author:
 
22
 *      Craig Knudsen, cknudsen@cknudsen.com, http://www.cknudsen.com/
 
23
 *
 
24
 * Home Page:
 
25
 *      http://www.cknudsen.com/gtimer/
 
26
 *
 
27
 * History:
 
28
 *      04-Apr-98       Created
 
29
 *
 
30
 ****************************************************************************/
 
31
 
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <sys/types.h>
 
35
#include <sys/stat.h>
 
36
#if HAVE_UNISTD_H
 
37
#include <unistd.h>
 
38
#endif
 
39
#include <fcntl.h>
 
40
#if HAVE_STRING_H
 
41
#include <string.h>
 
42
#endif
 
43
 
 
44
#ifdef GTIMER_MEMDEBUG
 
45
#include "memdebug/memdebug.h"
 
46
#endif
 
47
 
 
48
#define CONFIG_DEFAULTS
 
49
#include "config.h"
 
50
 
 
51
#define MAX_ATTR        256
 
52
#define MAX_LEN         1024
 
53
 
 
54
/*
 
55
** Local variables.
 
56
*/
 
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 */
 
61
 
 
62
static char *my_strtok (
 
63
#ifndef _NO_PROTO
 
64
  char *ptr1, char *tok
 
65
#endif
 
66
);
 
67
 
 
68
 
 
69
 
 
70
 
 
71
/*
 
72
** Read all the attributes in the specified file.
 
73
*/
 
74
int configReadAttributes ( path )
 
75
char *path;
 
76
{
 
77
  int loop, old;
 
78
  char *text = NULL, *ptr;
 
79
  struct stat buf;
 
80
  int fd;
 
81
 
 
82
  modified = 0;
 
83
 
 
84
  for ( loop = 0; loop < num_attr; loop++ ) {
 
85
    free ( attrnames[loop] );
 
86
    free ( attrvalues[loop] );
 
87
  }
 
88
  num_attr = 0;
 
89
 
 
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] );
 
97
    num_attr++;
 
98
  }
 
99
 
 
100
  if ( stat ( path, &buf ) != 0 )
 
101
    return ( -1 );
 
102
  fd = open ( path, O_RDONLY );
 
103
  if ( fd >= 0 ) {
 
104
    text = (char *) malloc ( buf.st_size + 1 );
 
105
    read ( fd, text, buf.st_size );
 
106
    text[buf.st_size] = '\0';
 
107
    close ( fd );
 
108
    ptr = my_strtok ( text, "\n" );
 
109
    while ( ptr ) {
 
110
      for ( old = -1, loop = 0; loop < num_attr; loop++ ) {
 
111
        if ( strcmp ( attrnames[loop], ptr ) == 0 ) {
 
112
          old = loop;
 
113
          break;
 
114
        }
 
115
      }
 
116
      if ( old >= 0 ) {
 
117
        ptr = my_strtok ( NULL, "\n" );
 
118
        if ( ! ptr )
 
119
          break;
 
120
        free ( attrvalues[old] );
 
121
        attrvalues[old] = (char *) malloc ( strlen ( ptr ) + 1 );
 
122
        strcpy ( attrvalues[old], ptr );
 
123
      }
 
124
      else {
 
125
        attrnames[num_attr] = (char *) malloc ( strlen ( ptr ) + 1 );
 
126
        strcpy ( attrnames[num_attr], ptr );
 
127
        ptr = my_strtok ( NULL, "\n" );
 
128
        if ( ! ptr )
 
129
          break;
 
130
        attrvalues[num_attr] = (char *) malloc ( strlen ( ptr ) + 1 );
 
131
        strcpy ( attrvalues[num_attr], ptr );
 
132
        num_attr++;
 
133
      }
 
134
      ptr = my_strtok ( NULL, "\n" );
 
135
    }
 
136
  }
 
137
  else {
 
138
    return ( -1 );
 
139
  }
 
140
 
 
141
  if ( text )
 
142
    free ( text );
 
143
  return ( 0 );
 
144
  
 
145
}
 
146
 
 
147
 
 
148
 
 
149
 
 
150
/*
 
151
** Get the value for a specified attribute.
 
152
*/
 
153
int configGetAttribute ( attribute, value )
 
154
char *attribute;
 
155
char **value;
 
156
{
 
157
  int loop;
 
158
 
 
159
  for ( loop = 0; loop < num_attr; loop++ ) {
 
160
    if ( strcmp ( attrnames[loop], attribute ) == 0 ) {
 
161
      *value = attrvalues[loop];
 
162
      return ( 0 );
 
163
    }
 
164
  }
 
165
 
 
166
  return ( -1 );
 
167
 
 
168
}
 
169
 
 
170
 
 
171
/*
 
172
** Get a value in int form.
 
173
*/
 
174
int configGetAttributeInt ( attribute, value )
 
175
char *attribute;
 
176
int *value;
 
177
{
 
178
  char *ptr;
 
179
  int ret;
 
180
 
 
181
  ret = configGetAttribute ( attribute, &ptr );
 
182
  if ( ret == 0 ) {
 
183
    *value = atoi ( ptr );
 
184
    return 0;
 
185
  } else
 
186
    return -1;
 
187
}
 
188
 
 
189
 
 
190
/*
 
191
** Set the value for a specified attribute.
 
192
*/
 
193
int configSetAttribute ( attribute, value )
 
194
char *attribute;
 
195
char *value;
 
196
{
 
197
  int loop;
 
198
 
 
199
  modified = 1;
 
200
 
 
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 );
 
206
      return ( 0 );
 
207
    }
 
208
  }
 
209
 
 
210
  /*
 
211
  ** Attribute does not exits.  Add it.
 
212
  */
 
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 );
 
217
  num_attr++;
 
218
 
 
219
  return ( 0 );
 
220
 
 
221
}
 
222
 
 
223
 
 
224
/*
 
225
** Set the value for a specified attribute.
 
226
*/
 
227
int configSetAttributeInt ( attribute, value )
 
228
char *attribute;
 
229
int value;
 
230
{
 
231
  char temp[50];
 
232
  sprintf ( temp, "%d", value );
 
233
  return ( configSetAttribute ( attribute, temp ) );
 
234
}
 
235
 
 
236
 
 
237
 
 
238
 
 
239
/*
 
240
** Save the attributes in memory to the specified file.
 
241
*/
 
242
int configSaveAttributes ( attrfile )
 
243
char *attrfile;
 
244
{
 
245
  FILE *fp;
 
246
  int loop;
 
247
 
 
248
  fp = fopen ( attrfile, "w" );
 
249
  if ( ! fp ) {
 
250
    return ( -1 );
 
251
  }
 
252
 
 
253
  for ( loop = 0; loop < num_attr; loop++ ) {
 
254
    fprintf ( fp, "%s\n%s\n", attrnames[loop], attrvalues[loop] );
 
255
  }
 
256
  fclose ( fp );
 
257
 
 
258
  modified = 0;
 
259
 
 
260
  return ( 0 );
 
261
}
 
262
 
 
263
 
 
264
 
 
265
/*
 
266
** Tell the caller if changes have been made since last read/save.
 
267
*/
 
268
int configModified ()
 
269
{
 
270
  return ( modified );
 
271
}
 
272
 
 
273
 
 
274
 
 
275
/*
 
276
** Clear out all values.
 
277
*/
 
278
void configClear ()
 
279
{
 
280
  int loop;
 
281
 
 
282
  for ( loop = 0; loop < num_attr; loop++ ) {
 
283
    free ( attrnames[loop] );
 
284
    free ( attrvalues[loop] );
 
285
  }
 
286
  num_attr = 0;
 
287
  modified = 1;
 
288
}
 
289
 
 
290
 
 
291
static char *my_strtok ( ptr1, tok )
 
292
char *ptr1;
 
293
char *tok;
 
294
{
 
295
  static char *last;
 
296
  char *p;
 
297
 
 
298
  if ( ! ptr1 )
 
299
    ptr1 = last;
 
300
  else
 
301
    last = ptr1;
 
302
 
 
303
  if ( ! ptr1 )
 
304
    return ( NULL );
 
305
 
 
306
  for ( p = ptr1; *p != '\0'; p++ ) {
 
307
    if ( strncmp ( p, tok, strlen ( tok ) ) == 0 ) {
 
308
      *p = '\0';
 
309
      last = p + strlen ( tok );
 
310
      return ( ptr1 );
 
311
    }
 
312
  }
 
313
 
 
314
  last = NULL;
 
315
  return ( ptr1 );
 
316
}
 
317