// Programmed by Anthony Barbachan // Programmed in Turbo C++ 3.0 // Note: This is the source for the string librarys (STRING?.LIB) // Version 4.00 // Last Modified On 7/3/95 // Version: 4.01 // Last modified on: 1/21/1999 // Changes: Changed name from string to b_string to prevent compatability // errors with newer versions of C++. Header and source filenames // were also adjusted for name change. #ifndef __BSTRING_CPP__ #define __BSTRING_CPP__ #include "bstring.h" #include #include ///////////////////////////////////////*************************************** // Constructor #2 b_string::b_string(const char* s) { if((str = strdup(s)) == NULL) MemError(); } ///////////////////////////////////////*************************************** // Constructor #3 b_string::b_string(b_string& s) { if((str = strdup((char *) s)) == NULL) MemError(); } ///////////////////////////////////////*************************************** // This function deletes the current string void b_string::Del() { if(str != NULL) // If string is present { free(str); // Free memory from string str = NULL; // Secure pointer } } ///////////////////////////////////////*************************************** // This function assigns a new string b_string& b_string::operator = (const char* s) { Del(); // Delete any previous string if(s != NULL) // If s is a string { if((str = strdup(s)) == NULL) // Duplicate string s MemError(); // If error } return *this; // Return a refrence to this object } ///////////////////////////////////////*************************************** // This function appends string s to the current string b_string& b_string::operator += (const char* s) { if(str == NULL) // If no current string return *this = s; // Use assign operator if(s != NULL) // If s is a string { // Reallocate current memory block if((str = (char *) realloc(str, strlen(str)+strlen(s)+1u)) == NULL) MemError(); strcat(str, s); } return *this; } ///////////////////////////////////////*************************************** // This function returns a string containing s appended to this string b_string b_string::operator + (const char* s) { b_string temp (str); return temp += s; } ///////////////////////////////////////*************************************** // This function creates a copy of string s up to maxlen characters b_string& b_string::StrNCpy(const char* s, size_t maxlen) { Del(); // Delete any previous string if(s != NULL) // If s is a string { size_t len = strlen(s); // Get lenth if(len < maxlen) maxlen = len;// Choose shortest lenth if((str = (char *) calloc(maxlen + 1u, sizeof(char))) == NULL) MemError(); // If memory allocation error strncpy(str, s, maxlen); // Copy new string } return *this; // Return a refrence to this object } ///////////////////////////////////////*************************************** // This function appends up to maxlen characters to str b_string& b_string::StrNCat(const char* s, size_t maxlen) { if(str == NULL) // If no current string return StrNCpy(s, maxlen); // Use assign function if(s != NULL) // If s is a string { // Reallocate current memory block size_t len = strlen(s); // Get lenth if(len < maxlen) maxlen = len;// Choose shortest lenth str = (char *) realloc(str, (strlen(str)+maxlen+1u) * sizeof(char)); if(str == NULL) MemError(); strncat(str, s, maxlen); // Append string portion } return *this; // Return a refrence to this object } ///////////////////////////////////////*************************************** // This function copies s into str b_string& b_string::StpCpy(const char* s) { if(str == NULL) // If no current string return *this = s; // Use assign function else if(s != NULL) // If s is a string { if(strlen(str) < strlen(s)) // If more memory is needed *this = s; // Use assign function else // If stpcpy can be used strcpy(str, s); // Copy s into str } return *this; // Return a refrence to this object } ///////////////////////////////////////*************************************** // This function extracts a token from str and returns it b_string b_string::StrTok(const char* s) { b_string tokbuf; char* temp = NULL; if((str != NULL)&& (s != NULL)) // If s is a string { tokbuf = strtok(str, s); if((temp = strtok(NULL, s)) != NULL) { size_t len = (strlen(temp) + 1u) * sizeof(char); memmove(str, temp, len); if((str = (char *) realloc(str, len)) == NULL) MemError(); } } return tokbuf; } ///////////////////////////////////////*************************************** // This function extracts a token from str and returns it in tokbuf b_string& b_string::StrTok(b_string& tokbuf, const char* s) { char* temp = NULL; if((str != NULL)&& (s != NULL)) // If s is a string { tokbuf = strtok(str, s); if((temp = strtok(NULL, s)) != NULL) { size_t len = (strlen(temp) + 1u) * sizeof(char); memmove(str, temp, len); if((str = (char *) realloc(str, len)) == NULL) MemError(); } } return tokbuf; } ///////////////////////////////////////*************************************** //*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ istream& operator >> (istream& input, b_string& str) { char buffer[128]; // Local buffer large enougth for 1 dos line input >> buffer; // Get info from input stream str = buffer; // str will copy the string into itself return input; // Return a refrence to the input stream } //*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ #endif