version 0.1.15
[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::LowerCase(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                 std::string boardname=(*i).m_name;\r
60                 StringFunctions::Convert(boardname,boardname);\r
61                 TiXmlElement *tr=new TiXmlElement("Board");\r
62                 tid->LinkEndChild(tr);\r
63                 tr->LinkEndChild(XMLCreateCDATAElement("Name",boardname));\r
64                 tr->LinkEndChild(XMLCreateCDATAElement("Description",(*i).m_description));\r
65         }\r
66 \r
67         td.Accept(&tp);\r
68         return std::string(tp.CStr());\r
69 }\r
70 \r
71 void BoardListXML::Initialize()\r
72 {\r
73         m_boards.clear();\r
74 }\r
75 \r
76 const bool BoardListXML::ParseXML(const std::string &xml)\r
77 {\r
78         std::string name;\r
79         std::string description;\r
80         TiXmlDocument td;\r
81         td.Parse(xml.c_str());\r
82 \r
83         if(!td.Error())\r
84         {\r
85                 TiXmlText *txt;\r
86                 TiXmlHandle hnd(&td);\r
87                 TiXmlNode *node;\r
88 \r
89                 Initialize();\r
90 \r
91                 node=hnd.FirstChild("BoardList").FirstChild("Board").ToElement();\r
92                 while(node)\r
93                 {\r
94                         name="";\r
95                         description="";\r
96 \r
97                         TiXmlHandle hnd2(node);\r
98                         txt=hnd2.FirstChild("Name").FirstChild().ToText();\r
99                         if(txt)\r
100                         {\r
101                                 name=txt->ValueStr();\r
102                                 StringFunctions::LowerCase(name,name);\r
103                         }\r
104                         txt=hnd2.FirstChild("Description").FirstChild().ToText();\r
105                         if(txt)\r
106                         {\r
107                                 description=txt->ValueStr();\r
108                         }\r
109 \r
110                         if(name!="" && description!="")\r
111                         {\r
112                                 m_boards.push_back(board(name,description));\r
113                         }\r
114                         \r
115                         node=node->NextSibling("Board");\r
116                 }\r
117                 return true;\r
118 \r
119         }\r
120         else\r
121         {\r
122                 return false;\r
123         }\r
124 }\r