473,412 Members | 5,361 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,412 software developers and data experts.

switching on strings in standard C

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

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

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

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

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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: slick_shoes | last post by:
What would be the best way to switch program flow based on a string the user enters? I found out the hard way that char*'s can't be used in a switch statement, so i've resorted to stringing if-else...
3
by: Dennis Wheeler | last post by:
I'm trying to find a commandline solution for switching projects. Currently I have to modify the IIS virtual directory path to the source files, and then open the solution file in .Net to be...
3
by: Robert Dell | last post by:
I have a problem comparing strings in an order form i'm writing. I want to give a running total at the bottom of the page and it appears to be working except it doesn't compare correctly (it...
5
by: JRB | last post by:
Hi, I'm creating a small C#program that communicates through the serial port. I have a separate thread that continuously takes in data from an external device in a while loop. The problem is that...
1
by: TeeCo | last post by:
Hi folks. I'm trying to change the location of the Access mdb file I connect to using OleDb and am having trouble. I'm using Visual C# 2005 and the default values I use for the ConnectionString...
5
by: BBands | last post by:
I'd like to see if a string exists, even approximately, in another. For example if "black" exists in "blakbird" or if "beatles" exists in "beatlemania". The application is to look though a long...
1
by: marcwentink | last post by:
Hi, In my ASP.NET application I and others before me have made to language resource files: strings.resx and strings.en.resx Now I can remember something about location and general settings...
74
by: cman | last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like you can with arrays. If not, why not? If so, how? cman
4
by: CoreyWhite | last post by:
/* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.