| 1 | /********************************************************************** |
| 2 | typeconversion.cc: class to handle type conversions |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2020 European Union (Joint Research Centre) |
| 5 | |
| 6 | This file is part of jiplib. |
| 7 | |
| 8 | jiplib is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | jiplib is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | ***********************************************************************/ |
| 21 | /////////////////// Specializations ///////////////// |
| 22 | #include "typeconversion.h" |
| 23 | #include "ogr_feature.h" |
| 24 | |
| 25 | ///specialization for string |
| 26 | template<> std::string string2type(std::string const& s){ |
| 27 | return s; |
| 28 | } |
| 29 | |
| 30 | template<> double string2type(std::string const& s){ |
| 31 | std::istringstream i; |
| 32 | i.precision(12); |
| 33 | i.str(s); |
| 34 | double x; |
| 35 | if (!(i >> std::setprecision(12) >> x) ) |
| 36 | throw BadConversion(s); |
| 37 | return x; |
| 38 | } |
| 39 | |
| 40 | template<> float string2type(std::string const& s){ |
| 41 | std::istringstream i; |
| 42 | i.precision(12); |
| 43 | i.str(s); |
| 44 | float x; |
| 45 | if (!(i >> std::setprecision(12) >> x) ) |
| 46 | throw BadConversion(s); |
| 47 | return x; |
| 48 | } |
| 49 | |
| 50 | ///specialization for OGRFieldType |
| 51 | template<> OGRFieldType string2type(std::string const& s){ |
| 52 | OGRFieldType ftype; |
| 53 | int ogr_typecount=11;//hard coded for now! |
| 54 | for(int iType = 0; iType < ogr_typecount; ++iType){ |
| 55 | if( OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType) != NULL |
| 56 | && EQUAL(OGRFieldDefn::GetFieldTypeName((OGRFieldType)iType),s.c_str())) |
| 57 | ftype=(OGRFieldType) iType; |
| 58 | } |
| 59 | return ftype; |
| 60 | } |
| 61 | |
| 62 | ///specialization for bool |
| 63 | template<> std::string type2string(bool const& value){ |
| 64 | if(value) |
| 65 | return("true"); |
| 66 | else |
| 67 | return("false"); |
| 68 | } |
| 69 | |
| 70 | ///specialization for string |
| 71 | template<> std::string type2string(std::string const& value){ |
| 72 | // if(value.empty()) |
| 73 | // return("<empty string>"); |
| 74 | // else |
| 75 | return(value); |
| 76 | } |
| 77 | |
| 78 | ///specialization for float |
| 79 | template<> std::string type2string(float const& value){ |
| 80 | std::ostringstream oss; |
| 81 | // oss.precision(1); |
| 82 | // oss.setf(ios::fixed); |
| 83 | oss << value; |
| 84 | return oss.str(); |
| 85 | } |
| 86 | |
| 87 | ///specialization for double |
| 88 | template<> std::string type2string(double const& value){ |
| 89 | std::ostringstream oss; |
| 90 | // oss.precision(1); |
| 91 | // oss.setf(ios::fixed); |
| 92 | oss << value; |
| 93 | return oss.str(); |
| 94 | } |