📄 tstring.c
字号:
//---------------------------------------------------------
/*
File name: Tstring.c
Comments: This file contains the implementation of the
text_string type. Variables of the text_string
type are really identifiers. The actual strings
are implemented by the internal_text_string
type. Instances of the internal_text_string type
are kept in an instance list which is only visible
to functions in this file. Variables of type
text_string identify strings in the instance list
*/
//---------------------------------------------------------
//---------------------------------------------------------
// Include files
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "Tstring.h"
#include "InstList.h"
#include "MiscType.h"
// End include files
//---------------------------------------------------------
//---------------------------------------------------------
// End Type definitions
typedef struct
{
char *charArray;
int charsAllocated;
} internal_text_string;
// End Type definitions
//---------------------------------------------------------
//---------------------------------------------------------
// Module Global Variable
// Declare and initialize the instance list.
DeclareInstanceList(stringList);
// End Module Global Variable
//---------------------------------------------------------
//---------------------------------------------------------
// Prototypes
static void FreeCharacters(internal_text_string *theString);
// End Prototypes
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringCreate
Parameters
In: None.
Out: errorStatus - Specifies a pointer to a
variable into which
In/Out: None.
Return Values: If this function can allocate and initialize
a text_string, it returns the identifier of
the text_string. Otherwise, it returns
TSE_CANT_ALLOCATE_STRING.
Comments: Other functions must call TextStringCreate()
to instantiate a text_string. Functions can
declare text_string variables. However, the
text_string type only contains identifiers of
internal_text_string instances. The actual
instances are kept in an instance list.
Programs must call TextStringFree() to delete
the instance associated with a text_string
identifier from the instance list.
*/
text_string TextStringCreate(text_string_error *errorStatus)
{
text_string theString = -1;
internal_text_string *stringInstance = NULL;
assert(errorStatus);
// Allocate memory for an internal_text_string.
stringInstance =
(internal_text_string *)calloc(
1,
sizeof(internal_text_string));
// If the memory was allocated...
if (stringInstance != NULL)
{
// Add the internal_text_string to the instance list.
theString =
(text_string)InstanceListAddItem(
&stringList,
(instance_list_item)stringInstance);
// If it was not added...
if ((theString < 0) ||
(InstanceListGetLastError(&stringList) != ILE_NO_ERROR))
{
// Set an error condition.
*errorStatus = TSE_CANT_ALLOCATE_STRING;
}
}
// Else the memory was not allocated...
else
{
// Set an error condition.
*errorStatus = TSE_CANT_ALLOCATE_STRING;
}
return (theString);
}
// End TextStringCreate
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringSetFromCharArray
Parameters
In: destinationString - Contains instance ID
of the destination text_string.
charArray - Contains a pointer to the
source array of characters.
Out: None.
In/Out: None.
Return Values: Upon success, this function returns
TSE_NO_ERROR. If it can't allocate memory
for the characters in the destination
text_string, it returns
TSE_CANT_ALLOCATE_STRING.
Comments: This function copies the characters from
charArray into the string specified by
destinationString. If the destination
string already contains characters, the
memory for those characters is freed. This
function then allocates a block of memory
large enough to hold the characters from
charArray (plus the null character).
*/
text_string_error TextStringSetFromCharArray(
text_string destinationString,
const char * const charArray)
{
int length=0;
text_string_error errorStatus = TSE_NO_ERROR;
internal_text_string *theString = NULL;
assert(TextStringIsValid(destinationString));
// Get the destination string from the instance list.
theString =
(internal_text_string *)InstanceListGetItem(&stringList,
destinationString);
// If the string was found...
assert(InstanceListGetLastError(&stringList) == ILE_NO_ERROR);
// If the destination string contains characters...
if (theString->charArray!=NULL)
{
assert(theString->charsAllocated > 0);
// Free the memory for them.
FreeCharacters(theString);
}
// If the character array is not empty...
if (charArray!=NULL)
{
// Get its length.
length = strlen(charArray);
}
// If the length is greater than 0...
if (length>0)
{
// Allocate memory for the characters.
theString->charArray = calloc(length+1,sizeof(char));
// If the memory was allocated...
if (theString->charArray)
{
// Copy characters into it.
strcpy(theString->charArray,charArray);
// Set the length of the destination string.
theString->charsAllocated = length+1;
}
// Else the memory was not allocated...
else
{
errorStatus = TSE_CANT_ALLOCATE_STRING;
}
}
return (errorStatus);
}
// End TextStringSetFromCharArray
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringSetFromTextString
Parameters
In: destinationString - Contains the identifier
of the destination string.
sourceString - Contains the identifier of
the source string.
Out: None.
In/Out: None.
Return Values: Upon success, this function returns
TSE_NO_ERROR. If it can't allocated memory
for the characters in the destination
string, it returns TSE_CANT_ALLOCATE_STRING.
Comments: This function copies the characters from
the source string into the destination string.
If the destination string already contains
characters, the memory for those characters is
freed. This function then allocates a block
of memory in the destination string that is
large enough to hold the characters from the
source string (plus the null character).
*/
text_string_error TextStringSetFromTextString(
text_string destinationString,
text_string sourceString)
{
int length=0;
text_string_error errorStatus = TSE_NO_ERROR;
internal_text_string *source = NULL, *destination = NULL;
assert(TextStringIsValid(destinationString));
assert(TextStringIsValid(sourceString));
// Get the source string from the instance list.
source =
(internal_text_string *)InstanceListGetItem(&stringList,
sourceString);
assert(InstanceListGetLastError(&stringList) == ILE_NO_ERROR);
// Get the destination string.
destination =
(internal_text_string *)InstanceListGetItem(&stringList,
destinationString);
assert(InstanceListGetLastError(&stringList) == ILE_NO_ERROR);
// If the source or destination string was found...
if (errorStatus == TSE_NO_ERROR)
{
// If the destination string contains characters...
if (destination->charArray != NULL)
{
// Free the memory for them.
FreeCharacters(destination);
}
// If the source text_string is not empty...
if (source->charArray != NULL)
{
assert(source->charsAllocated != 0);
// Get its length.
length = strlen(source->charArray);
}
// If the length is greater than 0...
if (length > 0)
{
// Allocate memory for the characters.
destination->charArray = calloc(length+1,sizeof(char));
// If the memory was allocated...
if (destination->charArray)
{
// Copy characters into it.
strcpy(destination->charArray,source->charArray);
// Set the length of the destination string.
destination->charsAllocated = length+1;
}
// Else the memory was not allocated...
else
{
errorStatus = TSE_CANT_ALLOCATE_STRING;
}
}
}
return (errorStatus);
}
// End TextStringSetFromTextString
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringSetCharacter
Parameters
In: theString - Contains the identifier of the
destination string.
characterIndex - Specifies the zero-based
index number of the position in the
destination string where the character
will be copied.
theCharacter - Contains the character to
copy into the string.
Out: None.
In/Out: None.
Return Values: In the current implementation, this function
always returns the value TSE_NO_ERROR.
Comments: Programs call this function to set a
character in an existing position in a
string. The memory for the character in the
destination string must already be allocated
before this function is invoked. It is not
possible to use this function to set a
character in a position that is beyond the
end of the string (after the null character).
This is true even if the memory for that
position is already allocated. To add
characters to the end of a string, call
TextStringAppendCharacter().
*/
text_string_error TextStringSetCharacter(
text_string theString,
char theCharacter,
int characterIndex)
{
int length;
internal_text_string *destination = NULL;
text_string_error errorStatus = TSE_NO_ERROR;
assert(TextStringIsValid(theString));
destination =
(internal_text_string *)InstanceListGetItem(&stringList,
theString);
assert(InstanceListGetLastError(&stringList) == ILE_NO_ERROR);
length = strlen(destination->charArray);
assert((characterIndex >= 0) &&
(characterIndex < length));
// Set the character.
destination->charArray[characterIndex]=theCharacter;
return (errorStatus);
}
// End TextStringSetCharacter
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringGetCharacter
Parameters
In: sourceString - Contains the identifier of
the source string.
characterIndex - Specifies the zero-based
index number of the position in the
source string from which the character
will be copied.
Out: None.
In/Out: None.
Return Values: This function returns the character from
the location in theString that is specified
by characterIndex.
Comments: See Return Values.
*/
char TextStringGetCharacter(text_string sourceString,
int characterIndex)
{
int length;
internal_text_string *source = NULL;
char theCharacter = '\0';
assert(TextStringIsValid(sourceString));
// Get the string from the instance list.
source =
(internal_text_string *)InstanceListGetItem(&stringList,
sourceString);
assert(InstanceListGetLastError(&stringList) == ILE_NO_ERROR);
assert(source->charArray!=NULL);
length = strlen(source->charArray);
assert((characterIndex>=0) && (characterIndex<length));
theCharacter = source->charArray[characterIndex];
return (theCharacter);
}
// End TextStringGetCharacter
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringAppendCharacter
Parameters
In: theString - Contains the identifier of the
destination string.
theCharacter - Specifies the character to
append to the string.
Out: None.
In/Out: None.
Return Values: If it is successfull, this function returns
TSE_NO_ERROR. Otherwise, it returns
TSE_CANT_ALLOCATE_STRING.
Comments: This function appends a character to a
string. The string may or may not already
contain characters. If the string does not
contain enough memory for the new character,
more memory is allocated.
*/
text_string_error TextStringAppendCharacter(
text_string destinationString,
char theCharacter)
{
text_string_error errorStatus = TSE_NO_ERROR;
int length=0;
char *temp;
internal_text_string *theString = NULL;
assert(TextStringIsValid(destinationString));
// Get the string from the instance list.
theString =
(internal_text_string *)InstanceListGetItem(
&stringList,
destinationString);
// If the string was not found...
if (InstanceListGetLastError(&stringList) != ILE_NO_ERROR)
{
errorStatus = TSE_STRING_DOES_NOT_EXIST;
}
// Else the string is in the instance list...
else
{
// If the string is not empty...
if (theString->charArray!=NULL)
{
// Get its length.
length = strlen(theString->charArray);
}
// If the string has some room in it...
if (length<theString->charsAllocated-1)
{
// Add the character.
theString->charArray[length]=theCharacter;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -