Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
StringUtils.h
/* |
** Apple Macintosh Developer Technical Support |
** |
** Header file for collection of String Utilities for DTS Sample code |
** |
** Copyright © 1988-1992 Apple Computer, Inc. |
** All rights reserved. |
*/ |
#ifndef __STRINGUTILS__ |
#define __STRINGUTILS__ |
#ifndef __TYPES__ |
#include <Types.h> |
#endif |
#ifdef __cplusplus |
extern "C" { |
#endif |
/* These are duplicates of c-library functions. The reason for duplicating them |
** is so that the StringUtils code can be small and linked in with other code that |
** stays resident at all times. It is possible that when you call code to do |
** something as seemingly innocent as getting the length of a string, memory can |
** move. This is because the code you are calling isn't necessarily in memory. |
** If the code segment that contains the code you are calling isn't in ram, then |
** it has to be loaded. Loading the code may cause memory compaction, and therefore |
** memory can move. The pointer to the string is already pushed on the stack prior |
** to the call, so if you passed a pointer into an unlocked handle, after calling |
** the code, that handle may have moved, and therefore the pointer is invalid. |
** |
** To prevent the above problem, alternate names were used for these common library |
** functions. Link this code into the same segment that holds main(), and you will |
** be guaranteed that they will be in memory whenever you call them. */ |
short clen(char *cptr); |
/* |
** ¦ Return the length of the c-string. |
** |
** INPUT: cptr Pointer to c-string to get length for. |
** RESULT: short Length of c-string. |
** |
** Return the length of the c-string. */ |
char *ccat(char *s1, char *s2); |
/* |
** ¦ Catenate two c-strings. |
** |
** INPUT: s1 Destination string to catenate onto. |
** s2 String to catenate with. |
** RESULT: char * Pointer to target string. |
** |
** Catenate two c-strings (s2 onto end of s1). */ |
char *ccpy(char *s1, char *s2); |
/* |
** ¦ Copy a c-string. |
** |
** INPUT: s1 Destination string for copy. |
** s2 String to copy. |
** RESULT: char * Pointer to target string. |
** |
** Copy a c-string (s2 onto s1). */ |
short pcmp(StringPtr s1, StringPtr s2); |
/* |
** ¦ Compare two pascal-strings. |
** |
** INPUT: s1 String effectively to the "left" of the compare. |
** s2 String effectively to the "right" of the compare. |
** RESULT: short Signed value. If < 0, then s1 < s2. |
** If > 0, then s1 > s2. |
** if = 0, then strings are equal. |
** |
** Compare two pascal-strings. */ |
void pcat(StringPtr d, StringPtr s); |
/* |
** ¦ Catenate two pascal-strings. |
** |
** INPUT: d Destination string to catenate onto. |
** s String to catenate with. |
** |
** Catenate two pascal-strings (s onto end of d). */ |
void pcpy(StringPtr d, StringPtr s); |
/* |
** ¦ Copy a pascal-string. |
** |
** INPUT: d Destination string for copy. |
** s String to copy. |
** |
** Copy a pascal-string (s onto d). */ |
void c2p(char *cptr); |
/* |
** ¦ Convert a c-string to a pascal-string. |
** |
** INPUT: cptr The string to convert. |
** |
** Convert a c-string to a pascal-string. |
** |
** __________ |
** |
** Also see: p2c. */ |
void p2c(StringPtr cptr); |
/* |
** ¦ Convert a pascal-string to a c-string. |
** |
** INPUT: cptr The string to convert. |
** |
** Convert a pascal-string to a c-string. |
** |
** __________ |
** |
** Also see: c2p. */ |
/*****************************************************************************/ |
/* These are useful, relatively small routines for string manipulation. As with the above calls, |
** link them into the code segment that holds main(). |
** |
** With the below functions, you will have most of the functionality of sprintf using shorts and |
** longs. It will take more calls, but only what you call is linked in. */ |
/**/ |
void ccatchr(char *cptr, char c, short count); |
/* |
** ¦ Catenate a single character multiple times onto the designated string. |
** |
** INPUT: cptr String to catenate character to. |
** c Character to catenate. |
** count Number of times to catenate char. |
** |
** Catenate a single character multiple times onto the designated string. |
** |
** __________ |
** |
** Also see: ccpychr, pcatchr, pcpychr. */ |
void ccatpaddec(char *cptr, char padChr, short minApnd, short maxApnd, long v); |
/* |
** ¦ Convert value into base-10 text and catenate it onto the string, with padding. |
** |
** INPUT: cptr String to catenate to. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and catenate. |
** |
** Convert the value into text for the base-10 number and catenate it to the designated string |
** with padding. The value is assumed to be signed. If you wish to have an unsigned decimal |
** value, call ccatnum with a base of 10. |
** |
** __________ |
** |
** Also see: ccatdec. */ |
void ccatdec(char *cptr, long v); |
/* |
** ¦ Convert value into base-10 text and catenate it onto the string. |
** |
** INPUT: cptr String to catenate to. |
** v Value to convert to text and catenate. |
** |
** Convert the value into text for the base-10 number and catenate it to |
** the designated string. The value is assumed to be signed. If you wish |
** to have an unsigned decimal value, call ccatnum with a base of 10. |
** |
** __________ |
** |
** Also see: ccatpaddec. */ |
void ccatpadhex(char *cptr, char padChr, short minApnd, short maxApnd, long v); |
/* |
** ¦ Convert value into base-16 text and catenate it onto the string, with padding. |
** |
** INPUT: cptr String to catenate to. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and catenate. |
** |
** Convert the value into text for the base-16 number and catenate it to the designated |
** string with padding. The value is assumed to be unsigned. |
** |
** __________ |
** |
** Also see: ccathex. */ |
void ccathex(char *cptr, long v); |
/* |
** ¦ Convert value into base-16 text and catenate it onto the string, with no padding. |
** |
** INPUT: cptr String to catenate to. |
** v Value to convert to text and catenate. |
** |
** Convert the value into text for the base-16 number and catenate it to the designated |
** string with no padding options. The value is assumed to be unsigned. |
** |
** __________ |
** |
** Also see: ccatpadhex. */ |
void ccatpadnum(char *cptr, char padChr, short minApnd, short maxApnd, long v, short base); |
/* |
** ¦ Convert value into designated base text and catenate it onto the string, with padding. |
** |
** INPUT: cptr String to catenate to. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and catenate. |
** base Base number is to be represented in. |
** |
** Convert the value into text for the designated base and catenate it to the designated string |
** with padding. If the base is negative, then the output is a signed number. |
** |
** __________ |
** |
** Also see: ccatnum. */ |
void ccatnum(char *cptr, long v, short base); |
/* |
** ¦ Convert value into designated text and catenate it onto the string, with no padding. |
** |
** INPUT: cptr String to catenate to. |
** v Value to convert to text and catenate. |
** base Designated base for conversion. (If negative, conversion is signed.) |
** |
** Convert the value into text of the designated base and catenate it to the |
** string with no padding options. If the base is negative, then the conversion is signed. |
** |
** __________ |
** |
** Also see: ccatpadnum. */ |
void ccpychr(char *cptr, char c, short count); |
/* |
** ¦ Copy a single character multiple times into the designated string. |
** |
** INPUT: cptr String to replace. |
** c Character to place in string. |
** count Number of times character should be copied. |
** |
** Copy a single character multiple times onto the designated string. |
** |
** __________ |
** |
** Also see: ccatchr. */ |
void ccpypaddec(char *cptr, char padChr, short minApnd, short maxApnd, long v); |
/* |
** ¦ Convert value into base-10 text and copy it into the string, with padding. |
** |
** INPUT: cptr String to copy into. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and copy. |
** |
** Convert the value into text for the base-10 number and copy it into the designated string |
** with padding. The value is assumed to be signed. If you wish to have an unsigned decimal |
** value, call ccpypadnum with a base of 10. |
** |
** __________ |
** |
** Also see: ccpydec. */ |
void ccpydec(char *cptr, long v); |
/* |
** ¦ Convert value into base-10 text and copy it into the string. |
** |
** INPUT: cptr String to copy into. |
** v Value to convert to text and copy. |
** |
** Convert the value into text for the base-10 number and copy it into |
** the designated string. The value is assumed to be signed. If you wish |
** to have an unsigned decimal value, call ccatnum with a base of 10. |
** |
** __________ |
** |
** Also see: ccpypaddec. */ |
void ccpypadhex(char *cptr, char padChr, short minApnd, short maxApnd, long v); |
/* |
** ¦ Convert value into base-16 text and copy it into the string, with padding. |
** |
** INPUT: cptr String to copy into. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and copy. |
** |
** Convert the value into text for the base-16 number and copy it into the designated |
** string with padding. The value is assumed to be unsigned. |
** |
** __________ |
** |
** Also see: ccpyhex. */ |
void ccpyhex(char *cptr, long v); |
/* |
** ¦ Convert value into base-16 text and copy it into the string, with no padding. |
** |
** INPUT: cptr String to copy into. |
** v Value to convert to text and copy. |
** |
** Convert the value into text for the base-16 number and copy it into the designated |
** string with no padding options. The value is assumed to be unsigned. |
** |
** __________ |
** |
** Also see: ccpypadhex. */ |
void ccpypadnum(char *cptr, char padChr, short minApnd, short maxApnd, long v, short base); |
/* |
** ¦ Convert value into designated base text and copy it into the string, with padding. |
** |
** INPUT: cptr String to copy into. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and copy. |
** base Base number is to be represented in. |
** |
** Convert the value into text for the designated base and copy it into the designated string |
** with padding. If the base is negative, then the output is a signed number. |
** |
** __________ |
** |
** Also see: ccpynum. */ |
void ccpynum(char *cptr, long v, short base); |
/* |
** ¦ Convert value into designated text and copy it into the string, with no padding. |
** |
** INPUT: cptr String to copy into. |
** v Value to convert to text and copy. |
** base Designated base for conversion. (If negative, conversion is signed.) |
** |
** Convert the value into text of the designated base and copy it into the |
** string with no padding options. If the base is negative, then the conversion is signed. |
** |
** __________ |
** |
** Also see: ccpypadnum. */ |
long c2dec(char *cptr, short *charsUsed); |
/* |
** ¦ Convert the c-string to a decimal number. |
** |
** INPUT: cptr String to convert to a decimal number. |
** charsUsed Pointer to short to hold # of chars scanned in the conversion. |
** (Pass in nil if you donÕt care.) |
** RESULT: long Decimal value. |
** |
** Convert the c-string to a decimal number. |
** |
** __________ |
** |
** Also see: c2hex, c2num. */ |
long c2hex(char *cptr, short *charsUsed); |
/* |
** ¦ Convert the c-string to a hex number. |
** |
** INPUT: cptr String to convert to a hex number. |
** charsUsed Pointer to short to hold # of chars scanned in the conversion. |
** (Pass in nil if you donÕt care.) |
** RESULT: long Hex value. |
** |
** Convert the c-string to a hex number. |
** |
** __________ |
** |
** Also see: c2dec, c2num. */ |
long c2num(char *cptr, short base, short *charsUsed); |
/* |
** ¦ Convert the c-string to a number of the designated base. |
** |
** INPUT: cptr String to convert to a number of the designated base. |
** base Number for the conversion. (If base is negative, conversion is signed.) |
** charsUsed Pointer to short to hold # of chars scanned in the conversion. |
** (Pass in nil if you donÕt care.) |
** RESULT: long Numeric value. |
** |
** Convert the c-string to a number of the designated base. If there are characters |
** found that determine the base, they will be used instead of the indicated base. |
** |
** __________ |
** |
** Also see: c2dec, c2hex. */ |
short GetLastBase(Boolean handleChars); |
/* |
** ¦ Returns what base was used when text is converted to a num and sets mode. |
** |
** INPUT: handleChars True if you want c2num or p2num (directly or indirectly) |
** to handle character constants. |
** |
** Sometimes the string being analyzed indicates what base the conversion should be |
** (i.e., starting with a $). Also, a common string data type is the 1 to 4 char string, |
** surrounded by single ticks. If you want to find out what the base was for the |
** string, just call GetLastBase. You also pass in a handleChars value of true or |
** false. (The default is false.) If you pass in true, then c2num, called directly or |
** indirectly, will convert these strings to numbers. */ |
/**/ |
void pcatchr(StringPtr pptr, char c, short count); |
/* |
** ¦ Catenate a single character multiple times onto the designated string. |
** |
** INPUT: pptr String to catenate character to. |
** c Character to catenate. |
** count Number of times to catenate char. |
** |
** Catenate a single character multiple times onto the designated string. |
** |
** __________ |
** |
** Also see: pcpychr. */ |
void pcatpaddec(StringPtr pptr, char padChr, short minApnd, short maxApnd, long v); |
/* |
** ¦ Convert value into base-10 text and catenate it onto the string, with padding. |
** |
** INPUT: pptr String to catenate to. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and catenate. |
** |
** Convert the value into text for the base-10 number and catenate it to the designated string |
** with padding. The value is assumed to be signed. If you wish to have an unsigned decimal |
** value, call pcatnum with a base of 10. |
** |
** __________ |
** |
** Also see: pcatdec. */ |
void pcatdec(StringPtr pptr, long v); |
/* |
** ¦ Convert value into base-10 text and catenate it onto the string. |
** |
** INPUT: pptr String to catenate to. |
** v Value to convert to text and catenate. |
** |
** Convert the value into text for the base-10 number and catenate it to |
** the designated string. The value is assumed to be signed. If you wish |
** to have an unsigned decimal value, call pcatnum with a base of 10. |
** |
** __________ |
** |
** Also see: pcatpaddec. */ |
void pcatpadhex(StringPtr pptr, char padChr, short minApnd, short maxApnd, long v); |
/* |
** ¦ Convert value into base-16 text and catenate it onto the string, with padding. |
** |
** INPUT: pptr String to catenate to. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and catenate. |
** |
** Convert the value into text for the base-16 number and catenate it to the designated |
** string with padding. The value is assumed to be unsigned. |
** |
** __________ |
** |
** Also see: pcathex. */ |
void pcathex(StringPtr pptr, long v); |
/* |
** ¦ Convert value into base-16 text and catenate it onto the string, with no padding. |
** |
** INPUT: pptr String to catenate to. |
** v Value to convert to text and catenate. |
** |
** Convert the value into text for the base-16 number and catenate it to the designated |
** string with no padding options. The value is assumed to be unsigned. |
** |
** __________ |
** |
** Also see: pcatpadhex. */ |
long pcatnum(StringPtr pptr, long v, short base); |
/* |
** ¦ Convert value into designated text and catenate it onto the string, with no padding. |
** |
** INPUT: pptr String to catenate to. |
** v Value to convert to text and catenate. |
** base Designated base for conversion. (If negative, conversion is signed.) |
** |
** Convert the value into text of the designated base and catenate it to the |
** string with no padding options. If the base is negative, then the conversion is signed. |
** |
** __________ |
** |
** Also see: pcatpadnum. */ |
void pcatpadnum(StringPtr pptr, char padChr, short minApnd, short maxApnd, long v, short base); |
/* |
** ¦ Convert value into designated base text and catenate it onto the string, with padding. |
** |
** INPUT: pptr String to catenate to. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and catenate. |
** base Base number is to be represented in. |
** |
** Convert the value into text for the designated base and catenate it to the designated string |
** with padding. If the base is negative, then the output is a signed number. |
** |
** __________ |
** |
** Also see: pcatnum. */ |
void pcpychr(StringPtr pptr, char c, short count); |
/* |
** ¦ Copy a single character multiple times into the designated string. |
** |
** INPUT: pptr String to replace. |
** c Character to place in string. |
** count Number of times character should be copied. |
** |
** Copy a single character multiple times onto the designated string. |
** |
** __________ |
** |
** Also see: pcatchr. */ |
void pcpypaddec(StringPtr pptr, char padChr, short minApnd, short maxApnd, long v); |
/* |
** ¦ Convert value into base-10 text and copy it into the string, with padding. |
** |
** INPUT: pptr String to copy into. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and copy. |
** |
** Convert the value into text for the base-10 number and copy it into the designated string |
** with padding. The value is assumed to be signed. If you wish to have an unsigned decimal |
** value, call pcpypadnum with a base of 10. |
** |
** __________ |
** |
** Also see: pcpydec. */ |
void pcpydec(StringPtr pptr, long v); |
/* |
** ¦ Convert value into base-10 text and copy it into the string. |
** |
** INPUT: pptr String to copy into. |
** v Value to convert to text and copy. |
** |
** Convert the value into text for the base-10 number and copy it into |
** the designated string. The value is assumed to be signed. If you wish |
** to have an unsigned decimal value, call ccatnum with a base of 10. |
** |
** __________ |
** |
** Also see: pcpypaddec. */ |
void pcpypadnum(StringPtr pptr, char padChr, short minApnd, short maxApnd, long v, short base); |
/* |
** ¦ Convert value into designated base text and copy it into the string, with padding. |
** |
** INPUT: pptr String to copy into. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and copy. |
** base Base number is to be represented in. |
** |
** Convert the value into text for the designated base and copy it into the designated string |
** with padding. If the base is negative, then the output is a signed number. |
** |
** __________ |
** |
** Also see: pcpynum. */ |
void pcpynum(StringPtr pptr, long v, short base); |
/* |
** ¦ Convert value into designated text and copy it into the string, with no padding. |
** |
** INPUT: pptr String to copy into. |
** v Value to convert to text and copy. |
** base Designated base for conversion. (If negative, conversion is signed.) |
** |
** Convert the value into text of the designated base and copy it into the |
** string with no padding options. If the base is negative, then the conversion is signed. |
** |
** __________ |
** |
** Also see: pcpypadnum. */ |
void pcpypadhex(StringPtr pptr, char padChr, short minApnd, short maxApnd, long v); |
/* |
** ¦ Convert value into base-16 text and copy it into the string, with padding. |
** |
** INPUT: pptr String to copy into. |
** padChr Character to pad with (or 0 for none). |
** minApnd Minimum length of string, if there is a non-0 pad char. |
** maxApnd Maximum length of string, including padding. |
** v Value to convert to text and copy. |
** |
** Convert the value into text for the base-16 number and copy it into the designated |
** string with padding. The value is assumed to be unsigned. |
** |
** __________ |
** |
** Also see: pcpyhex. */ |
void pcpyhex(StringPtr pptr, long v); |
/* |
** ¦ Convert value into base-16 text and copy it into the string, with no padding. |
** |
** INPUT: pptr String to copy into. |
** v Value to convert to text and copy. |
** |
** Convert the value into text for the base-16 number and copy it into the designated |
** string with no padding options. The value is assumed to be unsigned. |
** |
** __________ |
** |
** Also see: pcpypadhex. */ |
long p2dec(StringPtr pptr, short *charsUsed); |
/* |
** ¦ Convert the pascal-string to a decimal number. |
** |
** INPUT: pptr String to convert to a decimal number. |
** charsUsed Pointer to short to hold # of chars scanned in the conversion. |
** (Pass in nil if you donÕt care.) |
** RESULT: long Decimal value. |
** |
** Convert the pascal-string to a decimal number. |
** |
** __________ |
** |
** Also see: p2hex, p2num. */ |
long p2hex(StringPtr pptr, short *charsUsed); |
/* |
** ¦ Convert the pascal-string to a hex number. |
** |
** INPUT: pptr String to convert to a hex number. |
** charsUsed Pointer to short to hold # of chars scanned in the conversion. |
** (Pass in nil if you donÕt care.) |
** RESULT: long Hex value. |
** |
** Convert the pascal-string to a hex number. |
** |
** __________ |
** |
** Also see: p2dec, p2num. */ |
long p2num(StringPtr pptr, short base, short *charsUsed); |
/* |
** ¦ Convert the pascal-string to a number of the designated base. |
** |
** INPUT: pptr String to convert to a number of the designated base. |
** base Number for the conversion. (If base is negative, conversion is signed.) |
** charsUsed Pointer to short to hold # of chars scanned in the conversion. |
** (Pass in nil if you donÕt care.) |
** RESULT: long Numeric value. |
** |
** Convert the pascal-string to a number of the designated base. If there are characters |
** found that determine the base, they will be used instead of the indicated base. |
** |
** __________ |
** |
** Also see: p2dec, p2hex. */ |
/**/ |
short GetHexByte(char *cptr); |
/* |
** ¦ Internal function for returning a hex value from 2 characters. |
** |
** |
** INPUT: cptr Pointer to characters to analyze. |
** RESULT: short Hex value obtained from characters. |
** |
** This does not analyze just true hex values. The first character has special meaning for |
** AppsToGo functions. (This is really meant to be an internal function.) If the first |
** character is a Ò=Ó, then the next characterÕs ascii value is returned as the hex value |
** for the two characters. If the first character is a ÒÅÓ, then the second character |
** is upper-cased, and then the ascii value of that is returned. */ |
Boolean EqualHandle(void *h1, void *h2); |
/* |
** ¦ Checks to see if two handles are identical. |
** |
** INPUT: h1 First handle for compare. |
** h2 Second handle for compare. |
** RESULT: Boolean Returns true if handles are equal. |
** |
** This function checks to see if two handles are identical. */ |
Boolean EqualData(void *v1, void *v2, long size); |
/* |
** ¦ Checks to see if two blocks of data are identical. |
** |
** INPUT: v1 Pointer to first data block. |
** v2 Pointer to second data block. |
** size Length of data blocks. |
** RESULT: Boolean Returns true if blocks are equal. |
** |
** This function checks to see if two blocks of data are identical. */ |
void SetMem(void *vptr, unsigned char c, unsigned long len); |
/* |
** ¦ Sets a block of memory to a specified value. |
** |
** INPUT: vptr Pointer to block of memory to set. |
** c Byte value to store into memory. |
** len Length of block to set to specified value. |
** |
** Sets a block of memory to a specified value. */ |
#ifdef __cplusplus |
} |
#endif |
#endif |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-07-22