- Timestamp:
- 2015-02-20T22:50:54Z (9 years ago)
- Branches:
- master
- Children:
- 0b9daac, 3d45471, 7733b8c
- Parents:
- af359b4
- git-author:
- Indent <please@…> (19-02-15 05:47:20)
- git-committer:
- dequis <dx@…> (20-02-15 22:50:54)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/ini.c
raf359b4 r5ebff60 1 1 /********************************************************************\ 2 2 * BitlBee -- An IRC to other IM-networks gateway * 3 3 * * … … 26 26 #include "bitlbee.h" 27 27 28 ini_t *ini_open( char *file)28 ini_t *ini_open(char *file) 29 29 { 30 30 int fd; 31 31 ini_t *ini = NULL; 32 32 struct stat fi; 33 34 if ( ( fd = open( file, O_RDONLY )) != -1 &&35 fstat( fd, &fi) == 0 &&33 34 if ((fd = open(file, O_RDONLY)) != -1 && 35 fstat(fd, &fi) == 0 && 36 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 ) 39 { 40 memset( ini, 0, sizeof( ini_t ) ); 37 (ini = g_malloc(sizeof(ini_t) + fi.st_size + 1)) && 38 read(fd, ini->file, fi.st_size) == fi.st_size) { 39 memset(ini, 0, sizeof(ini_t)); 41 40 ini->size = fi.st_size; 42 41 ini->file[ini->size] = 0; 43 42 ini->cur = ini->file; 44 43 ini->c_section = ""; 45 46 close( fd);47 44 45 close(fd); 46 48 47 return ini; 49 48 } 50 49 51 if( fd >= 0 ) 52 close( fd ); 53 54 ini_close( ini ); 50 if (fd >= 0) { 51 close(fd); 52 } 53 54 ini_close(ini); 55 55 56 56 return NULL; … … 59 59 /* Strips leading and trailing whitespace and returns a pointer to the first 60 60 non-ws character of the given string. */ 61 static char *ini_strip_whitespace( char *in)61 static char *ini_strip_whitespace(char *in) 62 62 { 63 63 char *e; 64 64 65 while ( g_ascii_isspace( *in ) )65 while (g_ascii_isspace(*in)) { 66 66 in++; 67 } 67 68 68 e = in + strlen( in) - 1;69 while ( e > in && g_ascii_isspace( *e ) )69 e = in + strlen(in) - 1; 70 while (e > in && g_ascii_isspace(*e)) { 70 71 e--; 72 } 71 73 e[1] = 0; 72 74 73 75 return in; 74 76 } 75 77 76 int ini_read( ini_t *file)78 int ini_read(ini_t *file) 77 79 { 78 80 char *s; 79 80 while( file->cur && file->cur < file->file + file->size ) 81 { 81 82 while (file->cur && file->cur < file->file + file->size) { 82 83 char *e, *next; 83 84 84 85 file->line++; 85 86 86 87 /* Find the end of line */ 87 if( ( e = strchr( file->cur, '\n' ) ) != NULL ) 88 { 88 if ((e = strchr(file->cur, '\n')) != NULL) { 89 89 *e = 0; 90 90 next = e + 1; 91 } 92 else 93 { 91 } else { 94 92 /* No more lines. */ 95 e = file->cur + strlen( file->cur);93 e = file->cur + strlen(file->cur); 96 94 next = NULL; 97 95 } 98 96 99 97 /* Comment? */ 100 if ( ( s = strchr( file->cur, '#' ) ) != NULL )98 if ((s = strchr(file->cur, '#')) != NULL) { 101 99 *s = 0; 102 103 file->cur = ini_strip_whitespace( file->cur ); 104 105 if( *file->cur == '[' ) 106 {100 } 101 102 file->cur = ini_strip_whitespace(file->cur); 103 104 if (*file->cur == '[') { 107 105 file->cur++; 108 if( ( s = strchr( file->cur, ']' ) ) != NULL ) 109 { 106 if ((s = strchr(file->cur, ']')) != NULL) { 110 107 *s = 0; 111 108 file->c_section = file->cur; 112 109 } 113 } 114 else if( ( s = strchr( file->cur, '=' ) ) != NULL ) 115 { 110 } else if ((s = strchr(file->cur, '=')) != NULL) { 116 111 *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 { 112 file->key = ini_strip_whitespace(file->cur); 113 file->value = ini_strip_whitespace(s + 1); 114 115 if ((s = strchr(file->key, '.')) != NULL) { 122 116 *s = 0; 123 117 file->section = file->key; 124 118 file->key = s + 1; 125 } 126 else 127 { 119 } else { 128 120 file->section = file->c_section; 129 121 } 130 122 131 123 file->cur = next; 132 124 return 1; … … 136 128 file->cur = next; 137 129 } 138 130 139 131 return 0; 140 132 } 141 133 142 void ini_close( ini_t *file)134 void ini_close(ini_t *file) 143 135 { 144 g_free( file);136 g_free(file); 145 137 }
Note: See TracChangeset
for help on using the changeset viewer.