| 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_data.cc |
| 21 |
* |
| 22 |
* @author Kai Sterker |
| 23 |
* @brief dlgedit configuration data |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <algorithm> |
| 28 |
#include <sys/stat.h> |
| 29 |
#include <glib/gstdio.h> |
| 30 |
#include <dirent.h> |
| 31 |
#include "cfg_data.h" |
| 32 |
#include "dlg_cmdline.h" |
| 33 |
|
| 34 |
/** |
| 35 |
* Global access to the dlgedit configuration |
| 36 |
*/ |
| 37 |
CfgData* CfgData::data = NULL; |
| 38 |
|
| 39 |
// ctor |
| 40 |
CfgData::CfgData () |
| 41 |
{ |
| 42 |
// make this configuration globally available |
| 43 |
data = this; |
| 44 |
} |
| 45 |
|
| 46 |
// dtor |
| 47 |
CfgData::~CfgData () |
| 48 |
{ |
| 49 |
data = NULL; |
| 50 |
|
| 51 |
// delete all projects |
| 52 |
for (std::vector<CfgProject*>::iterator i = Projects.begin (); i != Projects.end (); i++) |
| 53 |
delete *i; |
| 54 |
} |
| 55 |
|
| 56 |
// add entry to list of projects |
| 57 |
void CfgData::addProject (const std::string &project) |
| 58 |
{ |
| 59 |
std::vector<CfgProject*>::iterator i; |
| 60 |
|
| 61 |
// check whether the project already exists |
| 62 |
for (i = Projects.begin (); i != Projects.end (); i++) |
| 63 |
if ((*i)->name () == project) break; |
| 64 |
|
| 65 |
// if exists -> overwrite |
| 66 |
if (i != Projects.end ()) (*i)->load (); |
| 67 |
|
| 68 |
// otherwise create a new one |
| 69 |
else |
| 70 |
{ |
| 71 |
CfgProject *p = new CfgProject (project); |
| 72 |
|
| 73 |
// try to load it |
| 74 |
if (p->load ()) Projects.push_back (p); |
| 75 |
else delete p; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// get basedir associated with a certain project |
| 80 |
std::string CfgData::getBasedir (const std::string & project) |
| 81 |
{ |
| 82 |
std::vector<CfgProject*>::iterator i; |
| 83 |
|
| 84 |
// try to locate project |
| 85 |
for (i = Projects.begin (); i != Projects.end (); i++) |
| 86 |
if ((*i)->name () == project) |
| 87 |
return (*i)->basedir (); |
| 88 |
|
| 89 |
return ""; |
| 90 |
} |
| 91 |
|
| 92 |
// set basedir of a certain project |
| 93 |
void CfgData::setBasedir (const std::string & project, const std::string & basedir) |
| 94 |
{ |
| 95 |
std::vector<CfgProject*>::iterator i; |
| 96 |
|
| 97 |
// try to locate project |
| 98 |
for (i = Projects.begin (); i != Projects.end (); i++) |
| 99 |
if ((*i)->name () == project) |
| 100 |
{ |
| 101 |
(*i)->setBasedir (basedir); |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
// project does not exist, so create it |
| 106 |
CfgProject *p = new CfgProject (project); |
| 107 |
p->setBasedir (basedir); |
| 108 |
Projects.push_back (p); |
| 109 |
} |
| 110 |
|
| 111 |
// return list of projects available in given data directory |
| 112 |
std::vector<std::string> CfgData::projectsFromDatadir () |
| 113 |
{ |
| 114 |
struct dirent * d; |
| 115 |
struct stat statbuf; |
| 116 |
DIR *mydir = opendir (DlgCmdline::datadir.c_str()); |
| 117 |
std::string name, filename, path = DlgCmdline::datadir + "/"; |
| 118 |
std::vector<std::string> projects; |
| 119 |
|
| 120 |
// default project |
| 121 |
projects.push_back (std::string ("none")); |
| 122 |
|
| 123 |
// no such directory |
| 124 |
if (!mydir) return projects; |
| 125 |
|
| 126 |
// get all directories inside |
| 127 |
while ((d = readdir (mydir)) != NULL) |
| 128 |
{ |
| 129 |
name = d->d_name; |
| 130 |
filename = path + name; |
| 131 |
|
| 132 |
// ignore '.' and '..' directories |
| 133 |
if (name != "." && name != "..") |
| 134 |
{ |
| 135 |
stat (filename.c_str (), &statbuf); |
| 136 |
|
| 137 |
// fill vector with valid entries |
| 138 |
if (S_ISDIR (statbuf.st_mode)) |
| 139 |
projects.push_back (name); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// cleanup |
| 144 |
closedir (mydir); |
| 145 |
|
| 146 |
return projects; |
| 147 |
} |
| 148 |
|
| 149 |
// save configuration data |
| 150 |
void CfgData::save (std::ofstream &out) |
| 151 |
{ |
| 152 |
// save list of projects |
| 153 |
for (std::vector<CfgProject*>::iterator i = Projects.begin (); i != Projects.end (); i++) |
| 154 |
(*i)->save (out); |
| 155 |
} |