423,867 Members | 3,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,867 IT Pros & Developers. It's quick & easy.

switching on strings in standard C

P: n/a
Hi,

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

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

a. agay

Nov 15 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On 12 Jul 2005 07:52:14 -0700, ag**@vms.huji.ac.il
<ag**@vms.huji.ac.il> wrote:
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
Nov 15 '05 #2

P: n/a
On 12 Jul 2005 07:52:14 -0700, in comp.lang.c , ag**@vms.huji.ac.il
wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #3

P: n/a
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

Nov 15 '05 #4

P: n/a

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

Nov 15 '05 #5

P: n/a

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

Nov 15 '05 #6

P: n/a
ag**@vms.huji.ac.il writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #7

P: n/a

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

Nov 15 '05 #8

P: n/a
ag**@vms.huji.ac.il writes:
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!


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9

P: n/a

Keith Thompson wrote:
ag**@vms.huji.ac.il writes:
Dear Keith Thompson,

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

Nov 15 '05 #10

P: n/a
ag**@vms.huji.ac.il writes:
Keith Thompson wrote:
ag**@vms.huji.ac.il writes:
> Dear Keith Thompson,
>

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #11

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
ag**@vms.huji.ac.il writes:
Keith Thompson wrote:
ag**@vms.huji.ac.il writes:
> Dear Keith Thompson,
>
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 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.

Nov 15 '05 #12

P: n/a

Keith Thompson wrote:
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

Nov 15 '05 #13

P: n/a
James Dow Allen wrote:
Keith Thompson wrote:
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.


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

Nov 15 '05 #14

P: n/a
Antonio Contreras <an*****@gmail.com> wrote:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #15

P: n/a
Christopher Benson-Manica wrote:
Antonio Contreras <an*****@gmail.com> wrote:
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.


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.

Nov 15 '05 #16

P: n/a
On 7 Aug 2005 22:40:27 -0700, "James Dow Allen"
<jd*********@yahoo.com> wrote:

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.
--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.