Modified gets_ws() function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Poelstra

    #1

    Modified gets_ws() function

    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.
  • Andrew Poelstra

    #2
    Re: Modified gets_ws() function

    On 2006-08-12, Andrew Poelstra <apoelstra@fals e.sitewrote:
    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) &&
    This should be: (!buff || i < maxlen)

    --
    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

    • Andrew Poelstra

      #3
      Re: Modified gets_ws() function

      On 2006-08-12, Andrew Poelstra <apoelstra@fals e.sitewrote:
      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;
      >
      And here I need to null-terminate:
      /* We need to check in case maxlen is 0. */
      if (buff && maxlen)
      buff[i] = 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

      • Barry Schwarz

        #4
        Re: Modified gets_ws() function

        On Sat, 12 Aug 2006 16:57:39 GMT, Andrew Poelstra
        <apoelstra@fals e.sitewrote:
        >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);
        You apparently have reason to be concerned about fh being NULL.
        int stop_white = 1;
        int ch = getc (fh);
        If it is, this invokes undefined behavior.
        >
        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;
        >}

        Remove del for email

        Comment

        Working...