1999-03-29 19:52:54 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-03-29 19:52:54 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-03-29 19:52:54 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-03-29 19:52:54 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:43:54 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-03-29 19:52:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsDebug.h"
|
1999-04-30 18:49:40 +00:00
|
|
|
#include "nsIAllocator.h"
|
1999-03-29 19:52:54 +00:00
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "plstr.h"
|
|
|
|
|
1999-04-30 18:49:40 +00:00
|
|
|
// If the allocator changes, fix it here.
|
|
|
|
#define XPIDL_STRING_ALLOC(__len) ((PRUnichar*) nsAllocator::Alloc((__len) * sizeof(PRUnichar)))
|
|
|
|
#define XPIDL_CSTRING_ALLOC(__len) ((char*) nsAllocator::Alloc((__len) * sizeof(char)))
|
|
|
|
#define XPIDL_FREE(__ptr) (nsAllocator::Free(__ptr))
|
1999-03-29 19:52:54 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsXPIDLString
|
|
|
|
|
|
|
|
nsXPIDLString::nsXPIDLString()
|
1999-07-16 22:00:52 +00:00
|
|
|
: mBuf(0),
|
|
|
|
mBufOwner(PR_FALSE)
|
1999-03-29 19:52:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsXPIDLString::~nsXPIDLString()
|
|
|
|
{
|
|
|
|
if (mBufOwner && mBuf)
|
|
|
|
XPIDL_FREE(mBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-16 15:43:55 +00:00
|
|
|
nsXPIDLString::operator const PRUnichar*()
|
|
|
|
{
|
|
|
|
return mBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-29 19:52:54 +00:00
|
|
|
PRUnichar*
|
|
|
|
nsXPIDLString::Copy(const PRUnichar* aString)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aString, "null ptr");
|
|
|
|
if (! aString)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
PRInt32 len = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
const PRUnichar* p = aString;
|
|
|
|
while (*p++)
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUnichar* result = XPIDL_STRING_ALLOC(len + 1);
|
|
|
|
if (result) {
|
|
|
|
PRUnichar* q = result;
|
|
|
|
while (*aString) {
|
|
|
|
*q = *aString;
|
|
|
|
q++;
|
|
|
|
aString++;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRUnichar**
|
|
|
|
nsXPIDLString::StartAssignmentByValue()
|
|
|
|
{
|
|
|
|
if (mBufOwner && mBuf)
|
|
|
|
XPIDL_FREE(mBuf);
|
|
|
|
|
1999-06-12 16:39:11 +00:00
|
|
|
mBuf = 0;
|
1999-03-29 19:52:54 +00:00
|
|
|
mBufOwner = PR_TRUE;
|
|
|
|
return &mBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const PRUnichar**
|
|
|
|
nsXPIDLString::StartAssignmentByReference()
|
|
|
|
{
|
|
|
|
if (mBufOwner && mBuf)
|
|
|
|
XPIDL_FREE(mBuf);
|
|
|
|
|
1999-06-12 16:39:11 +00:00
|
|
|
mBuf = 0;
|
1999-03-29 19:52:54 +00:00
|
|
|
mBufOwner = PR_FALSE;
|
|
|
|
return (const PRUnichar**) &mBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsXPIDLCString
|
|
|
|
|
|
|
|
nsXPIDLCString::nsXPIDLCString()
|
1999-07-16 22:00:52 +00:00
|
|
|
: mBuf(0),
|
|
|
|
mBufOwner(PR_FALSE)
|
1999-03-29 19:52:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsXPIDLCString::~nsXPIDLCString()
|
|
|
|
{
|
|
|
|
if (mBufOwner && mBuf)
|
|
|
|
XPIDL_FREE(mBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-11 23:00:38 +00:00
|
|
|
nsXPIDLCString& nsXPIDLCString::operator =(const char* aCString)
|
|
|
|
{
|
|
|
|
if (mBufOwner && mBuf)
|
|
|
|
XPIDL_FREE(mBuf);
|
|
|
|
|
2000-02-24 06:42:36 +00:00
|
|
|
if (aCString) {
|
2000-02-24 05:34:28 +00:00
|
|
|
mBuf = Copy(aCString);
|
2000-02-24 06:42:36 +00:00
|
|
|
mBufOwner = PR_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mBuf = 0;
|
|
|
|
mBufOwner = PR_FALSE;
|
|
|
|
}
|
1999-09-11 23:00:38 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-16 15:43:55 +00:00
|
|
|
nsXPIDLCString::operator const char*()
|
|
|
|
{
|
|
|
|
return mBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-29 19:52:54 +00:00
|
|
|
char*
|
|
|
|
nsXPIDLCString::Copy(const char* aCString)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aCString, "null ptr");
|
|
|
|
if (! aCString)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
PRInt32 len = PL_strlen(aCString);
|
|
|
|
char* result = XPIDL_CSTRING_ALLOC(len + 1);
|
|
|
|
if (result)
|
|
|
|
PL_strcpy(result, aCString);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char**
|
|
|
|
nsXPIDLCString::StartAssignmentByValue()
|
|
|
|
{
|
|
|
|
if (mBufOwner && mBuf)
|
|
|
|
XPIDL_FREE(mBuf);
|
|
|
|
|
1999-06-12 16:39:11 +00:00
|
|
|
mBuf = 0;
|
1999-03-29 19:52:54 +00:00
|
|
|
mBufOwner = PR_TRUE;
|
|
|
|
return &mBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char**
|
|
|
|
nsXPIDLCString::StartAssignmentByReference()
|
|
|
|
{
|
|
|
|
if (mBufOwner && mBuf)
|
|
|
|
XPIDL_FREE(mBuf);
|
|
|
|
|
1999-06-12 16:39:11 +00:00
|
|
|
mBuf = 0;
|
1999-03-29 19:52:54 +00:00
|
|
|
mBufOwner = PR_FALSE;
|
|
|
|
return (const char**) &mBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|