Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > switching on strings in standard C

Reply
Thread Tools

switching on strings in standard C

 
 
agay@vms.huji.ac.il
Guest
Posts: n/a
 
      07-12-2005
Hi,

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

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


Thanks

a. agay

 
Reply With Quote
 
 
 
 
Chris Croughton
Guest
Posts: n/a
 
      07-12-2005
On 12 Jul 2005 07:52:14 -0700,
<> 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
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      07-12-2005
On 12 Jul 2005 07:52:14 -0700, in comp.lang.c ,
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 =----
 
Reply With Quote
 
agay@vms.huji.ac.il
Guest
Posts: n/a
 
      07-13-2005
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

 
Reply With Quote
 
agay@vms.huji.ac.il
Guest
Posts: n/a
 
      07-13-2005

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

 
Reply With Quote
 
agay@vms.huji.ac.il
Guest
Posts: n/a
 
      07-13-2005

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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-13-2005
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) kst- <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.
 
Reply With Quote
 
agay@vms.huji.ac.il
Guest
Posts: n/a
 
      07-19-2005

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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-19-2005
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) kst- <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.
 
Reply With Quote
 
agay@vms.huji.ac.il
Guest
Posts: n/a
 
      08-07-2005

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Process Switching vs. Fast/CEF Switching? asdf Cisco 7 05-29-2007 05:26 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
add pexpect to the standard library, standard "install" mechanism. funkyj Python 5 01-20-2006 08:35 PM
How standard is the standard library? steve.leach Python 1 04-18-2005 04:07 PM
Switching on strings... slick_shoes C++ 3 07-21-2003 05:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57