eb9ca8c757afb90d6d44c94902f55f819d263841
[fms.git] / src / freenet / boardlistxml.cpp
1 #include "../../include/freenet/boardlistxml.h"\r
2 \r
3 #ifdef XMEM\r
4         #include <xmem.h>\r
5 #endif\r
6 \r
7 BoardListXML::BoardListXML()\r
8 {\r
9         Initialize();\r
10 }\r
11 \r
12 void BoardListXML::AddBoard(const std::string &name, const std::string &description)\r
13 {\r
14         if(name!="" && description!="")\r
15         {\r
16                 m_boards.push_back(board(name,description));\r
17         }\r
18 }\r
19 \r
20 const std::string BoardListXML::GetDescription(const long index)\r
21 {\r
22         if(index>=0 && index<GetCount())\r
23         {\r
24                 return m_boards[index].m_description;\r
25         }\r
26         else\r
27         {\r
28                 return "";\r
29         }\r
30 }\r
31 \r
32 const std::string BoardListXML::GetName(const long index)\r
33 {\r
34         if(index>=0 && index<GetCount())\r
35         {\r
36                 return m_boards[index].m_name;\r
37         }\r
38         else\r
39         {\r
40                 return "";\r
41         }\r
42 }\r
43 \r
44 std::string BoardListXML::GetXML()\r
45 {\r
46         TiXmlDocument td;\r
47         TiXmlDeclaration *tdec=new TiXmlDeclaration("1.0","UTF-8","");\r
48         TiXmlElement *tid;\r
49         TiXmlPrinter tp;\r
50 \r
51         td.LinkEndChild(tdec);\r
52         tid=new TiXmlElement("BoardList");\r
53         td.LinkEndChild(tid);\r
54 \r
55         for(std::vector<board>::iterator i=m_boards.begin(); i!=m_boards.end(); i++)\r
56         {\r
57                 TiXmlElement *tr=new TiXmlElement("Board");\r
58                 tid->LinkEndChild(tr);\r
59                 tr->LinkEndChild(XMLCreateCDATAElement("Name",(*i).m_name));\r
60                 tr->LinkEndChild(XMLCreateCDATAElement("Description",(*i).m_description));\r
61         }\r
62 \r
63         td.Accept(&tp);\r
64         return std::string(tp.CStr());\r
65 }\r
66 \r
67 void BoardListXML::Initialize()\r
68 {\r
69         m_boards.clear();\r
70 }\r
71 \r
72 const bool BoardListXML::ParseXML(const std::string &xml)\r
73 {\r
74         std::string name;\r
75         std::string description;\r
76         TiXmlDocument td;\r
77         td.Parse(xml.c_str());\r
78 \r
79         if(!td.Error())\r
80         {\r
81                 TiXmlText *txt;\r
82                 TiXmlHandle hnd(&td);\r
83                 TiXmlNode *node;\r
84 \r
85                 Initialize();\r
86 \r
87                 node=hnd.FirstChild("BoardList").FirstChild("Board").ToElement();\r
88                 while(node)\r
89                 {\r
90                         name="";\r
91                         description="";\r
92 \r
93                         TiXmlHandle hnd2(node);\r
94                         txt=hnd2.FirstChild("Name").FirstChild().ToText();\r
95                         if(txt)\r
96                         {\r
97                                 name=txt->ValueStr();\r
98                         }\r
99                         txt=hnd2.FirstChild("Description").FirstChild().ToText();\r
100                         if(txt)\r
101                         {\r
102                                 description=txt->ValueStr();\r
103                         }\r
104 \r
105                         if(name!="" && description!="")\r
106                         {\r
107                                 m_boards.push_back(board(name,description));\r
108                         }\r
109                         \r
110                         node=node->NextSibling("Board");\r
111                 }\r
112                 return true;\r
113 \r
114         }\r
115         else\r
116         {\r
117                 return false;\r
118         }\r
119 }