Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleBSTRUTF.cpp

Example how to convert a BSTR to UTF-8 such that the string in XML would not be corrupted

Author
Evers
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <Base/Common/XMLIO.hh>
#include <libxml/encoding.h>
#ifdef WIN32
#endif
using namespace BIAS;
using namespace std;
/**
@example ExampleBSTRUTF.cpp
@brief Example how to convert a BSTR to UTF-8 such that the string in XML would not be corrupted
@ingroup g_examples
@author Evers
*/
#ifdef WIN32
string ConvertBSTRToString(BSTR pSrc)
{
DWORD cb,cwch = ::SysStringLen(pSrc);
char *szOut = NULL;
string result;
cb = ::WideCharToMultiByte(CP_ACP, 0, pSrc, cwch + 1, NULL, 0, 0, 0);
if(cb) {
szOut = new char[cb];
if(szOut) {
szOut[cb - 1] = '\0';
if(!::WideCharToMultiByte(CP_ACP, 0,pSrc, cwch + 1, szOut, cb, 0, 0))
delete []szOut;
else {
result = szOut;
delete[] szOut;
}
}
}
return result;
}
#endif
int main (int /*argc*/, char** /*argv*/)
{
string MyAnsiString,MyUTF8String;
#ifdef WIN32
BSTR MyBstr;
MyBstr = ::SysAllocStringLen(NULL,14);
MyBstr[0] = 85;
MyBstr[1] = 83;
MyBstr[2] = 66;
MyBstr[3] = 45;
MyBstr[4] = 86;
MyBstr[5] = 105;
MyBstr[6] = 100;
MyBstr[7] = 101;
MyBstr[8] = 111;
MyBstr[9] = 103;
MyBstr[10] = 101;
MyBstr[11] = 114;
MyBstr[12] = 228;
MyBstr[13] = 116;
MyAnsiString = ConvertBSTRToString(MyBstr);
#endif
XMLIO::IsoLatin1ToUtf8(MyAnsiString,MyUTF8String);
// remember some node pointers
xmlNodePtr rootNode;
xmlNodePtr currentNode2;
// create an instance of XMLIO and initialize it with a new XML-File
XMLIO myXML;
// root node is named "XMLTest"
rootNode = myXML.create("XMLTest");
// set some nodes and attributes
currentNode2 = myXML.addChildNode(rootNode, "Child1");
myXML.addAttribute(currentNode2, "AsUTF8", MyUTF8String );
myXML.addAttribute(currentNode2, "AsAnsi", MyAnsiString );
myXML.addContent(currentNode2, MyUTF8String );
// write the whole predefined structure
string MyFilename = "Test-"+MyAnsiString+".xml";
if (myXML.write(MyFilename) <0) exit(1);
return 0;
}