📄 base_obj.cpp
字号:
{
if (pSomeData == aPointer)
return (aPointer);
pSomeData = GetNext ();
}
// If we get here, we never found the item in question, so give up
return (NULL);
}
//-------------------------------------------------------------------------------------
// Function: NumberOfObjWith
// Here we hunt down an object in the list with the given pointer and return the
// sequence number in the list of that item. It the item's not there return -1.
int CBasicList::NumberOfObjWith (void* aPointer)
{
void* pSomeData; // Pointer to any old node being looked at
// Begin at the head of the list. Check every node. If its data pointer matches
// the pointer given in this function's argument, quit searching and return it
pSomeData = GetHead ();
int Counter = 0;
while (pSomeData != NULL)
{
if (pSomeData == aPointer)
return (Counter);
pSomeData = GetNext ();
Counter++;
}
// If we get here, we never found the item in question, so give up
return (-1);
}
//-------------------------------------------------------------------------------------
// Function: GetObjNumber
// This function goes and finds the item at the given number, i.e. the n-th item
// in the list (if that n-th item exists). It returns a pointer to that item's
// data, unless the item doesn't exist in which case it returns NULL.
void* CBasicList::GetObjNumber (int aNumber)
{
CListNode* pCur; // Pointer to current item in the list
int Counter; // Integer counter for getting to item #N
// Begin at head of list. Count through aNumber items, unless we hit the end
pCur = pFirst;
for (Counter = 0; Counter < aNumber; Counter++)
{
if (pCur == NULL)
return (NULL);
pCur = pCur->pNextNode;
}
// If we get here, there probably is an N-th node, so return its data pointer
if (pCur != NULL)
return (pCur->pNodeData);
else
return (NULL);
}
//-------------------------------------------------------------------------------------
// Function: HowMany
// This function returns the number of items in the list.
int CBasicList::HowMany (void)
{
return (NumEntries);
}
//-------------------------------------------------------------------------------------
// Function: GetCurrentIndex
// Here we return the index number of the currently accessed list item.
int CBasicList::GetCurrentIndex (void)
{
return (CurrentIndex);
}
//=====================================================================================
// Class: CBasicArray
// This class implements an array which stores data of any type (as long as the
// size of a data element can be fixed). It's intended to be used as a base
// class for arrays with more civilized interfaces.
//=====================================================================================
//-------------------------------------------------------------------------------------
// Constructor: CBasicArray
// This constructor sets up a logging array of the given type with a starting
// buffer of the given size.
CBasicArray::CBasicArray (unsigned aDataSize, unsigned aArraySize)
{
BufferSize = aArraySize; // Save number of elements
TotalSize = 0; // Begin with no buffer ready
DataSize = aDataSize; // Remember how big the data is
Expand (); // This allocates the first block of memory
}
//-------------------------------------------------------------------------------------
// Destructor: ~CBaseArray
// This destructor frees memory which has been used by the arrays in the list.
CBasicArray::~CBasicArray (void)
{
// Delete all the buffers
for (char* pBuf = (char*)GetHead (); pBuf != NULL; pBuf = (char*)GetNext())
DELETE_ARRAY pBuf;
}
//-------------------------------------------------------------------------------------
// Function: Flush
// This function resets the pointers to the beginning of the data, effectively
// restoring the array to its 'empty' state, ready to accept some data.
void CBasicArray::Flush (void)
{
// Delete all the buffers and zap the list nodes
for (char* pBuf = (char*)GetHead (); pBuf != NULL; pBuf = (char*)GetNext())
DELETE_ARRAY pBuf;
CBasicList::RemoveNodes ();
// Create the first buffer and save a pointer to it in the buffer pointer list
char* pNewBuf = new char[BufferSize * DataSize];
if (pNewBuf != NULL)
{
CBasicList::Insert ((void*)(pNewBuf));
TotalSize = BufferSize;
}
}
//-------------------------------------------------------------------------------------
// Function: Expand
// This function is used to add another buffer onto the array. It returns the
// new total size of the array if everything went OK and 0 if there are problems.
unsigned CBasicArray::Expand (void)
{
char* pNewBuf = new char[BufferSize * DataSize];
if (pNewBuf != NULL)
{
CBasicList::Insert ((void*)(pNewBuf));
TotalSize += BufferSize;
return (TotalSize);
}
else
return (0);
}
//-------------------------------------------------------------------------------------
// Operator: []
// This operator is somewhat analogous to the array indexing operator [] used for
// normal arrays, except that it returns a *pointer* to the item at the given
// index. (It must, because the array might be storing any kind of data.) If
// you ask for an element which isn't in the range of the array, [] returns NULL.
// Ditto if the data you're asking for hasn't been initialized yet.
void* CBasicArray::operator[] (int aIndex)
{
if (aIndex >= 0)
return (operator[] ((unsigned)(aIndex)));
else
return (NULL);
}
void* CBasicArray::operator[] (unsigned aIndex)
{
if (aIndex >= TotalSize)
return (NULL);
// Find which buffer the item is in, and get a pointer to that buffer
void* WhichBuffer = GetObjNumber (aIndex / BufferSize);
if (WhichBuffer == NULL)
return (NULL);
// Go to the element (within the buffer) which we want; return a pointer to it
return ((void*)(((char*)WhichBuffer) + ((aIndex % BufferSize) * DataSize)));
}
//=====================================================================================
// Class: CString
// A simple class to implement a buffer for character strings is implemented
// here. Actually it's mostly just to ease the process of dealing with strings
// of dynamically variable length.
//=====================================================================================
//-------------------------------------------------------------------------------------
// Constructor: CString
// If someone calls the constructor without specifying a size, call the real
// CString constructor to create a CString object with a 256 byte buffer.
CString::CString (void)
{
CString ((size_t)256); // one to start off with a 1KB buffer
}
//-------------------------------------------------------------------------------------
// Constructor: CString
// This constructor creates a CString object with a buffer of the given size.
CString::CString (size_t aBufSize)
{
// Allocate a starting buffer, and any 'next' buffer does not exist yet
TheBuffer = new char[aBufSize];
NextString = NULL;
OutString = NULL;
if (TheBuffer == NULL) // Buffer's not allocated! Don't use it
BufferSize = 0;
else // Buffer's OK; make it contain empty string
{
BufferSize = aBufSize - 1; // Leave a byte of space for terminating '\0'
strcpy (TheBuffer, "\0");
}
SizeLeft = BufferSize; // This doesn't include the '\0' at the end
}
//-------------------------------------------------------------------------------------
// Destructor: ~CString
// This one clears out the memory used by the CString object.
CString::~CString (void)
{
Flush (); // Free all memory used by this and overflow CStrings
DELETE_ARRAY TheBuffer; // Delete the entire array
}
//-------------------------------------------------------------------------------------
// Function: Flush
// This function clears out the memory used by the CString object and leaves us
// with an empty string. In doing so it deletes any overflow objects which may
// have been created to hold the extra parts of this string.
void CString::Flush (void)
{
if (OutString != NULL) // Frees string used to send out text if it exists
{
DELETE_ARRAY OutString;
OutString = NULL;
}
if (NextString != NULL) // Deletes overflow object, if there is one
{
delete NextString;
NextString = NULL;
}
strcpy (TheBuffer, "\0"); // Return member data to starting values
SizeLeft = BufferSize;
}
//-------------------------------------------------------------------------------------
// Function: GetString (public version, returns character pointer)
// This function returns a pointer to a newly allocated character string which
// contains all the stuff which was written to this string, including the over-
// flow which went to any other CString objects we've created to hold it.
const char *CString::GetString (void)
{
size_t TotalSize; // How many bytes in the entire string
// If a string has been sent out previously, free its memory now
if (OutString != NULL)
delete (OutString);
// Find out how big the output string must be, then allocate for it
TotalSize = GetSize ();
OutString = new char[TotalSize];
if (OutString == 0)
return ("*** ERROR: Can't allocate memory for string ***\n");
// Now fill the output string with the string in this (and overflow) buffer(s)
strcpy (OutString, TheBuffer);
if (NextString != NULL)
NextString->GetString (OutString);
return (OutString);
}
//-------------------------------------------------------------------------------------
// Function: GetString (overloaded to take character pointer parameter)
// This function concatentates the contents of the buffer in this object instance
// into the character buffer which was passed to it. There had better be room.
void CString::GetString (char *OutString)
{
// Put the contents of this object's buffer into the output
strcat (OutString, TheBuffer);
// Check to see if there's another object with more stuff; if so, get it
if (NextString != NULL)
NextString->GetString (OutString);
}
//-------------------------------------------------------------------------------------
// Function: GetSize
// This function returns the size of this string in bytes.
size_t CString::GetSize (void)
{
if (NextString == NULL)
return (BufferSize - SizeLeft + 1);
else
return (BufferSize - SizeLeft + NextString->GetSize ());
}
//-------------------------------------------------------------------------------------
// Function: WriteToFile
// This function writes the contents of the string to the file whose handle is
// given.
void CString::WriteToFile (FILE *OutFile)
{
char *OutChar; // Character currently being written to file
// Write the contents of this object to the output file
for (OutChar = TheBuffer; *OutChar != '\0'; OutChar++)
fputc (*OutChar, OutFile);
// If there is an overflow object, write its contents to the output file too
if (NextString != NULL)
NextString->WriteToFile (OutFile);
}
//-------------------------------------------------------------------------------------
// Operator: =
// Here we overload the assignment operator so that character strings may be
// copied into the CString object very easily. Also the << operator may follow
// an assignment, as "=" returns an object reference.
CString& CString::operator= (const char *NewString)
{
Flush (); // Empty this string of all contents
(*this) << NewString; // Add the new character string into this object
return (*this);
}
//-------------------------------------------------------------------------------------
// Operator: << (for character pointer)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -