📄 tstring.c
字号:
//---------------------------------------------------------
/*
File name: Tstring.c
Comments: This file contains the implementation of the
text_string type.
*/
//---------------------------------------------------------
// Include files
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "Tstring.h"
// End include files
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringInitString
Parameters
In: None.
Out: theString - Contains a pointer to the
text_string to initialize.
In/Out: None.
Return Values: This function does not return a value.
Comments: Programs use this function to set a
text_string into a known state. If this
function is not called before the
text_string is used, it is likely that
there will be a fatal program error.
*/
void TextStringInitString(text_string *theString)
{
assert(theString!=NULL);
theString->charArray = NULL;
theString->charsAllocated = 0;
}
// End TextStringInitString
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringSetFromCharArray
Parameters
In: charArray - Contains a pointer to the
source array of characters.
Out: destinationString - Contains a pointer to
the destination text_string.
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 a text_string. If the
text_string already contains characters,
the memory for those characters is freed.
This function then allocates a block of
memory that is 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;
assert(destinationString != NULL);
// If the destination string contains characters...
if (destinationString->charArray!=NULL)
{
// Free the memory for them.
TextStringFree(destinationString);
}
// 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.
destinationString->charArray =
calloc(length+1,sizeof(char));
// If the memory was allocated...
if (destinationString->charArray)
{
// Copy characters into it.
strcpy(destinationString->charArray,
charArray);
// Set the length of the destination string.
destinationString->charsAllocated = length+1;
}
// Else the memory was not allocated...
else
{
errorStatus = TSE_CANT_ALLOCATE_STRING;
}
}
return (errorStatus);
}
// End TextStringSetFromChars
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringSetFromTextString
Parameters
In: sourceString - Contains a pointer to the
source text_string.
Out: destinationString - Contains a pointer to
the destination text_string.
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
text_string, it returns
TSE_CANT_ALLOCATE_STRING.
Comments: This function copies the characters from
the source text_string into the destination
text_string. If the destination text_string
already contains characters, the memory for
those characters is freed. This function
then allocates a block of memory in the
destination text_string that is large enough
to hold the characters from the source
text_string (plus the null character).
*/
text_string_error TextStringSetFromTextString(
text_string *destinationString,
const text_string * const sourceString)
{
int length=0;
text_string_error errorStatus = TSE_NO_ERROR;
assert(destinationString != NULL);
assert(sourceString != NULL);
// If the destination string contains characters...
if (destinationString->charArray!=NULL)
{
// Free the memory for them.
TextStringFree(destinationString);
}
// If the source text_string is not empty...
if ((sourceString->charArray!=NULL) &&
(sourceString->charsAllocated!=0))
{
// Get its length.
length = strlen(sourceString->charArray);
}
// If the length is greater than 0...
if (length>0)
{
// Allocate memory for the characters.
destinationString->charArray =
calloc(length+1,sizeof(char));
// If the memory was allocated...
if (destinationString->charArray)
{
// Copy characters into it.
strcpy(destinationString->charArray,
sourceString->charArray);
// Set the length of the destination string.
destinationString->charsAllocated = length+1;
}
// Else the memory was not allocated...
else
{
errorStatus = TSE_CANT_ALLOCATE_STRING;
}
}
return (errorStatus);
}
// End TextStringSetString
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringSetCharacter
Parameters
In: theCharacter - Contains the character to
copy into the text_string.
characterIndex - Specifies the zero-based
index number of the position in the
destination string where the character
will be copied.
Out: theString - Contains a pointer to the
destination text_string.
In/Out: None.
Return Values: This function does not return a value.
Comments: Programs call this function to set a
character in an existing position in a
text_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().
*/
void TextStringSetCharacter(text_string *theString,
char theCharacter,
int characterIndex)
{
int length;
assert(theString!=NULL);
length = strlen(theString->charArray);
assert((characterIndex >= 0) &&
(characterIndex < length));
// Set the character.
theString->charArray[characterIndex]=theCharacter;
}
// End TextStringSetCharacter
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringGetCharacter
Parameters
In: theString - Contains a pointer to the
source text_string.
characterIndex - Specifies the zero-based
index number of the position in the
source text_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(const text_string * const theString,
int characterIndex)
{
int length;
assert(theString!=NULL);
assert(theString->charArray!=NULL);
length = strlen(theString->charArray);
assert((characterIndex>=0) && (characterIndex<length));
return (theString->charArray[characterIndex]);
}
// TextStringGetCharacter
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringAppendCharacter
Parameters
In: theCharacter - Specifies the character to
append to the text_string.
Out: None.
In/Out: theString - Contains a pointer to the
destination text_string.
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
text_string. The text_string may or may
not already contain characters. If the
text_string does not contain enough memory
for the new character, more memory is
allocated.
*/
text_string_error TextStringAppendCharacter(
text_string *theString,
char theCharacter)
{
text_string_error errorStatus = TSE_NO_ERROR;
int length=0;
char *temp;
assert(theString!=NULL);
// 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;
theString->charArray[length+1]='\0';
}
// Else the string is empty...
else if (theString->charsAllocated==0)
{
// Allocate space for the character & the '\0';
theString->charArray = calloc(2,sizeof (char));
if (theString->charArray!=NULL)
{
theString->charArray[0]=theCharacter;
theString->charArray[1]='\0';
theString->charsAllocated = 2;
}
else
{
errorStatus = TSE_CANT_ALLOCATE_STRING;
}
}
// Else the string is full...
else
{
// Allocate more memory.
temp = calloc(theString->charsAllocated+1,sizeof(char));
// If the memory was allocated...
if (temp!=NULL)
{
// Copy the string into the new memory.
strcpy(temp,theString->charArray);
// Append the character and the '\0'.
temp[theString->charsAllocated-1]=theCharacter;
temp[theString->charsAllocated]='\0';
// Free the old memory.
free(theString->charArray);
// Set a pointer to the new memory.
theString->charArray = temp;
// Increase the allocation size.
theString->charsAllocated++;
}
else
{
errorStatus = TSE_CANT_ALLOCATE_STRING;
}
}
return (errorStatus);
}
// TextStringAppendCharacter
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringPrintString
Parameters
In: theString - Contains a pointer to the
source text_string.
Out: None.
In/Out: None.
Return Values: This function returns the number of
characters it prints to the screen.
Comments: Programs call this function to print a
text_string to the screen.
*/
int TextStringPrintString(const text_string * const theString)
{
int charsPrinted=0;
assert(theString != NULL);
if (theString->charArray)
{
charsPrinted = printf("%s",theString->charArray);
}
return (charsPrinted);
}
// TextStringPrintString
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringScanString
Parameters
In: None.
Out: None.
In/Out: theString - Contains a pointer to the
source text_string.
Return Values: This function returns the number of
characters it reads from the keyboard.
Comments: Programs call this function to get a
text_string from the keyboard.
*/
int TextStringScanString(text_string *theString)
{
int inputStringLength;
int destinationStringLength;
char inputChar;
// Get the length of the destination string.
destinationStringLength =
TextStringGetLength(theString);
// While the user types in characters...
for (inputStringLength = 0;
((inputChar = getchar()) != EOF) &&
(inputChar!= '\n');
inputStringLength++)
{
/* If the length of the input string is less than
the length of the destination string... */
if (inputStringLength < destinationStringLength)
{
// Set the character.
TextStringSetCharacter(theString,
inputChar,
inputStringLength);
}
// Else the destination string is full...
else
{
// Append the character.
TextStringAppendCharacter(theString,inputChar);
destinationStringLength =
TextStringGetLength(theString);
}
}
// Mare sure there's a null character at the end.
theString->charArray[inputStringLength] = '\0';
return (inputStringLength);
}
// End TextStringScanString
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringGetLength
Parameters
In: theString - Contains a pointer to the
text_string.
Out: None.
In/Out: None.
Return Values: This function returns the length of the
text_string.
Comments: Applications use this function to retrieve
the number of characters (not including the
'\0') in a text_string.
*/
int TextStringGetLength(
const text_string * const theString)
{
int length=0;
assert(theString!=NULL);
// If the string is not empty...
if (theString->charArray!=NULL)
{
// Get its length.
length = strlen(theString->charArray);
}
return (length);
}
// TextStringGetLength
//---------------------------------------------------------
//---------------------------------------------------------
/*
Function: TextStringFree
Parameters
In: None.
Out: None.
In/Out: theString - Contains a pointer to the
text_string.
Return Values: This function does not return a value.
Comments: Applications use this function to free the
memory used by the characters in a
text_string. If this function is not called
before the text_string goes out of scope,
a memory leak may occur.
*/
void TextStringFree(text_string *theString)
{
assert(theString != NULL);
if (theString->charArray != NULL)
{
free(theString->charArray);
theString->charArray = NULL;
theString->charsAllocated = 0;
}
assert(theString->charsAllocated==0);
}
// End TextStringFree
//---------------------------------------------------------
// end TString.c
//---------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -