C-FAQ 19.7

D

Default User

CBFalconer said:
Default User wrote:

At most that requires slight expansion of 19.6


Ah. I'd missed that when I went over it, as there wasn't a question
with strtok() in the subject and the search feature didn't turn up one
either. If I were a newbie looking for help on strtok() I suspect I'd
have similar difficulty.

Perhaps then an index for each function mentioned within the FAQ would
help.



Brian
 
C

CBFalconer

Default said:
Ah. I'd missed that when I went over it, as there wasn't a question
with strtok() in the subject and the search feature didn't turn up
one either. If I were a newbie looking for help on strtok() I
suspect I'd have similar difficulty.

Perhaps then an index for each function mentioned within the FAQ
would help.

As has been pointed out, that should have read 13.6. All this
points out the advantage of text format (available). I simply
access that with less and search for whatever is of interest,
including possible section.question numbers. The result is instant
gratification. I can use the identical mechanism on N869.txt, a
suitable formatted version of which is available on my site (bz2
compressed for quick download).
 
D

dcorbit

Default User wrote:
[snip]
Besides questions related to the newsgroup itself, the most glaring
omission in the FAQ that comes readily to mind is any discussion of
strtok(). It needs: "How do use this crazy thing?"; "Why not use it?";
and "What can be used instead?" at the least.

Here is a trivially simple replacement that I use from time to time:

#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <ctype.h>

/* The default delimiters are chosen as some ordinary white space
characters: */
static const char default_delimiters[] = {' ', '\n', '\t', '\r', '\f',
0};

/*
* The tokenize() function is similar to a reentrant version of
strtok().
* It parses tokens from 'string', where tokens are substrings
separated by characters from 'delimiter_list'.
* To get the first token from 'string', tokenize() is called with
'string' as its first parameter.
* Remaining tokens from 'string' are obtained by calling tokenize()
with NULL for the first parameter.
* The string of delimiters, identified by 'delimiter_list', can change
from call to call.
* If the string of delimiters is NULL, then the standard list
'default_delimiters' (see above) is used.
* tokenize() modifies the memory pointed to by 'string', because it
writes null characters into the buffer.
*/
char *tokenize(char *string, const char *delimiter_list, char
**placeholder)
{
if (delimiter_list == NULL)
delimiter_list = default_delimiters;

if (delimiter_list[0] == 0)
delimiter_list = default_delimiters;

if (string == NULL)
string = *placeholder;

if (string == NULL)
return NULL;
/*
* The strspn() function computes the length of the initial segment of
the first string
* that consists entirely of characters contained in the second string.
*/
string += strspn(string, delimiter_list);
if (!string[0]) {
*placeholder = string;
return NULL;
} else {
char *token;
token = string;
/*
* The strpbrk() function finds the first occurrence of any character
contained in the second string
* found in the first string.
*/
string = strpbrk(token, delimiter_list);
if (string == NULL)
*placeholder = token + strlen(token);
else {
*string++ = 0;
*placeholder = string;
}
return token;
}
}

#ifdef UNIT_TEST
char test_string0[] = "This is a test. This is only a test.
If it were an actual emergency, you would be dead.";
char test_string1[] = "This is a also a test. This is only
a test. If it were an actual emergency, you would be dead. 12345";
char test_string2[] = "The quick brown fox jumped over the
lazy dog's back 1234567890 times.";
char test_string3[] = " \t\r\n\fThe quick brown fox jumped
over the lazy dog's back 1234567890 times.";
char test_string4[] = "This is a test. This is only a test.
If it were an actual emergency, you would be dead.";
char test_string5[] = "This is a also a test. This is only
a test. If it were an actual emergency, you would be dead. 12345";
char test_string6[] = "The quick brown fox jumped over the
lazy dog's back 1234567890 times.";
char test_string7[] = " \t\r\n\fThe quick brown fox jumped
over the lazy dog's back 1234567890 times.";

#include <stdio.h>

char whitespace[UCHAR_MAX + 1];

/* This test will create token separators as any whitespace or any
punctuation marks: */
void init_whitespace()
{
int i;
int index = 0;
for (i = 0; i < UCHAR_MAX; i++) {
if (isspace(i)) {
whitespace[index++] = (char) i;
}
if (ispunct(i)) {
whitespace[index++] = (char) i;
}
}
}

/*
TNX Gerd.
*/
void spin_test(char *test_string, char *white)
{
char *p = NULL;
char *token;
token = tokenize(test_string, white, &p);
while (token) {
puts(token);
token = tokenize(NULL, white, &p);
}
}

int main(void)
{
init_whitespace();
puts("Whitespace is whitespace+punctuation");
spin_test(test_string0, whitespace);
spin_test(test_string1, whitespace);
spin_test(test_string2, whitespace);
spin_test(test_string3, whitespace);
puts("Whitespace is simple whitespace");
spin_test(test_string4, NULL);
spin_test(test_string5, NULL);
spin_test(test_string6, NULL);
spin_test(test_string7, NULL);
return 0;
}
#endif
 
C

CBFalconer

Default User wrote:
[snip]
Besides questions related to the newsgroup itself, the most
glaring omission in the FAQ that comes readily to mind is any
discussion of strtok(). It needs: "How do use this crazy thing?";
"Why not use it?"; and "What can be used instead?" at the least.

Here is a trivially simple replacement that I use from time to
time:
.... snip code ...

and here is one I use:

/* ------- file toksplit.h ----------*/
#ifndef H_toksplit_h
# define H_toksplit_h

# ifdef __cplusplus
extern "C" {
# endif

#include <stddef.h>

/* copy over the next token from an input string, after
skipping leading blanks (or other whitespace?). The
token is terminated by the first appearance of tokchar,
or by the end of the source string.

The caller must supply sufficient space in token to
receive any token, Otherwise tokens will be truncated.

Returns: a pointer past the terminating tokchar.

This will happily return an infinity of empty tokens if
called with src pointing to the end of a string. Tokens
will never include a copy of tokchar.

released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
*/

const char *toksplit(const char *src, /* Source of tokens */
char tokchar, /* token delimiting char */
char *token, /* receiver of parsed token */
size_t lgh); /* length token can receive */
/* not including final '\0' */

# ifdef __cplusplus
}
# endif
#endif
/* ------- end file toksplit.h ----------*/

/* ------- file toksplit.c ----------*/
#include "toksplit.h"

/* copy over the next token from an input string, after
skipping leading blanks (or other whitespace?). The
token is terminated by the first appearance of tokchar,
or by the end of the source string.

The caller must supply sufficient space in token to
receive any token, Otherwise tokens will be truncated.

Returns: a pointer past the terminating tokchar.

This will happily return an infinity of empty tokens if
called with src pointing to the end of a string. Tokens
will never include a copy of tokchar.

A better name would be "strtkn", except that is reserved
for the system namespace. Change to that at your risk.

released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
Revised 2006-06-13
*/

const char *toksplit(const char *src, /* Source of tokens */
char tokchar, /* token delimiting char */
char *token, /* receiver of parsed token */
size_t lgh) /* length token can receive */
/* not including final '\0' */
{
if (src) {
while (' ' == *src) src++;

while (*src && (tokchar != *src)) {
if (lgh) {
*token++ = *src;
--lgh;
}
src++;
}
if (*src && (tokchar == *src)) src++;
}
*token = '\0';
return src;
} /* toksplit */

#ifdef TESTING
#include <stdio.h>

#define ABRsize 6 /* length of acceptable token abbreviations */

/* ---------------- */

static void showtoken(int i, char *tok)
{
putchar(i + '1'); putchar(':');
puts(tok);
} /* showtoken */

/* ---------------- */

int main(void)
{
char teststring[] = "This is a test, ,, abbrev, more";

const char *t, *s = teststring;
int i;
char token[ABRsize + 1];

puts(teststring);
t = s;
for (i = 0; i < 4; i++) {
t = toksplit(t, ',', token, ABRsize);
showtoken(i, token);
}

puts("\nHow to detect 'no more tokens' while truncating");
t = s; i = 0;
while (*t) {
t = toksplit(t, ',', token, 3);
showtoken(i, token);
i++;
}

puts("\nUsing blanks as token delimiters");
t = s; i = 0;
while (*t) {
t = toksplit(t, ' ', token, ABRsize);
showtoken(i, token);
i++;
}
return 0;
} /* main */

#endif
/* ------- end file toksplit.c ----------*/
 
D

Dik T. Winter

>
> The Message-ID for your message is <[email protected]>, and
> will be forever. Anybody can look your message up in a good or even a
> halfway usable Usenet archive, using nothing but that Message-ID.

Not entirely true. It can be deleted from the archives, or even not make
it to the archives (and both do occur).
>
> It used to be. And even if the larger complete version would have to be
> split over two articles, that's two Message-IDs. Not something to break
> your back over, I'd say.

That's two Message-ID's for each time it is posted. And as in such
reposts a "Supercedes" header is in most cases is put in, it erases the
at the same time the old Message-ID's.
 
K

Keith Thompson

Dik T. Winter said:
Not entirely true. It can be deleted from the archives, or even not make
it to the archives (and both do occur).
[...]

Right.

I like having the FAQ on a web site. The current FAQ happens to be on
a web site. Message-IDs change every time the Faq is posted, and can
be difficult or impossible to remember; the FAQ's URL has not changed
in some time, and is easy to remember. And if Message-IDs were so
much more convenient than URLs, people would use Message-IDs when they
cite the FAQ; I don't recall ever seeing anyone do so.
 
D

Default User

CBFalconer said:
As has been pointed out, that should have read 13.6.

I saw that, and that wasn't what I was commenting about. There's no
question about strtok() itself, nor does it come up when using that
search feature on the page.

All this
points out the advantage of text format (available).

Or have an option for all questions and answers to be presented at
once. As it is, one can get all questions displayed, but if the desired
information doesn't actually appear in the question itself, that
doesn't help.
I simply
access that with less and search for whatever is of interest,
including possible section.question numbers. The result is instant
gratification. I can use the identical mechanism on N869.txt, a
suitable formatted version of which is available on my site (bz2
compressed for quick download).

On the whole, I'd rather direct people to the web site.



Brian
 
D

Default User

Default User wrote:
[snip]
Besides questions related to the newsgroup itself, the most glaring
omission in the FAQ that comes readily to mind is any discussion of
strtok(). It needs: "How do use this crazy thing?"; "Why not use
it?"; and "What can be used instead?" at the least.

Here is a trivially simple replacement that I use from time to time:

As this doesn't appear in the FAQ, it's not really helpful. I didn't
mention it because I'm unable to formulate my own solution, but because
it's a fairly frequently asked question here. Or rather, "how come this
strtok() thing doesn't work?" is.




Brian
 
R

Richard Bos

The latter, yes, but the FAQ is posted monthly. It would be unusual for
none of the copies to arrive at a given archive. The former, that should
not happen unless Steve puts an X-no-archive header in the FAQ, which
would surprise me.
Right.

I like having the FAQ on a web site.

So do I; but I much prefer having it on the newsgroup _as well_. In
fact, given the choice, I'd prefer having it on the group alone to
having just the website; but obviously having both is the superior
option by far.

Richard
 
K

Keith Thompson

So do I; but I much prefer having it on the newsgroup _as well_. In
fact, given the choice, I'd prefer having it on the group alone to
having just the website; but obviously having both is the superior
option by far.

Hypothetically, if the FAQ were just on the web site and not posted to
the newsgroup, we could still direct people to it with a URL and a
question number. If it were on the newsgroup but not on a web site,
the newbies who don't bother checking the FAQ before posting most
likely would not have the patience to track down the FAQ.

But yes, it's nice to have it in both places.
 
S

Steve Summit

Sorry, all, I only just now encountered this thread. (Those
harboring dark suspicions that the FAQ list maintainer does not,
in fact, monitor the group regularly any more, and that when he
does it's usually by kibozing, are all too accurate.)

A few more points and clarifications beyond those already offered
by others:


Indeed. Thanks for your contribution, Jacob. I agree that the
various MS-DOS references are getting sort of hoary-looking.

Richard "the mind reader" Heathfield has echoed my thoughts exactly.
(The careful reader will already have noticed that there *are*
some VAX/VMS details mentioned in 19.1...)

I wrote virtually all of the words in it, with the exception
of some by acknowledged friends who offered suggestions with the
understanding that I'd use them, and three or so verbatim quotes
(such as the one in 11.35) which are both attributed and used
with permission.

In particular, the wording of all the questions is also mine,
mostly in order to keep them succinct, although this has had the
side benefit that I get to duck criticisms that I've set someone
up for ridicule or violated their copyright (and yes, this did
happen, once.)

Why do I have the right to publish it as a book for a profit?
What does comp.lang.c get out of that deal? Was this a
barefaced, soul-destroying, community-robbing sell-out on my part?
Believe me, those questions (that is, the possibility that they
might one day be asked) haunted me as I was consummating the
Addison-Wesley deal.

I have the right to publish it because all the words are mine
and, to be blunt, I put a hell of a lot of work into them.
But comp.lang.c got a lot out of the deal as well. I insisted
on this, and Addison-Wesley was glad to acquiesce.

The HTML conversion came out of the book effort; I might well
never have gotten around to that conversion otherwise. The FAQ
list grew in size by a factor of about five during the book
publication effort, and a lot of that work "cross pollinated" the
free, posted version as well; the posted version grew by a factor
of about two as a result. (And there's been more, book-related
growth since then.)

I spent about half of 1995 working pretty much full-time on the
FAQ list. That amount of effort would never have been possible
without the book deal. But I decided that the newsgroup was
getting enough out of it that the "sell-out" was worth it.

They have just as much say as they ever did, which is: a lot.
Anybody who sends in suggestions gets all due consideration,
and credit if the suggestion is used. (I had only one big fight
with my editor as the book was going to print: she felt that the
gargantuan list of acknowledgements in the preface was unwieldy,
and she was right, but I insisted on keeping it, because I wanted
to give everyone credit who had contributed anything at all.)

jacob navia wrote again:
Did you? It doesn't look like I ever received your mail.
(But yes, these days I'm unfortunately more likely to see and
respond to emailed suggestions than newsgroup postings.)


Which I'm paying for, by the way (DNS registration and hosting)
out of my own pocket. (But I'm not asking for sympathy or
donations or anything; it *is* my gift, freely given, and anyway
there's all those rapacious book royalties to help offset the
hosting costs. :) )

We're still trying to decide whether to use Jim Carrey or Jack
Nicholson as the Null Pointer. Maddeningly, Gwyneth Paltrow
will not return my calls -- I had hoped to give her a shot at
the Struct Hack, but time is running out, so Carrie-Anne Moss
(who was everybody else's choice anyway) may get the role instead.

I think I composed the very first draft (it was all of seven or
eight questions long) in late 1988. But due to a conflict (see
below) I didn't end up posting anything until a year and a half
later. Eventually I guess I decided that retaining the 1988 date
was silly, so I dropped it, since nothing got seen publicly then.

My goodness. I would never have dreamed of trying to put my name
on it like that. I did write it, but it's not mine; it owes
hugely to comp.lang.c. All the questions were inspired by actual
comp.lang.c questions; comp.lang.c is the filter which lets us
find out what the beginners are *really* having trouble with,
regardless of how the experts think the topics should be
explained, and despite the best efforts of all the textbook
authors and course instructors out there. As the introduction
says, the FAQ List "still retains the flavor of a good
comp.lang.c discussion".

I've identified two friends who will decide what to do with the
FAQ list if that happens. (And I've even mentioned this to one
of them.)

I don't remember a database like that with Chris's name on it.
(Chris has since posted his recollections.) There was an effort,
announced by Raymond Chen (then at UCB) in 1989, to collect
user-contributed Q/A pairs which he was going to collate and post.
This happened after I'd started writing my nascent list but
before I'd, like, actually told anybody about it or anything.
I was sort of disappointed, but then in 1990 when Ray posted
that he hadn't gotten many contributions and was abandoning the
effort, I could go ahead and present my work after all.

I got the job the same way any Usenet FAQ list maintainer gets
the job: I just started doing it one day.


Actually, the folks at Addison-Wesley have already had their say:
the book contract (inked all the way back in 1994) gives me the
explicit right to cross-pollinate the posted-to-Usenet list with
the underwritten-by-Addision-Wesley improvements, and even to
post the entire text of the book to the web. (But it took me
another -- gulp -- ten years to actually exercise my rights on
that point.)

I love wikis; a wikified version of the FAQ list is a wonderful
idea. The clc-wiki folks and I never managed to complete that
effort (this was mostly my fault, and for no reason other than
that I sort of dropped the ball), and I gather from this thread
that the clc-wiki has run dry, but it's still a good idea.

Richard said:
STM a Usenet article is easier to search than a website.

De gustibus non disputandum est.
Chacun à son goût.
Different strokes for different folks.
That's why there is, and will always be, both an HTML and
a flat-ASCII version. :)
 
J

jacob navia

Hi steve

Nice to read from you.

I am sorry. I wanted to write you to inform you from this
discussion, then I got engaged in other stuff and I couldn't
write you. It is nice that you found this thread.

Count on me for any help writing this part of the FAQ.

jacob
 
M

Mark Brader

Is there a legitimate reason why, if he were to disappear or stop
Just incidentally, that happened almost literally over at alt.usage.english.
Mark Israel wrote an excellent FAQ posting for that newsgroup and retained
copyright himself, but after his last update in 1997, he drifted away from
involvement with the newsgroup. And then in 2004 he was hit, not by a bus,
but by a pickup truck, and suffered brain damage that left him with aphasia.

Today the newsgroup has an associated web site (at alt-usage-english.org)
and things that Mark's FAQ list didn't cover are now posted there as
separate "FAQ supplement" items. It's an awkward way of doing things,
but the alternative was to start over from scratch.
I've identified two friends who will decide what to do with the
FAQ list if that happens. (And I've even mentioned this to one
of them.)

Good to hear.
--
Mark Brader "Clearly, neither Mark Brader nor
Toronto Steve Summit read the whole book..."
(e-mail address removed) -- Greg Black

My text in this article is in the public domain.
 
E

Eric Sosman

Steve said:
> [...]
Eric said:
[...] (Last I
heard, the print version was an Oprah selection and movie rights
were being negotiated; rumor has it that Undefined Behavior will
be played by Johnny Depp.)

We're still trying to decide whether to use Jim Carrey or Jack
Nicholson as the Null Pointer. Maddeningly, Gwyneth Paltrow
will not return my calls -- I had hoped to give her a shot at
the Struct Hack, but time is running out, so Carrie-Anne Moss
(who was everybody else's choice anyway) may get the role instead.

May I put in a vote for Angelina Jolie as Padding Bits?
 
O

Old Wolf

jacob said:
This is what is there now (2006, 20 Dec)

How can I do serial (``comm'') port I/O?

It's system-dependent. Under Unix, you typically open, read, and write a
device file in /dev, and use the facilities of the terminal driver to
adjust its characteristics. (See also questions 19.1 and 19.2.) Under
MS-DOS, you can use the predefined stream stdaux, or a special file like
COM1, or some primitive BIOS interrupts, or (if you require decent
performance) any number of interrupt-driven serial I/O packages. Several
netters recommend the book C Programmer's Guide to Serial
Communications, by Joe Campbell.

Reading/writing special files starting "COM" "LPT" "AUX" etc.
still works in Windows XP. (Many people consider this stupid,
but it is supported nonetheless). Some versions of Windows 98
even crashed if you tried to access a path like C:\com\com
using Windows Explorer.
 
R

Random832

2007-01-06 said:

I did make a substantial effort to avoid being accusatory, since most of
the "problems" are really only problems in principle (with the
"principle" mainly, but not entirely, being "what if this were someone
else we didn't trust")
My goodness. I would never have dreamed of trying to put my name
on it like that. I did write it, but it's not mine; it owes
hugely to comp.lang.c. All the questions were inspired by actual
comp.lang.c questions; comp.lang.c is the filter which lets us
find out what the beginners are *really* having trouble with,
regardless of how the experts think the topics should be
explained, and despite the best efforts of all the textbook
authors and course instructors out there. As the introduction
says, the FAQ List "still retains the flavor of a good
comp.lang.c discussion".

I guess I think that a FAQ should be community-maintained - one reason
is, as you've mentioned, the fact that you don't regularly read the
group anymore.
I got the job the same way any Usenet FAQ list maintainer gets
the job: I just started doing it one day.

Fair enough.
Actually, the folks at Addison-Wesley have already had their say:
the book contract (inked all the way back in 1994) gives me the
explicit right to cross-pollinate the posted-to-Usenet list with
the underwritten-by-Addision-Wesley improvements, and even to
post the entire text of the book to the web. (But it took me
another -- gulp -- ten years to actually exercise my rights on
that point.)

So what rights do _they_ have, then? Exclusive right to publish it in
print form only, or can they prevent you from sublicensing the material
to allow others to modify/distribute it? Though, I guess the fact that
they agreed to such a contract means either A: they'd be cool with it
anyway if you asked or B: it was 1994, they didn't know how big this
"internets" thing would become
I love wikis; a wikified version of the FAQ list is a wonderful
idea. The clc-wiki folks and I never managed to complete that
effort (this was mostly my fault, and for no reason other than
that I sort of dropped the ball), and I gather from this thread
that the clc-wiki has run dry, but it's still a good idea.

It is a wiki. You could, you know, just start posting it. Or declare
that others can. You could say right now "feel free to take anything
from the FAQ and incorporate it into the wiki", with attribution if you
want it to be, specify a license, etc.
 
R

Random832

2007-01-08 said:
Reading/writing special files starting "COM" "LPT" "AUX" etc.
still works in Windows XP. (Many people consider this stupid,
but it is supported nonetheless). Some versions of Windows 98
even crashed if you tried to access a path like C:\com\com
using Windows Explorer.

Didn't some versions of XP?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top