473,583 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3972
On 12 Jul 2005 07:52:14 -0700, ag**@vms.huji.a c.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(s tr))
{
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(ch ar *);

#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.a c.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.a c.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.c om, 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_Keit h) 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.c om, 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.a c.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.c om, 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.c om 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.c om interface just barely qualifies as a decent
newsreader *if* you use it properly.)

--
Keith Thompson (The_Other_Keit h) 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.a c.il writes:
Dear Keith Thompson,

Any decent newsreader will do this for you. Let it. (The
groups.google.c om 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
37327
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 statements together. Is there a more efficient way to do this? Thanks slick_shoes
3
2476
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 prompted with the "Web Access Failed" dialog, where I can then modify the path to match my IIS settings. Is there a registry setting or some other...
3
2024
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 always adds things up when you didn't select them) so i only added 2 of the items until I got it worked out. here's the page source (you can look at it...
5
2030
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 when I try and run another program, open a menu, or anything, I miss getting bytes. If I set the threads priority to above normal it works fine, but...
1
1926
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 are those generated by the Server Explorer. I can connect just fine using the default values. But when I comment out strDefaultConnectionFile and...
5
2221
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 list of songs and return any approximate matches along with a confidence factor. I have looked at edit distance, but that isn't a good choice for...
1
2164
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 and that it is possible to switch from one region/language to another, but
74
3100
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
455
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 COMPACT COHERENT CODE THAT WORKS WITH STRINGS, IT ALL BEGINS TO MAKE SINCE*/ /* The basics of C++ are Classes, that build Types. Which are used to...
0
7893
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7928
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8188
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5695
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3813
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2326
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1422
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.