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