Simple vsscanf source code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ray Mitchell

    Simple vsscanf source code

    Hello,

    I realize that the source code for vsscanf is available from several
    sources, such as GNU. However, all such source code I've found so far is
    filled with cryptic (to me) #ifdefs, system stuff, and basically all kinds
    of
    general things related to a particular implementation.

    I'm looking for a simple concise generic C99 version that I can easily adapt
    into my application without having to look all over the place for a
    seemingly
    unending litany of cryptic "helper" functions. I'm sure I could write my
    own
    version but I'd rather not go through the pain if I can avoid it.

    Thanks,
    Ray Mitchell
    --
    comp.lang.c.mod erated - moderation address: clcm@plethora.n et
  • Charlie Gordon

    #2
    Re: Simple vsscanf source code

    "Ray Mitchell" <RayMitchell@Me anOldTeacher.co m> wrote in message
    news:clcm-20041116-0010@plethora.n et...[color=blue]
    > Hello,
    >
    > I realize that the source code for vsscanf is available from several
    > sources, such as GNU. However, all such source code I've found so far is
    > filled with cryptic (to me) #ifdefs, system stuff, and basically all kinds
    > of
    > general things related to a particular implementation.
    >
    > I'm looking for a simple concise generic C99 version that I can easily adapt
    > into my application without having to look all over the place for a
    > seemingly
    > unending litany of cryptic "helper" functions. I'm sure I could write my
    > own
    > version but I'd rather not go through the pain if I can avoid it.[/color]

    Well no surprise! vsscanf() is one of the most complex functions you can find in
    the C library.
    Its semantics are contorted and extensive and there are a lot of small details
    to take care of, especially with portability as a constraint.
    What kind of extensions are you looking to add ?
    What features are you willing to remove to get a simpler version ?
    You can try to find Plauger's implementation from his book "The Standard C
    Library". An older version would be a better start.

    Chqrlie.
    --
    comp.lang.c.mod erated - moderation address: clcm@plethora.n et

    Comment

    • Hans-Bernhard Broeker

      #3
      Re: Simple vsscanf source code

      [F'up2 cut down --- should have been done by OP!]

      In comp.lang.c.mod erated Ray Mitchell <RayMitchell@me anoldteacher.co m> wrote:
      [color=blue]
      > I realize that the source code for vsscanf is available from several
      > sources, such as GNU. However, all such source code I've found so
      > far is filled with cryptic (to me) #ifdefs, system stuff, and
      > basically all kinds of general things related to a particular
      > implementation.[/color]

      And you think the authors of those versions put all those
      system-specifica and #if's in there for fun, or what?

      Here's a clue: if a simple, portable implementation were possible,
      odds are vsscanf() would never have become part of the standard
      library to begin with. The primary reason for including a function in
      the library, nowadays, is "the job can't be done (efficiently) in a
      platform-independent way". I.e. it's in libc *because* you can't
      write it yourself without becoming hugely platform-dependent.
      [color=blue]
      > I'm looking for a simple concise generic C99 version that I can
      > easily adapt into my application without having to look all over the
      > place for a seemingly unending litany of cryptic "helper" functions.[/color]

      In C99 you can't possibly need such a thing --- the very fact that a
      compiler toolchain supports C99 mandates that it already supplies you
      with a vsscanf() implementation.

      --
      Hans-Bernhard Broeker (broeker@physik .rwth-aachen.de)
      Even if all the snow were burnt, ashes would remain.
      --
      comp.lang.c.mod erated - moderation address: clcm@plethora.n et

      Comment

      • Jonathan Leffler

        #4
        Re: Simple vsscanf source code

        Ray Mitchell wrote:[color=blue]
        > I realize that the source code for vsscanf is available from
        > several sources, such as GNU. However, all such source code I've
        > found so far is filled with cryptic (to me) #ifdefs, system stuff,
        > and basically all kinds of general things related to a particular
        > implementation.
        >
        > I'm looking for a simple concise generic C99 version that I can
        > easily adapt into my application without having to look all over
        > the place for a seemingly unending litany of cryptic "helper"
        > functions. I'm sure I could write my own version but I'd rather not
        > go through the pain if I can avoid it.[/color]

        If you can meet the licencing terms, consider Plauger's The Standard C
        Library (where the standard in question is ISO 9899:1990).

        --
        Jonathan Leffler #include <disclaimer.h >
        Email: jleffler@earthl ink.net, jleffler@us.ibm .com
        Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/
        --
        comp.lang.c.mod erated - moderation address: clcm@plethora.n et

        Comment

        • MarcSmith

          #5
          Re: Simple vsscanf source code


          int vsscanf( char *buf, char *format, va_list argp )
          {
          char *fmtp;
          char *bufp;
          Bool suppress;
          int mytype, width, n, k = 0;
          char lastchar;

          bufp = buf;

          for (fmtp = format; *fmtp; fmtp++)
          { if (*fmtp == '%')
          { mytype = NORMAL_TYPE;
          suppress = FALSE;
          width = 0;
          lastchar = ' ';
          }
          else if (*fmtp == '*')
          suppress = TRUE;
          else if (isspace(*fmtp) );
          else if (isdigit(*fmtp) )
          { if (lastchar != '.')
          { width *= 10;
          width += (*fmtp - '0');
          }
          }
          else if (*fmtp == 'l' || *fmtp == 'L')
          mytype = LONG;
          else if (*fmtp == 'h')
          mytype = SHORT;
          else if (*fmtp == 'i' || *fmtp == 'd')
          { if (suppress)
          bufp = Advance(bufp);
          else if (mytype == SHORT)
          { k+=sscanf(bufp, "%hd%n",va_arg( argp,short*),&n );
          bufp+=n;
          }
          else if (mytype == LONG)
          { k+=sscanf(bufp, "%ld%n",va_arg( argp,long*),&n) ;
          bufp+=n;
          }
          else
          { k+=sscanf(bufp, "%d%n",va_arg(a rgp, int*),&n);
          bufp+=n;
          }
          }
          else if (*fmtp == 'f')
          { if (suppress)
          bufp = Advance(bufp);
          else if (mytype == LONG)
          { k+=sscanf(bufp, "%f%n",va_arg(a rgp, double*),&n);
          bufp+=n;
          }
          else
          { k+=sscanf(bufp, "%f%n",va_arg(a rgp, float*),&n);
          bufp+=n;
          }
          }
          else if (*fmtp == 's')
          { if (suppress)
          bufp = Advance(bufp);
          else {
          k+=sscanf(bufp, "%s%n",va_arg(a rgp, char*),&n);
          bufp+=n;
          }
          }
          else if (*fmtp == 'c')
          { if (!suppress)
          { k+=sscanf(bufp, "%c%n",va_arg(a rgp, char*),&n);
          bufp+=n;
          }
          else bufp++;
          }
          else lastchar = *fmtp;
          } /* for */
          return k;
          } /* vsscanf clone */

          static char *Advance( char *bufp )
          {
          char *new_bufp = bufp;

          /* Skip over nonwhite SPACE */
          while ((*new_bufp != ' ') && (*new_bufp != '\t') &&
          (*new_bufp != '\n') && (*new_bufp != '\0'))
          new_bufp++;

          /* Skip white SPACE */
          while ((*new_bufp == ' ') || (*new_bufp == '\t') ||
          (*new_bufp == '\n') || (*new_bufp == '\0'))
          new_bufp++;

          return new_bufp;
          } /* Advance *


          -
          MarcSmit
          -----------------------------------------------------------------------
          Posted via http://www.codecomments.co
          -----------------------------------------------------------------------

          Comment

          • Bjørn Augestad

            #6
            Re: Simple vsscanf source code

            MarcSmith wrote:
            [snip][color=blue]
            > int vsscanf( char *buf, char *format, va_list argp )[/color]

            const char* buf, const char* format, if you want to clone vsscanf().
            [color=blue]
            > {
            > char *fmtp;
            > char *bufp;
            > Bool suppress;
            > int mytype, width, n, k = 0;
            > char lastchar;
            >
            > bufp = buf;
            >
            > for (fmtp = format; *fmtp; fmtp++)
            > { if (*fmtp == '%')
            > { mytype = NORMAL_TYPE;
            > suppress = FALSE;
            > width = 0;
            > lastchar = ' ';
            > }
            > else if (*fmtp == '*')
            > suppress = TRUE;
            > else if (isspace(*fmtp) );
            > else if (isdigit(*fmtp) )
            > { if (lastchar != '.')
            > { width *= 10;
            > width += (*fmtp - '0');
            > }
            > }
            > else if (*fmtp == 'l' || *fmtp == 'L')
            > mytype = LONG;
            > else if (*fmtp == 'h')
            > mytype = SHORT;
            > else if (*fmtp == 'i' || *fmtp == 'd')
            > { if (suppress)
            > bufp = Advance(bufp);
            > else if (mytype == SHORT)
            > { k+=sscanf(bufp, "%hd%n",va_arg( argp,short*),&n );
            > bufp+=n;
            > }
            > else if (mytype == LONG)
            > { k+=sscanf(bufp, "%ld%n",va_arg( argp,long*),&n) ;
            > bufp+=n;
            > }
            > else
            > { k+=sscanf(bufp, "%d%n",va_arg(a rgp, int*),&n);
            > bufp+=n;
            > }
            > }
            > else if (*fmtp == 'f')
            > { if (suppress)
            > bufp = Advance(bufp);
            > else if (mytype == LONG)
            > { k+=sscanf(bufp, "%f%n",va_arg(a rgp, double*),&n);
            > bufp+=n;
            > }
            > else
            > { k+=sscanf(bufp, "%f%n",va_arg(a rgp, float*),&n);
            > bufp+=n;
            > }
            > }
            > else if (*fmtp == 's')
            > { if (suppress)
            > bufp = Advance(bufp);
            > else {
            > k+=sscanf(bufp, "%s%n",va_arg(a rgp, char*),&n);
            > bufp+=n;
            > }
            > }
            > else if (*fmtp == 'c')
            > { if (!suppress)
            > { k+=sscanf(bufp, "%c%n",va_arg(a rgp, char*),&n);
            > bufp+=n;
            > }
            > else bufp++;
            > }
            > else lastchar = *fmtp;
            > } /* for */
            > return k;
            > } /* vsscanf clone */[/color]

            [color=blue]
            >
            > static char *Advance( char *bufp )
            > {
            > char *new_bufp = bufp;
            >
            > /* Skip over nonwhite SPACE */
            > while ((*new_bufp != ' ') && (*new_bufp != '\t') &&
            > (*new_bufp != '\n') && (*new_bufp != '\0'))
            > new_bufp++;
            >
            > /* Skip white SPACE */
            > while ((*new_bufp == ' ') || (*new_bufp == '\t') ||
            > (*new_bufp == '\n') || (*new_bufp == '\0'))[/color]

            a) BUG:-------------------*new_bufp != '\0'

            b) How about using the isspace() macro instead?

            c) I didn't spend much time on this and the code is barely readable, but
            it looks to me that Advance() always skips to the end of bufp (assuming
            that the bug is fixed)?
            [color=blue]
            > new_bufp++;
            >
            > return new_bufp;
            > } /* Advance */[/color]



            Bjørn

            PS: Should the faq contain some kind of formatting rules for code posted
            here, and maybe include a sample indent.pro file or something?

            Comment

            • Charlie Gordon

              #7
              Re: Simple vsscanf source code

              "MarcSmith" <MarcSmith.1g64 ln@mail.codecom ments.com> wrote in message
              news:1101195389 .xlxydDUTPdSA8Y QbaxrQCA@tng...[color=blue]
              >
              > int vsscanf( char *buf, char *format, va_list argp )
              > {[/color]
              .... lots of very badly presented useless code[color=blue]
              > }[/color]

              Wrong prototype, buggy code, useless post : implementing vsscanf in terms of
              sscanf is a joke right ?

              Chqrlie.


              Comment

              • MarcSmith

                #8
                Re: Simple vsscanf source code


                i found a better version at:



                this one uses strtod, strtoi, strtol, instead of sscanf.

                can i use :
                Code
                -------------------

                -------------------
                tags in my post. maybe we should be able to


                -
                MarcSmit
                -----------------------------------------------------------------------
                Posted via http://www.codecomments.co
                -----------------------------------------------------------------------

                Comment

                Working...