Back to index

lightning-sunbird  0.9+nobinonly
txExpandedNameMap.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Jonas Sicking.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Jonas Sicking <sicking@bigfoot.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 "txExpandedNameMap.h"
00040 #include "txCore.h"
00041 #include <string.h>
00042 
00043 const int kTxExpandedNameMapAllocSize = 16;
00044 
00045 txExpandedNameMap::txExpandedNameMap(MBool aOwnsValues) :
00046     mItems(0), mItemCount(0), mBufferCount(0), mOwnsValues(aOwnsValues)
00047 {
00048 }
00049 
00050 txExpandedNameMap::~txExpandedNameMap()
00051 {
00052     clear();
00053 }
00054 
00062 nsresult txExpandedNameMap::add(const txExpandedName& aKey, TxObject* aValue)
00063 {
00064     int i;
00065     // Check if there already is an item with this key
00066     for (i = 0; i < mItemCount; ++i) {
00067         if (mItems[i].mLocalName == aKey.mLocalName &&
00068             mItems[i].mNamespaceID == aKey.mNamespaceID) {
00069             return NS_ERROR_XSLT_ALREADY_SET;
00070         }
00071     }
00072     
00073     // Allocate a new array if needed
00074     if (mBufferCount == mItemCount) {
00075         MapItem* newItems = new MapItem[mItemCount +
00076                                         kTxExpandedNameMapAllocSize];
00077         if (!newItems) {
00078             return NS_ERROR_OUT_OF_MEMORY;
00079         }
00080 
00081         mBufferCount += kTxExpandedNameMapAllocSize;
00082         memcpy(newItems, mItems, mItemCount * sizeof(MapItem));
00083         delete [] mItems;
00084         mItems = newItems;
00085     }
00086     
00087     mItems[mItemCount].mNamespaceID = aKey.mNamespaceID;
00088     mItems[mItemCount].mLocalName = aKey.mLocalName;
00089     NS_IF_ADDREF(mItems[mItemCount].mLocalName);
00090     mItems[mItemCount].mValue = aValue;
00091     ++mItemCount;
00092     
00093     return NS_OK;
00094 }
00095 
00103 nsresult txExpandedNameMap::set(const txExpandedName& aKey, TxObject* aValue)
00104 {
00105     int i;
00106     // Check if there already is an item with this key
00107     for (i = 0; i < mItemCount; ++i) {
00108         if (mItems[i].mLocalName == aKey.mLocalName &&
00109             mItems[i].mNamespaceID == aKey.mNamespaceID) {
00110             if (mOwnsValues) {
00111                 delete mItems[i].mValue;
00112             }
00113             mItems[i].mValue = aValue;
00114             return NS_OK;
00115         }
00116     }
00117     
00118     // Allocate a new array if needed
00119     if (mBufferCount == mItemCount) {
00120         MapItem* newItems = new MapItem[mItemCount +
00121                                         kTxExpandedNameMapAllocSize];
00122         if (!newItems) {
00123             return NS_ERROR_OUT_OF_MEMORY;
00124         }
00125 
00126         mBufferCount += kTxExpandedNameMapAllocSize;
00127         memcpy(newItems, mItems, mItemCount * sizeof(MapItem));
00128         delete [] mItems;
00129         mItems = newItems;
00130     }
00131     
00132     mItems[mItemCount].mNamespaceID = aKey.mNamespaceID;
00133     mItems[mItemCount].mLocalName = aKey.mLocalName;
00134     NS_IF_ADDREF(mItems[mItemCount].mLocalName);
00135     mItems[mItemCount].mValue = aValue;
00136     ++mItemCount;
00137     
00138     return NS_OK;
00139 }
00140 
00146 TxObject* txExpandedNameMap::get(const txExpandedName& aKey) const
00147 {
00148     int i;
00149     for (i = 0; i < mItemCount; ++i) {
00150         if (mItems[i].mLocalName == aKey.mLocalName &&
00151             mItems[i].mNamespaceID == aKey.mNamespaceID) {
00152             return mItems[i].mValue;
00153         }
00154     }
00155     return 0;
00156 }
00157 
00164 TxObject* txExpandedNameMap::remove(const txExpandedName& aKey)
00165 {
00166     TxObject* value = 0;
00167     int i;
00168     for (i = 0; i < mItemCount; ++i) {
00169         if (mItems[i].mLocalName == aKey.mLocalName &&
00170             mItems[i].mNamespaceID == aKey.mNamespaceID) {
00171             NS_IF_RELEASE(mItems[i].mLocalName);
00172             if (mOwnsValues) {
00173                 delete mItems[i].mValue;
00174             }
00175             else {
00176                 value = mItems[i].mValue;
00177             }
00178             --mItemCount;
00179             if (i != mItemCount) {
00180                 memcpy(&mItems[i], &mItems[mItemCount], sizeof(MapItem));
00181             }
00182         }
00183     }
00184     return value;
00185 }
00186 
00190 void txExpandedNameMap::clear()
00191 {
00192     int i;
00193     for (i = 0; i < mItemCount; ++i) {
00194         NS_IF_RELEASE(mItems[i].mLocalName);
00195         if (mOwnsValues) {
00196             delete mItems[i].mValue;
00197         }
00198     }
00199     delete [] mItems;
00200     mItems = nsnull;
00201     mItemCount = 0;
00202     mBufferCount = 0;
00203 }