Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // avp 2015 strtos.c get WORD from char[]
- #include <stdio.h>
- #include <string.h>
- /*
- before call: *p -- ptr to start search WORD in delimiters
- after call: *p -- ptr to next char after word (and *p - word == WORD length)
- returns ptr to WORD or 0
- */
- char *
- strtos (char **p, const char *delim)
- {
- char *word = 0;
- if (*((*p) += strspn(*p, delim))) {
- word = *p;
- (*p) += strcspn(*p, delim);
- }
- return word;
- }
- char *
- strdups (char **p, const char *delim)
- {
- char *word = strtos(p, delim);
- return word ? strndup(word, *p - word) : 0;
- }
- #ifdef TEST
- #include <stdlib.h>
- static inline char *
- strncpyz (char *dst, const char *src, int l)
- {
- char *r = dst;
- while (l && (*r++ = *src++))
- l--;
- if (!l)
- *r = 0;
- return dst;
- }
- int
- main (int ac, char *av[])
- {
- char *dlim = av[1] ? av[1] : (char *)" \t\r\n";
- char buf[1000], *w, *ep;
- while (fputs("enter: ", stdout), fflush(stdout), fgets(buf, 1000, stdin)) {
- #ifdef TDUP
- for (ep = buf; (w = strdups(&ep, dlim));) {
- printf("[%s] ", w);
- free(w);
- }
- #else
- char word[1000];
- for (ep = buf; (w = strtos(&ep, dlim));)
- printf("[%s] ", strncpyz(word, w, ep - w));
- #endif
- puts("");
- }
- return puts("End") == EOF;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement