SUMO - Simulation of Urban MObility
TplConvert.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Some conversion methods (from strings to other)
18 /****************************************************************************/
19 #ifndef TplConvert_h
20 #define TplConvert_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <string>
29 #include <sstream>
30 #include <cmath>
31 #include <limits>
32 #include <algorithm>
33 #include <xercesc/util/TransService.hpp>
34 #include <xercesc/util/TranscodingException.hpp>
36 #include <utils/common/StdDefs.h>
37 #include <utils/common/ToString.h>
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
47 class TplConvert {
48 public:
50 
51 
54  static inline std::string _2str(const XMLCh* const data) {
55  return _2str(data, (int)XERCES_CPP_NAMESPACE::XMLString::stringLen(data));
56  }
57 
61  static inline std::string _2str(const XMLCh* const data, int length) {
62  if (data == 0) {
63  throw EmptyData();
64  }
65  if (length == 0) {
66  return "";
67  }
68 #if _XERCES_VERSION < 30100
69  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(data);
70  std::string result(t);
71  XERCES_CPP_NAMESPACE::XMLString::release(&t);
72  return result;
73 #else
74  try {
75  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(data, "UTF-8");
76  return reinterpret_cast<const char*>(utf8.str());
77  } catch (XERCES_CPP_NAMESPACE::TranscodingException&) {
78  return "?";
79  }
80 #endif
81  }
83 };
84 
85 
86 #endif
87 
88 /****************************************************************************/
static std::string _2str(const XMLCh *const data)
converts a 0-terminated char-type array into std::string
Definition: TplConvert.h:54
static std::string _2str(const XMLCh *const data, int length)
converts a char-type array into std::string considering the given length
Definition: TplConvert.h:61