aboutsummaryrefslogtreecommitdiff
path: root/frontend/pisa_preference.cc
blob: 89b9c1594a5dcb97b8baa3eab110cb8fcbe020fb (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* 
   SANE EPSON backend
   Copyright (C) 2001 SEIKO EPSON CORPORATION

   Date		Author		Reason
   06/13/2001	N.Sasaki	New

   This file is part of the `iscan' program.

   This program 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.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   As a special exception, the copyright holders give permission
   to link the code of this program with the esmod library and
   distribute linked combinations including the two.  You must obey
   the GNU General Public License in all respects for all of the
   code used other then esmod.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "pisa_preference.h"

static char * get_line ( FILE * fp, char ** gotstr );
static char * dynamic_fgets ( FILE * fp );
static int parse_line ( char * ptr, cfg_struct * cfg );
static char * parse_word ( char * ptr, char ** word, cfg_key_type key );
static int store_value ( cfg_struct * cfg, char * parameter, char * value );
static char * remove_first_spaces ( char * ptr );
static void set_word ( FILE * fp, cfg_struct * cfg );
static void single_or_double_quote ( char * str, char * ret );

/*----------------------------------------------------------*/
void get_cfg ( char * file, cfg_struct * cfg, int num )
{
  FILE	* fp;
  char * ptr;
  char * line_buf;
  int i;

  if ( 0 == ( fp = fopen ( file, "r" ) ) )
    return;

  for ( i = 0; i < num; i++, cfg++ )
    {
      rewind ( fp );

      while ( 0 != ( ptr = get_line ( fp, & line_buf ) ) )
	{
	  if ( parse_line ( ptr, cfg ) )
	    {
	      free ( line_buf );
	      break;
	    }

	  free ( line_buf );
	}
    }

  fclose ( fp );
}

/*----------------------------------------------------------*/
void set_cfg ( char * file, cfg_struct * cfg, int num )
{
  FILE	* fp;
  int	i;

  if ( 0 == ( fp = fopen ( file, "w" ) ) )
    return;

  for ( i = 0; i < num; i++, cfg++ )
    set_word ( fp, cfg );

  fclose ( fp );
}

/*----------------------------------------------------------*/
static char * get_line ( FILE * fp, char ** gotstr )
{
  char * ptr;

  while ( 1 )
    {
      if ( 0 == ( * gotstr = dynamic_fgets ( fp ) ) )
	return 0;

      ptr = remove_first_spaces ( * gotstr );
      if ( * ptr != '#' && * ptr != '\0' )
	return ptr;

      free ( * gotstr );
    }
}

/*----------------------------------------------------------*/
static char * dynamic_fgets ( FILE * fp )
{
  char	* ptr;
  char	tmp [ 128 ];
  int	i;

  if ( 0 == ( ptr = ( char * ) malloc ( 1 ) ) )
    return 0;

  * ptr = '\0';

  for ( i = 0;; i++ )
    {
      if ( 0 == fgets ( tmp, 128, fp ) )
	{
	  free ( ptr );
	  return 0;
	}

      if ( 0 == ( ptr = ( char * ) realloc ( ptr, 127 * ( i + 1 ) + 1 ) ) )
	return 0;
      
      strcat ( ptr, tmp );

      if ( 0 != strchr ( tmp, '\n' ) )
	{
	  * strchr ( ptr, '\n' ) = '\0';
	  return ptr;
	}

      if ( 0 != feof ( fp ) )
	return ptr;
    }
}

/*----------------------------------------------------------*/
static char * remove_first_spaces ( char * ptr )
{
  while ( * ptr == ' ' || * ptr == '\t' )
    ptr++;

  return ptr;
}

/*----------------------------------------------------------*/
static int parse_line ( char * ptr, cfg_struct * cfg )
{
  char * parameter;
  char * value;

  if ( 0 == ( ptr = parse_word ( ptr, & parameter, CFG_PARAM ) ) )
    return 0;

  if ( 0 == ( ptr = parse_word ( ptr, & value, CFG_VALUE ) ) )
    return 0;
  
  if ( 0 == store_value ( cfg, parameter, value ) )
    return 0;
  
  free ( parameter );
  free ( value );

  return 1;
}

/*----------------------------------------------------------*/
static char * parse_word ( char * ptr, char ** word, cfg_key_type key )
{
  int			len = 0;
  cfg_quote_type	quote;

  switch ( * ptr )
    {
    case '\"':
      quote = CFG_QUOTE_DOUBLE;
      ptr++;
      break;
    case '\'':
      quote = CFG_QUOTE_SINGLE;
      ptr++;
      break;
    default:
      quote = CFG_QUOTE_NO;
      break;
    }

  while ( 1 )
    {
      if ( quote == CFG_QUOTE_NO )
	{
	  if ( * ( ptr + len ) == ' ' || * ( ptr + len ) == '\t' ||
	       * ( ptr + len ) == '\0' || * ( ptr + len ) == '#' ||
	       ( * ( ptr + len ) == '=' && key == CFG_PARAM ) )
	    break;
	}
      else if ( quote == CFG_QUOTE_DOUBLE )
	{
	  if ( * ( ptr + len ) == '\"' )
	    break;
	}
      else if ( quote == CFG_QUOTE_SINGLE )
	{
	  if ( * ( ptr + len ) == '\'' )
	    break;
	}

      if ( * ( ptr + len ) == '\0' )
	return 0;

      len++;
    }

  if ( 0 == ( * word = ( char * ) malloc ( len + 1 ) ) )
    return 0;

  strncpy ( * word, ptr, len );
  * ( * word + len ) = '\0';

  ptr += ( len + ( quote == CFG_QUOTE_NO ? 0 : 1 ) );

  ptr = remove_first_spaces ( ptr );

  switch ( key )
    {
    case CFG_PARAM:
      if ( * ptr != '=' )
	return 0;
      ptr++;
      ptr = remove_first_spaces ( ptr );
      break;
    case CFG_VALUE:
      if ( * ptr != '\0' && * ptr != '#' )
	return 0;
      break;
    }

  return ptr;
}

/*----------------------------------------------------------*/
static int store_value ( cfg_struct * cfg, char * parameter, char * value )
{
  long tmp, utmp;
  double dtmp;
  char * endptr;

  if ( 0 != strcasecmp ( parameter, cfg->name ) )
    return 0;

  errno = 0;

  switch ( cfg->type )
    {
    case CFG_BOOL:
      if ( 0 == strcasecmp ( value, "TRUE" ) ||
	   0 == strcasecmp ( value, "YES" ) ||
	   0 == strcasecmp ( value, "T" ) ||
	   0 == strcasecmp ( value, "Y" ) ||
	   0 == strcasecmp ( value, "1" ) )
	* ( int * ) ( cfg->value ) = 1;
      else
	* ( int * ) ( cfg->value ) = 0;
      return 1;
    case CFG_LONG:
      tmp = strtol ( value, & endptr, 10 );
      if ( * endptr )
	return 0;
      if ( errno == ERANGE )
	return 0;
      * ( long * ) ( cfg->value ) = tmp;
      return 1;
    case CFG_ULONG:
      utmp = strtoul ( value, & endptr, 10 );
      if ( * endptr )
	return 0;
      if ( errno == ERANGE )
	return 0;
      * ( unsigned long * ) ( cfg->value ) = utmp;
      return 1;
    case CFG_DOUBLE:
      dtmp = strtod ( value, & endptr );
      if ( * endptr )
	return 0;
      if ( errno == ERANGE )
	return 0;
      * ( double * ) ( cfg->value ) = dtmp;
      return 1;
    case CFG_STRING:
      strcpy ( ( char * ) cfg->value, value );
      return 1;
    default:
      return 0;
    }

  return 0;
}

/*----------------------------------------------------------*/
static void set_word ( FILE * fp, cfg_struct * cfg )
{
  char c [ 2 ];

  switch ( cfg->type )
    {
    case CFG_BOOL:
      fprintf ( fp, "%s\t= %s\n", cfg->name,
		( * ( int * ) ( cfg->value ) ) ? "true" : "false" );
      break;
    case CFG_LONG:
      fprintf ( fp, "%s\t= %ld\n", cfg->name, * ( long * ) ( cfg->value ) );
      break;
    case CFG_ULONG:
      fprintf ( fp, "%s\t= %lu\n", cfg->name,
		* ( unsigned long * ) ( cfg->value ) );
      break;
    case CFG_DOUBLE:
      fprintf ( fp, "%s\t= %f\n", cfg->name, * ( double * ) ( cfg->value ) );
      break;
    case CFG_STRING:
      single_or_double_quote (  ( char * ) cfg->value, c );
      fprintf ( fp, "%s\t= %s%s%s\n", cfg->name,
		c, ( char * ) cfg->value, c );
      break;
    }
}

/*----------------------------------------------------------*/
static void single_or_double_quote ( char * str, char * ret )
{
  ret [ 1 ] = '\0';

  if ( 0 != strchr ( str, '\"' ) )
    ret [ 0 ] = '\'';
  else if ( 0 != strchr ( str, '\'' ) ||
	    0 != strchr ( str, '#' ) ||
	    0 != strchr ( str, '\t' ) ||
	    0 != strchr ( str, ' ' ) )
    ret [ 0 ] = '\"';
  else
    ret [ 0 ] = '\0';
}