RTStrCopyP.cpp revision fa073a3bc9ea2b9d238fd31e618643c5f1531327
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai/* $Id$ */
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai/** @file
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * IPRT - RTStrCopyP.
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai */
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai/*
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * Copyright (C) 2010-2011 Oracle Corporation
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai *
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * This file is part of VirtualBox Open Source Edition (OSE), as
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * available from http://www.virtualbox.org. This file is free software;
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * you can redistribute it and/or modify it under the terms of the GNU
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * General Public License (GPL) as published by the Free Software
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * Foundation, in version 2 as it comes in the "COPYING" file of the
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai *
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * The contents of this file may alternatively be used under the terms
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * of the Common Development and Distribution License Version 1.0
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * VirtualBox OSE distribution, in which case the provisions of the
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * CDDL are applicable instead of those of the GPL.
134a1f4e3289b54e0f980e9cf05352e419a60beeCasper H.S. Dik *
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai * You may elect to license modified versions of this file under the
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi * terms and conditions of either the GPL or the CDDL or both.
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi */
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai/*******************************************************************************
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai* Header Files *
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai*******************************************************************************/
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai#include <iprt/string.h>
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai#include "internal/iprt.h"
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllaiRTDECL(int) RTStrCopyP(char **ppszDst, size_t *pcbDst, const char *pszSrc)
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai{
facf4a8d7b59fde89a8662b4f4c73a758e6c402cllai size_t const cchSrc = strlen(pszSrc);
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi size_t const cbDst = *pcbDst;
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi char *pszDst = *ppszDst;
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi if (RT_LIKELY(cchSrc < cbDst))
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi {
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi memcpy(pszDst, pszSrc, cchSrc + 1);
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi *ppszDst = pszDst += cchSrc;
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi *pcbDst -= cchSrc;
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi return VINF_SUCCESS;
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi }
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi if (cbDst != 0)
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi {
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi memcpy(*ppszDst, pszSrc, cbDst - 1);
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi *ppszDst = pszDst += cbDst - 1;
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi *pszDst = '\0';
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi *pcbDst = 1;
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi }
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi return VERR_BUFFER_OVERFLOW;
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi}
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert MustacchiRT_EXPORT_SYMBOL(RTStrCopyP);
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi
9e5aa9d8a21f8efa8ba9c9e4a0aa6edc66d07eb2Robert Mustacchi