-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCXMLDb.cpp
193 lines (152 loc) · 3.67 KB
/
CXMLDb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (c) 2012 Alexandros Nikolopoulos <[email protected]>
*
* 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.
*/
#include "CXMLDb.h"
CXMLDb::CXMLDb(map<string, string> settings)
{
m_FileName = settings["filename"];
m_BackupFile = 0;
pthread_mutex_init(&FileMutex, NULL);
}
CXMLDb::~CXMLDb()
{
}
int
CXMLDb::Connect(void)
{
return LoadBackupFile();
}
int
CXMLDb::Insert(DataContainer &Data)
{
list<map<string, string> > DataList;
DataList.push_back(Data);
return Insert(DataList);
}
int
CXMLDb::Insert(DataContainerList &DataList)
{
pthread_mutex_lock(&FileMutex);
if(!LoadBackupFile())
{
Log.error("Unable to Load XML file\n");
pthread_mutex_unlock(&FileMutex);
return false;
}
while (!DataList.empty())
{
DataContainer Data = DataList.front();
XMLNode *ProbeData = m_BackupFile->NewElement("pd");
for( DataContainer::iterator ii=Data.begin(); ii!=Data.end(); ++ii)
{
string name = (*ii).first;
string value = (*ii).second;
ProbeData->ToElement()->SetAttribute(name.c_str(),value.c_str());
}
m_BackupFile->FirstChildElement()->InsertEndChild(ProbeData);
DataList.pop_front();
}
if(!SaveBackupFile())
{
Log.error("Unable to save XML file\n");
pthread_mutex_unlock(&FileMutex);
return false;
}
pthread_mutex_unlock(&FileMutex);
return true;
}
int
CXMLDb:: Restore(DataContainer &Data)
{
return false; // not implemented
}
// Restores the cached entries to the database. It's spawned off as a seperate
int
CXMLDb::Restore(DataContainerList &DataList, unsigned int maxRows)
{
pthread_mutex_lock(&FileMutex);
if(!LoadBackupFile())
{
syslog(LOG_ERR, "Error opening Backup file\n");
pthread_mutex_unlock(&FileMutex);
return false;
}
XMLElement *CurrentElement = m_BackupFile->FirstChildElement()->FirstChildElement("pd");
// file is empty. Return true
if(!CurrentElement)
{
pthread_mutex_unlock(&FileMutex);
return true;
}
std::list<struct XMLElement*> XmlList;
while(CurrentElement && DataList.size() < maxRows)
{
DataContainer CurrentData;
const XMLAttribute *CurrentAtt = CurrentElement->FirstAttribute();
while(CurrentAtt)
{
string Name = CurrentAtt->Name();
string Value = CurrentAtt->Value();
CurrentData[Name] = Value;
CurrentAtt = CurrentAtt->Next();
}
DataList.push_back(CurrentData);
XmlList.push_back(CurrentElement);
CurrentElement = CurrentElement->NextSiblingElement();
}
while(!XmlList.empty())
{
XMLElement *Element2Delete = XmlList.front();
m_BackupFile->FirstChildElement()->DeleteChild(Element2Delete);
XmlList.pop_front();
}
SaveBackupFile();
pthread_mutex_unlock(&FileMutex);
return true;
}
int
CXMLDb::LoadBackupFile(void)
{
if(m_BackupFile)
return true;
m_BackupFile = new XMLDocument();
if(m_BackupFile->LoadFile(m_FileName.c_str()) != XML_NO_ERROR)
{
if(m_BackupFile->ErrorID() == XML_ERROR_FILE_NOT_FOUND)
return CreateBackupFile();
else
{
syslog(LOG_ERR, "Error Opening backup.xml: %d\n",
m_BackupFile->ErrorID());
delete m_BackupFile;
m_BackupFile = 0;
return false;
}
}
return true;
}
int
CXMLDb::SaveBackupFile(void)
{
if(!m_BackupFile)
return false;
if(m_BackupFile->SaveFile(m_FileName.c_str()))
return false;
return true;
}
int
CXMLDb::CreateBackupFile(void)
{
if(!m_BackupFile)
return false;
m_BackupFile->InsertEndChild(m_BackupFile->NewDeclaration(NULL));
m_BackupFile->InsertEndChild(m_BackupFile->NewElement("Backup"));
if(!m_BackupFile->SaveFile(m_FileName.c_str()))
return false;
return true;
}