Kuon  1.1.3
RoadNarrows Robotics Large Outdoor Mobile Robot Project
kuonXmlCfg.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: Kuon
4 //
5 // Library: libkuon
6 //
7 // File: kuonXmlCfg.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2014-04-09 14:29:33 -0600 (Wed, 09 Apr 2014) $
12  * $Rev: 3635 $
13  *
14  * \brief \h_kuon XML configuration class implementation.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2014-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 /*
24  * @EulaBegin@
25  *
26  * Permission is hereby granted, without written agreement and without
27  * license or royalty fees, to use, copy, modify, and distribute this
28  * software and its documentation for any purpose, provided that
29  * (1) The above copyright notice and the following two paragraphs
30  * appear in all copies of the source code and (2) redistributions
31  * including binaries reproduces these notices in the supporting
32  * documentation. Substantial modifications to this software may be
33  * copyrighted by their authors and need not follow the licensing terms
34  * described here, provided that the new terms are clearly indicated in
35  * all files where they apply.
36  *
37  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
38  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
39  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
40  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
41  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
42  * THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
45  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
46  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
47  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
48  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
49  *
50  * @EulaEnd@
51  */
52 ////////////////////////////////////////////////////////////////////////////////
53 
54 #include <string>
55 
56 #include "rnr/rnrconfig.h"
57 #include "rnr/log.h"
58 
59 #include "rnr/appkit/Xml.h"
60 
61 #include "Kuon/kuon.h"
62 #include "Kuon/kuonDescBase.h"
63 #include "Kuon/kuonDesc.h"
64 #include "Kuon/kuonXmlCfg.h"
65 
66 
67 using namespace std;
68 using namespace rnr;
69 using namespace kuon;
70 
71 
72 int KuonXmlCfg::createTemplateFile(const string &strXmlFileName)
73 {
74  FILE *fp;
75 
76  if( strXmlFileName.empty() )
77  {
78  setErrorMsg("No file name.");
79  LOGERROR("%s", m_bufErrMsg);
80  return -KUON_ECODE_XML;
81  }
82 
83  m_strXmlFileName = strXmlFileName;
84 
85  // open file
86  if( (fp = fopen(m_strXmlFileName.c_str(), "w+")) == NULL )
87  {
88  setErrorMsg("%s: %s(errno=%d).",
89  m_strXmlFileName.c_str(), strerror(errno), errno);
90  LOGERROR("%s", m_bufErrMsg);
91  return -KUON_ECODE_XML;
92  }
93 
94  makeXmlHead();
95  makeXmlTail();
96 
97  //
98  // XML head.
99  //
100  fprintf(fp, " <!-- RoadNarrows Kuon Top-Level Configuration -->\n");
101  fprintf(fp, "%s", m_strXmlHead.c_str());
102 
103  //
104  // Robotic base major element.
105  //
106  fprintf(fp, " <!-- Kuon robotic mobile platform -->\n");
107  fprintf(fp, " <%s product_id=\"PRODID\">\n", m_strMajElemBase.c_str());
108 
109  fprintf(fp, " <%s>PRODNAME</%s>\n",
110  m_strElemProdName.c_str(), m_strElemProdName.c_str());
111 
112  fprintf(fp, " <%s>\n PRODBRIEF\n </%s>\n",
113  m_strElemProdBrief.c_str(), m_strElemProdBrief.c_str());
114 
115  fprintf(fp, " <%s>PRODHWVER</%s>\n",
116  m_strElemProdHwVer.c_str(), m_strElemProdHwVer.c_str());
117 
118  fprintf(fp, " <%s>FRONTTIRE</%s>\n",
119  m_strElemProdFrontTire.c_str(), m_strElemProdFrontTire.c_str());
120 
121  fprintf(fp, " <%s>REARTIRE</%s>\n",
122  m_strElemProdRearTire.c_str(), m_strElemProdRearTire.c_str());
123 
124  fprintf(fp, " </%s>\n\n", m_strMajElemBase.c_str());
125 
126 
127  //
128  // XML tail
129  //
130  fprintf(fp, "%s", m_strXmlTail.c_str());
131 
132  fclose(fp);
133 
134  LOGDIAG3("Created file %s.", m_strXmlFileName.c_str());
135 
136  return KUON_OK;
137 }
138 
139 int KuonXmlCfg::setKuonDescFromDOM(KuonDesc &desc)
140 {
141  KuonDescBase *pDescBase;
142  TiXmlElement *pElem;
143  const char *sValue;
144  const char *sAttr;
145  const char *sText;
146  int n;
147  int rc;
148 
149  //
150  // Subsection descriptions.
151  //
152  pDescBase = desc.getBaseDesc();
153 
154  pDescBase->resetDesc();
155 
156  if( m_pElemRoot == NULL )
157  {
158  setErrorMsg("Missing DOM and/or <%s> root element missing.",
159  m_strRootElemName.c_str());
160  LOGERROR("%s", m_bufErrMsg);
161  return -KUON_ECODE_XML;
162  }
163 
164  // search secion elements
165  for(pElem = m_pElemRoot->FirstChildElement();
166  pElem != NULL;
167  pElem = pElem->NextSiblingElement())
168  {
169  if( (sValue = pElem->Value()) == NULL )
170  {
171  continue;
172  }
173 
174  // robotic base description
175  if( !strcasecmp(sValue, m_strMajElemBase.c_str()) )
176  {
177  setKuonBaseDescFromDOM(pElem, pDescBase);
178  }
179  }
180 
181  return KUON_OK;
182 }
183 
184 int KuonXmlCfg::setDOMFromKuonDesc(const KuonDesc &desc)
185 {
186  // TODO
187  return -KUON_ECODE_GEN;
188 }
189 
190 int KuonXmlCfg::setKuonBaseDescFromDOM(TiXmlElement *pElemMaj,
191  KuonDescBase *pDesc)
192 {
193  TiXmlElement *pElem;
194  const char *sValue;
195  string str;
196  int eProdId;
197  string strProdName;
198  string strProdBrief;
199  string strHwVer;
200  double fFrontTire = 0.0;
201  double fRearTire = 0.0;
202  int rc;
203 
204  if( (rc = strToInt(elemAttr(pElemMaj, m_strAttrProdId), eProdId)) < 0 )
205  {
206  setErrorMsg("%s: No %s attribute of <%s> found or value not an integer.",
207  m_strXmlFileName.c_str(),
208  m_strAttrProdId.c_str(),
209  m_strMajElemBase.c_str());
210  LOGERROR("%s", m_bufErrMsg);
211  return -KUON_ECODE_XML;
212  }
213 
214  // child elements
215  for(pElem = pElemMaj->FirstChildElement();
216  pElem != NULL;
217  pElem = pElem->NextSiblingElement())
218  {
219  if( (sValue = pElem->Value()) == NULL )
220  {
221  continue;
222  }
223 
224  // product name
225  else if( !strcasecmp(sValue, m_strElemProdName.c_str()) )
226  {
227  strProdName = elemText(pElem);
228  }
229 
230  // product brief
231  else if( !strcasecmp(sValue, m_strElemProdBrief.c_str()) )
232  {
233  strProdBrief = elemText(pElem);
234  }
235 
236  // product hardware version
237  else if( !strcasecmp(sValue, m_strElemProdHwVer.c_str()) )
238  {
239  strHwVer = elemText(pElem);
240  }
241 
242  // front tires radius
243  else if( !strcasecmp(sValue, m_strElemProdFrontTire.c_str()) )
244  {
245  str = elemText(pElem);
246  if( !str.empty() )
247  {
248  if( (rc = strToDouble(str, fFrontTire)) < 0 )
249  {
250  setErrorMsg("%s: Element <%s> text \"%s\" not an number.",
251  m_strXmlFileName.c_str(), m_strElemProdFrontTire.c_str(),
252  str.c_str());
253  LOGERROR("%s", m_bufErrMsg);
254  return -KUON_ECODE_XML;
255  }
256  }
257  }
258 
259  // rear tires radius
260  else if( !strcasecmp(sValue, m_strElemProdRearTire.c_str()) )
261  {
262  str = elemText(pElem);
263  if( !str.empty() )
264  {
265  if( (rc = strToDouble(str, fRearTire)) < 0 )
266  {
267  setErrorMsg("%s: Element <%s> text \"%s\" not an number.",
268  m_strXmlFileName.c_str(), m_strElemProdRearTire.c_str(),
269  str.c_str());
270  LOGERROR("%s", m_bufErrMsg);
271  return -KUON_ECODE_XML;
272  }
273  }
274  }
275  }
276 
277  pDesc->setDesc(eProdId, strProdName, strProdBrief, strHwVer,
278  fFrontTire, fRearTire);
279 
280  LOGDIAG3("%s: Kuon robotic base description set.",
281  m_strXmlFileName.c_str());
282 
283  return KUON_OK;
284 }
static const int KUON_OK
not an error, success
Definition: kuon.h:80
Kuon robotic mobile base escription class.
Definition: kuonDescBase.h:79
static const int KUON_ECODE_XML
XML error.
Definition: kuon.h:102
Kuon robotic base mobile platform description class interface.
The <b><i>Kuon</i></b> namespace encapsulates all <b><i>Kuon</i></b> related constructs.
Definition: kuon.h:66
RoadNarrows Kuon robot top-level header file.
static const int KUON_ECODE_GEN
general, unspecified error
Definition: kuon.h:82
<b><i>Kuon</i></b> XML configuration class interface.
Kuon full robotic mobile platform descripition class interface.
Kuon robotic manipulator full description class.
Definition: kuonDesc.h:83
void resetDesc()
Reset base description to the "unitialized" values.
KuonDescBase * getBaseDesc()
Get the <b><i>Kuon</i></b> base product description.
Definition: kuonDesc.h:156