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