/*****************************************************************************/ /* */ /* UNIT: NTL0_tsplit (Level 0 library routine) */ /* */ /* Author: Nikola Stojanovic */ /* */ /* Revision: 27 JUN 94 Version 1.0 */ /* */ /* Function: */ /* */ /* Procedure splits the received string (character buffer) to its "first" */ /* and "second" part, where "first" is the first sequence of contiguous */ /* non-white-space sharacters in the string, and "second" is everything else */ /* in the buffer, starting with the first occurence of a non-white-space */ /* character after the "first". Procedure leaves the original buffer intact, */ /* but copies the "first" part into separate (newly allocated) string, and */ /* leaves the pointer to "second" to point to a location in the original */ /* buffer. Procedure returns 1 if there was any text placed in "first" (in */ /* which case the pointer to first points to the new string containing its */ /* text), 0 otherwise (indicating that after possible skipping of initial */ /* white spaces end-of-string has been hit), in which case pointer to */ /* "first" is set to NULL; "second" will always point to some substring of */ /* the original, but it may be empty */ /* */ /*****************************************************************************/ #include #include #include "ntl0.h" /*****************************************************************************/ int NTL0_tsplit (char *buff, char **first, char **rest) { char *scan, *scan1; long int count; /* Eliminate spaces, tabs and newlines that may precede text in the buffer */ scan = buff; while ((*scan == ' ') || (*scan == '\t') || (*scan == '\n')) scan++; if (*scan == '\0') { /* There is no text left in the buffer */ *first = NULL; *rest = scan; return 0; } else { /* There is some significant text in the buffer before end-of-string */ /* Count the number of characters before the next separator */ count = 0; scan1 = scan; while ((*scan1 != ' ') && (*scan1 != '\t') && (*scan1 != '\n') && (*scan1 != '\0')) { count++; scan1++; } /* Now text has been found for the "first" part - allocate and copy it */ *first = NTL0_ckalloc (count + 1); scan1 = *first; while ((*scan != ' ') && (*scan != '\t') && (*scan != '\n') && (*scan != '\0')) { *scan1 = *scan; scan1++; scan++; } *scan1 = '\0'; /* Conclude the copy of the string with string terminator */ /* Skip all the spaces that may precede the "second" part of the buffer */ while ((*scan == ' ') || (*scan == '\t') || (*scan == '\n')) scan++; *rest = scan; return 1; /* Both "first" and "second" are set, so indicate status */ } }