| 1 |
/* |
| 2 |
Copyright (C) 2002 Kai Sterker <[email protected]> |
| 3 |
Part of the Adonthell Project <http://adonthell.nongnu.org> |
| 4 |
|
| 5 |
Dlgedit 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. |
| 9 |
|
| 10 |
Dlgedit is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with Dlgedit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* @file cfg_io.cc |
| 21 |
* |
| 22 |
* @author Kai Sterker |
| 23 |
* @brief I/O Routines for dlgedit configuration file |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <cstring> |
| 28 |
#include <cstdlib> |
| 29 |
#include <glib/gstdio.h> |
| 30 |
#include "cfg_io.h" |
| 31 |
#include "dlg_types.h" |
| 32 |
|
| 33 |
// The config file opened by the lexical scanner |
| 34 |
extern FILE* loadcfgin; |
| 35 |
|
| 36 |
extern int parse_cfgfile (std::string&, int&); |
| 37 |
|
| 38 |
// ctor; load config |
| 39 |
CfgIO::CfgIO () |
| 40 |
{ |
| 41 |
#if defined(__APPLE__) |
| 42 |
// OSX |
| 43 |
Dlgeditrc = std::string (getenv ("HOME")) + "/Library/Application Support/Adonthell/dlgeditrc"; |
| 44 |
#elif defined (WIN32) |
| 45 |
// Windows |
| 46 |
char *appDataDir = getenv ("APPDATA"); |
| 47 |
if (appDataDir != NULL && strlen (appDataDir) > 0) |
| 48 |
Dlgeditrc = std::string (getenv("APPDATA")) + "/Adonthell/dlgeditrc"; |
| 49 |
else |
| 50 |
Dlgeditrc = "./dlgeditrc"; |
| 51 |
#else |
| 52 |
// Unix |
| 53 |
char *xdgDir = getenv ("XDG_CONFIG_HOME"); |
| 54 |
if (xdgDir != NULL && strlen (xdgDir) > 0) |
| 55 |
Dlgeditrc = std::string (xdgDir) + "/adonthell/dlgeditrc"; |
| 56 |
else |
| 57 |
Dlgeditrc = std::string (getenv ("HOME")) + "/.config/adonthell/dlgeditrc"; |
| 58 |
#endif |
| 59 |
|
| 60 |
// loadcfgin is declared in lex.loadcfg.cc |
| 61 |
loadcfgin = g_fopen (Dlgeditrc.c_str (), "rb"); |
| 62 |
|
| 63 |
// open succeeded -> read configuration |
| 64 |
if (loadcfgin) |
| 65 |
{ |
| 66 |
load (); |
| 67 |
|
| 68 |
// cleanup |
| 69 |
fclose (loadcfgin); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// dtor; save config |
| 74 |
CfgIO::~CfgIO () |
| 75 |
{ |
| 76 |
save (); |
| 77 |
} |
| 78 |
|
| 79 |
// load config |
| 80 |
void CfgIO::load () |
| 81 |
{ |
| 82 |
int token = 1, n; |
| 83 |
std::string s; |
| 84 |
|
| 85 |
// as long as reading something from file ... |
| 86 |
while (token) |
| 87 |
{ |
| 88 |
// get next token |
| 89 |
switch (token = parse_cfgfile (s, n)) |
| 90 |
{ |
| 91 |
case LOAD_PROJECT: |
| 92 |
{ |
| 93 |
if (parse_cfgfile (s, n) == LOAD_STR) Data.addProject (s); |
| 94 |
break; |
| 95 |
} |
| 96 |
|
| 97 |
default: break; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
// save config |
| 105 |
void CfgIO::save () |
| 106 |
{ |
| 107 |
// open file for writing |
| 108 |
std::ofstream out (Dlgeditrc.c_str ()); |
| 109 |
|
| 110 |
// opening failed for some reasons |
| 111 |
if (!out) return; |
| 112 |
|
| 113 |
// write header |
| 114 |
out << "# Adonthell Dialogue Editor configuration file\n" << std::endl; |
| 115 |
|
| 116 |
// save data |
| 117 |
Data.save (out); |
| 118 |
} |