diff --git a/strTools.cpp b/strTools.cpp index 72a7d33..ce455f3 100644 --- a/strTools.cpp +++ b/strTools.cpp @@ -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.. @@ -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; diff --git a/strTools.h b/strTools.h index 9445a1b..c602921 100644 --- a/strTools.h +++ b/strTools.h @@ -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 \ No newline at end of file diff --git a/textBuff.cpp b/textBuff.cpp index 9cbef3a..32a54d7 100644 --- a/textBuff.cpp +++ b/textBuff.cpp @@ -3,21 +3,28 @@ #include + 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. @@ -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. } @@ -45,19 +52,20 @@ 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. } @@ -65,8 +73,8 @@ bool textBuff::addStr(char* inCStr,bool andNULL) { // 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. } @@ -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. } @@ -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. } } @@ -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) { // 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. } } diff --git a/textBuff.h b/textBuff.h index c93860a..3e3437b 100644 --- a/textBuff.h +++ b/textBuff.h @@ -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! //**************************************************************************************** @@ -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. @@ -38,6 +38,7 @@ class textBuff { void inc(int* index); char* buff; + char* returnStr; int numBytes; int head; int tail;