Back to index

lightning-sunbird  0.9+nobinonly
nsUnicharBuffer.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsUnicharBuffer.h"
00040 #include "nsCRT.h"
00041 
00042 #define MIN_BUFFER_SIZE 32
00043 
00044 UnicharBufferImpl::UnicharBufferImpl()
00045   : mBuffer(NULL), mSpace(0), mLength(0)
00046 {
00047 }
00048 
00049 NS_METHOD
00050 UnicharBufferImpl::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00051 {
00052   if (aOuter)
00053     return NS_ERROR_NO_AGGREGATION;
00054 
00055   UnicharBufferImpl* it = new UnicharBufferImpl();
00056   if (it == nsnull) 
00057     return NS_ERROR_OUT_OF_MEMORY;
00058 
00059   NS_ADDREF(it);
00060   nsresult rv = it->QueryInterface(aIID, aResult);
00061   NS_RELEASE(it);
00062   return rv;
00063 }
00064 
00065 NS_IMETHODIMP
00066 UnicharBufferImpl::Init(PRUint32 aBufferSize)
00067 {
00068   if (aBufferSize < MIN_BUFFER_SIZE) {
00069     aBufferSize = MIN_BUFFER_SIZE;
00070   }
00071   mSpace = aBufferSize;
00072   mLength = 0;
00073   mBuffer = new PRUnichar[aBufferSize];
00074   return mBuffer ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00075 }
00076 
00077 NS_IMPL_ISUPPORTS1(UnicharBufferImpl, nsIUnicharBuffer)
00078 
00079 UnicharBufferImpl::~UnicharBufferImpl()
00080 {
00081   if (nsnull != mBuffer) {
00082     delete[] mBuffer;
00083     mBuffer = nsnull;
00084   }
00085   mLength = 0;
00086 }
00087 
00088 NS_IMETHODIMP_(PRInt32)
00089 UnicharBufferImpl::GetLength() const
00090 {
00091   return mLength;
00092 }
00093 
00094 NS_IMETHODIMP_(PRInt32)
00095 UnicharBufferImpl::GetBufferSize() const
00096 {
00097   return mSpace;
00098 }
00099 
00100 NS_IMETHODIMP_(PRUnichar*)
00101 UnicharBufferImpl::GetBuffer() const
00102 {
00103   return mBuffer;
00104 }
00105 
00106 NS_IMETHODIMP_(PRBool)
00107 UnicharBufferImpl::Grow(PRInt32 aNewSize)
00108 {
00109   if (PRUint32(aNewSize) < MIN_BUFFER_SIZE) {
00110     aNewSize = MIN_BUFFER_SIZE;
00111   }
00112   PRUnichar* newbuf = new PRUnichar[aNewSize];
00113   if (nsnull != newbuf) {
00114     if (0 != mLength) {
00115       memcpy(newbuf, mBuffer, mLength * sizeof(PRUnichar));
00116     }
00117     delete[] mBuffer;
00118     mBuffer = newbuf;
00119     return PR_TRUE;
00120   }
00121   return PR_FALSE;
00122 }
00123 
00124 NS_COM nsresult
00125 NS_NewUnicharBuffer(nsIUnicharBuffer** aInstancePtrResult,
00126                     nsISupports* aOuter,
00127                     PRUint32 aBufferSize)
00128 {
00129   nsresult rv;
00130   nsIUnicharBuffer* buf;
00131   rv = UnicharBufferImpl::Create(aOuter, NS_GET_IID(nsIUnicharBuffer), 
00132                                  (void**)&buf);
00133   if (NS_FAILED(rv)) return rv;
00134   rv = buf->Init(aBufferSize);
00135   if (NS_FAILED(rv)) {
00136     NS_RELEASE(buf);
00137     return rv;
00138   }
00139   *aInstancePtrResult = buf;
00140   return rv;
00141 }