String parser in C

R

Thread Starter

Ron Gage

Hi folks:

I saw a message in here (I think from Curt) that relayed the old tale of how difficult string parsing in C is.

When I first embarked in this great journey called C programming, I thought the same thing. Compared to BASIC, C string handling basically sucks. Then I got to thinking "why not make my own equals to the basic commands I hold to be so
dear to my warped sense of thinking".

I am attaching a few routines that do the same things as the basic statements:
LEFT, MID, RIGHT and INSTR. With these 4 statements coded into C, string handling is now MUCH easier.

I am also including the "ini file parser" I use in my Dallas Semiconductor program. It gives a nice working example of how to implement the string functions (and parse a text file "easily" in C (TM)).

As with ALL of my code, the license is to go ahead and have fun with it. If you make any improvements, PLEASE return those improvements to me so I can improve what I have. That's it, no other requirements or restrictions.


--
Ron Gage - Saginaw, MI
([email protected])

_______________________________________________
LinuxPLC mailing list
[email protected]
http://linuxplc.org/mailman/listinfo/linuxplc
 
Hello,

Curt Wuollet:
> For the record, you didn't hear that from Curt, I think C for parsing is
> just fine.

It was me. FTR, I've been programming in C for years. It still sucks.

(As an exercise, write a routine that accepts an integer from the user.)

> Not worth learinig (or introducing) another language to avoid anyway.

Then again, if/when it's rewritten into C, I'd probably use pcre (Perl regular expressions library) or some other regular-expression library, at which point there'd seem to be little benefit switching.

> Ron Gage wrote:
> > I saw a message in here (I think from Curt) that relayed the old tale
> > of how difficult string parsing in C is.
...
> > I am attaching a few routines that do the same things as the basic
> > statements: LEFT, MID, RIGHT and INSTR. With these 4 statements coded
> > into C, string handling is now MUCH easier.

It's not left, mid, right and instr that are the problem; it's that in C, you have to do everything, every last thing manually, as though nobody had ever seen a string before.


Jiri
--
Jiri Baum <[email protected]>
Windows is not popular. Windows is *widespread*. Linux is popular.

_______________________________________________
LinuxPLC mailing list
[email protected]
http://linuxplc.org/mailman/listinfo/linuxplc
 
Hello,

Ron Gage:
> I am attaching a few routines that do the same things as the basic
> statements: LEFT, MID, RIGHT and INSTR. With these 4 statements coded
> into C, string handling is now MUCH easier.

What is `s' in the code?


> I am also including the "ini file parser" I use in my Dallas
> Semiconductor program. It gives a nice working example of how to
> implement the string functions (and parse a text file "easily" in C
> (TM)).

Actually, I'd count it as an exaple of how difficult parsing a text file is in C... You should not have to have the same code repeated over for every possible variable.

Rather than
atoi(mid(iline,y+10,0))
why not use
atoi(iline+10)
?

Why are you adding `y' which will always be zero at that point?

Celsius is spelled with an s in the middle.


Jiri
--
Jiri Baum <[email protected]>
Windows is not popular. Windows is *widespread*. Linux is popular.

_______________________________________________
LinuxPLC mailing list
[email protected]
http://linuxplc.org/mailman/listinfo/linuxplc
 
J

Johan Bengtsson

Well, getting an integer isn't THAT big problem (you know that), it would probably take some .... <10 minutes to fix, including additional time to make it really general, do you want it to get
the data from a FILE * or a char ** or from something else? And then it will accept decimal as well as hexadecimal values... yes, I can write it as soon as anyone answers my question. Do you want one for reading a double as well? supporting the following formats:
10000
10000.0
10000,0
10k
10.0k
10,000k
..01M
1e4

it will take a little bit longer but....

Would you prefere faster code or more easy changeable code? I suppose more changeable....


I do get your point however, it is easier in other languages with more built in support for that.


/Johan Bengtsson

----------------------------------------
P&L, the Academy of Automation
Box 252, S-281 23 H{ssleholm SWEDEN
Tel: +46 451 49 460, Fax: +46 451 89 833
E-mail: [email protected]
Internet: http://www.pol.se/
----------------------------------------

_______________________________________________
LinuxPLC mailing list
[email protected]
http://linuxplc.org/mailman/listinfo/linuxplc
 
J

Jan Krabbenbos

Hi Ron,

I have 'redone' your BASIC functions with the standard string function library. In the accompanying file I've created an example I made this afternoon.

C's string handling is very basic, but when you realize that you can work with pointers in combination with the string functions you can
create your own library of functions. Also for reading integer or floating point values.

--
Greetings,
Jan

/\ Jan Krabbenbos
/\ email: [email protected]
\/ www : http://www.krabbenbos.com
 
Jiri Baum:
> > (As an exercise, write a routine that accepts an integer from the user.)

[email protected]:
> Well, getting an integer isn't THAT big problem (you know that), it would
> probably take some .... <10 minutes to fix, including additional time to
> make it really general, do you want it to get the data from a FILE * or a
> char ** or from something else?

Get it from the user, check that it really is an integer that was typed in, and if not loop back and ask for it again.

> And then it will accept decimal as well as hexadecimal values...

Just decimal.

> Would you prefere faster code or more easy changeable code? I suppose
> more changeable....

I don't mind - it's supposed to be an exercise, so it doesn't matter as long as it's correct. I do insist, however, on handling arbitrary input.

> I do get your point however, it is easier in other languages with more
> built in support for that.


Jiri
--
Jiri Baum <[email protected]>
Windows is not popular. Windows is *widespread*. Linux is popular.

_______________________________________________
LinuxPLC mailing list
[email protected]
http://linuxplc.org/mailman/listinfo/linuxplc
 
Top