All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DBStatementCatalog.cpp
Go to the documentation of this file.
1 /*
2  * DBStatementCatalog.cpp
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <sys/types.h>
28 #include <dirent.h>
29 #include <errno.h>
30 
31 #include <fstream>
32 
33 using namespace std;
34 
35 namespace HSEPData {
36 
37  DBStatementCatalog* DBStatementCatalog::aInstance = nullptr;
38 
39  bool DBStatementCatalog::load() {
40 
41  bool vResult = false;
42 
43  DIR* vDir;
44  dirent* vDirEnt;
45  char vName[256+1];
46  string vFilePath;
47  string vLine;
48  string vValue;
49  fstream vFile;
50 
51  if ((vDir = opendir (aPath.c_str())) != nullptr) {
52  while ((vDirEnt = readdir (vDir)) != nullptr) {
53  size_t vLen = strlen(vDirEnt->d_name);
54  if (vLen > 4) {
55  if (strcmp(vDirEnt->d_name+vLen-4,".sql")==0) {
56  strncpy(vName,vDirEnt->d_name,vLen-4);
57  vName[vLen-4] = '\0';
58 
59  vFilePath = aPath;
60  vFilePath.append("/");
61  vFilePath.append(vDirEnt->d_name);
62 
63  vFile.open(vFilePath.c_str(),ios::in); // take care, must be read-only
64  vValue.clear();
65 
66  while (getline(vFile,vLine)) {
67  vValue.append(vLine);
68  vValue.append("\n");
69  vLine.clear();
70  }
71  vFile.close();
72 
73  aCatalog.put(vName,vValue);
74  aValid = true;
75  }
76  }
77  }
78  closedir(vDir);
79  }
80 
81  return vResult;
82 
83  } // DBStatementCatalog::load
84 
85 } // HSEPData namespace
86 
87 
88 
89