string.cpp revision 76a1b5545b9d2163fd30824791f7f78650c238ec
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync/* $Id$ */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
1ce069685b24d243eb0464f46d4c56b250c64445vboxsync/** @file
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync *
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * MS COM / XPCOM Abstraction Layer:
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * UTF-8 and UTF-16 string classes
9ce5d949e4f1572d445a5c0aecabe9de8b672c99vboxsync */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync/*
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * Copyright (C) 2006-2007 Oracle Corporation
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync *
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * available from http://www.virtualbox.org. This file is free software;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * you can redistribute it and/or modify it under the terms of the GNU
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * General Public License (GPL) as published by the Free Software
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#include "VBox/com/string.h"
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#include <iprt/err.h>
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#include <iprt/path.h>
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncnamespace com
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync{
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync// BSTR representing a null wide char with 32 bits of length prefix (0);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync// this will work on Windows as well as other platforms where BSTR does
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync// not use length prefixes
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncconst OLECHAR g_achEmptyBstr[3] = { 0, 0, 0 };
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncconst BSTR g_bstrEmpty = (BSTR)(&g_achEmptyBstr[2]);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync/* static */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncconst Bstr Bstr::Empty; /* default ctor is OK */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
9ce5d949e4f1572d445a5c0aecabe9de8b672c99vboxsync/* static */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncconst Utf8Str Utf8Str::Empty; /* default ctor is OK */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#if defined (VBOX_WITH_XPCOM)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncvoid Utf8Str::cloneTo(char **pstr) const
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync{
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync size_t cb = length() + 1;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync *pstr = (char*)nsMemory::Alloc(cb);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (!*pstr)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync throw std::bad_alloc();
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync memcpy(*pstr, c_str(), cb);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync}
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync#endif
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncUtf8Str& Utf8Str::toLower()
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync{
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (length())
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync ::RTStrToLower(m_psz);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync return *this;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync}
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncUtf8Str& Utf8Str::toUpper()
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync{
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (length())
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync ::RTStrToUpper(m_psz);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync return *this;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync}
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncvoid Utf8Str::stripTrailingSlash()
376b92d26cc4fad78e813cf33afcc0784adc9b19vboxsync{
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync RTPathStripTrailingSlash(m_psz);
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync jolt();
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync}
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsyncvoid Utf8Str::stripFilename()
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync{
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync RTPathStripFilename(m_psz);
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync jolt();
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync}
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsyncvoid Utf8Str::stripExt()
376b92d26cc4fad78e813cf33afcc0784adc9b19vboxsync{
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync RTPathStripExt(m_psz);
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync jolt();
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync}
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync/**
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync * Internal function used in Utf8Str copy constructors and assignment when
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * copying from a UTF-16 string.
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync *
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * As with the iprt::ministring::copyFrom() variants, this unconditionally
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * sets the members to a copy of the given other strings and makes
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * no assumptions about previous contents. This can therefore be used
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * both in copy constructors, when member variables have no defined
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * value, and in assignments after having called cleanup().
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync *
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * This variant converts from a UTF-16 string, most probably from
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * a Bstr assignment.
508452243fd3328f7b9e0405d39fb9dc004e31b8vboxsync *
508452243fd3328f7b9e0405d39fb9dc004e31b8vboxsync * @param s
508452243fd3328f7b9e0405d39fb9dc004e31b8vboxsync */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncvoid Utf8Str::copyFrom(CBSTR s)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync{
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (s && *s)
f409459bdd4c15cdb8d7fb6c6d54338cce9ac814vboxsync {
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync int vrc = RTUtf16ToUtf8Ex((PRTUTF16)s, // PCRTUTF16 pwszString
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync RTSTR_MAX, // size_t cwcString: translate entire string
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync &m_psz, // char **ppsz: output buffer
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync 0, // size_t cch: if 0, func allocates buffer in *ppsz
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync &m_cbLength); // size_t *pcch: receives the size of the output string, excluding the terminator.
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (RT_FAILURE(vrc))
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync {
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if ( vrc == VERR_NO_STR_MEMORY
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync || vrc == VERR_NO_MEMORY
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync )
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync throw std::bad_alloc();
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync // @todo what do we do with bad input strings? throw also? for now just keep an empty string
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync m_cbLength = 0;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync m_cbAllocated = 0;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync m_psz = NULL;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync }
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync else
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync m_cbAllocated = m_cbLength + 1;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync }
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync else
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync {
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync m_cbLength = 0;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync m_cbAllocated = 0;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync m_psz = NULL;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync }
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync}
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsyncvoid Utf8StrFmt::init(const char *format, va_list args)
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync{
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync if (!format || !*format)
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync {
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync m_cbLength = 0;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync m_cbAllocated = 0;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync m_psz = NULL;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync }
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync else
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync {
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync m_cbLength = RTStrAPrintfV(&m_psz, format, args);
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync m_cbAllocated = m_cbLength + 1;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync }
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync}
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync} /* namespace com */
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync