switching on strings in standard C

C

Chris Croughton

I would like to get feedback on a "switching on strings" utility:

http://shum.huji.ac.il/~agay/sos

Since it's GPL I won't look at the source (I use the zlib licence which
is free and non-contaminating), but looking at the description it seems
feasible. However, it has some disadvantages:

Not all compilers support long long in switches (indeed, C89 compilers
don't usually support long long at all; gcc -ansi -pedantic gives a
warning).

Only 10 characters (8 if all characters are allowed in the string) are
supported.

You need a separate list of strings (and defined labels) from the
labels in the code.

Since the numbers for the #defines are presumably either
hand calculated or are generated by a program from the input strings, if
the latter I would use a code generator to generate the encoding
function from the source, similar to what is done for gettext, scanning
through the code, so I'd have switches looking something like:

switch (encodeString(str))
{
case RED("red"):
case BLUE("blue"):
case SOMETHING_ELSE("another"):
...
}

The generator would go through looking for appropriate switches, and
would then define macros as found in the case labels:

int encodeString(char *);

#define RED(x) 1
#define BLUE(x) 2
#define SOMETHING_ELSE(x) 3

Those would be output to a header file, and a generator function (using
a "perfect hash" of some kind, or possibly a state table as in lex if I
were after efficiency) output to a C file to be included in the build.
The generator could be quite simplistic in parsing the source, for
instance ignoring anything except cases starting on a new line (with
leading spaces) and any cases without a string argument to the macro.

(In fact now you've given me the idea I might write one...)

Commenting on your example (which isn't GPL), I notice that if it hits
EOF it goes into an infinite loop, since you don't check the return
value of scanf...

(Why do you have a 900 second refresh on every page?)

Chris C
 
M

Mark McIntyre

I would like to get feedback on a "switching on strings" utility:

This would seem to be a library that claims to let you map strings
onto ints so you can switch on them.

It doesn't really do this - it seems to create macros that can be used
in switches eg #define RED 35678LL, and then to provide another macro
which maps an input over to one of the macros at runtime, so you can
pretend to switch on it.

I can't see much need for it myself, and if I needed it, I'd roll my
own, customised to the usage I had.
 
A

agay

Dear Chris Croughton,


Thanks for the excellent comments!

I'm sorry you don't like the GPL. It would be nice to have comments on
the code. Yes I know it's ugly, a major rewrite is planned around V1.0.

Please note that sos have two (actually three) modes. It can generate
CPP definitions or a switch skeleton. I prefer the second mode as it
makes it possible to use case labels which are not names and prevents
collisions with predefined macros, C keywords etc.

I agree with the three disadvantages you identified:

* With a compiler which doesn't support long long
in switches we will get down to five significant
characters and that is less than satisfactory.
Do C99 requires support of long long in switches
or only in calculations? I don't have a copy.

* The limit of 10 significant characters is far
from ideal yet it may prove to be not too bad
in practice.

* Yes, we must feed the sos program with the list
of strings in order to calculate the integers
and I agree this is inconvenient.

I also thought on writing a little pre-processor. The case label
strings could be enclosed in angle brackets to make it easier for the
pre-processor to find and convert them to integers. The switch
statement could be called something different to help the pre-processor
replace it (sos_switch?). I took a quick peek at m4 and didn't like it
so I decided to write one in C but didn't start yet.

This is free software, go ahead! You are invited to use the sos
encoding functions. GNU says the zlib license is compatible with the
GPL.
Commenting on your example (which isn't GPL), I notice that if it hits
EOF it goes into an infinite loop, since you don't check the return
value of scanf...

I tried it with ctrl-d and it just repeated the last input. Could it
loop on some other machine?
(Why do you have a 900 second refresh on every page?)

I endlessly change the docs and wanted readers not to get stuck with a
stale cache copy.

a. agay
 
A

agay

Dear Mark McIntyre,


Yes, you are right, formally this is just pretending
to switch on strings. Using a special pre-processor
(see Chris's post and my reply) could be a bit closer
to the "real thing" - compiler support.

a. agay
 
A

agay

Dear Mark McIntyre,


Yes, you are right, formally this is just pretending to switch on
strings. Using a special pre-processor (see Chris's post and my reply)
could be a bit closer to the "real thing" - compiler support.

a. agay
 
K

Keith Thompson

Dear Mark McIntyre,

Yes, you are right, formally this is just pretending
to switch on strings. Using a special pre-processor
(see Chris's post and my reply) could be a bit closer
to the "real thing" - compiler support.

Please provide some context and proper attributions when posting a
followup.

For the Nth time, where N is a *very* large number,

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
A

agay

Dear Keith Thompson,

Please provide some context and proper attributions when posting a
followup.

For the Nth time, where N is a *very* large number,

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Thanks for the important tips!

The sos docs and code are now in a more or less coherent state
thanks to the wonderful comments I got.

You are invited to visit:

http://shum.huji.ac.il/~agay/sos

Comments would be greatly appreciated.

a. agay
 
K

Keith Thompson

Dear Keith Thompson,



Thanks for the important tips!

Ok, let's try this again.

I just tried posting a followup to my previous article using
groups.google.com and the above technique. (I didn't actually post
the followup.) It created a text box starting with the line

"Keith Thompson wrote:"

That's the attribution line. Just leave it alone. If you're going to
quote something from another article, the attribution line should go
along with the quotation.

In the above, you addressed your followup to me, but you still didn't
directly indicate that I wrote the material you quoted.

Any decent newsreader will do this for you. Let it. (The
groups.google.com interface just barely qualifies as a decent
newsreader *if* you use it properly.)
 
A

agay

Keith said:
Any decent newsreader will do this for you. Let it. (The
groups.google.com interface just barely qualifies as a decent
newsreader *if* you use it properly.)

It took me some weeks but at last I understood your post.
You see, I didn't use a newsreader in years and it was not
obvious that people using one wouldn't be able to see the
whole thread at once but only one post at a time.

I guess that's the rationale for the conventions you have
been advocating.

Thanks

a.agay
 
K

Keith Thompson

It took me some weeks but at last I understood your post.
You see, I didn't use a newsreader in years and it was not
obvious that people using one wouldn't be able to see the
whole thread at once but only one post at a time.

I guess that's the rationale for the conventions you have
been advocating.

Thanks for the feedback. I think I'll put together a slightly
expanded version of the Google warning I've been posting (the one that
CBFalconer has been using as his signature).

The proper solution, of course, would be for Google to fix their
interface, but apparently their "Don't be evil" motto is just
decorative.
 
K

Kenny McCormack

Thanks for the feedback. I think I'll put together a slightly
expanded version of the Google warning I've been posting (the one that
CBFalconer has been using as his signature).

Well, I could be wrong, but I think it was implying that all the rest of us
need to join the 21st Century.
The proper solution, of course, would be for Google to fix their
interface, but apparently their "Don't be evil" motto is just
decorative.

Compared to MS, they're not.

But that's about all you can say.
 
J

James Dow Allen

Keith said:
The proper solution, of course, would be for Google to fix their
interface, but apparently their "Don't be evil" motto is just
decorative.

Not exactly. It's a "bait and switch." They did have
a benevolent interface at first, but they've built up
goodwill and now want to spend it in the cash-cow phase
of their business plan. Welcome to post-literate
economics.

BTW, will those who post early in a thread (or messages
11, 21, 31, etc.) *please* use very short lines.
Otherwise Google's interface will obliterate the last
part of each line, overwriting it with paid ads.

My excuse is I post only from "cafes." Happy to hear of
any good non-Google alternative for posting on Usenet.

James
 
A

Antonio Contreras

James said:
Not exactly. It's a "bait and switch." They did have
a benevolent interface at first, but they've built up
goodwill and now want to spend it in the cash-cow phase
of their business plan. Welcome to post-literate
economics.

BTW, will those who post early in a thread (or messages
11, 21, 31, etc.) *please* use very short lines.
Otherwise Google's interface will obliterate the last
part of each line, overwriting it with paid ads.

Nice idea, but except the initial post and the first chain of replies
there's no way to know which position your message will end in, as
messages can be added on top of yours (when someone replies to a post
above you). Not to mention that you can browse the thread sorted by
replies ("view as a tree") or sorted by date. Then again, sometimes,
when certain words appear in the thread, the paid ads can get very,
very long, so your recomendation should also be applied to the post
that make 2, 12, 22... 3, 13, 23... just in case.

Anyway, I use google groups and I've never had the end of a line
"obliterated by paid ads".
My excuse is I post only from "cafes." Happy to hear of
any good non-Google alternative for posting on Usenet.

My excuse is that I post while at work (mainly) and I don't have
administration rigths in the PC I use, and I'm not about to go to the
SysAdmin and tell him that I need a newsreader installed...
 
C

Christopher Benson-Manica

Antonio Contreras said:
My excuse is that I post while at work (mainly) and I don't have
administration rigths in the PC I use, and I'm not about to go to the
SysAdmin and tell him that I need a newsreader installed...

If you have ssh and aren't afraid to use it, there is at least one
Unix system (the one I'm posting from, SDF) with Usenet access. It
costs $1 for a lifetime membership, although the base functionality is
limited. You might check it out.
 
A

Antonio Contreras

Christopher said:
If you have ssh and aren't afraid to use it, there is at least one
Unix system (the one I'm posting from, SDF) with Usenet access. It
costs $1 for a lifetime membership, although the base functionality is
limited. You might check it out.

Thanks for the advice. I have accounts in two linux machines and in one
Solaris workstation in the university I studied in, so I guess I could
post from them... (I connect to them using putty) But sinceresly, I
would have to learn the interface, and google groups may not be that
good, but it isn't that bad either, and its ability to search the
groups is priceless.
 
A

Alan Balmer

My excuse is I post only from "cafes." Happy to hear of
any good non-Google alternative for posting on Usenet.

There are several zero or low-cost news servers available. I use
http://news.individual.net/ which offers excellent service for 10
EUR/year.

In conjunction with that, you can use a portable newsreader. Portable
Thunderbird works pretty well. I use Agent, but I'm not sure it can be
made portable.
 

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

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top