home *** CD-ROM | disk | FTP | other *** search
-
- #include <string.h>
- #include <stdlib.h>
-
- #include "NString.h"
- #include "NString_Misc.h"
-
- //_________________________________________________________________________________
-
- NString& NString::operator= (const char *s)
- {
- const char *fname = "operator= (const char *)";
- unsigned long int new_length;
-
- if (s == NULL)
- s = "";
-
- new_length = strlen(s);
-
- if (sb->refs > 1) // there's another variable looking at this string body
- {
- if (! GetNewSB(new_length))
- OUT_OF_MEM(fname);
- }
- else
- {
- if (! ReallocStrBuf(new_length))
- OUT_OF_MEM(fname);
- sb->len = new_length;
- }
-
- strcpy(sb->str, s);
- return (*this);
- }
-
- //_________________________________________________________________________________
-
- NString& NString::operator= (const char c)
- {
- const char *fname = "operator= (const char)"; // the method's name (for error handling purposes)
-
- if (c == '\0')
- USAGE_ERR(fname, "Attempted assignment of NUL character to NString");
-
- if (sb->refs > 1) // there's another variable looking at this string body
- {
- if (! GetNewSB(1))
- OUT_OF_MEM(fname);
- }
- else // this string body isn't being used by anyone else (sb->refs == 1)
- {
- if (! ReallocStrBuf(1))
- OUT_OF_MEM(fname);
- sb->len = 1;
- }
-
- sb->str[0] = c; // define the string contents
- sb->str[1] = '\0';
-
- return (*this);
- }
-
- //_________________________________________________________________________________
-
- NString& NString::operator= (const NString& s)
- {
- s.sb->refs++; // protect against "st = st"
- if ((--(sb->refs)) == 0) // the old string body isn't being used any more
- {
- free(sb->str);
- delete sb;
- }
- sb = s.sb; // set this variable to look at the same string body as "s"
- return (*this);
- }
-