[421b7d2] | 1 | /* ini.c |
---|
[d1b1380] | 2 | * .ini file routines |
---|
[9ff87ea] | 3 | * Copyright (C) 1995-2001,2003 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
[846746e] | 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
[1b71c05] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
[a420b49] | 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | # include <config.h> |
---|
| 22 | #endif |
---|
[d1b1380] | 23 | |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <string.h> |
---|
| 27 | #include <ctype.h> |
---|
[693388e] | 28 | |
---|
| 29 | #include "debug.h" |
---|
[d1d5985] | 30 | #include "filename.h" |
---|
[693388e] | 31 | #include "hash.h" |
---|
| 32 | #include "ini.h" |
---|
[d1d5985] | 33 | #include "message.h" |
---|
[d1b1380] | 34 | #include "useful.h" |
---|
| 35 | |
---|
| 36 | /* for testing separately from Survex */ |
---|
| 37 | #ifdef TEST_ALONE |
---|
| 38 | /* says when two distinct strings hash to same value */ |
---|
| 39 | #define REPORT_COLLISION |
---|
| 40 | |
---|
[647407d] | 41 | /* good enough for testing */ |
---|
[d1d5985] | 42 | #define strcasecmp(x,y) strcmp((x),(y)) |
---|
[d1b1380] | 43 | |
---|
[a420b49] | 44 | char * |
---|
| 45 | osstrdup(char *s) |
---|
| 46 | { |
---|
[d1b1380] | 47 | char *t; |
---|
[a420b49] | 48 | t = malloc(strlen(s) + 1); |
---|
[d1b1380] | 49 | if (!t) { |
---|
| 50 | printf("out of memory\n"); |
---|
| 51 | exit(1); |
---|
| 52 | } |
---|
[a420b49] | 53 | strcpy(t, s); |
---|
[d1b1380] | 54 | return t; |
---|
| 55 | } |
---|
| 56 | #include <assert.h> |
---|
[4c07c51] | 57 | #define SVX_ASSERT(M) assert(M) |
---|
[d1b1380] | 58 | #endif |
---|
| 59 | |
---|
[a420b49] | 60 | #if 0 |
---|
| 61 | void |
---|
| 62 | ini_write(const char *section, const char *var, const char *value) |
---|
| 63 | { |
---|
| 64 | FILE *fh = stdout; |
---|
| 65 | scan_for(section, var); |
---|
| 66 | |
---|
| 67 | static char *last_section = NULL; |
---|
| 68 | if (!last_section || strcasecmp(last_section, section) != 0) { |
---|
| 69 | if (last_section) putc('\n', fh); |
---|
| 70 | fprintf(fh, "[%s]\n", section); |
---|
| 71 | last_section = section; |
---|
[d1b1380] | 72 | } |
---|
[a420b49] | 73 | fprintf(stdout, "%s=%s\n", var, value); |
---|
[d1b1380] | 74 | } |
---|
[a420b49] | 75 | #endif |
---|
[d1b1380] | 76 | |
---|
[a420b49] | 77 | char ** |
---|
[eb18f4d] | 78 | ini_read(FILE **fh_list, const char *section, const char **vars) |
---|
[a420b49] | 79 | { |
---|
[d1b1380] | 80 | int n, c; |
---|
| 81 | char **vals; |
---|
[647407d] | 82 | int *hash_tab; |
---|
[d1b1380] | 83 | |
---|
[4c07c51] | 84 | SVX_ASSERT(fh_list != NULL); |
---|
| 85 | SVX_ASSERT(section != NULL); |
---|
| 86 | SVX_ASSERT(vars != NULL); |
---|
[d1b1380] | 87 | |
---|
| 88 | /* count how many variables to look up */ |
---|
[a420b49] | 89 | n = 0; |
---|
| 90 | while (vars[n]) n++; |
---|
[cb3d1e2] | 91 | |
---|
[a420b49] | 92 | hash_tab = malloc(n * sizeof(int)); |
---|
| 93 | vals = malloc(n * sizeof(char*)); |
---|
[d1b1380] | 94 | if (!hash_tab || !vals) { |
---|
| 95 | free(hash_tab); |
---|
| 96 | return NULL; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /* calculate hashes (to save on strcmp-s) */ |
---|
[a420b49] | 100 | for (c = 0; vars[c]; c++) { |
---|
[693388e] | 101 | hash_tab[c] = hash_lc_string(vars[c]); |
---|
[a420b49] | 102 | vals[c] = NULL; |
---|
[d1b1380] | 103 | } |
---|
| 104 | |
---|
[4c07c51] | 105 | SVX_ASSERT(c == n); /* counted vars[] twice and got different answers! */ |
---|
[d1b1380] | 106 | |
---|
[a420b49] | 107 | while (1) { |
---|
[647407d] | 108 | char *p, *var, *val; |
---|
| 109 | char buf[1024]; |
---|
| 110 | int hash; |
---|
[d1b1380] | 111 | |
---|
[647407d] | 112 | int fInSection = 0; |
---|
[cb3d1e2] | 113 | |
---|
[647407d] | 114 | FILE *fh = *fh_list++; |
---|
| 115 | |
---|
| 116 | if (!fh) break; |
---|
| 117 | rewind(fh); |
---|
| 118 | while (1) { |
---|
| 119 | if (feof(fh)) { |
---|
| 120 | /* if (fWrite) { insert_section(); insert_lines(); fDone=1; } */ |
---|
| 121 | break; |
---|
| 122 | } |
---|
[d1b1380] | 123 | |
---|
[647407d] | 124 | /* read line and sort out terminator */ |
---|
| 125 | if (!fgets(buf, 1024, fh)) break; |
---|
| 126 | p = strpbrk(buf, "\n\r"); |
---|
| 127 | if (p) *p = '\0'; |
---|
| 128 | |
---|
| 129 | /* skip blank lines */ |
---|
| 130 | if (buf[0] == '\0') continue; |
---|
| 131 | |
---|
| 132 | /* deal with a section heading */ |
---|
| 133 | if (buf[0] == '[' && buf[strlen(buf) - 1] == ']') { |
---|
| 134 | buf[strlen(buf) - 1] = '\0'; |
---|
| 135 | if (fInSection) { |
---|
| 136 | /* if (fWrite) { insert_lines(); fDone=1; } */ |
---|
| 137 | /* now leaving wanted section, so stop on this file */ |
---|
| 138 | break; |
---|
| 139 | } |
---|
| 140 | /* printf("[%s] [%s]\n", section, buf + 1);*/ |
---|
| 141 | if (!strcasecmp(section, buf + 1)) fInSection = 1; |
---|
| 142 | continue; |
---|
[d1b1380] | 143 | } |
---|
| 144 | |
---|
[647407d] | 145 | if (!fInSection) continue; |
---|
[d1b1380] | 146 | |
---|
[647407d] | 147 | p = strchr(buf, '='); |
---|
| 148 | if (!p) continue; /* non-blank line with no = sign! */ |
---|
[cb3d1e2] | 149 | |
---|
[647407d] | 150 | *p = '\0'; |
---|
| 151 | var = buf; |
---|
| 152 | val = p + 1; |
---|
| 153 | |
---|
| 154 | /* hash the variable name and see if it's in the list passed in */ |
---|
[693388e] | 155 | hash = hash_lc_string(var); |
---|
[647407d] | 156 | for (c = n - 1; c >= 0; c--) { |
---|
| 157 | if (hash == hash_tab[c]) { |
---|
| 158 | if (strcasecmp(var, vars[c]) == 0) { |
---|
| 159 | /* if (fWrite) { replace_line(); hash_tab[c]=-1; } else */ |
---|
| 160 | vals[c] = osstrdup(val); |
---|
| 161 | /* set to value hash can't generate to ignore further matches */ |
---|
| 162 | hash_tab[c] = -1; |
---|
| 163 | } else { |
---|
[d1b1380] | 164 | #ifdef REPORT_COLLISION |
---|
[647407d] | 165 | printf("`%s' and `%s' both hash to %d!\n",var,vars[c],hash); |
---|
[d1b1380] | 166 | #endif |
---|
[647407d] | 167 | } |
---|
| 168 | } |
---|
| 169 | } |
---|
[d1b1380] | 170 | } |
---|
| 171 | } |
---|
[cb3d1e2] | 172 | |
---|
[d1b1380] | 173 | free(hash_tab); |
---|
| 174 | return vals; |
---|
| 175 | } |
---|
| 176 | |
---|
[a420b49] | 177 | char ** |
---|
[aad2af5] | 178 | ini_read_hier(FILE **fh_list, const char *section, const char **v) |
---|
[a420b49] | 179 | { |
---|
[d1b1380] | 180 | int i, j; |
---|
[eb18f4d] | 181 | char **vals; |
---|
| 182 | int *to; |
---|
[255f57b9] | 183 | char **vars; |
---|
[d1b1380] | 184 | |
---|
[4c07c51] | 185 | SVX_ASSERT(fh_list != NULL); |
---|
| 186 | SVX_ASSERT(section != NULL); |
---|
| 187 | SVX_ASSERT(v != NULL); |
---|
[eb18f4d] | 188 | |
---|
[aad2af5] | 189 | vals = ini_read(fh_list, section, v); |
---|
[eb18f4d] | 190 | if (!vals) return NULL; |
---|
| 191 | |
---|
[aad2af5] | 192 | /*{int i; printf("[%s]\n",section);for(i=0;v[i];i++)printf("%d:%s\"%s\"\n",i,v[i],vals[i]?vals[i]:"(null)");}*/ |
---|
[eb18f4d] | 193 | i = 0; |
---|
| 194 | while (v[i]) i++; |
---|
| 195 | vars = malloc((i + 1) * sizeof(char*)); /* + 1 to include NULL */ |
---|
| 196 | to = malloc(i * sizeof(int)); |
---|
| 197 | if (!vars || !to) { |
---|
| 198 | free(vars); |
---|
| 199 | free(to); |
---|
| 200 | free(vals); |
---|
| 201 | return NULL; |
---|
| 202 | } |
---|
| 203 | memcpy(vars, v, (i + 1) * sizeof(char*)); |
---|
[aad2af5] | 204 | |
---|
| 205 | for (i = 1, j = 1; vars[i]; i++) { |
---|
| 206 | /* printf("%s: %s %d\n",vars[i],vals[i]?vals[i]:"(null)",to[i]);*/ |
---|
| 207 | if (!vals[i]) { |
---|
| 208 | vars[j] = vars[i]; |
---|
| 209 | to[j] = i; |
---|
| 210 | j++; |
---|
| 211 | } |
---|
[eb18f4d] | 212 | } |
---|
[421b7d2] | 213 | |
---|
[aad2af5] | 214 | while (vals[0] && j > 1) { |
---|
| 215 | char **x; |
---|
[d1b1380] | 216 | |
---|
[aad2af5] | 217 | vars[j] = NULL; |
---|
| 218 | |
---|
[9ff87ea] | 219 | x = ini_read(fh_list, vals[0], (const char **)vars); |
---|
[aad2af5] | 220 | if (!x) { |
---|
| 221 | free(vals); |
---|
| 222 | vals = NULL; |
---|
| 223 | break; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | /*{int i; printf("[%s]\n",vals[0]);for(i=0;vars[i];i++)printf("%d:%s\"%s\"\n",i,vars[i],vals[i]?vals[i]:"(null)");}*/ |
---|
| 227 | |
---|
| 228 | free(vals[0]); |
---|
| 229 | vals[0] = x[0]; |
---|
[d1b1380] | 230 | |
---|
[a420b49] | 231 | for (i = 1, j = 1; vars[i]; i++) { |
---|
[eb18f4d] | 232 | /* printf("%s: %s %d\n",vars[i],vals[i]?vals[i]:"(null)",to[i]);*/ |
---|
[aad2af5] | 233 | if (x[i]) { |
---|
| 234 | if (x) vals[to[i]] = x[i]; |
---|
[eb18f4d] | 235 | } else { |
---|
| 236 | vars[j] = vars[i]; |
---|
| 237 | to[j] = to[i]; |
---|
| 238 | j++; |
---|
| 239 | } |
---|
[d1b1380] | 240 | } |
---|
[eb18f4d] | 241 | free(x); |
---|
| 242 | } |
---|
[aad2af5] | 243 | |
---|
| 244 | free(vals[0]); |
---|
| 245 | vals[0] = NULL; |
---|
[d1b1380] | 246 | free(vars); |
---|
| 247 | free(to); |
---|
| 248 | |
---|
| 249 | return vals; |
---|
| 250 | } |
---|