Skip to content

Commit

Permalink
Fixing textBuff
Browse files Browse the repository at this point in the history
First the idea of a shared return string was just BAD. Not my best idea. It's been removed.
textBuff now can do strings without messing up. This was the start of all this. This also included rewriting lilParser to 2.0 but that's over there not here.
  • Loading branch information
leftCoast committed Mar 27, 2024
1 parent 10b303d commit c5ff598
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 135 deletions.
21 changes: 2 additions & 19 deletions strTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ void lwrCase(char* inStr) {
// Load in this string. Allocate, or re-allocate your char* to save it.
bool heapStr(char** resultStr,const char* inStr) {

int numChars;
bool success;
int numChars;
bool success;

success = false; // We need to prove our success!
if (inStr) { // If we got a non-NULL string..
Expand Down Expand Up @@ -99,20 +99,3 @@ void tempStr::setStr(const char* inStr) { heapStr(&theStr,inStr); }
int tempStr::numChars(void) { return strlen(theStr); }

const char* tempStr::getStr(void) { return (const char*)theStr; }



//****************************************************************************************
// returnStr :
//
// returnStr is just a string pointer that has be pre-initialized at NULL and can be
// reallocated and used by functions as a return string. Granted, its only good for the
// moment immediately after it has been returned. So the calling function must copy or
// use it immediately. But it makes returning strings a LOT easier.
//
// heapStr(&returnStr,yourStr); // Allocates then copies your string to be returned.
// freeStr(&returnStr); // Resets it to NULL. (Optional, to pass back a NULL)
//****************************************************************************************


char* returnStr = NULL;
41 changes: 12 additions & 29 deletions strTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,49 +53,32 @@ extern void freeStr(char** resultStr);
// string only until it goes out of scope, and then it automatically recycles the memory
// for you.
//
// tempStr myStr(readThing(pinNum)); // Copy the output of a string function.
// tempStr myStr(readThing(pinNum)); // Copy the output of a string function.
//
// -or-
//
// tempStr myStr; // Or an empty one. Ready to save a string.
// tempStr myStr; // Or an empty one. Ready to save a string.
//
// myStr.setStr(readThing(pinNum)); // You can save a string later. Or reuse.
// formatAndDisplay(myStr.getStr()); // We don't worry about what the readThing() does.
// // We now have a local copy.
// myStr.setStr(readThing(pinNum)); // You can save a string later. Or reuse.
// formatAndDisplay(myStr.getStr()); // We don't worry about what the readThing() does.
// // We now have a local copy.
//
// return; // When tempStr goes out of scope, It recycles.
// return; // When tempStr goes out of scope, It recycles.
//****************************************************************************************


class tempStr {

public:
tempStr(const char* inStr=NULL);
virtual ~tempStr(void);
tempStr(const char* inStr=NULL);
virtual ~tempStr(void);

void setStr(const char* inStr);
int numChars(void);
const char* getStr(void);
void setStr(const char* inStr);
int numChars(void);
const char* getStr(void);

char* theStr;
char* theStr;
};



//****************************************************************************************
// returnStr :
//
// returnStr is just a string pointer that has be pre-initialized at NULL and can be
// reallocated and used by functions as a return string. Granted, its only good for the
// moment immediately after it has been returned. So the calling function must copy or
// use it immediately. But it makes returning strings a LOT easier.
//
// heapStr(&returnStr,yourStr); // Allocates then copies your string to be returned.
// freeStr(&returnStr); // Resets it to NULL. (Optional, to pass back a NULL)
//****************************************************************************************


extern char* returnStr;


#endif
186 changes: 102 additions & 84 deletions textBuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
#include <strTools.h>



textBuff::textBuff(int inNumBytes,bool inOverwrite) {

head = 0;
tail = 0;
isFull = false;
overwrite = inOverwrite;
buff = NULL;
while(inNumBytes && !resizeBuff(inNumBytes,&buff)) { // While inNumBytes>0 and resizing the bufer fails..
inNumBytes = inNumBytes/2; // Try for 1/2 the number of bytes?
}
numBytes = inNumBytes; // Note how many bytes we ended up with.
head = 0;
tail = 0;
isFull = false;
overwrite = inOverwrite;
buff = NULL;
returnStr = NULL;
while(inNumBytes && !resizeBuff(inNumBytes,&buff)) { // While inNumBytes>0 and resizing the buffer fails..
inNumBytes = inNumBytes/2; // Try for 1/2 the number of bytes?
}
numBytes = inNumBytes; // Note how many bytes we ended up with.
}


textBuff::~textBuff(void) { resizeBuff(0,&buff); }
// Destructor, clean up what we allocated.
textBuff::~textBuff(void) {

resizeBuff(0,&buff);
resizeBuff(0,&returnStr);
}


// Add a charactor and update the state. If its full it can take two different actions.
Expand All @@ -27,16 +34,16 @@ textBuff::~textBuff(void) { resizeBuff(0,&buff); }
bool textBuff::addChar(char inChar) {


if (overwrite && full()) { // If we're full and overwrite is true..
(void)readChar(); // Make room by dumping the oldest char.
}
if (!full()) { // If not full..
buff[tail] = inChar; // Save off the incoming char.
inc(&tail); // Increment the tail pointer.
isFull = tail==head; // Check and note if we are full.
return true; // Return a success.
}
return false; // Couldn't save the new char, return a failure.
if (overwrite && full()) { // If we're full and overwrite is true..
(void)readChar(); // Make room by dumping the oldest char.
}
if (!full()) { // If not full..
buff[tail] = inChar; // Save off the incoming char.
inc(&tail); // Increment the tail pointer.
isFull = tail==head; // Check and note if we are full.
return true; // Return a success.
}
return false; // Couldn't save the new char, return a failure.
}


Expand All @@ -45,28 +52,29 @@ bool textBuff::addChar(char inChar) {
// overwriting you will loose some of your oldest data.
bool textBuff::addStr(char* inCStr,bool andNULL) {

int i;
bool success;

success = false; // Well, we ain't a success yet.
i = 0; // Start up our counter..
while(inCStr[i]!='\0') { // While we are not potinting at the NULL char..
success = addChar(inCStr[i]); // Blindly stuff the char into the buffer.
i++; // Increment counter.
}
if (andNULL) { // If they want the null char saved..
success = addChar('\0'); // We add one in. Its the little things we do for you.
}
return success; // Return if they all went in.
int
i;
bool success;

success = false; // Well, we ain't a success yet.
i = 0; // Start up our counter..
while(inCStr[i]!='\0') { // While we are not pointing at the NULL char..
success = addChar(inCStr[i]); // Blindly stuff the char into the buffer.
i++; // Increment counter.
}
if (andNULL) { // If they want the null char saved..
success = addChar('\0'); // We add one in. Its the little things we do for you.
}
return success; // Return if they all went in.
}


// Pass back the next char on the list, unless we got nothin' then pass back a /0.
// Don't change anything.
char textBuff::peekHead(void) {

if (empty()) return '\0'; // If empty, return a \0.
return buff[head]; // Otherwise, return the char that's next to read.
if (empty()) return '\0'; // If empty, return a \0.
return buff[head]; // Otherwise, return the char that's next to read.
}


Expand All @@ -76,13 +84,13 @@ char textBuff::peekIndex(int index) {

int absIndex;

if (empty()) return '\0'; // If empty, return a '\0'.
if (index>numChars()) return '\0'; // If we don't have that one, return a '\0'.
absIndex = head + index; // Take a shot at the absolute index
if (absIndex>=numBytes) { // If we are past the buffer size..
absIndex = absIndex - numBytes; // Subtract the buffer size from the absolute.
}
return buff[absIndex]; // Otherwise, return the char that's next to read.
if (empty()) return '\0'; // If empty, return a '\0'.
if (index>numChars()) return '\0'; // If we don't have that one, return a '\0'.
absIndex = head + index; // Take a shot at the absolute index
if (absIndex>=numBytes) { // If we are past the buffer size..
absIndex = absIndex - numBytes; // Subtract the buffer size from the absolute.
} //
return buff[absIndex]; // Otherwise, return the char that's next to read.
}


Expand All @@ -93,13 +101,13 @@ char textBuff::readChar(void) {

char theChar;

if (!empty()) { // If we have some chars..
theChar = buff[head]; // Save off the oldest char.
inc(&head); // Do the indexing update.
isFull = false; // In any case we are no longer full.
return theChar; // Return the oldest char.
} else { // Else, we were empty..
return '\0'; // Pass back a \0.
if (!empty()) { // If we have some chars..
theChar = buff[head]; // Save off the oldest char.
inc(&head); // Do the indexing update.
isFull = false; // In any case we are no longer full.
return theChar; // Return the oldest char.
} else { // Else, we were empty..
return '\0'; // Pass back a \0.
}
}

Expand All @@ -109,31 +117,22 @@ char textBuff::readChar(void) {
// returns Just a c string consisting of '\0'.
char* textBuff::readStr(void) {

int numBytes;
int i;
int j;

numBytes = numChars(); // Lets save off the number of/. bytes stored.
if (empty()) { // If there are no bytes..
resizeBuff(1,&returnStr); // Resize the buff for empty string.
returnStr[0] = '\0'; // Copy in the \0.
} else { // Else, there are bytes in there..
i = 0; // Starting at zero.
while(buff[i]&&i<numBytes) i++; // Run down the buff looking for a \0.
if (i<numBytes) { // If i is less than numBytes, we hit a \0..
resizeBuff(i+1,&returnStr); // Resize the buff to hold the bytes & \0.
for (j=0;j<i;j++) { // For each char..
returnStr[j] = readChar(); // Copy it into the output string.
} //
} else { // Else, i = numBytes.. we hit the end.
resizeBuff(numBytes+1,&returnStr); // Resize the buff to fit.
for (j=0;j<i;j++) { // Read out all the buffer bytes.
returnStr[j] = readChar(); // Stuffing them into the return string.
} //
returnStr[j] = '\0'; // This one needs an \0 appended.
} //
} //
return returnStr; // Send it on it's way.
resizeBuff(1,&returnStr); // Resize the buff for empty string.
returnStr[0] = '\0'; // Copy in the \0.
if (!empty() && resizeBuff(strlen()+1,&returnStr)) { // If there are bytes AND we can allocate enough..
i = -1; // Starting at -1.
do { // Repeat..
i++; // Bump up the index. (Now starting at zero)
returnStr[i] = readChar(); // Read a char into our returnStr.
} while(returnStr[i]!='\0'&&!empty()); // If we DIDN'T read a null and STILL have chars..
if (returnStr[i]!='\0') { // If we didn't get a NULL char.
i++; // Bump up the index.
returnStr[i] = '\0'; // Pop a '\0' after the last char.
} //
} //
return returnStr; // Send it on it's way.
}


Expand All @@ -144,18 +143,37 @@ int textBuff::buffSize(void) { return numBytes; }
// Return the numnber of charactors THAT ARE stored in the buffer.
int textBuff::numChars(void) {

if (empty()) { // If its empty..
return 0; // Well, that would be zero.
} else if (full()) { // If its full..
return numBytes; // Return the number of bytes we store.
} else if (head<tail) { // Head less that tail.
return tail - head; // Data is between them, return the difference.
} else { // And lastly.. Head is greater than tail..
return numBytes - (head - tail); // Kinda' opposite. Data is between them on the other side.
if (empty()) { // If its empty..
return 0; // Well, that would be zero.
} else if (full()) { // If its full..
return numBytes; // Return the number of bytes we store.
} else if (head<tail) { // Head less that tail.
return tail - head; // Data is between them, return the difference.
} else { // And lastly.. Head is greater than tail..
return numBytes - (head - tail); // Kinda' opposite. Data is between them on the other side.
}
}


// How many chars would we read out if asked for a string? Not including the trailing NULL.
int textBuff::strlen(void) {

int count;
int max;
int strIdx;

if (empty()) return 0; // Well if we got none we give none.
strIdx = head; // Copied from readChar().
max = numChars(); // store the number of chars we have.
count = 0; // And starting at zero chars to read.
while(buff[strIdx]!='\0'&&count<max) { // If we're not pointing at null, and not at the limit of data..
count++; // Bump up the char count.
inc(&strIdx); // Increment the local index.
} //
return count; // And when complete, we return count.
}


// Return whethere the buffer is empty or not.
bool textBuff::empty(void) { return ((head==tail)&&!isFull); }

Expand All @@ -169,16 +187,16 @@ void textBuff::clear(void) {

head = 0;
tail = 0;
isFull = false; // In any case we are no longer full. (Missed that in the original)
isFull = false; // In any case we are no longer full. (Missed that in the original)
}


// Increment an index. Hop over to zero if we go past the end.
void textBuff::inc(int* index) {

*index = *index + 1; // Bump up this index.
if (*index>=numBytes) { // If this index went off the end..
*index = 0; // Set it to zero.
*index = *index + 1; // Bump up this index.
if (*index>=numBytes) { // If this index went off the end..
*index = 0; // Set it to zero.
}
}

7 changes: 4 additions & 3 deletions textBuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// stream before you can process it. This happens when hardware dumps data and you have no
// control over when and how much. This gives a place to stream all that text. Then you
// can read it back and deal with it at your leisure. You can even read it out as its
// being streamed in. That's pretty handy!
// being streamed in. Now that's pretty handy!
//****************************************************************************************


Expand All @@ -21,15 +21,15 @@ class textBuff {
textBuff(int inNumBytes,bool inOverwrite=false);
~textBuff(void);


bool addChar(char inChar); // Add a char.
bool addStr(char* inCStr,bool andNULL=true); // Add a c string. (With or without the nul terminator.)
char peekHead(void); // Look at the next char to come out.
char peekIndex(int index); // Have a look at the char at index. '\0' for no char.
char readChar(void); // Read out the next char. (removes it)
char* readStr(void); // Read out a c string. (removes it)
char* readStr(void); // Read out a c string. (removes it) NOTE : copy result it's a temp.
int buffSize(void); // How many chars CAN we store?
int numChars(void); // How many chars ARE we storing?
int strlen(void); // strlen() for the next string to read. Not counting the '\0'.
bool empty(void); // Are we empty?
bool full(void); // Are we full?
void clear(void); // Dump all the chars, reset to empty.
Expand All @@ -38,6 +38,7 @@ class textBuff {
void inc(int* index);

char* buff;
char* returnStr;
int numBytes;
int head;
int tail;
Expand Down

0 comments on commit c5ff598

Please sign in to comment.