switching on strings in standard C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • agay@vms.huji.ac.il

    switching on strings in standard C

    Hi,

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




    Thanks

    a. agay

  • Chris Croughton

    #2
    Re: switching on strings in standard C

    On 12 Jul 2005 07:52:14 -0700, agay@vms.huji.a c.il
    <agay@vms.huji. ac.il> wrote:
    [color=blue]
    > I would like to get feedback on a "switching on strings" utility:
    >
    > http://shum.huji.ac.il/~agay/sos[/color]

    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

    Comment

    • Mark McIntyre

      #3
      Re: switching on strings in standard C

      On 12 Jul 2005 07:52:14 -0700, in comp.lang.c , agay@vms.huji.a c.il
      wrote:
      [color=blue]
      >I would like to get feedback on a "switching on strings" utility:[/color]

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

      Comment

      • agay@vms.huji.ac.il

        #4
        Re: switching on strings in standard C

        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.
        [color=blue]
        > 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...[/color]

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

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

        a. agay

        Comment

        • agay@vms.huji.ac.il

          #5
          Re: switching on strings in standard C


          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

          Comment

          • agay@vms.huji.ac.il

            #6
            Re: switching on strings in standard C


            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

            Comment

            • Keith Thompson

              #7
              Re: switching on strings in standard C

              agay@vms.huji.a c.il writes:[color=blue]
              > 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.[/color]

              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) kst-u@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.

              Comment

              • agay@vms.huji.ac.il

                #8
                Re: switching on strings in standard C


                Dear Keith Thompson,

                [color=blue]
                > 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.[/color]

                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:



                Comments would be greatly appreciated.

                a. agay

                Comment

                • Keith Thompson

                  #9
                  Re: switching on strings in standard C

                  agay@vms.huji.a c.il writes:[color=blue]
                  > Dear Keith Thompson,
                  >
                  >[color=green]
                  >> 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.[/color]
                  >
                  > Thanks for the important tips![/color]

                  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) kst-u@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.

                  Comment

                  • agay@vms.huji.ac.il

                    #10
                    Re: switching on strings in standard C


                    Keith Thompson wrote:[color=blue]
                    > agay@vms.huji.a c.il writes:[color=green]
                    > > Dear Keith Thompson,
                    > >[/color]
                    > 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.)[/color]

                    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

                    Comment

                    • Keith Thompson

                      #11
                      Re: switching on strings in standard C

                      agay@vms.huji.a c.il writes:[color=blue]
                      > Keith Thompson wrote:[color=green]
                      >> agay@vms.huji.a c.il writes:[color=darkred]
                      >> > Dear Keith Thompson,
                      >> >[/color]
                      >> 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.)[/color]
                      >
                      > 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.[/color]

                      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_Keit h) kst-u@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.

                      Comment

                      • Kenny McCormack

                        #12
                        Newsreaders - and degrees of evil (Was: Re: switching on strings in standard C)

                        In article <lniryh60c5.fsf @nuthaus.mib.or g>,
                        Keith Thompson <kst-u@mib.org> wrote:[color=blue]
                        >agay@vms.huji. ac.il writes:[color=green]
                        >> Keith Thompson wrote:[color=darkred]
                        >>> agay@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.)[/color]
                        >>
                        >> 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.[/color]
                        >
                        >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).[/color]

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

                        Compared to MS, they're not.

                        But that's about all you can say.

                        Comment

                        • James Dow Allen

                          #13
                          Re: switching on strings in standard C


                          Keith Thompson wrote:[color=blue]
                          > The proper solution, of course, would be for Google to fix their
                          > interface, but apparently their "Don't be evil" motto is just
                          > decorative.[/color]

                          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

                          Comment

                          • Antonio Contreras

                            #14
                            Re: switching on strings in standard C

                            James Dow Allen wrote:[color=blue]
                            > Keith Thompson wrote:[color=green]
                            > > The proper solution, of course, would be for Google to fix their
                            > > interface, but apparently their "Don't be evil" motto is just
                            > > decorative.[/color]
                            >
                            > 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.[/color]

                            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
                            "obliterate d by paid ads".
                            [color=blue]
                            > My excuse is I post only from "cafes." Happy to hear of
                            > any good non-Google alternative for posting on Usenet.[/color]

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

                            Comment

                            • Christopher Benson-Manica

                              #15
                              [OT was: Re: switching on strings in standard C]

                              Antonio Contreras <anconor@gmail. com> wrote:
                              [color=blue]
                              > 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...[/color]

                              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)cybers pace.org | don't, I need to know. Flames welcome.

                              Comment

                              Working...