- Timestamp:
- 2009-10-17T17:24:52Z (15 years ago)
- Branches:
- master
- Children:
- c48a033
- Parents:
- 0c41177 (diff), 2e44b1f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/ini.c
r0c41177 rc5bc47b 2 2 * BitlBee -- An IRC to other IM-networks gateway * 3 3 * * 4 * Copyright 2002-200 5Wilmer van der Gaast and others *4 * Copyright 2002-2008 Wilmer van der Gaast and others * 5 5 \********************************************************************/ 6 6 … … 28 28 ini_t *ini_open( char *file ) 29 29 { 30 ini_t *ini = g_new0( ini_t, 1 ); 30 int fd; 31 ini_t *ini = NULL; 32 struct stat fi; 31 33 32 if( ( ini->fp = fopen( file, "r" ) ) == NULL ) 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 ) 33 39 { 34 g_free( ini ); 35 return( NULL ); 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; 36 49 } 50 51 if( fd >= 0 ) 52 close( fd ); 37 53 38 return( ini ); 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; 39 74 } 40 75 41 76 int ini_read( ini_t *file ) 42 77 { 43 char key[MAX_STRING], s[MAX_STRING], *t; 44 int i; 78 char *s; 45 79 46 while( !feof( file->fp ))80 while( file->cur && file->cur < file->file + file->size ) 47 81 { 48 *s = 0;49 fscanf( file->fp, "%127[^\n#]s", s );50 f scanf( file->fp, "%*[^\n]s" );51 fgetc( file->fp ); /* Skip newline */ 52 file->line ++;53 if( strchr( s, '=' ))82 char *e, *next; 83 84 file->line++; 85 86 /* Find the end of line */ 87 if( ( e = strchr( file->cur, '\n' ) ) != NULL ) 54 88 { 55 sscanf( s, "%[^ =]s", key ); 56 if( ( t = strchr( key, '.' ) ) ) 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 ) 57 109 { 58 *t = 0; 59 strcpy( file->section, key ); 60 t ++; 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; 61 125 } 62 126 else 63 127 { 64 strcpy( file->section, file->c_section ); 65 t = key; 128 file->section = file->c_section; 66 129 } 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;73 130 74 return( 1 ); 131 file->cur = next; 132 return 1; 75 133 } 76 else if( ( t = strchr( s, '[' ) ) ) 77 { 78 strcpy( file->c_section, t + 1 ); 79 t = strchr( file->c_section, ']' ); 80 *t = 0; 81 } 134 /* else: noise/comment/etc, let's just ignore it. */ 135 136 file->cur = next; 82 137 } 83 return( 0 ); 138 139 return 0; 84 140 } 85 141 86 142 void ini_close( ini_t *file ) 87 143 { 88 fclose( file->fp );89 144 g_free( file ); 90 145 }
Note: See TracChangeset
for help on using the changeset viewer.