Changes in lib/ini.c [0182773:df1694b]
Legend:
- Unmodified
- Added
- Removed
-
lib/ini.c
r0182773 rdf1694b 2 2 * BitlBee -- An IRC to other IM-networks gateway * 3 3 * * 4 * Copyright 2002-200 8Wilmer van der Gaast and others *4 * Copyright 2002-2005 Wilmer van der Gaast and others * 5 5 \********************************************************************/ 6 6 … … 28 28 ini_t *ini_open( char *file ) 29 29 { 30 int fd; 31 ini_t *ini = NULL; 32 struct stat fi; 30 ini_t *ini = g_new0( ini_t, 1 ); 33 31 34 if( ( fd = open( file, O_RDONLY ) ) != -1 && 35 fstat( fd, &fi ) == 0 && 36 fi.st_size <= 16384 && 37 ( ini = g_malloc( sizeof( ini_t ) + fi.st_size + 1 ) ) && 38 read( fd, ini->file, fi.st_size ) == fi.st_size ) 32 if( ( ini->fp = fopen( file, "r" ) ) == NULL ) 39 33 { 40 memset( ini, 0, sizeof( ini_t ) ); 41 ini->size = fi.st_size; 42 ini->file[ini->size] = 0; 43 ini->cur = ini->file; 44 ini->c_section = ""; 45 46 close( fd ); 47 48 return ini; 34 g_free( ini ); 35 return( NULL ); 49 36 } 50 51 if( fd >= 0 )52 close( fd );53 37 54 ini_close( ini ); 55 56 return NULL; 57 } 58 59 /* Strips leading and trailing whitespace and returns a pointer to the first 60 non-ws character of the given string. */ 61 static char *ini_strip_whitespace( char *in ) 62 { 63 char *e; 64 65 while( isspace( *in ) ) 66 in++; 67 68 e = in + strlen( in ) - 1; 69 while( e > in && isspace( *e ) ) 70 e--; 71 e[1] = 0; 72 73 return in; 38 return( ini ); 74 39 } 75 40 76 41 int ini_read( ini_t *file ) 77 42 { 78 char *s; 43 char key[MAX_STRING], s[MAX_STRING], *t; 44 int i; 79 45 80 while( file->cur && file->cur < file->file + file->size)46 while( !feof( file->fp ) ) 81 47 { 82 char *e, *next;83 84 f ile->line++;85 86 /* Find the end of line */87 if( ( e = strchr( file->cur, '\n' ) ) != NULL)48 *s = 0; 49 fscanf( file->fp, "%127[^\n#]s", s ); 50 fscanf( file->fp, "%*[^\n]s" ); 51 fgetc( file->fp ); /* Skip newline */ 52 file->line ++; 53 if( strchr( s, '=' ) ) 88 54 { 89 *e = 0; 90 next = e + 1; 91 } 92 else 93 { 94 /* No more lines. */ 95 e = file->cur + strlen( file->cur ); 96 next = NULL; 97 } 98 99 /* Comment? */ 100 if( ( s = strchr( file->cur, '#' ) ) != NULL ) 101 *s = 0; 102 103 file->cur = ini_strip_whitespace( file->cur ); 104 105 if( *file->cur == '[' ) 106 { 107 file->cur++; 108 if( ( s = strchr( file->cur, ']' ) ) != NULL ) 55 sscanf( s, "%[^ =]s", key ); 56 if( ( t = strchr( key, '.' ) ) ) 109 57 { 110 *s = 0; 111 file->c_section = file->cur; 112 } 113 } 114 else if( ( s = strchr( file->cur, '=' ) ) != NULL ) 115 { 116 *s = 0; 117 file->key = ini_strip_whitespace( file->cur ); 118 file->value = ini_strip_whitespace( s + 1 ); 119 120 if( ( s = strchr( file->key, '.' ) ) != NULL ) 121 { 122 *s = 0; 123 file->section = file->key; 124 file->key = s + 1; 58 *t = 0; 59 strcpy( file->section, key ); 60 t ++; 125 61 } 126 62 else 127 63 { 128 file->section = file->c_section; 64 strcpy( file->section, file->c_section ); 65 t = key; 129 66 } 67 sscanf( t, "%s", file->key ); 68 t = strchr( s, '=' ) + 1; 69 for( i = 0; t[i] == ' '; i ++ ); 70 strcpy( file->value, &t[i] ); 71 for( i = strlen( file->value ) - 1; file->value[i] == 32; i -- ) 72 file->value[i] = 0; 130 73 131 file->cur = next; 132 return 1; 74 return( 1 ); 133 75 } 134 /* else: noise/comment/etc, let's just ignore it. */ 135 136 file->cur = next; 76 else if( ( t = strchr( s, '[' ) ) ) 77 { 78 strcpy( file->c_section, t + 1 ); 79 t = strchr( file->c_section, ']' ); 80 *t = 0; 81 } 137 82 } 138 139 return 0; 83 return( 0 ); 140 84 } 141 85 142 86 void ini_close( ini_t *file ) 143 87 { 88 fclose( file->fp ); 144 89 g_free( file ); 145 90 }
Note: See TracChangeset
for help on using the changeset viewer.