libmib astring usage
There are just a few simple points for creating, using and and destroying astrings, as represented in the following example code:
char *pasz = 0; /* Always initialize an astring to 0 before use */
int ind;
for(ind = 0;ind < 1000;ind++) {
astrcat(&pasz,"Repeat"); /* use astring equivalents for
str[n]cpy and str[n]cat */
}
printf("Length: %d\n",strlen(pasz)); /* use pasz as a regular 'char *' */
astrfree(&pasz); /* use astrfree() or free() to prevent leaks. */
- Always initialize an astring to 0 before the first use.
char *ppasz = 0;
- Use the astring equivalents for strcpy(), strncpy(), strcat(), or strncat(). They are declared as follows:
char *astrcpy(char **ppasz,const char *pSrc);
char *astrn0cpy(char **ppasz,const char *pSrc,int n);
/* NOTE: astrn0cpy Always stores the ending \0 */
char *astrcat(char **ppasz,const char *pszSrc);
char *astrn0cat(char **ppasz,const char *pSrc,int n);
/* NOTE: astrn0cat Always stores the ending \0 */
- Use other non-modifying string functions as usual. (strchr, strspn, etc.)
- Always assume that the string could have been moved after calling an astring function. Don't hold onto stale pointers. (See how afgets() is implemented using an index, not a pointer.)
- (Use astrensure() to permit custom/advanced buffer manipulation.)
- When finished, always call astrfree() or free() to prevent memory leaks.
astrfree(&ppasz);
- The implementation "over-allocates" memory (to reduce copying.) Use the
astrstatic() function if you have large numbers of "long-lived" astrings, see
astring advanced usage.
- Take advantage of the "astring-enhanced" sprintf and fgets functions.
int avsprintf(char **ppasz, const char *format,va_list ap);
int asprintf(char **ppasz, const char *format,...);
char *afgets(char **ppasz,FILE *f);
int afgettoch(char **ppasz,FILE *f, char chStop)
/* Reads up to and including the next chStop character,
storing a \0 afterwards. Returns number of characters read. */
astring advanced usage discusses adjusting the implementation strategies and settings for efficiency and debugging, as well as how to use astrensure() and astrstatic() for creating functions which extend and shrink the buffer.
Get the Source
astring advanced usage
Using astrensure() to write your own limitless string functions.
Examples
Example: an "fgrep-like" filter using astrings easily handles unlimited line lengths in a small amount of code.
Up to: Libmib Allocated String Functions
Up to: Libmib: Character String Processing
Up to: Libmib
This Libmib documentation may not be distributed.
Copyright 1998, Forrest J. Cavalier III
Mib Software
High Reuse Software Development