utf8string.h

Go to the documentation of this file.
00001 // OSB library ********************************************* -*- C++ -*-
00011 /*
00012   AUTHOR(S): Andreas Huggel (ahu)
00013 
00014   RCS information
00015    $Name: OSB_060808 $
00016    $Revision: 1.11 $
00017 
00018   License
00019    OSB rating and billing library for communication networks
00020    Copyright (C) 2004, 2005, 2006  OSB systems
00021 
00022    This file may be distributed and/or modify under the terms of the
00023    GNU General Public License (GPL) as published by the Free Software
00024    Foundation which is provided in the file LICENSE.GPL included in the
00025    packaging of this file.
00026 
00027    The file is distributed in the hope that it will be useful, but WITHOUT
00028    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00029    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00030    for more details.
00031 
00032    Holders of a OSB Commercial License may use this file under the terms
00033    and conditions of this commercial license.
00034  */
00035 #ifndef _UTF8STRING_H_
00036 #define _UTF8STRING_H_
00037 
00038 // *********************************************************************
00039 // included header files
00040 // + standard includes
00041 #include <string>
00042 
00043 // + local headers
00044 #include "utf8stringC.h"
00045 
00046 namespace OSB_CORBA {
00047     using corba::common::Utf8String;
00048 
00049 // *********************************************************************
00050 // inline definitions
00051 
00053     inline std::string::size_type toSizeType(CORBA::ULong l)
00054     {
00055         return l; // let the compiler complain
00056     }
00057 
00059     inline CORBA::ULong toULong(std::string::size_type st)
00060     {
00061         return st; // let the compiler complain
00062     }
00063 
00065     inline const char* toConstCharPtr(const CORBA::Octet* o)
00066     {
00067         const unsigned char* uc = o; // let the compiler complain
00068         return reinterpret_cast<const char*>(uc);
00069     }
00070 
00072     inline char* toCharPtr(CORBA::Octet* o)
00073     {
00074         unsigned char* uc = o; // let the compiler complain
00075         return reinterpret_cast<char*>(uc);
00076     }
00077 
00079     inline const CORBA::Octet* toConstOctetPtr(const char* c)
00080     {
00081         const unsigned char* uc
00082             = reinterpret_cast<const unsigned char*>(c);
00083         const CORBA::Octet* o = uc; // let the compiler complain
00084         return o;
00085     }
00086 
00088     inline CORBA::Octet* toOctetPtr(char* c)
00089     {
00090         unsigned char* uc = reinterpret_cast<unsigned char*>(c);
00091         CORBA::Octet* o = uc;  // let the compiler complain
00092         return o;
00093     }
00094 
00104     inline std::string& fromUtf8(
00105               std::string& dest,
00106         const Utf8String&  src
00107     )
00108     {
00109         const CORBA::Octet* o = src.get_buffer();
00110         if (o == 0) return dest = std::string("");
00111         // the string constructor will throw exception
00112         // when the char* parameter pointer empty
00113         return dest = std::string(toConstCharPtr(o),
00114                                   toSizeType(src.length()));
00115     }
00116 
00125     inline std::string fromUtf8(const Utf8String& src)
00126     {
00127         std::string rc;
00128         return fromUtf8(rc, src);
00129     }
00130 
00142     inline Utf8String* toUtf8(const std::string& src)
00143     {
00144         // First use allocbuf() to allocate a buffer to hold the string
00145         // and copy the contents from the C++ string into this
00146         // buffer. This does not make assumptions about the string
00147         // containing zeros. Then use C++ new and the Corba data
00148         // constructor of the Utf8String to dynamically allocate a
00149         // Utf8String and initialise it. Set the release flag to true,
00150         // indicating that the Utf8String should take ownership of the
00151         // passed buffer (and deallocate it with freebuf()).
00152 
00153         // Note 1: This is exception safe because allocbuf() does not
00154         //         throw.
00155 
00156         // Note 2: No UTF8 conversion is currently done.
00157 
00158         // ToDo: what happens if allocbuf() fails (returns 0)?
00159         //       - should we return an 'empty' Utf8String?
00160         //         (current behaviour, which is easier to handle
00161         //          for the caller)
00162         //       - or should we return 0
00163         //         (like other Corba function do)
00164 
00165         CORBA::ULong len = src.length();
00166         CORBA::Octet* buf = Utf8String::allocbuf(len);
00167         if (buf != 0) src.copy(toCharPtr(buf), toSizeType(len));
00168 
00169         return new Utf8String(len, len, buf, 1);    // can throw.
00170     }
00171 }                                       // namespace OSB_CORBA
00172 #endif                                  // #ifndef _UTF8STRING_H_

Generated on Sat Sep 2 14:25:53 2006 for OSB Library by  doxygen 1.4.7