Xml Scol plugin
sXml.cpp
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25/*
26 Xml library based on TinyXml2
27 First version : november 2012
28 Author : Bastien Bourineau
29*/
30
31
45#include <scolPlugin.h>
46#include "tinyxml2.h"
47#include <string>
48#include <set>
49#include <scolMemoryHelper.hpp>
50
51#ifdef _DEBUG
52 //#define _SCOL_DEBUG_ 1
53#endif
54
56#ifdef SCOL_STATIC
57extern cbmachine ww;
58extern mmachine mm;
59#else
60cbmachine ww;
61mmachine mm;
62#endif
63
64class SXml
65{
66public:
68 {
69 xmlDoc = doc;
70 }
71
72 ~SXml()
73 {
74 SAFE_DELETE(xmlDoc);
75 nodeslist.clear();
76 }
77
78 bool AddNode(tinyxml2::XMLNode* xmlNode)
79 {
80 std::pair<std::set<tinyxml2::XMLNode*>::iterator, bool> ret;
81 ret = nodeslist.insert(xmlNode);
82
83 return ret.second;
84 }
85
86 void RemoveNode(tinyxml2::XMLNode* xmlNode)
87 {
88 nodeslist.erase(xmlNode);
89
90 // remove childs
91 for(tinyxml2::XMLNode* child = xmlNode->FirstChild(); child != 0; child = child->NextSibling())
92 {
93 RemoveNode(child);
94 }
95 }
96
97 bool NodeExist(tinyxml2::XMLNode* xmlNode)
98 {
99 std::set<tinyxml2::XMLNode*>::iterator it = nodeslist.find(xmlNode);
100 if (it != nodeslist.end())
101 return true;
102
103 return false;
104 }
105
106 tinyxml2::XMLDocument* GetDocument()
107 {
108 return xmlDoc;
109 }
110
111 std::string ToString()
112 {
114 xmlDoc->Print(&stream);
115 return stream.CStr();
116 }
117
118private:
119 tinyxml2::XMLDocument* xmlDoc;
120 std::set<tinyxml2::XMLNode*> nodeslist;
121};
122
123int OBJTYPSXML;
124
125inline tinyxml2::XMLNode* GetXmlNode(mmachine m, int ixmlnode)
126{
127 tinyxml2::XMLNode* xmlNode = MMgetPointer<tinyxml2::XMLNode*>(m, MTOP(MMfetch(m, MTOP(ixmlnode), 0)));
128 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(MMfetch(m, MTOP(ixmlnode), 1)));
129 if (!xmlNode || !sXmlDoc)
130 {
131 return 0;
132 }
133
134 int objDoc = OBJfindTH(m, OBJTYPSXML, SCOL_PTR sXmlDoc);
135 if (objDoc == NIL || (MMgetPointer<SXml*>(m, objDoc) == 0))
136 {
137 return 0;
138 }
139
140 if (sXmlDoc->NodeExist(xmlNode))
141 return xmlNode;
142
143 return 0;
144}
145
146int destroyXmlObj(mmachine m, SCOL_PTR_TYPE handsys, int objm)
147{
148 SXml* xmlDoc = MMgetPointer<SXml*>(m, MTOP(objm));
149 SAFE_DELETE(xmlDoc);
150
151 MMsetPointer<SXml*>(m, MTOP(objm), 0);
152 return 0;
153}
154
163int _CreateXml(mmachine m)
164{
165 #ifdef _SCOL_DEBUG_
166 MMechostr(MSKDEBUG,"_CreateXml\n");
167 #endif
168
169 // channel test
170 if (MMget(m, 0) == NIL)
171 return 0;
172
174 if (!xmlDoc)
175 {
176 MMechostr(MSKDEBUG, "_CreateXml : New XMLDocument failed.\n");
177 MMset(m, 0, NIL);
178 return 0;
179 }
180
181 SXml* sXmlDoc = new SXml(xmlDoc);
182 if (!sXmlDoc)
183 {
184 MMechostr(MSKDEBUG, "_CreateXml : New SXml failed.\n");
185 SAFE_DELETE(xmlDoc);
186 MMset(m, 0, NIL);
187 return 0;
188 }
189
190 // Allocate a block in the stack for a table of ObjSXML object
191 if ((MMpushPointer(m, sXmlDoc) != 0))
192 {
193 SAFE_DELETE(sXmlDoc);
194 MMset(m, 0, NIL);
195 return MERRMEM;
196 }
197
198 // Create a new object
199 return OBJcreate(m, OBJTYPSXML, SCOL_PTR sXmlDoc, NIL, 0);
200}
201
211int _OpenXml(mmachine m)
212{
213 #ifdef _SCOL_DEBUG_
214 MMechostr(MSKDEBUG,"_OpenXml\n");
215 #endif
216
217 int ifile = MMpull(m);
218 if (MMget(m, 0) == NIL)
219 {
220 MMechostr(MSKDEBUG, "_OpenXml : Channel nil.\n");
221 MMset(m, 0, NIL);
222 return 0;
223 }
224
225 if (ifile == NIL)
226 {
227 MMechostr(MSKDEBUG, "_OpenXml : File nil.\n");
228 MMset(m, 0, NIL);
229 return 0;
230 }
231
232 std::string path = MMstartstr(m, MTOP(ifile));
233
235 if (!xmlDoc)
236 {
237 MMechostr(MSKDEBUG, "_OpenXml : New XMLDocument failed.\n");
238 MMset(m, 0, NIL);
239 return 0;
240 }
241
242 if(xmlDoc->LoadFile(path.c_str()) != tinyxml2::XML_SUCCESS)
243 {
244 MMechostr(MSKDEBUG, "_OpenXml : load of %s failed\n", path.c_str());
245 // this crash //MMechostr(MSKDEBUG, "on : %s\n", xmlDoc->GetErrorStr1());
246 SAFE_DELETE(xmlDoc);
247 MMset(m, 0, NIL);
248 return 0;
249 }
250
251 SXml* sXmlDoc = new SXml(xmlDoc);
252 if (!sXmlDoc)
253 {
254 MMechostr(MSKDEBUG, "_OpenXml : New SXml failed.\n");
255 SAFE_DELETE(xmlDoc);
256 MMset(m, 0, NIL);
257 return 0;
258 }
259
260 // Allocate a block in the stack for a table of ObjSXML objects
261 if ((MMpushPointer(m, sXmlDoc) != 0))
262 {
263 SAFE_DELETE(sXmlDoc);
264 MMset(m, 0, NIL);
265 return MERRMEM;
266 }
267
268 // Create a new object
269 return OBJcreate(m, OBJTYPSXML, SCOL_PTR sXmlDoc, NIL, 0);
270}
271
281int _OpenXmlS(mmachine m)
282{
283 #ifdef _SCOL_DEBUG_
284 MMechostr(MSKDEBUG,"_OpenXmlS\n");
285 #endif
286
287 int icont = MMpull(m);
288 if ((MMget(m, 0) == NIL) || (icont == NIL))
289 {
290 MMset(m, 0, NIL);
291 return 0;
292 }
293
294 std::string content = MMstartstr(m, MTOP(icont));
295
297 if (!xmlDoc)
298 {
299 MMechostr(MSKDEBUG, "_OpenXmlS : New XMLDocument failed.\n");
300 MMset(m, 0, NIL);
301 return 0;
302 }
303
304 if(xmlDoc->Parse(content.c_str()) != tinyxml2::XML_NO_ERROR)
305 {
306 MMechostr(MSKDEBUG, "_OpenXmlS : load of content failed\n");
307 SAFE_DELETE(xmlDoc);
308 MMset(m, 0, NIL);
309 return 0;
310 }
311
312 SXml* sXmlDoc = new SXml(xmlDoc);
313 if (!sXmlDoc)
314 {
315 MMechostr(MSKDEBUG, "_OpenXmlS : New SXml failed.\n");
316 SAFE_DELETE(xmlDoc);
317 MMset(m, 0, NIL);
318 return 0;
319 }
320
321 // Allocate a block in the stack for a table of ObjSXML objects
322 if ((MMpushPointer(m, sXmlDoc) != 0))
323 {
324 SAFE_DELETE(sXmlDoc);
325 MMset(m, 0, NIL);
326 return MERRMEM;
327 }
328
329 // Create a new object
330 return OBJcreate(m, OBJTYPSXML, SCOL_PTR sXmlDoc, NIL, 0);
331}
332
342int _SaveXml(mmachine m)
343{
344 #ifdef _SCOL_DEBUG_
345 MMechostr(MSKDEBUG,"_SaveXml\n");
346 #endif
347
348 int ifile = MMpull(m);
349 int ixml = MMpull(m);
350 if ((ixml == NIL) || (ifile == NIL))
351 {
352 MMechostr(MSKDEBUG, "_SaveXml : param nil.\n");
353 MMpush(m, NIL);
354 return 0;
355 }
356 std::string path = MMstartstr(m, MTOP(ifile));
357
358 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(ixml));
359 if (!sXmlDoc)
360 {
361 MMechostr(MSKDEBUG, "_SaveXml : bad ObjSXML object.\n");
362 MMpush(m, NIL);
363 return 0;
364 }
365
366 if(sXmlDoc->GetDocument()->SaveFile(path.c_str()) != tinyxml2::XML_NO_ERROR)
367 {
368 MMechostr(MSKDEBUG, "_SaveXml : error on save file.\n");
369 MMpush(m, NIL);
370 return 0;
371 }
372
373 MMpush(m, ITOM(1));
374 return 0;
375}
376
386int _DestroyXml(mmachine m)
387{
388#ifdef _SCOL_DEBUG_
389MMechostr(MSKDEBUG, "_DestroyXml\n");
390#endif
391
392 int mlTab = MMget(m, 0);
393 if (mlTab == NIL)
394 {
395 MMechostr(MSKDEBUG, "_DestroyXml : object NIL");
396 MMset(m, 0, NIL);
397 return 0;
398 }
399
400 OBJdelTM(m, OBJTYPSXML, mlTab);
401
402 MMset(m, 0, ITOM(0));
403
404#ifdef _SCOL_DEBUG_
405 MMechostr(MSKDEBUG, "ok\n");
406#endif
407 return 0;
408}
409
418int _GetXmlRootNodes(mmachine m)
419{
420 #ifdef _SCOL_DEBUG_
421 MMechostr(MSKDEBUG,"_GetXmlRootNodes\n");
422 #endif
423
424 int ixml = MMpull(m);
425 if (ixml == NIL)
426 {
427 MMpush(m, NIL);
428 return 0;
429 }
430
431 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(ixml));
432 if (!sXmlDoc)
433 {
434 MMechostr(MSKDEBUG, "_GetXmlRootNodes : bad ObjSXML object.\n");
435 MMpush(m, NIL);
436 return 0;
437 }
438
439 int k = 0;
440 unsigned int count = 0;
441 tinyxml2::XMLNode* child = 0;
442
443 // get the node list
444 for(child = sXmlDoc->GetDocument()->FirstChild(); child != 0; child = child->NextSibling())
445 {
446 //verify if the node is not a text or other
447 tinyxml2::XMLElement* xmlElem = child->ToElement();
448 if (!xmlElem)
449 continue;
450
451 sXmlDoc->AddNode(child);
452
453 if ((MMpushPointer(m, child) != 0))
454 return MERRMEM;
455
456 if ((MMpushPointer(m, sXmlDoc) != 0))
457 return MERRMEM;
458
459 MMpush(m, 2*2);
460 MBdeftab(m);
461
462 count++;
463 }
464
465 if (MMpush(m, NIL))
466 return MERRMEM;
467
468 for(unsigned int j = 0; j < count; j++)
469 {
470 if (MMpush(m, 2*2))
471 return MERRMEM;
472 if ((k = MBdeftab(m)))
473 return k;
474 }
475
476 return 0;
477}
478
487int _GetXmlNodeChilds(mmachine m)
488{
489 #ifdef _SCOL_DEBUG_
490 MMechostr(MSKDEBUG,"_GetXmlNodeChilds\n");
491 #endif
492
493 int ixmlnode = MMpull(m);
494 if (ixmlnode == NIL)
495 {
496 MMpush(m, NIL);
497 return 0;
498 }
499
500 tinyxml2::XMLNode* xmlNode = GetXmlNode(m, ixmlnode);
501 if (!xmlNode)
502 {
503 MMechostr(MSKDEBUG, "_GetXmlNodeChilds : bad ObjSXMLNode object.\n");
504 MMpush(m, NIL);
505 return 0;
506 }
507
508 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(MMfetch(m, MTOP(ixmlnode), 1)));
509 if (!sXmlDoc)
510 {
511 MMechostr(MSKDEBUG, "_GetXmlNodeChilds : bad ObjSXMLNode object.\n");
512 MMpush(m, NIL);
513 return 0;
514 }
515
516 int k = 0;
517 unsigned int count = 0;
518 tinyxml2::XMLNode* child = 0;
519
520 // get the node list
521 for(child = xmlNode->FirstChild(); child != 0; child = child->NextSibling())
522 {
523 //verify if the node is not a text or other
524 tinyxml2::XMLElement* xmlElem = child->ToElement();
525 if (!xmlElem)
526 continue;
527
528 sXmlDoc->AddNode(child);
529
530 if ((MMpushPointer(m, child) != 0))
531 return MERRMEM;
532
533 if ((MMpushPointer(m, sXmlDoc) != 0))
534 return MERRMEM;
535
536 MMpush(m, 2 * 2);
537 MBdeftab(m);
538
539 count++;
540 }
541
542 if (MMpush(m, NIL))
543 return MERRMEM;
544
545 for(unsigned int j = 0; j < count; j++)
546 {
547 if (MMpush(m, 2*2))
548 return MERRMEM;
549 if (k = MBdeftab(m))
550 return k;
551 }
552
553 return 0;
554}
555
564int _GetXmlNodeValue(mmachine m)
565{
566 #ifdef _SCOL_DEBUG_
567 MMechostr(MSKDEBUG,"_GetXmlNodeValue\n");
568 #endif
569
570 int ixmlnode = MMpull(m);
571 if (ixmlnode == NIL)
572 {
573 MMpush(m, NIL);
574 return 0;
575 }
576
577 tinyxml2::XMLNode* xmlNode = GetXmlNode(m, ixmlnode);
578 if (!xmlNode)
579 {
580 MMechostr(MSKDEBUG, "_GetXmlNodeValue : bad ObjSXMLNode object.\n");
581 MMpush(m, NIL);
582 return 0;
583 }
584
585 Mpushstrbloc(m, (char*)xmlNode->Value());
586 return 0;
587}
588
598int _SetXmlNodeValue(mmachine m)
599{
600 #ifdef _SCOL_DEBUG_
601 MMechostr(MSKDEBUG,"_SetXmlNodeValue\n");
602 #endif
603
604 int ivalue = MMpull(m);
605 int ixmlnode = MMget(m, 0);
606 if ((ixmlnode == NIL) || (ivalue == NIL))
607 {
608 MMset(m, 0, NIL);
609 return 0;
610 }
611 std::string value = MMstartstr(m, MTOP(ivalue));
612
613 tinyxml2::XMLNode* xmlNode = GetXmlNode(m, ixmlnode);
614 if (!xmlNode)
615 {
616 MMechostr(MSKDEBUG, "_SetXmlNodeValue : bad ObjSXMLNode object.\n");
617 MMset(m, 0, NIL);
618 return 0;
619 }
620
621 xmlNode->SetValue(value.c_str());
622
623 return 0;
624}
625
634int _GetXmlNodeContent(mmachine m)
635{
636 #ifdef _SCOL_DEBUG_
637 MMechostr(MSKDEBUG,"_GetXmlNodeValue\n");
638 #endif
639
640 int ixmlnode = MMpull(m);
641 if (ixmlnode == NIL)
642 {
643 MMpush(m, NIL);
644 return 0;
645 }
646
647 tinyxml2::XMLNode* xmlNode = GetXmlNode(m, ixmlnode);
648 if (!xmlNode)
649 {
650 MMechostr(MSKDEBUG, "_GetXmlNodeContent : bad ObjSXMLNode object.\n");
651 MMpush(m, NIL);
652 return 0;
653 }
654
655 xmlNode = xmlNode->FirstChild();
656 // no content
657 if (!xmlNode)
658 {
659 MMpush(m, NIL);
660 return 0;
661 }
662
663 tinyxml2::XMLText* xmlText = xmlNode->ToText();
664 // not a content type
665 if (!xmlText)
666 {
667 MMpush(m, NIL);
668 return 0;
669 }
670
671 Mpushstrbloc(m, (char*)xmlText->Value());
672 return 0;
673}
674
684int _SetXmlNodeContent(mmachine m)
685{
686 #ifdef _SCOL_DEBUG_
687 MMechostr(MSKDEBUG,"_SetXmlNodeContent\n");
688 #endif
689
690 int icontent = MMpull(m);
691 int ixmlnode = MMget(m, 0);
692 if (ixmlnode == NIL)
693 {
694 MMset(m, 0, NIL);
695 return 0;
696 }
697 std::string content = (icontent == NIL) ? "" : MMstartstr(m, MTOP(icontent));
698
699 tinyxml2::XMLNode* xmlNode = GetXmlNode(m, ixmlnode);
700 if (!xmlNode)
701 {
702 MMechostr(MSKDEBUG, "_SetXmlNodeContent : bad ObjSXMLNode object.\n");
703 MMset(m, 0, NIL);
704 return 0;
705 }
706
707 tinyxml2::XMLNode* xmlContentNode = xmlNode->FirstChild();
708 tinyxml2::XMLText* xmlText = 0;
709 // no content
710 if (!xmlContentNode)
711 {
712 if (content.empty())
713 {
714 return 0;
715 }
716 else
717 {
718 // add a node for content
719 xmlText = xmlNode->GetDocument()->NewText("");
720 xmlNode->InsertFirstChild(xmlText);
721 }
722 }
723 else
724 {
725 xmlText = xmlContentNode->ToText();
726 }
727
728 // not a content type
729 if (!xmlText)
730 {
731 MMset(m, 0, NIL);
732 return 0;
733 }
734 else
735 {
736 //remove the node if empty text
737 if (content.empty())
738 {
739 xmlNode->DeleteChild(xmlContentNode);
740 return 0;
741 }
742 }
743
744 xmlText->SetCData(true);
745 xmlText->SetValue(content.c_str());
746 return 0;
747}
748
758{
759 #ifdef _SCOL_DEBUG_
760 MMechostr(MSKDEBUG,"_GetXmlNodeAttributes\n");
761 #endif
762
763 int ixmlnode = MMpull(m);
764 if (ixmlnode == NIL)
765 {
766 MMpush(m, NIL);
767 return 0;
768 }
769
770 tinyxml2::XMLNode* xmlNode = GetXmlNode(m, ixmlnode);
771 if (!xmlNode)
772 {
773 MMechostr(MSKDEBUG, "_GetXmlNodeAttributes : bad ObjSXMLNode object.\n");
774 MMpush(m, NIL);
775 return 0;
776 }
777
778 tinyxml2::XMLElement* xmlElem = xmlNode->ToElement();
779 if (!xmlElem)
780 {
781 MMechostr(MSKDEBUG, "_GetXmlNodeAttributes : Can not convert ObjXMLNode %s to Element.\n", xmlNode->Value());
782 MMpush(m, NIL);
783 return 0;
784 }
785
786 int k = 0;
787 unsigned int count = 0;
788 const tinyxml2::XMLAttribute* xmlAttrib = 0;
789
790 // get the attributes list
791 for(xmlAttrib = xmlElem->FirstAttribute(); xmlAttrib != 0; xmlAttrib = xmlAttrib->Next(), count++)
792 {
793 Mpushstrbloc(m, (char*)xmlAttrib->Name());
794 Mpushstrbloc(m, (char*)xmlAttrib->Value());
795 MMpush(m, 2*2);
796 if (k = MBdeftab(m))
797 return k;
798 }
799
800 if (MMpush(m, NIL))
801 return MERRMEM;
802
803 for(unsigned int j = 0; j < count; j++)
804 {
805 if (MMpush(m, 2*2))
806 return MERRMEM;
807 if (k = MBdeftab(m))
808 return k;
809 }
810
811 return 0;
812}
813
824{
825 #ifdef _SCOL_DEBUG_
826 MMechostr(MSKDEBUG,"_GetXmlNodeAttributes\n");
827 #endif
828
829 int iattributes = MMpull(m);
830 int ixmlnode = MMget(m, 0);
831 if (ixmlnode == NIL)
832 {
833 MMset(m, 0, NIL);
834 return 0;
835 }
836
837 tinyxml2::XMLNode* xmlNode = GetXmlNode(m, ixmlnode);
838 if (!xmlNode)
839 {
840 MMechostr(MSKDEBUG, "_SetXmlNodeAttributes : bad ObjSXMLNode object.\n");
841 MMset(m, 0, NIL);
842 return 0;
843 }
844
845 tinyxml2::XMLElement* xmlElem = xmlNode->ToElement();
846 if (!xmlElem)
847 {
848 MMechostr(MSKDEBUG, "_SetXmlNodeAttributes : Can not convert ObjXMLNode %s to Element.\n", xmlNode->Value());
849 MMset(m, 0, NIL);
850 return 0;
851 }
852
853 const tinyxml2::XMLAttribute* xmlAttrib = 0;
854
855 // remove all attributes
856 // get the attributes list
857 for(xmlAttrib = xmlElem->FirstAttribute(); xmlAttrib != 0; xmlAttrib = xmlAttrib->Next())
858 {
859 xmlElem->DeleteAttribute(xmlAttrib->Name());
860 }
861
862 int q = 0;
863 int iname = 0;
864 int ivalue = 0;
865 while (iattributes != NIL)
866 {
867 q = MMfetch(m, MTOP(iattributes), 0);
868 if (q != NIL)
869 {
870 iname = MMfetch(m, MTOP(q), 0);
871 ivalue = MMfetch(m, MTOP(q), 1);
872 // bad parameter cancel
873 if (iname == NIL)
874 continue;
875
876 std::string sname = MMstartstr(m, MTOP(iname));
877 std::string svalue = "";
878 if (ivalue != NIL)
879 svalue = MMstartstr(m, MTOP(ivalue));
880
881 xmlElem->SetAttribute(sname.c_str(), svalue.c_str());
882 }
883
884 iattributes = MMfetch(m, MTOP(iattributes), 1);
885 };
886
887 return 0;
888}
889
898int _RemoveXmlNode(mmachine m)
899{
900 #ifdef _SCOL_DEBUG_
901 MMechostr(MSKDEBUG,"_RemoveXmlNode\n");
902 #endif
903
904 int ixmlnode = MMpull(m);
905 if (ixmlnode == NIL)
906 {
907 MMpush(m, NIL);
908 return 0;
909 }
910
911 tinyxml2::XMLNode* xmlNode = GetXmlNode(m, ixmlnode);
912 if (!xmlNode)
913 {
914 MMechostr(MSKDEBUG, "_RemoveXmlNode : bad ObjSXMLNode object.\n");
915 MMpush(m, NIL);
916 return 0;
917 }
918
919 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(MMfetch(m, MTOP(ixmlnode), 1)));
920 if (!sXmlDoc)
921 {
922 MMechostr(MSKDEBUG, "_RemoveXmlNode : bad ObjSXMLNode object.\n");
923 MMpush(m, NIL);
924 return 0;
925 }
926
927 sXmlDoc->RemoveNode(xmlNode);
928 sXmlDoc->GetDocument()->DeleteNode(xmlNode);
929 MMstore(m, MTOP(ixmlnode), 0, 0);
930 MMstore(m, MTOP(ixmlnode), 1, 0);
931
932 MMpush(m, ITOM(1));
933 return 0;
934}
935
945int _AddXmlRootNode(mmachine m)
946{
947 #ifdef _SCOL_DEBUG_
948 MMechostr(MSKDEBUG,"_AddXmlRootNode\n");
949 #endif
950
951 int ivalue = MMpull(m);
952 int ixml = MMpull(m);
953 if ((ixml == NIL) || (ivalue == NIL))
954 {
955 MMpush(m, NIL);
956 return 0;
957 }
958 std::string value = MMstartstr(m, MTOP(ivalue));
959
960 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(ixml));
961 if (!sXmlDoc)
962 {
963 MMechostr(MSKDEBUG, "_AddXmlRootNode : bad ObjSXML object.\n");
964 MMpush(m, NIL);
965 return 0;
966 }
967
968 tinyxml2::XMLNode* nXmlNode = sXmlDoc->GetDocument()->NewElement(value.c_str());
969 sXmlDoc->GetDocument()->InsertEndChild(nXmlNode);
970 sXmlDoc->AddNode(nXmlNode);
971
972 if ((MMpushPointer(m, nXmlNode) != 0))
973 return MERRMEM;
974
975 if ((MMpushPointer(m, sXmlDoc) != 0))
976 return MERRMEM;
977
978 int k = MMpush(m, 2 * 2);
979 MBdeftab(m);
980
981 return k;
982}
983
993int _AddXmlNode(mmachine m)
994{
995 #ifdef _SCOL_DEBUG_
996 MMechostr(MSKDEBUG,"_AddXmlNode\n");
997 #endif
998
999 int ivalue = MMpull(m);
1000 int ixmlnode = MMpull(m);
1001 if ((ixmlnode == NIL) || (ivalue == NIL))
1002 {
1003 MMpush(m, NIL);
1004 return 0;
1005 }
1006 std::string value = MMstartstr(m, MTOP(ivalue));
1007
1008 tinyxml2::XMLNode* xmlNode = GetXmlNode(m, ixmlnode);
1009 if (!xmlNode)
1010 {
1011 MMechostr(MSKDEBUG, "_AddXmlNode : bad ObjSXMLNode object.\n");
1012 MMpush(m, NIL);
1013 return 0;
1014 }
1015
1016 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(MMfetch(m, MTOP(ixmlnode), 1)));
1017 if (!sXmlDoc)
1018 {
1019 MMechostr(MSKDEBUG, "_AddXmlNode : bad ObjSXMLNode object.\n");
1020 MMpush(m, NIL);
1021 return 0;
1022 }
1023
1024 tinyxml2::XMLNode* nXmlNode = xmlNode->GetDocument()->NewElement(value.c_str());
1025 xmlNode->InsertEndChild(nXmlNode);
1026 sXmlDoc->AddNode(nXmlNode);
1027
1028 if ((MMpushPointer(m, nXmlNode) != 0))
1029 return MERRMEM;
1030
1031 if ((MMpushPointer(m, sXmlDoc) != 0))
1032 return MERRMEM;
1033
1034 int k = MMpush(m, 2 * 2);
1035 MBdeftab(m);
1036
1037 return k;
1038}
1039
1048int _GetXmlContent(mmachine m)
1049{
1050 #ifdef _SCOL_DEBUG_
1051 MMechostr(MSKDEBUG, "_GetXmlContent\n");
1052 #endif
1053
1054 int ixml = MMpull(m);
1055 if (ixml == NIL)
1056 {
1057 MMechostr(MSKDEBUG, "_GetXmlContent : param nil.\n");
1058 MMpush(m, NIL);
1059 return 0;
1060 }
1061
1062 SXml* sXmlDoc = MMgetPointer<SXml*>(m, MTOP(ixml));
1063 if (!sXmlDoc)
1064 {
1065 MMechostr(MSKDEBUG, "_GetXmlContent : bad ObjSXML object.\n");
1066 MMpush(m, NIL);
1067 return 0;
1068 }
1069
1070 std::string content = sXmlDoc->ToString();
1071
1072 Mpushstrbloc(m, (char*)content.c_str());
1073 return 0;
1074}
1075
1076
1077// Scol / Package **************************************************************
1078static NativeDefinition sxmlDef[] =
1079{
1080 {"ObjSXML", TYPTYPE, NULL, NULL},
1081 {"ObjSXMLNode", TYPTYPE, NULL, NULL},
1082 {"_CreateXml", 1, "fun [Chn] ObjSXML", _CreateXml},
1083 {"_OpenXml", 2, "fun [Chn P] ObjSXML", _OpenXml},
1084 {"_OpenXmlS", 2, "fun [Chn S] ObjSXML", _OpenXmlS},
1085 {"_SaveXml", 2, "fun [ObjSXML W] I", _SaveXml},
1086 {"_DestroyXml", 1, "fun [ObjSXML] I", _DestroyXml},
1087 {"_GetXmlRootNodes", 1, "fun [ObjSXML] [ObjSXMLNode r1]", _GetXmlRootNodes},
1088 {"_GetXmlNodeChilds", 1, "fun [ObjSXMLNode] [ObjSXMLNode r1]", _GetXmlNodeChilds},
1089 {"_GetXmlNodeValue", 1, "fun [ObjSXMLNode] S", _GetXmlNodeValue},
1090 {"_SetXmlNodeValue", 2, "fun [ObjSXMLNode S] ObjSXMLNode", _SetXmlNodeValue},
1091 {"_GetXmlNodeContent", 1, "fun [ObjSXMLNode] S", _GetXmlNodeContent},
1092 {"_SetXmlNodeContent", 2, "fun [ObjSXMLNode S] ObjSXMLNode", _SetXmlNodeContent},
1093 {"_GetXmlNodeAttributes", 1, "fun [ObjSXMLNode] [[S S] r1]", _GetXmlNodeAttributes},
1094 {"_SetXmlNodeAttributes", 2, "fun [ObjSXMLNode [[S S] r1]] ObjSXMLNode", _SetXmlNodeAttributes},
1095 {"_RemoveXmlNode", 1, "fun [ObjSXMLNode] I", _RemoveXmlNode},
1096 {"_AddXmlRootNode", 2, "fun [ObjSXML S] ObjSXMLNode", _AddXmlRootNode},
1097 {"_AddXmlNode", 2, "fun [ObjSXMLNode S] ObjSXMLNode", _AddXmlNode},
1098 {"_GetXmlContent", 1, "fun [ObjSXML] S", _GetXmlContent}
1099};
1100
1101// Everything inside _cond and _endcond is ignored by doxygen
1103
1104int InitSXML(mmachine m)
1105{
1106 OBJTYPSXML = OBJregister(0, 1, destroyXmlObj, "OBJTYPSXML");
1107 return PKhardpak2(m, "sXml.pkg-1.0", sizeof(sxmlDef)/sizeof(sxmlDef[0]), sxmlDef);
1108}
1109
1113#ifndef SCOL_STATIC
1114extern "C" SCOL_EXPORT int ScolLoadPlugin(mmachine m, cbmachine w)
1115#else
1116extern "C" SCOL_EXPORT int ScolSxmlLoadPlugin(mmachine m, cbmachine w)
1117#endif
1118{
1119 SCOLinitplugin(w);
1120 return InitSXML(m);
1121}
1122
1123
1127#ifndef SCOL_STATIC
1128extern "C" SCOL_EXPORT int ScolUnloadPlugin()
1129#else
1130extern "C" SCOL_EXPORT int ScolSxmlUnloadPlugin()
1131#endif
1132{
1133 return 0;
1134}
1135
Definition sXml.cpp:65
const char * Name() const
The name of the attribute.
Definition tinyxml2.h:962
const XMLAttribute * Next() const
The next attribute in the list.
Definition tinyxml2.h:970
const char * Value() const
The value of the attribute.
Definition tinyxml2.h:966
XMLError Parse(const char *xml, size_t nBytes=(size_t)(-1))
void Print(XMLPrinter *streamer=0)
XMLError LoadFile(const char *filename)
XMLElement * NewElement(const char *name)
XMLError SaveFile(const char *filename, bool compact=false)
void DeleteNode(XMLNode *node)
Definition tinyxml2.h:1486
void SetAttribute(const char *name, const char *value)
Sets the named attribute to value.
Definition tinyxml2.h:1194
const XMLAttribute * FirstAttribute() const
Return the first attribute in the list.
Definition tinyxml2.h:1225
void DeleteAttribute(const char *name)
const char * Value() const
Definition tinyxml2.h:591
void SetValue(const char *val, bool staticMem=false)
Definition tinyxml2.cpp:603
virtual XMLText * ToText()
Safely cast to Text, or null.
Definition tinyxml2.h:543
void DeleteChild(XMLNode *node)
Definition tinyxml2.cpp:646
const XMLDocument * GetDocument() const
Get the XMLDocument that owns this XMLNode.
Definition tinyxml2.h:530
virtual XMLElement * ToElement()
Safely cast to an Element, or null.
Definition tinyxml2.h:539
const XMLNode * FirstChild() const
Get the first child node, or null if none exists.
Definition tinyxml2.h:615
XMLNode * InsertFirstChild(XMLNode *addThis)
Definition tinyxml2.cpp:676
XMLNode * InsertEndChild(XMLNode *addThis)
Definition tinyxml2.cpp:653
const XMLNode * NextSibling() const
Get the next (right) sibling node of this node.
Definition tinyxml2.h:667
const char * CStr() const
Definition tinyxml2.h:1850
void SetCData(bool isCData)
Declare whether this should be CDATA or standard text.
Definition tinyxml2.h:804
int _GetXmlRootNodes(mmachine m)
_GetXmlRootNodes : This function retrieve the root nodes of an ObjSXML object
Definition sXml.cpp:418
int _AddXmlRootNode(mmachine m)
_AddXmlRootNode : This function adds a root ObjSXMLNode
Definition sXml.cpp:945
int _GetXmlNodeContent(mmachine m)
_GetXmlNodeContent : This function retrieve the content of an ObjSXMLNode object
Definition sXml.cpp:634
int _OpenXmlS(mmachine m)
_OpenXmlS : This function create an ObjSXML object from a string
Definition sXml.cpp:281
int _CreateXml(mmachine m)
_CreateXml : This function create an empty ObjSXML object
Definition sXml.cpp:163
int _GetXmlNodeAttributes(mmachine m)
_GetXmlNodeAttributes : This function retrieve the attrtibutes of an ObjSXMLNode object
Definition sXml.cpp:757
int _DestroyXml(mmachine m)
_DestroyXml : This function destroy an ObjSXML object
Definition sXml.cpp:386
int _AddXmlNode(mmachine m)
_AddXmlNode : This function add an ObjSXMLNode
Definition sXml.cpp:993
int _SaveXml(mmachine m)
_SaveXml : This function save an ObjSXML object to a file
Definition sXml.cpp:342
int _GetXmlContent(mmachine m)
_GetXmlContent : This function return an ObjSXML object content
Definition sXml.cpp:1048
int _GetXmlNodeChilds(mmachine m)
_GetXmlNodeChilds : This function retrieve the child nodes of an ObjSXMLNode object
Definition sXml.cpp:487
int _OpenXml(mmachine m)
_OpenXml : This function create an ObjSXML object from a file
Definition sXml.cpp:211
int _SetXmlNodeContent(mmachine m)
_SetXmlNodeContent : This function set the content of an ObjSXMLNode object
Definition sXml.cpp:684
int _SetXmlNodeValue(mmachine m)
_SetXmlNodeValue : This function set the value of an ObjSXMLNode object
Definition sXml.cpp:598
int _GetXmlNodeValue(mmachine m)
_GetXmlNodeValue : This function retrieve the value of an ObjSXMLNode object
Definition sXml.cpp:564
int _RemoveXmlNode(mmachine m)
_RemoveXmlNode : This function remove an ObjSXMLNode
Definition sXml.cpp:898
int _SetXmlNodeAttributes(mmachine m)
_SetXmlNodeAttributes : This function set the attrtibutes of an ObjSXMLNode object
Definition sXml.cpp:823