examples/core/encoding/main.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
3 
4  This file is part of the TerraLib - a Framework for building GIS enabled applications.
5 
6  TerraLib is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  TerraLib is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with TerraLib. See COPYING. If not, write to
18  TerraLib Team at <terralib-team@terralib.org>.
19  */
20 
21 /*!
22  \file terralib/examples/core/encoding/main.cpp
23 
24  \brief Examples for the Terralib Encoding Module
25 
26  \author Matheus Cavassan Zaglia
27 */
28 
29 // TerraLib
31 #include <terralib_buildconfig.h>
32 
33 // STL
34 #include <cstdlib>
35 #include <fstream>
36 #include <iostream>
37 
38 int main(int argc, char *argv[])
39 {
40  {
41  std::cout << "===> Handling character encoding CP1252 <===" << std::endl;
42  std::ifstream input_file(TERRALIB_DATA_DIR "/encoding/arq_cp1252.txt");
43 
44  std::string text_in_cp1252((std::istreambuf_iterator<char>(input_file)),
45  std::istreambuf_iterator<char>());
46 
47  std::cout << "Original text size in CP1252: " << text_in_cp1252.size() << std::endl;
48  std::cout << "Original text in CP1252:\n" << text_in_cp1252 << std::endl << std::endl;
49 
50  std::string text_in_utf8 = te::core::CharEncoding::toUTF8(text_in_cp1252, te::core::EncodingType::CP1252);
51 
52  std::cout << "Text size when converted from CP1252 to UTF-8: " << text_in_utf8.size() << std::endl;
53  std::cout << "Text converted from CP1252 to UTF-8:\n" << text_in_utf8 << std::endl << std::endl ;
54  }
55 
56  {
57  std::cout << "===> Handling character encoding LATIN1 <===" << std::endl;
58 
59  std::ifstream input_file(TERRALIB_DATA_DIR "/encoding/arq_latin1.txt");
60 
61  std::string text_in_latin1((std::istreambuf_iterator<char>(input_file)),
62  std::istreambuf_iterator<char>());
63 
64  std::cout << "Original text size in LATIN1: " << text_in_latin1.size() << std::endl;
65  std::cout << "Original text in LATIN1:\n" << text_in_latin1 << std::endl << std::endl;
66 
67  std::string text_in_utf8 = te::core::CharEncoding::toUTF8(text_in_latin1, te::core::EncodingType::LATIN1);
68 
69  std::cout << "Text size when converted from LATIN1 to UTF-8: " << text_in_utf8.size() << std::endl;
70  std::cout << "Text converted from LATIN1 to UTF-8:\n" << text_in_utf8 << std::endl << std::endl ;
71  }
72 
73  {
74  std::cout << "===> // Handling character encoding UTF8 <===" << std::endl;
75 
76  std::ifstream input_file(TERRALIB_DATA_DIR "/encoding/arq_utf8.txt");
77 
78  std::string text_in_utf8((std::istreambuf_iterator<char>(input_file)),
79  std::istreambuf_iterator<char>());
80 
81  std::cout << "Original text size in UTF8: " << text_in_utf8.size() << std::endl;
82  std::cout << "Original text in UTF8:\n" << text_in_utf8 << std::endl << std::endl;
83 
84  std::string text_in_latin1 = te::core::CharEncoding::fromUTF8(text_in_utf8, te::core::EncodingType::LATIN1);
85 
86  std::cout << "Text size when converted from UTF8 to LATIN1: " << text_in_latin1.size() << std::endl;
87  std::cout << "Text converted from UTF8 to LATIN1:\n" << text_in_latin1 << std::endl << std::endl;
88 
89  std::string text_in_cp1252 = te::core::CharEncoding::fromUTF8(text_in_utf8, te::core::EncodingType::CP1252);
90 
91  std::cout << "Text size when converted from UTF8 to CP1252: " << text_in_cp1252.size() << std::endl;
92  std::cout << "Text converted from UTF8 to CP1252:\n" << text_in_cp1252 << std::endl ;
93  }
94 
95  return EXIT_SUCCESS;
96 }
static std::string fromUTF8(const std::string &src)
Convert a string in UTF-8 to the current locale encoding.
int main(int argc, char *argv[])
A class for handling character enconding/decoding.
static std::string toUTF8(const std::string &src)
Convert a string from a current locale encoding to UTF-8.