Debian Codegraph @

CodeLite/archive.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2008 by Eran Ifrah
5 // file name : archive.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25 #include "archive.h"
26
27 #include "clFontHelper.h"
28 #include "serialized_object.h"
29 #include "wx_xml_compatibility.h"
30
31 #include <memory>
32 #include <wx/colour.h>
33 #include <wx/xml/xml.h>
34
35 namespace
36 {
37 const wxChar breakpointName[] = wxT("Breakpoint");
38 const wxChar breakpointArrayName[] = wxT("BreakpointArray");
39 } // namespace
40
41 // helper functions
42 static wxXmlNode* FindNodeByName(const wxXmlNode* parent, const wxString& tagName, const wxString& name)
43 {
44 if(!parent) {
45 return NULL;
46 }
47
48 wxXmlNode* child = parent->GetChildren();
49 while(child) {
50 if(child->GetName() == tagName) {
51 if(child->GetAttribute(wxT("Name"), wxEmptyString) == name) {
52 return child;
53 }
54 }
55 child = child->GetNext();
56 }
57 return NULL;
58 }
59
60 static void SetNodeContent(wxXmlNode* node, const wxString& text)
61 {
62 wxXmlNode* n = node->GetChildren();
63 wxXmlNode* contentNode = NULL;
64 while(n) {
65 if(n->GetType() == wxXML_TEXT_NODE || n->GetType() == wxXML_CDATA_SECTION_NODE) {
66 contentNode = n;
67 break;
68 }
69 n = n->GetNext();
70 }
71
72 if(contentNode) {
73 // remove old node
74 node->RemoveChild(contentNode);
75 delete contentNode;
76 }
77
78 if(!text.IsEmpty()) {
79 contentNode = new wxXmlNode(wxXML_TEXT_NODE, wxEmptyString, text);
80 node->AddChild(contentNode);
81 }
82 }
83
84 static void SetCDATANodeContent(wxXmlNode* node, const wxString& text)
85 {
86 wxXmlNode* n = node->GetChildren();
87 wxXmlNode* contentNode = NULL;
88 while(n) {
89 if(n->GetType() == wxXML_TEXT_NODE || n->GetType() == wxXML_CDATA_SECTION_NODE) {
90 contentNode = n;
91 break;
92 }
93 n = n->GetNext();
94 }
95
96 if(contentNode) {
97 // remove old node
98 node->RemoveChild(contentNode);
99 delete contentNode;
100 }
101
102 if(!text.IsEmpty()) {
103 contentNode = new wxXmlNode(wxXML_CDATA_SECTION_NODE, wxEmptyString, text);
104 node->AddChild(contentNode);
105 }
106 }
107
108 // class Tab info
109 TabInfo::TabInfo()
110 : m_firstVisibleLine(0)
111 , m_currentLine(0)
112 {
113 }
114
115 TabInfo::~TabInfo() {}
116
117 void TabInfo::DeSerialize(Archive& arch)
118 {
119 arch.Read(wxT("FileName"), m_fileName);
120 arch.Read(wxT("FirstVisibleLine"), m_firstVisibleLine);
121 arch.Read(wxT("CurrentLine"), m_currentLine);
122 arch.Read(wxT("Bookmarks"), m_bookmarks);
123 arch.Read(wxT("CollapsedFolds"), m_folds);
124 }
125
126 void TabInfo::Serialize(Archive& arch)
127 {
128 arch.Write(wxT("FileName"), m_fileName);
129 arch.Write(wxT("FirstVisibleLine"), m_firstVisibleLine);
130 arch.Write(wxT("CurrentLine"), m_currentLine);
131 arch.Write(wxT("Bookmarks"), m_bookmarks);
132 arch.Write(wxT("CollapsedFolds"), m_folds);
133 }
134
135 // class Archive
136 Archive::Archive()
137 : m_root(NULL)
138 {
139 }
140
141 Archive::~Archive() {}
142
143 bool Archive::Write(const wxString& name, SerializedObject* obj)
144 {
145 Archive arch;
146 wxXmlNode* node = FindNodeByName(m_root, wxT("SerializedObject"), name);
147 if(node) {
148 m_root->RemoveChild(node);
149 delete node;
150 }
151
152 node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("SerializedObject"));
153 m_root->AddChild(node);
154 node->AddAttribute(wxT("Name"), name);
155
156 arch.SetXmlNode(node);
157 obj->Serialize(arch);
158 return true;
159 }
160
161 bool Archive::Read(const wxString& name, SerializedObject* obj)
162 {
163 Archive arch;
164 wxXmlNode* node = FindNodeByName(m_root, wxT("SerializedObject"), name);
165 if(node) {
166 arch.SetXmlNode(node);
167 obj->DeSerialize(arch);
168 return true;
169 }
170 return false;
171 }
172
173 bool Archive::Write(const wxString& name, const wxArrayString& arr)
174 {
175 if(!m_root) {
176 return false;
177 }
178 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("wxArrayString"));
179 m_root->AddChild(node);
180 node->AddAttribute(wxT("Name"), name);
181
182 // add an entry for each wxString in the array
183 for(size_t i = 0; i < arr.GetCount(); i++) {
184 wxXmlNode* child = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("wxString"));
185 node->AddChild(child);
186 child->AddAttribute(wxT("Value"), arr.Item(i));
187 }
188 return true;
189 }
190
191 bool Archive::Write(const wxString& name, std::vector<TabInfo>& _vTabInfoArr)
192 {
193 if(!m_root) {
194 return false;
195 }
196 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("TabInfoArray"));
197 m_root->AddChild(node);
198 node->AddAttribute(wxT("Name"), name);
199
200 // add an entry for each wxString in the array
201 for(size_t i = 0; i < _vTabInfoArr.size(); i++) {
202 wxXmlNode* child = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("TabInfo"));
203 Archive arch;
204 arch.SetXmlNode(child);
205 _vTabInfoArr[i].Serialize(arch);
206 node->AddChild(child);
207 }
208 return true;
209 }
210
211 bool Archive::Write(const wxString& name, std::vector<int>& _vInt)
212 {
213 if(!m_root) {
214 return false;
215 }
216 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("IntVector"));
217 m_root->AddChild(node);
218 node->AddAttribute(wxT("Name"), name);
219
220 // add an entry for each int in the vector
221 for(size_t i = 0; i < _vInt.size(); ++i) {
222 wxXmlNode* child = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("IntVectorItem"));
223 node->AddChild(child);
224 child->AddAttribute(wxT("Value"), wxString::Format(wxT("%i"), _vInt.at(i)));
225 }
226 return true;
227 }
228
229 bool Archive::Write(const wxString& name, const StringMap& str_map)
230 {
231 if(!m_root) {
232 return false;
233 }
234
235 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("StringMap"));
236 m_root->AddChild(node);
237 node->AddAttribute(wxT("Name"), name);
238
239 // add an entry for each wxString in the array
240 StringMap::const_iterator iter = str_map.begin();
241 for(; iter != str_map.end(); iter++) {
242 wxXmlNode* child = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("MapEntry"));
243 node->AddChild(child);
244 child->AddAttribute(wxT("Key"), iter->first);
245 child->AddAttribute(wxT("Value"), iter->second);
246 }
247 return true;
248 }
249
250 #if wxUSE_GUI
251 bool Archive::Write(const wxString& name, wxSize size)
252 {
253 if(!m_root) {
254 return false;
255 }
256
257 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("wxSize"));
258 m_root->AddChild(node);
259 node->AddAttribute(wxT("Name"), name);
260
261 wxString xstr, ystr;
262 xstr << size.x;
263 ystr << size.y;
264
265 node->AddAttribute(wxT("x"), xstr);
266 node->AddAttribute(wxT("y"), ystr);
267 return true;
268 }
269 bool Archive::Write(const wxString& name, wxPoint pt)
270 {
271 if(!m_root) {
272 return false;
273 }
274
275 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("wxPoint"));
276 m_root->AddChild(node);
277 node->AddAttribute(wxT("Name"), name);
278
279 wxString xstr, ystr;
280 xstr << pt.x;
281 ystr << pt.y;
282
283 node->AddAttribute(wxT("x"), xstr);
284 node->AddAttribute(wxT("y"), ystr);
285 return true;
286 }
287 #endif
288
289 bool Archive::Read(const wxString& name, wxArrayString& arr)
290 {
291 if(!m_root) {
292 return false;
293 }
294
295 wxXmlNode* node = FindNodeByName(m_root, wxT("wxArrayString"), name);
296 if(node) {
297 // fill the output array with the values
298 arr.Clear();
299 wxXmlNode* child = node->GetChildren();
300 while(child) {
301 if(child->GetName() == wxT("wxString")) {
302 wxString value;
303 value = child->GetAttribute(wxT("Value"), wxEmptyString);
304 arr.Add(value);
305 }
306 child = child->GetNext();
307 }
308 return true;
309 }
310 return false;
311 }
312
313 bool Archive::Read(const wxString& name, std::vector<TabInfo>& _vTabInfoArr)
314 {
315 if(!m_root) {
316 return false;
317 }
318
319 Archive arch;
320 wxXmlNode* node = FindNodeByName(m_root, wxT("TabInfoArray"), name);
321 if(node) {
322 // fill the output array with the values
323 _vTabInfoArr.clear();
324 wxXmlNode* child = node->GetChildren();
325 while(child) {
326 if(child->GetName() == wxT("TabInfo")) {
327 arch.SetXmlNode(child);
328 TabInfo oTabInfo;
329 oTabInfo.DeSerialize(arch);
330 _vTabInfoArr.push_back(oTabInfo);
331 }
332 child = child->GetNext();
333 }
334 return true;
335 }
336 return false;
337 }
338
339 bool Archive::Read(const wxString& name, std::vector<int>& _vInt)
340 {
341 if(!m_root) {
342 return false;
343 }
344
345 wxXmlNode* node = FindNodeByName(m_root, wxT("IntVector"), name);
346 if(node) {
347 // fill the output array with the values
348 _vInt.clear();
349 wxXmlNode* child = node->GetChildren();
350 while(child) {
351 if(child->GetName() == wxT("IntVectorItem")) {
352 long value;
353 wxString stringvalue = child->GetAttribute(wxT("Value"), wxEmptyString);
354 if(stringvalue.ToLong(&value)) {
355 _vInt.push_back(value);
356 }
357 }
358 child = child->GetNext();
359 }
360 return true;
361 }
362 return false;
363 }
364
365 bool Archive::Read(const wxString& name, StringMap& str_map)
366 {
367 if(!m_root) {
368 return false;
369 }
370
371 wxXmlNode* node = FindNodeByName(m_root, wxT("StringMap"), name);
372 if(node) {
373 // fill the output array with the values
374 str_map.clear();
375 wxXmlNode* child = node->GetChildren();
376 while(child) {
377 if(child->GetName() == wxT("MapEntry")) {
378 wxString value;
379 wxString key;
380 key = child->GetAttribute(wxT("Key"), wxEmptyString);
381 value = child->GetAttribute(wxT("Value"), wxEmptyString);
382 str_map[key] = value;
383 }
384 child = child->GetNext();
385 }
386 return true;
387 }
388 return false;
389 }
390 #if wxUSE_GUI
391 bool Archive::Read(const wxString& name, wxSize& size)
392 {
393 if(!m_root) {
394 return false;
395 }
396
397 wxXmlNode* node = FindNodeByName(m_root, wxT("wxSize"), name);
398 if(node) {
399 long v;
400 wxString value;
401 value = node->GetAttribute(wxT("x"), wxEmptyString);
402 value.ToLong(&v);
403 size.x = v;
404
405 value = node->GetAttribute(wxT("y"), wxEmptyString);
406 value.ToLong(&v);
407 size.y = v;
408 return true;
409 }
410 return false;
411 }
412
413 bool Archive::Read(const wxString& name, wxPoint& pt)
414 {
415 if(!m_root) {
416 return false;
417 }
418
419 wxXmlNode* node = FindNodeByName(m_root, wxT("wxPoint"), name);
420 if(node) {
421 long v;
422 wxString value;
423 value = node->GetAttribute(wxT("x"), wxEmptyString);
424 value.ToLong(&v);
425 pt.x = v;
426
427 value = node->GetAttribute(wxT("y"), wxEmptyString);
428 value.ToLong(&v);
429 pt.y = v;
430 return true;
431 }
432 return false;
433 }
434 #endif
435
436 bool Archive::Write(const wxString& name, int value) { return WriteSimple(value, wxT("int"), name); }
437
438 bool Archive::Read(const wxString& name, int& value)
439 {
440 if(!m_root) {
441 return false;
442 }
443
444 long v;
445 bool res = ReadSimple(v, wxT("int"), name);
446 if(res) {
447 value = v;
448 }
449 return res;
450 }
451
452 bool Archive::Write(const wxString& name, long value) { return WriteSimple(value, wxT("long"), name); }
453
454 bool Archive::Read(const wxString& name, long& value) { return ReadSimple(value, wxT("long"), name); }
455
456 bool Archive::Write(const wxString& name, bool value) { return WriteSimple(value ? 1 : 0, wxT("bool"), name); }
457
458 bool Archive::Read(const wxString& name, bool& value)
459 {
460 long v;
461 bool res = ReadSimple(v, wxT("bool"), name);
462 if(res) {
463 v == 0 ? value = false : value = true;
464 }
465 return res;
466 }
467
468 bool Archive::Write(const wxString& name, const wxString& str)
469 {
470 if(!m_root) {
471 return false;
472 }
473 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("wxString"));
474 m_root->AddChild(node);
475 node->AddAttribute(wxT("Value"), str);
476 node->AddAttribute(wxT("Name"), name);
477 return true;
478 }
479
480 bool Archive::WriteCData(const wxString& name, const wxString& str)
481 {
482 if(!m_root) {
483 return false;
484 }
485
486 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("CData"));
487 m_root->AddChild(node);
488
489 SetCDATANodeContent(node, str);
490 node->AddAttribute(wxT("Name"), name);
491 return true;
492 }
493
494 bool Archive::ReadCData(const wxString& name, wxString& value)
495 {
496 if(!m_root) {
497 return false;
498 }
499
500 wxXmlNode* node = FindNodeByName(m_root, wxT("CData"), name);
501 if(node) {
502 // get the content node
503 value = node->GetNodeContent();
504 value.Trim().Trim(false);
505 return true;
506 }
507 return false;
508 }
509
510 bool Archive::Read(const wxString& name, wxString& value)
511 {
512 if(!m_root) {
513 return false;
514 }
515 wxXmlNode* node = FindNodeByName(m_root, wxT("wxString"), name);
516 if(node) {
517 value = node->GetAttribute(wxT("Value"), value);
518 return true;
519 }
520 return false;
521 }
522
523 bool Archive::Read(const wxString& name, size_t& value)
524 {
525 long v = 0;
526 bool res = Read(name, v);
527 if(res) {
528 value = v;
529 }
530 return res;
531 }
532
533 bool Archive::Read(const wxString& name, wxFileName& fileName)
534 {
535 wxString value;
536 bool res = Read(name, value);
537 if(res) {
538 fileName = wxFileName(value);
539 }
540 return res;
541 }
542
543 bool Archive::Write(const wxString& name, size_t value) { return Write(name, (long)value); }
544
545 bool Archive::Write(const wxString& name, const wxFileName& fileName) { return Write(name, fileName.GetFullPath()); }
546
547 void Archive::SetXmlNode(wxXmlNode* node) { m_root = node; }
548
549 bool Archive::WriteSimple(long value, const wxString& typeName, const wxString& name)
550 {
551 if(!m_root)
552 return false;
553
554 wxString propValue;
555 propValue << value;
556
557 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, typeName);
558 m_root->AddChild(node);
559 node->AddAttribute(wxT("Value"), propValue);
560 node->AddAttribute(wxT("Name"), name);
561 return true;
562 }
563
564 bool Archive::ReadSimple(long& value, const wxString& typeName, const wxString& name)
565 {
566 if(!m_root)
567 return false;
568
569 value = 0;
570 wxXmlNode* node = FindNodeByName(m_root, typeName, name);
571 if(node) {
572 wxString val = node->GetAttribute(wxT("Value"), wxEmptyString);
573 val.ToLong(&value);
574 return true;
575 }
576 return false;
577 }
578
579 #if wxUSE_GUI
580 bool Archive::Read(const wxString& name, wxColour& colour)
581 {
582 if(!m_root) {
583 return false;
584 }
585
586 wxXmlNode* node = FindNodeByName(m_root, wxT("wxColour"), name);
587 wxString value;
588 if(node) {
589 value = node->GetAttribute(wxT("Value"), wxEmptyString);
590 }
591
592 if(value.IsEmpty()) {
593 return false;
594 }
595
596 colour = wxColour(value);
597 return true;
598 }
599 bool Archive::Write(const wxString& name, const wxColour& colour)
600 {
601 if(!m_root) {
602 return false;
603 }
604 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("wxColour"));
605 m_root->AddChild(node);
606 node->AddAttribute(wxT("Value"), colour.GetAsString());
607 node->AddAttribute(wxT("Name"), name);
608 return true;
609 }
610 #endif
611
612 bool Archive::Write(const wxString& name, const wxStringMap_t& strinMap)
613 {
614 if(!m_root) {
615 return false;
616 }
617
618 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("std_string_map"));
619 m_root->AddChild(node);
620 node->AddAttribute(wxT("Name"), name);
621
622 // add an entry for each wxString in the array
623 wxStringMap_t::const_iterator iter = strinMap.begin();
624 for(; iter != strinMap.end(); ++iter) {
625 wxXmlNode* child = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("MapEntry"));
626 node->AddChild(child);
627 child->AddAttribute(wxT("Key"), iter->first);
628 SetNodeContent(child, iter->second);
629 }
630 return true;
631 }
632
633 bool Archive::Read(const wxString& name, wxStringMap_t& strinMap)
634 {
635 if(!m_root) {
636 return false;
637 }
638
639 wxXmlNode* node = FindNodeByName(m_root, wxT("std_string_map"), name);
640 if(node) {
641 // fill the output array with the values
642 strinMap.clear();
643 wxXmlNode* child = node->GetChildren();
644 while(child) {
645 if(child->GetName() == wxT("MapEntry")) {
646 wxString value;
647 wxString key;
648 key = child->GetAttribute(wxT("Key"), wxEmptyString);
649 value = child->GetNodeContent();
650 strinMap[key] = value;
651 }
652 child = child->GetNext();
653 }
654 return true;
655 }
656 return false;
657 }
658
659 bool Archive::Read(const wxString& name, wxStringSet_t& s)
660 {
661 if(!m_root) {
662 return false;
663 }
664
665 wxXmlNode* node = FindNodeByName(m_root, wxT("std_string_set"), name);
666 if(node) {
667 // fill the output array with the values
668 s.clear();
669 wxXmlNode* child = node->GetChildren();
670 while(child) {
671 if(child->GetName() == wxT("SetEntry")) {
672 wxString value;
673 value = child->GetNodeContent();
674 s.insert(value);
675 }
676 child = child->GetNext();
677 }
678 return true;
679 }
680 return false;
681 }
682
683 bool Archive::Write(const wxString& name, const wxStringSet_t& s)
684 {
685 if(!m_root) {
686 return false;
687 }
688
689 wxXmlNode* node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("std_string_set"));
690 m_root->AddChild(node);
691 node->AddAttribute(wxT("Name"), name);
692
693 // add an entry for each wxString in the array
694 wxStringSet_t::const_iterator iter = s.begin();
695 for(; iter != s.end(); ++iter) {
696 wxXmlNode* child = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("SetEntry"));
697 node->AddChild(child);
698 SetNodeContent(child, *iter);
699 }
700 return true;
701 }
702 #if wxUSE_GUI
703 bool Archive::Read(const wxString& name, wxFont& font, const wxFont& defaultFont)
704 {
705 wxString strFont;
706 bool res = Read(name, strFont);
707 if(!res || strFont.IsEmpty()) {
708 font = defaultFont;
709 return false;
710 }
711 font = clFontHelper::FromString(strFont);
712 return true;
713 }
714
715 bool Archive::Write(const wxString& name, const wxFont& font) { return Write(name, clFontHelper::ToString(font)); }
716 #endif