📄 contain.cxx
字号:
/* * contain.cxx * * Container Classes * * Portable Windows Library * * Copyright (c) 1993-1998 Equivalence Pty. Ltd. * * The contents of this file are subject to the Mozilla Public License * Version 1.0 (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/MPL/ * * 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. * * The Original Code is Portable Windows Library. * * The Initial Developer of the Original Code is Equivalence Pty. Ltd. * * Portions are Copyright (C) 1993 Free Software Foundation, Inc. * All Rights Reserved. * * Contributor(s): ______________________________________. * * $Log: contain.cxx,v $ * Revision 1.87 2000/06/26 11:17:20 robertj * Nucleus++ port (incomplete). * * Revision 1.86 2000/04/07 06:29:46 rogerh * Add a short term workaround for an Internal Compiler Error on MAC OS X when * returning certain types of PString. Submitted by Kevin Packard. * * Revision 1.85 2000/02/05 22:36:09 craigs * Fixed problem caused by last modification * * Revision 1.84 2000/02/04 19:34:26 craigs * Fixed problem with changing size of referenced objects * * Revision 1.83 2000/01/25 14:05:35 robertj * Added optimisation to array comparisons if referencing same array. * * Revision 1.82 1999/08/18 01:45:13 robertj * Added concatenation function to "base type" arrays. * * Revision 1.81 1999/08/17 03:46:40 robertj * Fixed usage of inlines in optimised version. * * Revision 1.80 1999/08/12 12:12:47 robertj * GCC 2.95 compatibility. * * Revision 1.79 1999/05/28 14:01:53 robertj * Added initialisers to string containers (list, sorted list and set). * * Revision 1.78 1999/04/18 09:36:31 robertj * Get date grammar build. * * Revision 1.77 1999/04/16 14:38:37 craigs * Changes to make getdate.y compile under Linux * * Revision 1.76 1998/10/28 00:58:40 robertj * Changed PStringStream so flush or endl does not clear the string output, this now * just sets the string to minimum size. * * Revision 1.75 1998/10/13 14:06:18 robertj * Complete rewrite of memory leak detection code. * * Revision 1.74 1998/09/23 06:21:54 robertj * Added open source copyright license. * * Revision 1.73 1998/09/22 02:42:39 robertj * Fixed problem treating unsigned integer as signed in PString contructor. * * Revision 1.72 1998/09/15 08:26:42 robertj * Fixed a number of warnings at maximum optimisation. * * Revision 1.71 1998/09/14 12:36:29 robertj * Fixed bug causing memory leak due to uninitialised member variable for dynamic allocation of arrays. * * Revision 1.70 1998/08/21 05:24:07 robertj * Added hex dump capability to base array types. * Added ability to have base arrays of static memory blocks. * * Revision 1.69 1998/03/17 10:13:23 robertj * Fixed bug in Trim() should do all white space not just the space character. * * Revision 1.68 1998/01/26 00:37:48 robertj * Fixed PString & operator putting space in if right hand side is empty string, it shouldn't.. * Added Execute() functions to PRegularExpression that take PINDEX references instead of PIntArrays. * Added FindRegEx function to PString that returns position and length. * * Revision 1.67 1997/12/11 13:32:49 robertj * Added AsUnsigned() function to convert string to DWORD. * * Revision 1.66 1997/07/08 13:14:41 robertj * Fixed bug where freeing null pointer. * * Revision 1.65 1997/06/08 04:48:04 robertj * Added regular expressions. * * Revision 1.64 1997/03/02 03:41:42 robertj * Fixed bug in not being able to construct a zero length PStringArray. * * Revision 1.63 1996/10/08 13:13:25 robertj * Added operator += and &= for char so no implicit PString construction. * * Revision 1.62 1996/09/14 12:45:57 robertj * Fixed bug in PString::Splice() function, no end of string put in. * * Revision 1.61 1996/08/22 13:21:55 robertj * Fixed major bug in FindLast(), could scan all of memory in negative direction. * * Revision 1.60 1996/08/08 10:08:45 robertj * Directory structure changes for common files. * * Revision 1.59 1996/05/26 03:46:27 robertj * Compatibility to GNU 2.7.x * * Revision 1.58 1996/05/15 10:17:02 robertj * Fixed idiotic bug in string compare, caseless version always matched. * * Revision 1.57 1996/05/09 12:17:10 robertj * Fixed incorrect use of memcmp/strcmp return value. * Added assertion when finding empty string. * * Revision 1.56 1996/04/14 02:52:39 robertj * Fixed bug in PString::FindLast(), never found sub-strings. * * Revision 1.55 1996/03/31 08:58:49 robertj * Fixed hash function for strings to work for caseless strings. * * Revision 1.54 1996/03/16 04:56:59 robertj * Fixed bug in PStringStream assignment oeprator getting pointers wrong. * * Revision 1.53 1996/03/02 03:20:11 robertj * Fixed bug in PString::Find() not finding substring if exactly same as string. * * Revision 1.52 1996/02/22 10:23:54 robertj * Fixed buf in *= operator only comparing up to shortest string. * Fixed bug in & operator for if left string is empty. * * Revision 1.51 1996/02/19 13:34:53 robertj * Removed PCaselessString hash function to fix dictionary match failure. * Fixed *= operator yet again. * * Revision 1.50 1996/02/08 12:20:44 robertj * Added new operators to PString for case insensitive compare and spaced concatenate. * Fixed bug in Find() not finding case insensitive substrings. * * Revision 1.49 1996/02/03 11:08:51 robertj * Changed memcpy to memove to guarentee string operations will work correctly * when moving overlapping strings around eg in PString::Splice(). * * Revision 1.48 1996/01/28 14:12:22 robertj * Fixed bug in Tokenise() for first token empty and PINDEX unsigned. * * Revision 1.47 1996/01/28 02:53:40 robertj * Added assert into all Compare functions to assure comparison between compatible objects. * Fixed bug in Find() function, subset sum calculation added one to many bytes. * * Revision 1.46 1996/01/24 14:43:19 robertj * Added initialisers to string dictionaries. * * Revision 1.45 1996/01/23 13:17:38 robertj * Added Replace() function to strings. * String searching algorithm rewrite. * * Revision 1.44 1996/01/02 12:51:05 robertj * Mac OS compatibility changes. * Removed requirement that PArray elements have parameterless constructor.. * * Revision 1.43 1995/10/14 15:07:42 robertj * Changed arrays to not break references, but strings still need to. * * Revision 1.42 1995/06/17 00:46:20 robertj * Added flag for PStringArray constructor to create caseless strings. * Fixed bug in arrays when size set to zero. * * Revision 1.41 1995/06/04 12:39:59 robertj * Made char * array all const in PStringArray constructor. * * Revision 1.40 1995/04/25 11:29:38 robertj * Fixed Borland compiler warnings. * * Revision 1.39 1995/04/02 09:27:27 robertj * Added "balloon" help. * * Revision 1.38 1995/03/12 04:46:02 robertj * Fixed use of PCaselessString as dictionary key. * * Revision 1.37 1995/01/15 04:56:28 robertj * Fixed PStringStream for correct pointer calculations in output. * * Revision 1.36 1995/01/10 11:44:13 robertj * Removed PString parameter in stdarg function for GNU C++ compatibility. * * Revision 1.35 1995/01/09 12:32:56 robertj * Removed unnecesary return value from I/O functions. * Changed function names due to Mac port. * * Revision 1.34 1995/01/04 10:57:08 robertj * Changed for HPUX and GNU2.6.x * * Revision 1.33 1995/01/03 09:39:08 robertj * Put standard malloc style memory allocation etc into memory check system. * * Revision 1.32 1994/12/13 11:50:56 robertj * Added MakeUnique() function to all container classes. * * Revision 1.31 1994/12/12 13:13:17 robertj * Fixed bugs in PString mods just made. * * Revision 1.30 1994/12/12 10:16:27 robertj * Restructuring and documentation of container classes. * Renaming of some macros for declaring container classes. * Added some extra functionality to PString. * Added start to 2 byte characters in PString. * Fixed incorrect overrides in PCaselessString. * * Revision 1.29 1994/12/05 11:19:36 robertj * Moved SetMinSize from PAbstractArray to PContainer. * * Revision 1.28 1994/11/28 12:37:29 robertj * Added dummy parameter to container classes. * * Revision 1.27 1994/10/30 11:50:44 robertj * Split into Object classes and Container classes. * Changed mechanism for doing notification callback functions. * * Revision 1.26 1994/10/23 03:43:07 robertj * Changed PBaseArray so can have zero elements in it. * Added Printf style constructor to PString. * * Revision 1.25 1994/09/25 10:49:44 robertj * Added empty functions for serialisation. * * Revision 1.24 1994/08/21 23:43:02 robertj * Added object serialisation classes. * Changed parameter before variable argument list to NOT be a reference. * * Revision 1.23 1994/08/04 12:57:10 robertj * Rewrite of memory check code. * * Revision 1.22 1994/08/01 03:40:28 robertj * Fixed PString() constructor from integer * * Revision 1.21 1994/07/27 05:58:07 robertj * Synchronisation. * * Revision 1.20 1994/07/25 03:38:38 robertj * Added more memory tests. * * Revision 1.19 1994/07/17 10:46:06 robertj * Added number conversions to PString. * * Revision 1.18 1994/06/25 11:55:15 robertj * Unix version synchronisation. * * Revision 1.17 1994/04/20 12:17:44 robertj * assert changes * * Revision 1.16 1994/04/11 12:08:37 robertj * Fixed bug in memory leak hash table hash function, cant have negative numbers. * * Revision 1.15 1994/04/03 08:34:18 robertj * Added help and focus functionality. * * Revision 1.14 1994/04/01 14:01:11 robertj * Streams and stuff. * * Revision 1.13 1994/03/07 07:47:00 robertj * Major upgrade * * Revision 1.12 1994/01/15 03:14:22 robertj * Mac portability problems. * * Revision 1.11 1994/01/03 04:42:23 robertj * Mass changes to common container classes and interactors etc etc etc. * * Revision 1.10 1993/12/31 06:53:02 robertj * Made inlines optional for debugging purposes. * * Revision 1.9 1993/12/24 04:20:52 robertj * Mac CFront port. * * Revision 1.8 1993/12/16 00:51:46 robertj * Made some container functions const. * * Revision 1.7 1993/12/15 21:10:10 robertj * Fixed reference system used by container classes. * Plugged memory leaks in PList and PSortedList. * * Revision 1.6 1993/12/14 18:44:56 robertj * Added RemoveAll() function to collections. * Fixed bug in list processing when being destroyed (removes the item being * deleted from the list before deleting it). * Changed GetIndex() so does not assert if entry not in collection. * * Revision 1.5 1993/12/04 05:22:38 robertj * Added more string functions. * * Revision 1.4 1993/09/27 16:35:25 robertj * Fixed bugs in sorted list. * Fixed compatibility problem with sprintf return value (SVR4). * Change function for making string array to a constructor. * * Revision 1.3 1993/08/27 18:17:47 robertj * Fixed bugs in PAbstractSortedList (including some formatting). * * Revision 1.2 1993/08/21 01:50:33 robertj * Made Clone() function optional, default will assert if called. * * Revision 1.8 1993/08/01 14:05:27 robertj * Added const to ToLower() and ToUpper() in the PString class. * * Revision 1.7 1993/07/16 14:40:55 robertj * Added PString constructor for individual characters. * Added string to C style literal format. * * Revision 1.6 1993/07/15 05:02:57 robertj * Removed redundant word in PString enum for string types. * * Revision 1.5 1993/07/15 04:29:39 robertj * Added new constructor to convert from other string formats. * Fixed sprintf variable parameter list bug. * * Revision 1.4 1993/07/14 12:41:52 robertj * Fixed comment leader. * * Revision 1.3 1993/07/14 02:06:34 robertj * Fixed header comment for RCS. */#include <ptlib.h>#include <ctype.h>#ifdef __NUCLEUS_PLUS__extern "C" int vsprintf(char *, const char *, va_list);#endif#ifndef __STDC__#define __STDC__ 1#endif#include "regex.h"#if !P_USE_INLINES#include "ptlib/contain.inl"#endif#define new PNEW///////////////////////////////////////////////////////////////////////////////PContainer::PContainer(PINDEX initialSize){ reference = new Reference(initialSize); PAssertNULL(reference);}PContainer::PContainer(int, const PContainer * cont){ reference = new Reference(0); *PAssertNULL(reference) = *cont->reference;}PContainer::PContainer(const PContainer & cont){ reference = PAssertNULL(cont.reference); reference->count++;}PContainer & PContainer::operator=(const PContainer & cont){ if (reference != cont.reference) { if (!IsUnique()) reference->count--; else { DestroyContents(); delete reference; } reference = PAssertNULL(cont.reference); reference->count++; } return *this;}void PContainer::Destruct(){ if (reference != NULL) { if (reference->count > 1) reference->count--; else { DestroyContents(); delete reference; } reference = NULL; }}BOOL PContainer::SetMinSize(PINDEX minSize){ PASSERTINDEX(minSize); if (minSize < GetSize()) minSize = GetSize(); return SetSize(minSize);}BOOL PContainer::MakeUnique(){ if (IsUnique()) return TRUE; reference->count--; reference = new Reference(GetSize()); return FALSE;}///////////////////////////////////////////////////////////////////////////////PAbstractArray::PAbstractArray(PINDEX elementSizeInBytes, PINDEX initialSize) : PContainer(initialSize){ elementSize = elementSizeInBytes; PAssert(elementSize != 0, PInvalidParameter); if (GetSize() == 0) theArray = NULL; else theArray = (char *)calloc(GetSize(), elementSize); allocatedDynamically = TRUE;}PAbstractArray::PAbstractArray(PINDEX elementSizeInBytes, const void *buffer, PINDEX bufferSizeInElements, BOOL dynamicAllocation) : PContainer(bufferSizeInElements){ elementSize = elementSizeInBytes; PAssert(elementSize != 0, PInvalidParameter); allocatedDynamically = dynamicAllocation; if (GetSize() == 0) theArray = NULL; else if (dynamicAllocation) { PINDEX sizebytes = elementSize*GetSize(); theArray = (char *)malloc(sizebytes); memcpy(theArray, PAssertNULL(buffer), sizebytes); } else theArray = (char *)buffer;}void PAbstractArray::DestroyContents(){ if (theArray != NULL) { if (allocatedDynamically) free(theArray); theArray = NULL; }}void PAbstractArray::CopyContents(const PAbstractArray & array){ elementSize = array.elementSize; theArray = array.theArray; allocatedDynamically = array.allocatedDynamically;}void PAbstractArray::CloneContents(const PAbstractArray * array){ elementSize = array->elementSize; PINDEX sizebytes = elementSize*GetSize(); char * newArray = (char *)malloc(sizebytes); if (newArray == NULL) reference->size = 0; else memcpy(newArray, array->theArray, sizebytes); theArray = newArray; allocatedDynamically = TRUE;}PObject::Comparison PAbstractArray::Compare(const PObject & obj) const{ PAssert(obj.IsDescendant(PAbstractArray::Class()), PInvalidCast); const PAbstractArray & other = (const PAbstractArray &)obj; char * otherArray = other.theArray; if (theArray == otherArray) return EqualTo; if (elementSize < other.elementSize) return LessThan; if (elementSize > other.elementSize) return GreaterThan; PINDEX thisSize = GetSize(); PINDEX otherSize = other.GetSize(); if (thisSize < otherSize) return LessThan; if (thisSize > otherSize) return GreaterThan; if (thisSize == 0) return EqualTo; int retval = memcmp(theArray, otherArray, elementSize*thisSize); if (retval < 0) return LessThan; if (retval > 0) return GreaterThan; return EqualTo;}BOOL PAbstractArray::SetSize(PINDEX newSize){ PINDEX newsizebytes = elementSize*newSize; PINDEX oldsizebytes = elementSize*GetSize(); char * newArray; if (!IsUnique()) { if (newsizebytes == 0) newArray = NULL; else { if ((newArray = (char *)malloc(newsizebytes)) == NULL) return FALSE; if (theArray != NULL) memcpy(newArray, theArray, PMIN(oldsizebytes, newsizebytes)); } reference->count--; reference = new Reference(newSize); } else { if (newsizebytes == oldsizebytes) return TRUE; if (theArray != NULL) { if (newsizebytes == 0) { if (allocatedDynamically) free(theArray); newArray = NULL; } else if (allocatedDynamically) { if ((newArray = (char *)realloc(theArray, newsizebytes)) == NULL) return FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -