-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.c
90 lines (76 loc) · 2.57 KB
/
config.c
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
#if 0
File name: config.c, configuration file interface routines
x86 Emulating Debugger (EDB)
Copyright (C) 1991-1993 Serge Pachkovsky
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 1, 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.
If you need to contact me, feel free to write to ps@oci.unizh.ch
or to Serge Pachkovsky, Erligatterweg 61, Zuerich CH 8038, Switzerland
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include "config.h"
#include "cpu8086.h"
#include "os.h"
static int config_handle = -1 ;
int
open_config_section( const char *section )
{
int len = strlen( section ) ;
char buf[ MAX_CFG_STRING ] ;
if( config_handle != -1 ) lseek( config_handle, 0, SEEK_SET ) ;
if( ( config_handle = open( CONFIG_NAME, O_RDONLY | O_TEXT ) ) == -1 ){
cprint( "Can't open '%s' : %s\r\n", CONFIG_NAME, strerror( errno ) ) ;
return -1 ;
}
while( hgets( buf, sizeof buf, config_handle ) != NULL )
if( buf[ 0 ] == '[' && buf[ len + 1 ] == ']' && ! strncmp( section, buf + 1, len ) )
return 0 ;
close_config() ;
cprint( "There is no section '%s' in %s\r\n", section, CONFIG_NAME ) ;
return -1 ;
}
void
close_config( void )
{
if( config_handle != -1 ) close( config_handle ) ;
}
int
get_config_string( char *buf )
{
if( config_handle == -1 ){
cprint( "%s not opened\r\n", CONFIG_NAME ) ;
exit( -1 ) ;
}
while( 1 ){
if( hgets( buf, MAX_CFG_STRING, config_handle ) == NULL ) return -1 ;
if( strchr( buf, '\n' ) != NULL ) *strchr( buf, '\n' ) = 0 ;
if( strchr( buf, ';' ) != NULL ) *strchr( buf, ';' ) = 0 ;
if( buf[ 0 ] == 0 ) continue ;
if( buf[ 0 ] == '[' && buf[ strlen( buf ) - 1 ] == ']' ) return -1 ;
break ;
}
return 0 ;
}
int
process_YesNo( char *p )
{
strupr( p ) ;
if( strncmp( p, "YES", 3 ) == 0 ) return 1 ;
else if( strncmp( p, "NO", 2 ) == 0 ) return 0 ;
else if( strncmp( p, "ON", 2 ) == 0 ) return 1 ;
else if( strncmp( p, "OFF", 2 ) == 0 ) return 0 ;
else return -1 ;
}