home *** CD-ROM | disk | FTP | other *** search
-
- #include <string.h>
- #include <stdlib.h>
-
- #include "NString.h"
- #include "NString_Misc.h"
-
- //_________________________________________________________________________________
-
- char NString::operator[] (const unsigned long int k) const
- {
- if ((k == 0) || (k > sb->len))
- USAGE_ERR("operator[] (const unsigned long int)", "Attempted character read access in NString out of range");
- return (sb->str[k-1]);
- }
-
- //_________________________________________________________________________________
-
- char NString::setchar (const unsigned long int k, const char c)
- {
- const char *fname = "setchar (const unsigned long int, const char)"; // the method's name
- char old;
-
- if ((k == 0) || (k > sb->len))
- USAGE_ERR(fname, "Attempted character write access in NString out of range");
- if (c == '\0')
- USAGE_ERR(fname, "Attempted write of NUL character in NString");
- if (sb->refs > 1) // the string body is used by another NString: must duplicate it to prevent
- if (! DuplicateSB()) // changes in common body
- OUT_OF_MEM(fname);
- old = sb->str[k-1];
- sb->str[k-1] = c;
- return (old);
- }
-
- //_________________________________________________________________________________
-
- NString& NString::through(int (*action)(const char, const unsigned long int, NString&), direction_t dir)
- {
- strbody *thru_sb = sb;
- unsigned long int index;
- int stop = 0;
-
- if (sb->len == 0)
- return (*this);
-
- thru_sb->refs++; // the iterator is now looking at the string body
-
- if (dir == FORWARD)
- for (index=1; (! stop) && (index <= thru_sb->len); index++)
- stop = action(thru_sb->str[index-1], index, *this);
- else
- for (index=thru_sb->len; (! stop) && (index > 0); index--)
- stop = action(thru_sb->str[index-1], index, *this);
-
- if ((--(thru_sb->refs)) == 0) // the string body isn't used any longer -> must free it
- {
- free(thru_sb->str);
- delete thru_sb;
- }
-
- return (*this);
- }
-
-
-
-
-