473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #1
7 6630
"Ray Mitchell" <Ra*********@MeanOldTeacher.com> wrote in message
news:cl****************@plethora.net...
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.


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.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #2
[F'up2 cut down --- should have been done by OP!]

In comp.lang.c.moderated Ray Mitchell <Ra*********@meanoldteacher.com> wrote:
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.
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.
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.


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 (br*****@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #3
Ray Mitchell wrote:
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.


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: jl******@earthlink.net, jl******@us.ibm.com
Guardian of DBD::Informix v2003.04 -- http://dbi.perl.org/
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Nov 14 '05 #4

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(argp, int*),&n);
bufp+=n;
}
}
else if (*fmtp == 'f')
{ if (suppress)
bufp = Advance(bufp);
else if (mytype == LONG)
{ k+=sscanf(bufp,"%f%n",va_arg(argp, double*),&n);
bufp+=n;
}
else
{ k+=sscanf(bufp,"%f%n",va_arg(argp, float*),&n);
bufp+=n;
}
}
else if (*fmtp == 's')
{ if (suppress)
bufp = Advance(bufp);
else {
k+=sscanf(bufp,"%s%n",va_arg(argp, char*),&n);
bufp+=n;
}
}
else if (*fmtp == 'c')
{ if (!suppress)
{ k+=sscanf(bufp,"%c%n",va_arg(argp, 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
-----------------------------------------------------------------------

Nov 14 '05 #5
MarcSmith wrote:
[snip]
int vsscanf( char *buf, char *format, va_list argp )
const char* buf, const char* format, if you want to clone vsscanf().
{
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(argp, int*),&n);
bufp+=n;
}
}
else if (*fmtp == 'f')
{ if (suppress)
bufp = Advance(bufp);
else if (mytype == LONG)
{ k+=sscanf(bufp,"%f%n",va_arg(argp, double*),&n);
bufp+=n;
}
else
{ k+=sscanf(bufp,"%f%n",va_arg(argp, float*),&n);
bufp+=n;
}
}
else if (*fmtp == 's')
{ if (suppress)
bufp = Advance(bufp);
else {
k+=sscanf(bufp,"%s%n",va_arg(argp, char*),&n);
bufp+=n;
}
}
else if (*fmtp == 'c')
{ if (!suppress)
{ k+=sscanf(bufp,"%c%n",va_arg(argp, 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'))
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)?
new_bufp++;

return new_bufp;
} /* Advance */


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?
Nov 14 '05 #6
"MarcSmith" <Ma**************@mail.codecomments.com> wrote in message
news:1101195389.xlxydDUTPdSA8YQbaxrQCA@tng...

int vsscanf( char *buf, char *format, va_list argp )
{ .... lots of very badly presented useless code }


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

Chqrlie.
Nov 14 '05 #7

i found a better version at:

www.italios.it/os2/vsscanf_8c-source.html

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

Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jacek Generowicz | last post by:
:::Title::: A simple text markup utility :::/Title::: :::Section Introduction ::: I'm looking for something to help developers wirte documentation for bits of software they are writing. The...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
2
by: RoSsIaCrIiLoIA | last post by:
Is it good the use of va_arg? ____________________ #include <stdio.h> #include <stdlib.h> #include <stdarg.h> #include <errno.h> unsigned gcd_my(unsigned a, unsigned b) {unsigned t;
2
by: Ray Mitchell | last post by:
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...
4
by: smshahriar | last post by:
Hi, I want to scan from the following string all the hex numbers and populate an array of integers: 0x27 0x00 0x30 0x00 0x33 0x00 0x36 0x00
3
by: Elliot Rodriguez | last post by:
Hi: I am writing a WinForm app that contains a DataGrid control and a StatusBar control. My goal is to update the status bar using events from a separate class, as well as some other simple...
15
by: gjoneshtfc | last post by:
Hello, I have a simple problem that I just cannot get my head around! I currently have the following line in my ASP recordset: Recordset1.Source = "SELECT * FROM MainTable ORDER BY Price ASC"...
5
by: Joel | last post by:
In the course of my project, I must include some custom logic and would like to integrate a small script language in my application for that purpose. In C++, I used the LUA script language and I...
4
by: Gaijinco | last post by:
I have always used istringstream for inputting data from a string. But I heard someone talking about vsscanf() However from what I can find in the internet is not quite clear if is standard or...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.