Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleBSTRUTF.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <Base/Common/XMLIO.hh>
26 #include <libxml/encoding.h>
27 #ifdef WIN32
28 
29 #endif
30 using namespace BIAS;
31 using namespace std;
32 
33 
34 /**
35  @example ExampleBSTRUTF.cpp
36  @brief Example how to convert a BSTR to UTF-8 such that the string in XML would not be corrupted
37  @ingroup g_examples
38  @author Evers
39 */
40 
41 #ifdef WIN32
42 string ConvertBSTRToString(BSTR pSrc)
43 {
44  DWORD cb,cwch = ::SysStringLen(pSrc);
45  char *szOut = NULL;
46  string result;
47  cb = ::WideCharToMultiByte(CP_ACP, 0, pSrc, cwch + 1, NULL, 0, 0, 0);
48  if(cb) {
49  szOut = new char[cb];
50  if(szOut) {
51  szOut[cb - 1] = '\0';
52  if(!::WideCharToMultiByte(CP_ACP, 0,pSrc, cwch + 1, szOut, cb, 0, 0))
53  delete []szOut;
54  else {
55  result = szOut;
56  delete[] szOut;
57  }
58  }
59  }
60  return result;
61 }
62 
63 #endif
64 
65 
66 
67 
68 
69 
70 
71 
72 int main (int /*argc*/, char** /*argv*/)
73 {
74 
75  string MyAnsiString,MyUTF8String;
76 #ifdef WIN32
77 
78  BSTR MyBstr;
79  MyBstr = ::SysAllocStringLen(NULL,14);
80  MyBstr[0] = 85;
81  MyBstr[1] = 83;
82  MyBstr[2] = 66;
83  MyBstr[3] = 45;
84  MyBstr[4] = 86;
85  MyBstr[5] = 105;
86  MyBstr[6] = 100;
87  MyBstr[7] = 101;
88  MyBstr[8] = 111;
89  MyBstr[9] = 103;
90  MyBstr[10] = 101;
91  MyBstr[11] = 114;
92  MyBstr[12] = 228;
93  MyBstr[13] = 116;
94 
95  MyAnsiString = ConvertBSTRToString(MyBstr);
96 #endif
97 
98  XMLIO::IsoLatin1ToUtf8(MyAnsiString,MyUTF8String);
99  // remember some node pointers
100  xmlNodePtr rootNode;
101  xmlNodePtr currentNode2;
102 
103  // create an instance of XMLIO and initialize it with a new XML-File
104  XMLIO myXML;
105 
106  // root node is named "XMLTest"
107  rootNode = myXML.create("XMLTest");
108 
109  // set some nodes and attributes
110  currentNode2 = myXML.addChildNode(rootNode, "Child1");
111 
112  myXML.addAttribute(currentNode2, "AsUTF8", MyUTF8String );
113  myXML.addAttribute(currentNode2, "AsAnsi", MyAnsiString );
114 
115  myXML.addContent(currentNode2, MyUTF8String );
116  // write the whole predefined structure
117  string MyFilename = "Test-"+MyAnsiString+".xml";
118  if (myXML.write(MyFilename) <0) exit(1);
119 
120 
121  return 0;
122 
123 }
void addAttribute(const xmlNodePtr Node, const std::string &AttributeName, bool AttributeValue)
Add an attribute to a node.
Definition: XMLIO.cpp:156
int write(const std::string &Filename, bool AutoAddCompressionSuffix=true) const
Write the whole tree that was constructed in memory to disk.
Definition: XMLIO.cpp:379
xmlNodePtr create(const std::string &RootNodeName)
Create the base of a new XML-Tree in memory, already with a one and only root node.
Definition: XMLIO.cpp:88
static int IsoLatin1ToUtf8(const std::string &isoLatin1, std::string &utf8)
Convert character string from UTF-8 format to ISO 8895-1.
Definition: XMLIO.cpp:932
void addContent(const xmlNodePtr Node, const std::string &Content)
Add content to a node.
Definition: XMLIO.cpp:254
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
xmlNodePtr addChildNode(const xmlNodePtr ParentNode, const std::string &NewNodeName)
Add a child node to an incoming node with the given name.
Definition: XMLIO.cpp:131