/* * dbMetrix Database Tool v0.1 * Copyright (c) 1998 David E. Storey * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include #include "global.h" #define DBX_PREF_DIR ".dbMetrix" #define DBX_PREFERENCE_FILE "prefs" typedef enum { dx_boolean, dx_int, dx_string } pref_types; typedef struct { char *key; pref_types type; void *data; } preferences; preferences Preferences[] = { {"dstree.create.save_password", dx_boolean, (void *)FALSE}, {"dstree.table.show_fields", dx_boolean, (void *)TRUE}, {"query.result.output_window", dx_boolean, (void *)TRUE} }; int nPreferences = sizeof(Preferences)/sizeof(Preferences[0]); char pref_dir[1024] = "\0"; char pref_file[1024] = "\0"; /* prototypes */ int pref_load(); int pref_save(); void * pref_get(char *key) { int i = 0; while (i < nPreferences && strcmp(key, Preferences[i].key)) i++; if (i < nPreferences) return(Preferences[i].data); return(NULL); } int pref_put(char *key, void *data) { int i = 0; while (i < nPreferences && strcmp(key, Preferences[i].key)) i++; if (i < nPreferences) { Preferences[i].data = data; return(TRUE); } return(FALSE); } void pref_init(int argc, char **argv) { struct passwd *pwent; DIR *dirent; pwent = getpwuid(getuid()); g_snprintf(pref_dir, 1024, "%s/%s", pwent->pw_dir, DBX_PREF_DIR); g_snprintf(pref_file, 1024, "%s/%s", pref_dir, DBX_PREFERENCE_FILE); dirent = opendir(pref_dir); if (!dirent) { if (errno == ENOENT && (mkdir(pref_dir, 0700) != -1)); pref_load(); } else closedir(dirent); } int pref_load() { FILE *fd = NULL; if ((fd = fopen(pref_file, "r"))) { char instr[1024]; while(fgets(instr, 1024, fd)) { char *key; char *data; int i = 0; int n = strlen(instr); if (instr[0] == '#' || instr[0] == '\n') continue; key = instr; while (i < n) { if (instr[i] == '=') { int j = 0; instr[i] = '\0'; data = &instr[i+1]; while (j < nPreferences && strcmp(key, Preferences[j].key)) j++; if (j < nPreferences) { switch(Preferences[j].type) { case dx_boolean: Preferences[j].data = (void *)(data[0] == 'T'); break; case dx_int: Preferences[j].data = (void *)strtol(data, NULL, 10); break; case dx_string: break; } } continue; } i++; } } fclose(fd); return(TRUE); } return(FALSE); } int pref_save() { FILE *fd = NULL; if ((fd = fopen(pref_file, "w"))) { int i; for (i = 0; i < nPreferences; i++) { switch(Preferences[i].type) { case dx_boolean: fprintf(fd, "%s=%s\n", Preferences[i].key, Preferences[i].data ? "True" : "False"); break; case dx_int: fprintf(fd, "%s=%d\n", Preferences[i].key, (int)Preferences[i].data); break; case dx_string: fprintf(fd, "%s=%s\n", Preferences[i].key, (char *)Preferences[i].data); break; } } fclose(fd); } return(TRUE); }