home *** CD-ROM | disk | FTP | other *** search
-
- /*© Copyright 1988-1992 UserLand Software, Inc. All Rights Reserved.*/
-
-
- #include "appletops.h"
- #include "appletmemory.h"
- #include "appletstrings.h"
-
-
- boolean equalstrings (bs1, bs2) bigstring bs1, bs2; {
-
- /*
- return true if the two strings (pascal type, with length-byte) are
- equal. return false otherwise.
- */
-
- register ptrchar p1 = (ptrchar) bs1, p2 = (ptrchar) bs2;
- register char ct = *p1 + 1;
-
- while (ct--)
-
- if (*p1++ != *p2++)
-
- return (false);
-
- return (true); /*loop terminated*/
- } /*equalstrings*/
-
-
- boolean unicaseequalstrings (bs1, bs2) bigstring bs1, bs2; {
-
- bigstring bscopy1, bscopy2;
-
- copystring (bs1, bscopy1);
-
- copystring (bs2, bscopy2);
-
- alllower (bscopy1);
-
- alllower (bscopy2);
-
- return (equalstrings (bscopy1, bscopy2));
- } /*unicaseequalstrings*/
-
-
- void copystring (bssource, bsdest) void *bssource, *bsdest; {
-
- /*
- create a copy of bssource in bsdest. copy the length byte and
- all the characters in the source string.
-
- assume the strings are pascal strings, with the length byte in
- the first character of the string.
- */
-
- register short i, len;
-
- len = (short) ((char *) bssource) [0];
-
- for (i = 0; i <= len; i++)
- ((char *) bsdest) [i] = ((char *) bssource) [i];
- } /*copystring*/
-
-
- boolean pushstring (bssource, bsdest) bigstring bssource, bsdest; {
-
- /*
- insert the source string at the end of the destination string.
-
- assume the strings are pascal strings, with the length byte in
- the first character of the string.
- */
-
- register short lensource = stringlength (bssource);
- register short lendest = stringlength (bsdest);
- register char *psource, *pdest;
-
- if ((lensource + lendest) > lenbigstring)
- return (false);
-
- pdest = (ptrchar) bsdest + (char) lendest + 1;
-
- psource = (ptrchar) bssource + 1;
-
- bsdest [0] += (char) lensource;
-
- while (lensource--) *pdest++ = *psource++;
-
- return (true);
- } /*pushstring*/
-
-
- boolean pushspace (bs) bigstring bs; {
-
- bigstring bsspace;
-
- setstringlength (bsspace, 1);
-
- bsspace [1] = ' ';
-
- return (pushstring (bsspace, bs));
- } /*pushspace*/
-
-
- void pushlong (num, bsdest) long num; bigstring bsdest; {
-
- bigstring bsint;
-
- NumToString (num, bsint);
-
- pushstring (bsint, bsdest);
- } /*pushlong*/
-
-
- void pushint (num, bsdest) short num; bigstring bsdest; {
-
- pushlong ((long) num, bsdest);
- } /*pushint*/
-
-
- void allupper (bs) bigstring bs; {
-
- register char len = bs [0];
- register ptrchar p = (ptrchar) &bs [1];
- register char ch;
-
- while (len--) {
-
- ch = *p;
-
- if ((ch >= 'a') && (ch <= 'z'))
- *p -= 32;
-
- p++;
- } /*while*/
- } /*allupper*/
-
-
- void alllower (bs) bigstring bs; {
-
- register char len = bs [0];
- register ptrchar p = (ptrchar) &bs [1];
- register char ch;
-
- while (len--) {
-
- ch = *p;
-
- if ((ch >= 'A') && (ch <= 'Z'))
- *p += 32;
-
- p++;
- } /*while*/
- } /*alllower*/
-
-
- boolean stringlessthan (bs1, bs2) bigstring bs1, bs2; {
-
- register short i, ctloops;
- register char ch1, ch2;
- short len1, len2;
-
- len1 = (short) bs1 [0];
-
- len2 = (short) bs2 [0];
-
- ctloops = minint (len1, len2);
-
- for (i = 1; i <= ctloops; i++) {
-
- ch1 = bs1 [i];
-
- ch2 = bs2 [i];
-
- if (ch1 != ch2) /*we have our answer*/
- return (ch1 < ch2);
- } /*for*/
-
- return (len1 < len2); /*loop terminated, strings are equal up to the min length*/
- } /*stringlessthan*/
-
-
- void midstring (bssource, ix, len, bsdest) bigstring bssource, bsdest; short ix, len; {
-
- setstringlength (bsdest, len);
-
- moveleft (bssource + ix, bsdest + 1, (long) len);
- } /*midstring*/
-
-
- boolean pushchar (ch, bs) byte ch; bigstring bs; {
-
- /*
- insert the character at the end of a pascal string.
- */
-
- register short len;
-
- len = bs [0];
-
- if (len >= lenbigstring)
- return (false);
-
- bs [++len] = ch;
-
- bs [0] = len;
-
- return (true);
- } /*pushchar*/
-
-
- boolean scanstring (ch, bs, ix) byte ch; bigstring bs; short *ix; {
-
- /*
- return in ix the index in the string of the first occurence of chscan.
-
- return false if it wasn't found, true otherwise.
-
- dmb 10/26/90: p is now initialized correctly to bs + i, not bs + 1
- */
-
- register short i;
- register ptrbyte p;
- register byte c = ch;
- register short len = stringlength (bs);
-
- for (i = *ix, p = bs + i; i <= len; i++)
-
- if (*p++ == c) {
-
- *ix = i;
-
- return (true);
- }
-
- return (false);
- } /*scanstring*/
-
-
- boolean deletestring (bs, ixdelete, ctdelete) bigstring bs; short ixdelete, ctdelete; {
-
- /*
- delete ct chars in the indicated string, starting with the character
- at offset ix.
- */
-
- register short ix = ixdelete;
- register short ct = ctdelete;
- register short len = stringlength (bs);
- register long ctmove;
- register ptrbyte pfrom, pto;
-
- if ((ix > len) || (ix < 1))
- return (false);
-
- if (ct <= 0)
- return (ct == 0);
-
- ctmove = len - ix - ct + 1;
-
- if (ctmove > 0) {
-
- pfrom = bs + ix + ct;
-
- pto = bs + ix;
-
- moveleft (pfrom, pto, ctmove);
- }
-
- setstringlength (bs, len - ct);
-
- return (true);
- } /*deletestring*/
-
-
- boolean firstword (bssource, chdelim, bsdest) bigstring bssource, bsdest; char chdelim; {
-
- /*
- copy the first word from bs, and put it into bsdest.
-
- search forwards from the beginning of the source string until you
- find chdelim.
- */
-
- register short len = stringlength (bssource);
- register short i;
- register char ch = chdelim;
-
- for (i = 1; i <= len; i++) {
-
- if (bssource [i] == ch) {
-
- midstring (bssource, 1, i - 1, bsdest);
-
- return (true);
- }
- } /*for*/
-
- copystring (bssource, bsdest);
-
- return (true);
- } /*firstword*/
-
-
- boolean lastword (bssource, chdelim, bsdest) bigstring bssource, bsdest; char chdelim; {
-
- /*
- copy the last word from bs, and put it into bsdest.
-
- search backwards from the end of the source string until you find
- chdelim.
- */
-
- register short len = stringlength (bssource);
- register short i;
- register char ch = chdelim;
-
- for (i = len; i > 0; i--) {
-
- if (bssource [i] == ch) {
-
- midstring (bssource, i + 1, len - i, bsdest);
-
- return (true);
- }
- } /*for*/
-
- copystring (bssource, bsdest);
-
- return (true);
- } /*lastword*/
-
-
- short patternmatch (bigstring bspattern, bigstring bs) {
-
- short lenstring = stringlength (bs);
- short lenpattern = stringlength (bspattern);
- short ixstring = 1;
- byte chfirst;
- short i, ix;
-
- if ((lenstring == 0) || (lenpattern == 0))
- return (0);
-
- chfirst = bspattern [1];
-
- while (true) {
-
- if (bs [ixstring] == chfirst) { /*matched at least first character in string*/
-
- for (i = 2; i <= lenpattern; i++) {
-
- ix = ixstring + i - 1;
-
- if (ix > lenstring) /*gone off end of string, can't match*/
- return (0);
-
- if (bs [ix] != bspattern [i])
- goto L1;
- } /*for*/
-
- return (ixstring); /*loop terminated, full match*/
- }
-
- L1: /*advance to next character in string*/
-
- if (++ixstring > lenstring) /*reached end of string, not found*/
- return (0);
- } /*while*/
- } /*patternmatch*/
-
-
- void filledstring (char ch, short ct, bigstring bs) {
-
- if (ct < 0)
- ct = 0;
-
- bs [0] = ct;
-
- fillchar (&bs [1], (long) ct, ch);
- } /*filledstring*/
-
-
- boolean insertchar (char ch, bigstring bsdest) {
-
- register byte *pdest = bsdest;
- register short len = stringlength (pdest);
-
- if (len == lenbigstring)
- return (false);
-
- moveright (pdest + 1, pdest + 2, len);
-
- setstringlength (pdest, len + 1);
-
- pdest [1] = ch;
-
- return (true);
- } /*insertchar*/
-
-
-