All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
BoostUtils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/BoostUtils.cpp
22 
23  \brief This file contains several utility functions for dealing with Boost containers and algorithms.
24 */
25 
26 // TerraLib
27 #include "BoostUtils.h"
28 
29 // Boost
30 #include <boost/foreach.hpp>
31 #include <boost/lexical_cast.hpp>
32 
33 //#include <boost/locale.hpp>
34 
35 //#include <boost/locale/encoding.hpp>
36 //#include <boost/locale/util.hpp>
37 #include <boost/property_tree/ptree.hpp>
38 
39 
40 //class std::locale::id boost::locale::info::id;
41 
42 void te::common::Convert(const boost::property_tree::ptree& p, std::map<std::string, std::string>& dict)
43 {
44  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v, p)
45  {
46  //if(v.first.data().empty())
47  //if(v.second.data().empty())
48  //continue;
49 
50  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v2, v.second.get_child(""))
51  {
52  const std::string& f = v2.first.data();
53  const std::string& s = v2.second.data();
54 
55  dict[f] = s;
56  }
57  }
58 }
59 
60 void te::common::Convert(const boost::property_tree::ptree& p, std::vector<std::pair<std::string, std::string> > & vp)
61 {
62  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v, p)
63  {
64  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v2, v.second.get_child(""))
65  {
66  //const std::string& f = v2.first.data();
67  //const std::string& s = v2.second.data();
68  std::pair<std::string, std::string> in_pair;
69  in_pair.first = v2.first.data();
70  in_pair.second = v2.second.data();
71  vp.push_back(in_pair);
72  }
73  }
74 }
75 
76 void te::common::Convert(const boost::property_tree::ptree& p, std::vector<std::string>& vect)
77 {
78  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v, p)
79  {
80  vect.push_back(v.second.data());
81  continue;
82  }
83 }
84 
85 void te::common::Convert(const boost::property_tree::ptree& p, std::vector<unsigned char>& vectd)
86 {
87  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v, p)
88  {
89  //vectd.push_back(boost::lexical_cast<unsigned char>(v.second.data()));
90  vectd.push_back(boost::lexical_cast<unsigned char>(v.second.data()));
91  continue;
92  }
93 }
94 
95 void te::common::Convert(const boost::property_tree::ptree& p, std::vector<double>& vectd)
96 {
97  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v, p)
98  {
99  vectd.push_back(boost::lexical_cast<double>(v.second.data()));
100  continue;
101  }
102 }
103 
104 void te::common::Convert(const boost::property_tree::ptree& p, std::vector<size_t>& vect)
105 {
106  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v, p)
107  {
108  vect.push_back(boost::lexical_cast<size_t>(v.second.data()));
109  continue;
110  }
111 }
112 
113 void te::common::Convert(const boost::property_tree::ptree& p, std::vector<std::vector<double> >& vect)
114 {
115  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v, p)
116  {
117  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v2, v.second.get_child(""))
118  {
119  std::vector<double> envrep;
120  te::common::Convert(v2.second.get_child(""), envrep);
121  //{
122  // std::vector<double>::iterator it;
123  // double d[4]={0.0,0.0,0.0,0.0}; int i=0;
124  // for(it = envrep.begin(); it < envrep.end(); it++)
125  // {
126  // d[i] = (*it);
127  // i+= 1;
128  // }
129  //}
130  vect.push_back(envrep);
131  }
132  }
133 }
134 
135 void te::common::Convert(const boost::property_tree::ptree& p, std::vector<std::map<std::string, std::string> >& vectm)
136 {
137  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v, p)
138  {
139  // used to get the properties parameters into a map
140  std::map<std::string, std::string> dict;
141  BOOST_FOREACH(const boost::property_tree::ptree::value_type& v2, v.second.get_child(""))
142  {
143  const std::string& f = v2.first.data();
144  const std::string& s = v2.second.data();
145 
146  dict[f] = s;
147  }
148  vectm.push_back(dict);
149  }
150 }
151 
152 //std::string GetSystemEncoding()
153 //{
154 // std::string loc_name = boost::locale::util::get_system_locale();
155 //
156 // size_t pos = loc_name.find(".");
157 //
158 // if(pos == std::string::npos)
159 // return "";
160 //
161 // std::string encod = loc_name.substr(pos+1);
162 //
163 // if(encod.compare("windows-1252") == 0)
164 // return "ISO8859-1";
165 //
166 // return encod;
167 //}
168 
169 std::string te::common::ConvertLatin1UTFString(const std::string& data, const bool& toUtf)
170 {
171  return data;
172  //std::string encoding = GetSystemEncoding();
173 
174  //if(encoding.empty())
175  // return data;
176 
177  //if(toUtf)
178  // return boost::locale::conv::to_utf<char>(data, "ISO8859-1");
179  //else
180  //{
181  // if(encoding.compare("ISO8859-1") == 0)
182  // return data;
183 
184  // std::string word = data;
185 
186  // if (encoding.compare("UTF-8") != 0)
187  // word = boost::locale::conv::to_utf<char>(data, encoding);
188 
189  // return boost::locale::conv::from_utf<char>(data, "ISO8859-1");
190  //}
191 }
TECOMMONEXPORT std::string ConvertLatin1UTFString(const std::string &data, const bool &toUtf=true)
Converts a string from latin1 to utf8 and vice-versa.
Definition: BoostUtils.cpp:169
TECOMMONEXPORT void Convert(const boost::property_tree::ptree &p, std::map< std::string, std::string > &dict)
Converts a property tree node into a std::map.
Definition: BoostUtils.cpp:42
This file contains several utility functions for dealing with Boost containers and algorithms...