[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[72b6783e] | 4 | * Copyright 2002-2008 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* INI file reading code */ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | This program is free software; you can redistribute it and/or modify |
---|
| 11 | it under the terms of the GNU General Public License as published by |
---|
| 12 | the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | (at your option) any later version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, |
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | GNU General Public License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU General Public License with |
---|
| 21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
| 22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
| 23 | Suite 330, Boston, MA 02111-1307 USA |
---|
| 24 | */ |
---|
| 25 | #define BITLBEE_CORE |
---|
| 26 | #include "bitlbee.h" |
---|
| 27 | |
---|
| 28 | ini_t *ini_open( char *file ) |
---|
| 29 | { |
---|
[72b6783e] | 30 | int fd; |
---|
| 31 | ini_t *ini = NULL; |
---|
| 32 | struct stat fi; |
---|
[b7d3cc34] | 33 | |
---|
[72b6783e] | 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 ) |
---|
[b7d3cc34] | 39 | { |
---|
[72b6783e] | 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 = ""; |
---|
[0182773] | 45 | |
---|
| 46 | close( fd ); |
---|
| 47 | |
---|
[72b6783e] | 48 | return ini; |
---|
[b7d3cc34] | 49 | } |
---|
[0182773] | 50 | |
---|
[72b6783e] | 51 | if( fd >= 0 ) |
---|
| 52 | close( fd ); |
---|
[0182773] | 53 | |
---|
| 54 | ini_close( ini ); |
---|
[72b6783e] | 55 | |
---|
| 56 | return NULL; |
---|
[b7d3cc34] | 57 | } |
---|
| 58 | |
---|
[489f996] | 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; |
---|
| 74 | } |
---|
| 75 | |
---|
[b7d3cc34] | 76 | int ini_read( ini_t *file ) |
---|
| 77 | { |
---|
[72b6783e] | 78 | char *s; |
---|
[b7d3cc34] | 79 | |
---|
[72b6783e] | 80 | while( file->cur && file->cur < file->file + file->size ) |
---|
[b7d3cc34] | 81 | { |
---|
[72b6783e] | 82 | char *e, *next; |
---|
| 83 | |
---|
| 84 | file->line++; |
---|
| 85 | |
---|
| 86 | /* Find the end of line */ |
---|
| 87 | if( ( e = strchr( file->cur, '\n' ) ) != NULL ) |
---|
| 88 | { |
---|
[489f996] | 89 | *e = 0; |
---|
[72b6783e] | 90 | next = e + 1; |
---|
| 91 | } |
---|
| 92 | else |
---|
| 93 | { |
---|
| 94 | /* No more lines. */ |
---|
[489f996] | 95 | e = file->cur + strlen( file->cur ); |
---|
[72b6783e] | 96 | next = NULL; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /* Comment? */ |
---|
| 100 | if( ( s = strchr( file->cur, '#' ) ) != NULL ) |
---|
[489f996] | 101 | *s = 0; |
---|
[72b6783e] | 102 | |
---|
[489f996] | 103 | file->cur = ini_strip_whitespace( file->cur ); |
---|
[72b6783e] | 104 | |
---|
| 105 | if( *file->cur == '[' ) |
---|
| 106 | { |
---|
| 107 | file->cur++; |
---|
| 108 | if( ( s = strchr( file->cur, ']' ) ) != NULL ) |
---|
| 109 | { |
---|
| 110 | *s = 0; |
---|
| 111 | file->c_section = file->cur; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | else if( ( s = strchr( file->cur, '=' ) ) != NULL ) |
---|
[b7d3cc34] | 115 | { |
---|
[72b6783e] | 116 | *s = 0; |
---|
[489f996] | 117 | file->key = ini_strip_whitespace( file->cur ); |
---|
| 118 | file->value = ini_strip_whitespace( s + 1 ); |
---|
[72b6783e] | 119 | |
---|
| 120 | if( ( s = strchr( file->key, '.' ) ) != NULL ) |
---|
[b7d3cc34] | 121 | { |
---|
[72b6783e] | 122 | *s = 0; |
---|
| 123 | file->section = file->key; |
---|
| 124 | file->key = s + 1; |
---|
[b7d3cc34] | 125 | } |
---|
| 126 | else |
---|
| 127 | { |
---|
[72b6783e] | 128 | file->section = file->c_section; |
---|
[b7d3cc34] | 129 | } |
---|
| 130 | |
---|
[72b6783e] | 131 | file->cur = next; |
---|
| 132 | return 1; |
---|
[b7d3cc34] | 133 | } |
---|
[489f996] | 134 | /* else: noise/comment/etc, let's just ignore it. */ |
---|
[72b6783e] | 135 | |
---|
| 136 | file->cur = next; |
---|
[b7d3cc34] | 137 | } |
---|
[72b6783e] | 138 | |
---|
| 139 | return 0; |
---|
[b7d3cc34] | 140 | } |
---|
| 141 | |
---|
| 142 | void ini_close( ini_t *file ) |
---|
| 143 | { |
---|
| 144 | g_free( file ); |
---|
| 145 | } |
---|