I've added the capability that if the input is quoted, it will read until the
end quote (ignoring whitespace and the stop character, which is its default
behavior).
gets_ws.c:
/* Reads from fh into buff (buff may be NULL, in which case characters *
* are discarded), up until it reaches either stop_c or whitespace. (In *
* the special case that the first character is a quote, it will simply *
* read everything until it reaches an end quote.) Unless buff is NULL, *
* it reads at most (maxlen-1) characters and null-terminates buff. If *
* count is non-NULL, it will be filled with the number of characters *
* read. Returns 0 on success or EOF on error. */
int gets_ws (char *buff, size_t *count, size_t maxlen, char stop_c, FILE *fh)
{
size_t i = 0;
int err = (fh == NULL);
int stop_white = 1;
int ch = getc (fh);
switch (ch)
{
case '\"':
case '\'':
case '`':
stop_white = 0;
stop_c = ch;
break;
case EOF:
err = 1;
}
while ((!stop_white || !isspace (ch)) &&
(!buff || i >= maxlen) &&
(ch != stop_c))
{
if (buff && i < maxlen)
buff[i++] = ch;
if (ch == EOF)
{
err = (ch == EOF);
ch = stop_c;
} else {
ch = getc (fh);
}
++i;
}
if (count != NULL)
*count = i;
ungetc (ch, fh);
return err ? EOF : 0;
}
--
Andrew Poelstra <http://www.wpsoftware. net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
end quote (ignoring whitespace and the stop character, which is its default
behavior).
gets_ws.c:
/* Reads from fh into buff (buff may be NULL, in which case characters *
* are discarded), up until it reaches either stop_c or whitespace. (In *
* the special case that the first character is a quote, it will simply *
* read everything until it reaches an end quote.) Unless buff is NULL, *
* it reads at most (maxlen-1) characters and null-terminates buff. If *
* count is non-NULL, it will be filled with the number of characters *
* read. Returns 0 on success or EOF on error. */
int gets_ws (char *buff, size_t *count, size_t maxlen, char stop_c, FILE *fh)
{
size_t i = 0;
int err = (fh == NULL);
int stop_white = 1;
int ch = getc (fh);
switch (ch)
{
case '\"':
case '\'':
case '`':
stop_white = 0;
stop_c = ch;
break;
case EOF:
err = 1;
}
while ((!stop_white || !isspace (ch)) &&
(!buff || i >= maxlen) &&
(ch != stop_c))
{
if (buff && i < maxlen)
buff[i++] = ch;
if (ch == EOF)
{
err = (ch == EOF);
ch = stop_c;
} else {
ch = getc (fh);
}
++i;
}
if (count != NULL)
*count = i;
ungetc (ch, fh);
return err ? EOF : 0;
}
--
Andrew Poelstra <http://www.wpsoftware. net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Comment