Changeset 72b6783e
- Timestamp:
- 2008-12-24T09:00:42Z (16 years ago)
- Branches:
- master
- Children:
- 489f996
- Parents:
- 5469952
- Location:
- lib
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/ini.c
r5469952 r72b6783e 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 return ini; 36 46 } 37 47 38 return( ini ); 48 g_free( ini ); 49 if( fd >= 0 ) 50 close( fd ); 51 52 return NULL; 39 53 } 40 54 41 55 int ini_read( ini_t *file ) 42 56 { 43 char key[MAX_STRING], s[MAX_STRING], *t; 44 int i; 57 char *s; 45 58 46 while( !feof( file->fp ))59 while( file->cur && file->cur < file->file + file->size ) 47 60 { 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, '=' ) ) 61 char *e, *next; 62 63 file->line++; 64 65 /* Leading whitespace */ 66 while( *file->cur == ' ' || *file->cur == '\t' ) 67 file->cur++; 68 69 /* Find the end of line */ 70 if( ( e = strchr( file->cur, '\n' ) ) != NULL ) 54 71 { 55 sscanf( s, "%[^ =]s", key ); 56 if( ( t = strchr( key, '.' ) ) ) 72 next = e + 1; 73 } 74 else 75 { 76 /* No more lines. */ 77 e = file->cur + strlen( file->cur ) - 1; 78 next = NULL; 79 } 80 81 /* Comment? */ 82 if( ( s = strchr( file->cur, '#' ) ) != NULL ) 83 e = s - 1; 84 85 /* And kill trailing whitespace. */ 86 while( isspace( *e ) && e > file->cur ) 87 e--; 88 e[1] = 0; 89 90 printf( "Stripped line: '%s'\n", file->cur ); 91 92 if( *file->cur == '[' ) 93 { 94 file->cur++; 95 if( ( s = strchr( file->cur, ']' ) ) != NULL ) 57 96 { 58 *t = 0; 59 strcpy( file->section, key ); 60 t ++; 97 *s = 0; 98 file->c_section = file->cur; 99 100 printf( "Section started: %s\n", file->c_section ); 101 } 102 } 103 else if( ( s = strchr( file->cur, '=' ) ) != NULL ) 104 { 105 *s = 0; 106 file->value = s + 1; 107 while( isspace( *file->value ) ) 108 file->value++; 109 110 s--; 111 while( isspace( *s ) && s > file->cur ) 112 s--; 113 s[1] = 0; 114 file->key = file->cur; 115 116 if( ( s = strchr( file->key, '.' ) ) != NULL ) 117 { 118 *s = 0; 119 file->section = file->key; 120 file->key = s + 1; 61 121 } 62 122 else 63 123 { 64 strcpy( file->section, file->c_section ); 65 t = key; 124 file->section = file->c_section; 66 125 } 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 126 74 return( 1 ); 127 file->cur = next; 128 129 printf( "%s.%s = '%s'\n", file->section, file->key, file->value ); 130 131 return 1; 75 132 } 76 else if( ( t = strchr( s, '[' ) ) ) 77 { 78 strcpy( file->c_section, t + 1 ); 79 t = strchr( file->c_section, ']' ); 80 *t = 0; 81 } 133 /* else: noise, but let's just ignore it. */ 134 135 file->cur = next; 82 136 } 83 return( 0 ); 137 138 return 0; 84 139 } 85 140 86 141 void ini_close( ini_t *file ) 87 142 { 88 fclose( file->fp );89 143 g_free( file ); 90 144 } -
lib/ini.h
r5469952 r72b6783e 29 29 typedef struct 30 30 { 31 FILE *fp;32 31 int line; 33 char c_section[MAX_STRING]; 34 char section[MAX_STRING]; 35 char key[MAX_STRING]; 36 char value[MAX_STRING]; 32 char *c_section; 33 char *section; 34 char *key; 35 char *value; 36 int size; 37 char *cur, *tok; 38 char file[]; 37 39 } ini_t; 38 40
Note: See TracChangeset
for help on using the changeset viewer.