Working on changes, maybe working?

This commit is contained in:
Charles Descamps 2024-01-31 18:17:38 -07:00
parent 7b837ddf86
commit 3af7742043
8 changed files with 127 additions and 74 deletions

View file

@ -5,24 +5,24 @@
const int SUBSTRING_NOT_FOUND = 0;
// list of utils:
// string length
// getStringLength
// string copy
// copyString
// string concatenate
// catString
// string compare (<, >, =)
// compareString
// string substring get
// getSubString
// string substring find
// findSubString
// string set to lower
// toLowerCase
// string get to delimiter
// stringDelimiter
// string get to end of line
// stringEOL
// string length
// getStringLength
// string copy
// copyString
// string concatenate
// catString
// string compare (<, >, =)
// compareString
// string substring get
// getSubString
// string substring find
// findSubString
// string set to lower
// toLowerCase
// string get to delimiter
// stringDelimiter
// string get to end of line
// stringEOL
// function implementations
@ -39,20 +39,20 @@ Dependencies: malloc, free, getStringLength
void catString(char *outString, const char *inString)
{
// initialize variables
// set dest index
// set dest index
int destIndex = getStringLength(outString);
// set source str length
// set source str length
int sourceStrLen = getStringLength(inString);
// create temp string pointer
// create temp string pointer
char *tempString;
int sourceIndex = 0;
// copy source string in case of aliasing
tempString = (char *)malloc(sizeof(char) * sourceStrLen);
copyString(tempString, inString);
// loop to end of source string
while(tempString[sourceIndex] != NULL_CHAR)
while (tempString[sourceIndex] != NULL_CHAR)
{
// assign characters to end of destination string
outString[destIndex] = inString[sourceIndex];
@ -66,7 +66,7 @@ void catString(char *outString, const char *inString)
// release memory used for temp string
free(tempString);
// no return, void.
}
@ -93,12 +93,12 @@ int compareString(const char *firstString, const char *secondString)
int diff, index = 0;
// loop to end of string (within bounds)
while(firstString[index] != NULL_CHAR && secondString[index] != NULL_CHAR)
while (firstString[index] != NULL_CHAR && secondString[index] != NULL_CHAR)
{
// get diff of characters
diff = firstString[index] - secondString[index];
// check diff of characters
if(diff != 0)
if (diff != 0)
{
// return diff
return diff;
@ -126,12 +126,12 @@ void copyString(char *outString, const char *inString)
{
// initialize variables
int index = 0;
// check to see if outString is not the same as inString.
if(outString != inString)
if (outString != inString)
{
// loop to end of inString
while(inString[index] != NULL_CHAR)
while (inString[index] != NULL_CHAR)
{
// assign characters to end of outString
outString[index] = inString[index];
@ -141,8 +141,8 @@ void copyString(char *outString, const char *inString)
// end loop to end of inString
// set end of the outString
outString[index] = NULL_CHAR;
}
}
// no return, void.
}
@ -162,14 +162,14 @@ Dependencies: getStringLength
int findSubString(const char *testStr, const char *searchSubStr)
{
// initialize variables
// initialize test string length
// initialize test string length
int testStrLength = getStringLength(testStr);
// initialize master index (location of substring start point)
// initialize master index (location of substring start point)
int masterIndex = 0;
int searchIndex, internalIndex;
// loop across testStr
while(masterIndex < testStrLength)
while (masterIndex < testStrLength)
{
// set internal loop index to current test string index
internalIndex = masterIndex;
@ -177,14 +177,14 @@ int findSubString(const char *testStr, const char *searchSubStr)
searchIndex = 0;
// loop to end of testStr
// while testStr/searchSubStr characters are the same
while(internalIndex <= testStrLength &&
testStr[internalIndex] == searchSubStr[searchIndex])
while (internalIndex <= testStrLength &&
testStr[internalIndex] == searchSubStr[searchIndex])
{
// increment testStr and substring indices
internalIndex = internalIndex + 1;
searchIndex = searchIndex + 1;
// check for end of substring (search complete)
if(searchSubStr[searchIndex] == NULL_CHAR)
if (searchSubStr[searchIndex] == NULL_CHAR)
{
// return beginning location of substring
return masterIndex;
@ -195,7 +195,7 @@ int findSubString(const char *testStr, const char *searchSubStr)
masterIndex = masterIndex + 1;
}
// end loop across testStr
// return SUBSTRING_NOT_FOUND
return SUBSTRING_NOT_FOUND;
}
@ -227,24 +227,23 @@ bool getStringConstrained(FILE *inStream, bool clearLeadingNonPrintable,
// capture first value in stream using fgetc
intChar = fgetc(inStream);
// loop to clear non-printable or space if indicated
while((((intChar != EOF) &&
(clearLeadingNonPrintable && (intChar < (int)SPACE)))
|| (clearLeadingSpace && (intChar == (int)SPACE))))
while ((intChar != EOF) &&
((clearLeadingNonPrintable && intChar < (int)SPACE) || (clearLeadingSpace && intChar == (int)SPACE)))
{
// get next character in stream using fgetc
intChar = fgetc(inStream);
}
// end loop to clear non-printable or space if indicated
// check for end of file found
if(intChar == EOF)
if (intChar == EOF)
{
// return false (failed)
return false;
}
// loop to get input from file stream
while(((intChar != EOF && index < MAX_STR_LEN - 1) &&
(stopAtNonPrintable && intChar >= (int)SPACE))
|| ((!stopAtNonPrintable) && (intChar != (char)delimiter)))
while ((intChar != EOF && index < MAX_STR_LEN - 1) &&
((stopAtNonPrintable && intChar >= (int)SPACE) || (!stopAtNonPrintable)) &&
(intChar != (char)delimiter))
{
// [continues if not at end of file and max string len not reached ]
// [AND continues if not printable flag set and characters printable ]
@ -341,27 +340,27 @@ Device input/---: NONE
Device output/---: NONE
Dependencies: getStringLength, malloc, copyString, free
*/
void getSubString(char *destStr, const char *sourceStr,
int startIndex, int endIndex)
void getSubString(char *destStr, const char *sourceStr,
int startIndex, int endIndex)
{
// initialize variables
// set length of source string using getStringLength
// set length of source string using getStringLength
int sourceStrLen = getStringLength(sourceStr);
// initialize the destination index to zero
// initialize the destination index to zero
int destIndex = 0;
// initialize source index to start index (parameter)
// initialize source index to start index (parameter)
int sourceIndex = startIndex;
// create pointer for temp string
// create pointer for temp string
char *tempStr;
// check for indices within limits
if(startIndex >= 0 && startIndex <= endIndex && endIndex < sourceStrLen)
if (startIndex >= 0 && startIndex <= endIndex && endIndex < sourceStrLen)
{
// create temp string using malloc and copyString
tempStr = (char *)malloc(sourceStrLen + 1);
copyString(tempStr, sourceStr);
// loop across requested substring indices
while(sourceIndex <= endIndex)
while (sourceIndex <= endIndex)
{
// assign source character to destination element
destStr[destIndex] = tempStr[sourceIndex];
@ -393,17 +392,17 @@ Dependencies: toLowerCase
void strToLowerCase(char *outString, const char *inString)
{
// initialize variables
// get source string length
// get source string length
int sourceStrLen = getStringLength(inString);
// create temporary string pointer
// create temporary string pointer
char *tempStr;
int index = 0;
// copy source string in case of aliasing using malloc and copyString
tempStr = (char *)malloc((sizeof(char) * (sourceStrLen + 1)));
copyString(tempStr, inString);
// loop across source string
while(tempStr[index] != NULL_CHAR && index < MAX_STR_LEN)
while (tempStr[index] != NULL_CHAR && index < MAX_STR_LEN)
{
// set individual character to lowercase as needed, assigning back
outString[index] = toLowerCase(tempStr[index]);
@ -415,7 +414,7 @@ void strToLowerCase(char *outString, const char *inString)
// end loop across source string
// free temporary string
free(tempStr);
// no return, void.
}
@ -433,7 +432,7 @@ Dependencies: none
char toLowerCase(char testChar)
{
// check for uppercase letter
if(testChar >= 'A' && testChar <= 'Z')
if (testChar >= 'A' && testChar <= 'Z')
{
// return lowercase letter
return testChar - 'A' + 'a';