string.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift/* $Id$ */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift/** @file
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * InnoTek Portable Runtime - String Manipulation.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift/*
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Copyright (C) 2006 InnoTek Systemberatung GmbH
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
8cf870d281dc8c242f083d14dfef05f24aa5fceeJnRouvignac * This file is part of VirtualBox Open Source Edition (OSE), as
8cf870d281dc8c242f083d14dfef05f24aa5fceeJnRouvignac * available from http://www.virtualbox.org. This file is free software;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * you can redistribute it and/or modify it under the terms of the GNU
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * General Public License as published by the Free Software Foundation,
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * distribution. VirtualBox OSE is distributed in the hope that it will
8cf870d281dc8c242f083d14dfef05f24aa5fceeJnRouvignac * be useful, but WITHOUT ANY WARRANTY of any kind.
8cf870d281dc8c242f083d14dfef05f24aa5fceeJnRouvignac *
8cf870d281dc8c242f083d14dfef05f24aa5fceeJnRouvignac * If you received this file as part of a commercial VirtualBox
8cf870d281dc8c242f083d14dfef05f24aa5fceeJnRouvignac * distribution, then only the terms of your commercial VirtualBox
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * license agreement apply instead of the previous paragraph.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift/*******************************************************************************
a3d3ab94806056d2355afea6fe8daac41059b9fbludovicp* Header Files *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift*******************************************************************************/
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift#include <iprt/string.h>
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift#include <iprt/alloc.h>
bedb386242727f98834c64397487f48d5eb6116cboli#include <iprt/assert.h>
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift#include <iprt/err.h>
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift#include "internal/string.h"
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift#include <locale.h>
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
cd845149d3f0adb85c23d522477ffb6d7c16cc7ectissot
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake/**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Init C runtime locale
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * note: actually where is no need in this global var, use it only for
53247d28ba99538f841a13ea2cde01c3faa3ef36kenneth_suter * auto run of setlocale() func.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake/** @todo rewrite this to do setlocale() from some proper init function. */
98a1007979c3bb0ffa38f3eecff66b174172be67treydrakestatic int g_RTLocaleInited = (setlocale(LC_CTYPE, "") != NULL);
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake/**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Free string allocated by any of the non-UCS-2 string functions.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @returns iprt status code.
cd845149d3f0adb85c23d522477ffb6d7c16cc7ectissot * @param pszString Pointer to buffer with string to free.
cd845149d3f0adb85c23d522477ffb6d7c16cc7ectissot * NULL is accepted.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swiftRTR3DECL(void) RTStrFree(char *pszString)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift{
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift if (pszString)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift RTMemTmpFree(pszString);
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake}
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift/**
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * Allocates a new copy of the given UTF-8 string.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @returns Pointer to the allocated UTF-8 string.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift * @param pszString UTF-8 string to duplicate.
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift */
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swiftRTR3DECL(char *) RTStrDup(const char *pszString)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift{
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift Assert(VALID_PTR(pszString));
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift size_t cch = strlen(pszString) + 1;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift char *psz = (char *)RTMemAlloc(cch);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift if (psz)
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift memcpy(psz, pszString, cch);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift return psz;
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake}
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake/**
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake * Allocates a new copy of the given UTF-8 string.
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake *
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake * @returns iprt status code.
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake * @param ppszString Receives pointer of the allocated UTF-8 string.
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake * The returned pointer must be freed using RTStrFree().
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake * @param pszString UTF-8 string to duplicate.
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake */
98a1007979c3bb0ffa38f3eecff66b174172be67treydrakeRTR3DECL(int) RTStrDupEx(char **ppszString, const char *pszString)
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake{
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake Assert(VALID_PTR(ppszString));
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake Assert(VALID_PTR(pszString));
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift size_t cch = strlen(pszString) + 1;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift char *psz = (char *)RTMemAlloc(cch);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift if (psz)
98a1007979c3bb0ffa38f3eecff66b174172be67treydrake {
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift memcpy(psz, pszString, cch);
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift *ppszString = psz;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift return VINF_SUCCESS;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift }
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift return VERR_NO_MEMORY;
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift}
bb8874d71cdd8e5288297b9727703437c6dfcfedmatthew_swift
cd845149d3f0adb85c23d522477ffb6d7c16cc7ectissot