[ec62bb]: corelib / XMLprefs.cpp  Maximize  Restore  History

Download this file

568 lines (479 with data), 14.7 kB

  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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
* Copyright (c) 2003-2006 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license.php
*/
// XMLprefs.cpp : implementation file
//
#include "..\stdafx.h"
#include <atlcomcli.h> // needed for VS7.1, not 8
#include "XMLprefs.h"
#include "xml_import.h"
#include "MyString.h"
#include "PWSfile.h"
#include "corelib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CXMLprefs
void CXMLprefs::SetKeepXMLLock(bool state)
{
m_bKeepXMLLock = state;
// If a big update - load XML when set to true and unload it at the end
if (m_bKeepXMLLock)
LoadXML();
else {
// Save disabled when doing a big read/update - need to do it now.
SaveXML();
UnloadXML();
}
}
// get a int value
int CXMLprefs::Get(const CString &csBaseKeyName, const CString &csValueName,
const int &iDefaultValue)
{
/*
Since XML is text based and we have no schema, just convert to a string and
call the GetSettingString method.
*/
int iRetVal = iDefaultValue;
CString csDefaultValue;
csDefaultValue.Format(_T("%d"), iRetVal);
iRetVal = _ttoi(Get(csBaseKeyName, csValueName, csDefaultValue));
return iRetVal;
}
// get a string value
CString CXMLprefs::Get(const CString &csBaseKeyName, const CString &csValueName,
const CString &csDefaultValue)
{
int iNumKeys = 0;
CString csValue = csDefaultValue;
CString* pcsKeys = NULL;
// Add the value to the base key separated by a '\'
CString csKeyName(csBaseKeyName);
csKeyName += _T("\\");
csKeyName += csValueName;
// Parse all keys from the base key name (keys separated by a '\')
pcsKeys = ParseKeys(csKeyName, iNumKeys);
// Traverse the xml using the keys parsed from the base key name to find the correct node
if (pcsKeys) {
if (LoadXML()) { // load the xml document
MSXML2::IXMLDOMElementPtr rootElem = NULL;
MSXML2::IXMLDOMNodePtr foundNode = NULL;
m_pXMLDoc->get_documentElement(&rootElem); // root node
if (rootElem) {
// returns the last node in the chain
foundNode = FindNode(rootElem, pcsKeys, iNumKeys);
if (foundNode) {
// get the text of the node (will be the value we requested)
BSTR bstr = NULL;
foundNode->get_text(&bstr);
csValue = (CString)bstr;
if (bstr) {
SysFreeString(bstr);
bstr = NULL;
}
foundNode = NULL;
}
rootElem = NULL;
}
UnloadXML(); // dump the XML document
}
delete [] pcsKeys;
pcsKeys = NULL;
}
return csValue;
}
// set a int value
int CXMLprefs::Set(const CString &csBaseKeyName, const CString &csValueName,
const int &iValue)
{
/*
Since XML is text based and we have no schema, just convert to a string and
call the SetSettingString method.
*/
int iRetVal = 0;
CString csValue = _T("");
csValue.Format(_T("%d"), iValue);
iRetVal = Set(csBaseKeyName, csValueName, csValue);
return iRetVal;
}
// set a string value
int CXMLprefs::Set(const CString &csBaseKeyName, const CString &csValueName,
const CString &csValue)
{
int iRetVal = XML_SUCCESS;
int iNumKeys = 0;
CString* pcsKeys = NULL;
// Add the value to the base key separated by a '\'
CString csKeyName(csBaseKeyName);
csKeyName += _T("\\");
csKeyName += csValueName;
// Parse all keys from the base key name (keys separated by a '\')
pcsKeys = ParseKeys(csKeyName, iNumKeys);
// Traverse the xml using the keys parsed from the base key name to find the correct node
if (pcsKeys) {
if (LoadXML()) { // load the xml document
MSXML2::IXMLDOMElementPtr rootElem = NULL;
MSXML2::IXMLDOMNodePtr foundNode = NULL;
m_pXMLDoc->get_documentElement(&rootElem); // root node
if (rootElem) {
// returns the last node in the chain
foundNode = FindNode(rootElem, pcsKeys, iNumKeys, TRUE);
if (foundNode) {
// set the text of the node (will be the value we sent)
if (SUCCEEDED(foundNode->put_text(_bstr_t(csValue)))) {
if (!SaveXML()) {
iRetVal = XML_SAVE_FAILED; // save the changed XML
}
} else
iRetVal = XML_PUT_TEXT_FAILED;
foundNode = NULL;
} else
iRetVal = XML_NODE_NOT_FOUND;
rootElem = NULL;
}
UnloadXML(); // dump the XML document
}
else
iRetVal = XML_LOAD_FAILED;
delete [] pcsKeys;
pcsKeys = NULL;
}
return iRetVal;
}
// delete a key or chain of keys
BOOL CXMLprefs::DeleteSetting(const CString &csBaseKeyName, const CString &csValueName)
{
BOOL bRetVal = FALSE;
int iNumKeys = 0;
CString* pcsKeys = NULL;
CString csKeyName(csBaseKeyName);
if (!csValueName.IsEmpty()) {
csKeyName += _T("\\");
csKeyName += csValueName;
}
// Parse all keys from the base key name (keys separated by a '\')
pcsKeys = ParseKeys(csKeyName, iNumKeys);
// Traverse the xml using the keys parsed from the base key name to find the correct node.
if (pcsKeys) {
if (LoadXML()) { // load the xml document
MSXML2::IXMLDOMElementPtr rootElem = NULL;
MSXML2::IXMLDOMNodePtr foundNode = NULL;
m_pXMLDoc->get_documentElement(&rootElem); // root node
if (rootElem) {
// returns the last node in the chain
foundNode = FindNode(rootElem, pcsKeys, iNumKeys);
if (foundNode) {
// get the parent of the found node and use removeChild to delete the found node
MSXML2::IXMLDOMNodePtr parentNode = NULL;
foundNode->get_parentNode(&parentNode);
if (parentNode) {
if (SUCCEEDED(parentNode->removeChild(foundNode, NULL))) {
if (SaveXML()) {
bRetVal = TRUE; // save the changed XML
}
}
parentNode = NULL;
}
foundNode = NULL;
}
rootElem = NULL;
}
UnloadXML(); // dump the XML document
}
delete [] pcsKeys;
pcsKeys = NULL;
}
return bRetVal;
}
// Parse all keys from the base key name.
CString* CXMLprefs::ParseKeys(const CString &csFullKeyPath, int &iNumKeys)
{
CString* pcsKeys = NULL;
// replace spaces with _ since xml doesn't like them
CString csFKP(csFullKeyPath);
csFKP.Replace(_T(' '), _T('_'));
if (csFKP.GetAt(csFKP.GetLength() - 1) == _T('\\'))
csFKP.TrimRight(_T('\\')); // remove slashes on the end
CString csTemp(csFKP);
iNumKeys = csTemp.Remove(_T('\\')) + 1; // get a count of slashes
pcsKeys = new CString[iNumKeys]; // create storage for the keys
if (pcsKeys) {
int iFind = 0, iLastFind = 0, iCount = -1;
// get all of the keys in the chain
while (iFind != -1) {
iFind = csFKP.Find(_T("\\"), iLastFind);
if (iFind > -1) {
iCount++;
pcsKeys[iCount] = csFKP.Mid(iLastFind, iFind - iLastFind);
iLastFind = iFind + 1;
} else {
// make sure we don't just discard the last key in the chain
if (iLastFind < csFKP.GetLength()) {
iCount++;
pcsKeys[iCount] = csFKP.Right(csFKP.GetLength() - iLastFind);
}
}
}
}
return pcsKeys;
}
// load the XML file into the parser
BOOL CXMLprefs::LoadXML()
{
// Already loaded?
if (m_bXMLLoaded) return TRUE;
// Couldn't get it to work previously?
if (m_MSXML_Version == -1) return FALSE;
// No point continuing if we can't get the lock!
CMyString locker(_T(""));
if (!PWSfile::LockFile(m_csConfigFile, locker, false))
return FALSE;
BOOL b_OK = FALSE;
// initialize the XML parser
switch (m_MSXML_Version) {
case 0:
// First time through!
// Try 60
if (FAILED(m_pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_ALL))) {
// Try 40
if (FAILED(m_pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40), NULL, CLSCTX_ALL))) {
// Try 30
if (FAILED(m_pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30), NULL, CLSCTX_ALL))) {
AfxMessageBox(IDSC_NOXMLREADER, MB_OK);
m_MSXML_Version = -1;
return FALSE;
} else {
m_MSXML_Version = 30;
}
} else {
m_MSXML_Version = 40;
}
} else {
m_MSXML_Version = 60;
}
b_OK = TRUE;
break;
case 60:
if (SUCCEEDED(m_pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_ALL)))
b_OK = TRUE;
break;
case 40:
if (SUCCEEDED(m_pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40), NULL, CLSCTX_ALL)))
b_OK = TRUE;
break;
case 30:
if (SUCCEEDED(m_pXMLDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30), NULL, CLSCTX_ALL)))
b_OK = TRUE;
break;
default:
// Should never get here
ASSERT(0);
}
if (b_OK == FALSE)
return FALSE;
// Ensure we preserve all white space!
if (FAILED(m_pXMLDoc->put_preserveWhiteSpace(VARIANT_TRUE))) {
UnloadXML();
return FALSE;
}
VARIANT_BOOL vbSuccessful;
// see if the file exists
CFile file;
if (!file.Open(m_csConfigFile, CFile::modeReadWrite)) { // if not
// create it - IDSC_XMLHEADER
CComBSTR bstrXML;
bstrXML.LoadString(IDSC_XMLHEADER);
m_pXMLDoc->loadXML(bstrXML, &vbSuccessful);
} else { // if so
file.Close();
// load it
m_pXMLDoc->load(CComVariant::CComVariant((LPCTSTR)m_csConfigFile), &vbSuccessful);
}
m_bXMLLoaded = (vbSuccessful == VARIANT_TRUE);
if (m_bXMLLoaded)
return TRUE; // loaded
// an XML load error occurred so display the reason
MSXML2::IXMLDOMParseErrorPtr pIParseError = NULL;
m_pXMLDoc->get_parseError(&pIParseError);
if (pIParseError) {
long value, line, linepos;
BSTR bstr = NULL;
pIParseError->get_errorCode(&value);
pIParseError->get_reason(&bstr);
pIParseError->get_line(&line);
pIParseError->get_linepos(&linepos);
CString csMessage;
csMessage.Format(IDSC_XMLFILEERROR,
value, line, linepos, (char *)_bstr_t(bstr, TRUE));
CString cs_title;
cs_title.LoadString(IDSC_XMLLOADFAILURE);
MessageBox(NULL, csMessage, cs_title, MB_OK);
if (bstr) {
SysFreeString(bstr);
bstr = NULL;
}
pIParseError = NULL;
}
if (m_pXMLDoc != NULL) {
m_pXMLDoc.Release();
m_pXMLDoc = NULL;
}
PWSfile::UnlockFile(m_csConfigFile, false);
return FALSE;
}
void CXMLprefs::UnloadXML()
{
if (!m_bXMLLoaded || m_bKeepXMLLock)
return;
if (m_pXMLDoc != NULL) {
m_pXMLDoc.Release();
m_pXMLDoc = NULL;
}
m_bXMLLoaded = false;
PWSfile::UnlockFile(m_csConfigFile, false);
}
// save the XML file
BOOL CXMLprefs::SaveXML()
{
// If we are keeping the lock - save when we free it
if (m_bKeepXMLLock)
return TRUE;
// Now try to save!
if (SUCCEEDED(m_pXMLDoc->save(CComVariant::CComVariant(m_csConfigFile))))
return TRUE;
else
return FALSE;
}
void CXMLprefs::ReformatAndSave()
{
if (LoadXML() == FALSE)
return;
CString csConfigData;
IStream *pIStream;
STATSTG mStat;
VARIANT_BOOL vbSuccessful;
ULONG num;
// Get the string from the DOM
m_pXMLDoc->QueryInterface(IID_IStream, (void **)&pIStream);
pIStream->Stat(&mStat, 0);
const int ilen = (int)mStat.cbSize.QuadPart + 1;
TCHAR *lpszBuffer = csConfigData.GetBuffer(ilen);
pIStream->Read(lpszBuffer, ilen, &num);
csConfigData.ReleaseBuffer(num);
pIStream->Release();
// First remove all tabs, carriage returns and line-ends
csConfigData.Remove(_T('\t'));
csConfigData.Remove(_T('\r'));
csConfigData.Remove(_T('\n'));
// Define and then create the SAX reader and DOM writer.
MSXML2::ISAXXMLReaderPtr pSAXReader = NULL;
MSXML2::IMXWriterPtr pXMLWriter = NULL;
BOOL b_R_OK, b_W_OK;
b_R_OK = b_W_OK = FALSE;
switch (m_MSXML_Version) {
case 60:
if (SUCCEEDED(pSAXReader.CreateInstance(__uuidof(MSXML2::SAXXMLReader60),
NULL, CLSCTX_ALL)))
b_R_OK = TRUE;
if (SUCCEEDED(pXMLWriter.CreateInstance(__uuidof(MSXML2::MXXMLWriter60),
NULL, CLSCTX_ALL)))
b_W_OK = TRUE;
break;
case 40:
if (SUCCEEDED(pSAXReader.CreateInstance(__uuidof(MSXML2::SAXXMLReader40),
NULL, CLSCTX_ALL)))
b_R_OK = TRUE;
if (SUCCEEDED(pXMLWriter.CreateInstance(__uuidof(MSXML2::MXXMLWriter40),
NULL, CLSCTX_ALL)))
b_W_OK = TRUE;
break;
case 30:
if (SUCCEEDED(pSAXReader.CreateInstance(__uuidof(MSXML2::SAXXMLReader30),
NULL, CLSCTX_ALL)))
b_R_OK = TRUE;
if (SUCCEEDED(pXMLWriter.CreateInstance(__uuidof(MSXML2::MXXMLWriter30),
NULL, CLSCTX_ALL)))
b_W_OK = TRUE;
break;
default:
// Should never get here
ASSERT(0);
}
// Check created OK
ASSERT(b_R_OK && b_W_OK);
// Say we want it indented
pXMLWriter->put_indent(VARIANT_TRUE);
pXMLWriter->put_standalone(VARIANT_TRUE);
pXMLWriter->put_encoding(CComBSTR(L"UTF-8"));
// Create a reader ContentHandler and make it the writer
MSXML2::ISAXContentHandlerPtr pCH = pXMLWriter;
pSAXReader->putContentHandler(pCH);
// Parse the current XML and then reload it once reformatted
// But first convert from CString to VARIANT!
VARIANT vDocString;
vDocString.vt = VT_BSTR;
vDocString.bstrVal = csConfigData.AllocSysString();
pSAXReader->parse(vDocString);
VARIANT vNewDocString;
pXMLWriter->get_output(&vNewDocString);
m_pXMLDoc->loadXML(vNewDocString.bstrVal, &vbSuccessful);
ASSERT(vbSuccessful == VARIANT_TRUE);
// Free memory
SysFreeString(vDocString.bstrVal);
// Now free reader & writer (content handler is done automatically)
if (pXMLWriter != NULL) {
pXMLWriter.Release();
pXMLWriter = NULL;
pCH = NULL;
}
if (pSAXReader != NULL) {
pSAXReader.Release();
pSAXReader = NULL;
}
VERIFY(SaveXML() == TRUE);
UnloadXML();
}
// find a node given a chain of key names
MSXML2::IXMLDOMNodePtr CXMLprefs::FindNode(MSXML2::IXMLDOMNodePtr parentNode,
CString* pcsKeys, int iNumKeys,
bool bAddNodes /*= false*/)
{
MSXML2::IXMLDOMNodePtr foundNode = NULL;
MSXML2::IXMLDOMElementPtr rootElem = NULL, tempElem = NULL;
m_pXMLDoc->get_documentElement(&rootElem); // root element
for (int i=0; i<iNumKeys; i++) {
// find the node named X directly under the parent
HRESULT hr = parentNode->selectSingleNode(_bstr_t(pcsKeys[i]), &foundNode);
if (FAILED(hr) || foundNode == NULL) {
// if its not found...
if (bAddNodes) { // create the node and append to parent (Set only)
m_pXMLDoc->createElement(_bstr_t(pcsKeys[i]), &tempElem);
if (tempElem) {
parentNode->appendChild(tempElem, &foundNode);
}
// since we are traversing the nodes, we need to set the parentNode to our foundNode
parentNode = NULL;
parentNode = foundNode;
foundNode = NULL;
} else {
foundNode = NULL;
parentNode = NULL;
break;
}
} else {
// since we are traversing the nodes, we need to set the parentNode to our foundNode
parentNode = NULL;
parentNode = foundNode;
foundNode = NULL;
}
}
rootElem = NULL;
return parentNode;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks