B
Ben Pfaff
Michael B Allen said:However, they're not doing anyone any good with this code (at least the
code in the link posted by websnarf [1]). In fact I think they're probably
doing more harm than good. Modern code must be reentrant or it's basically
useless. The cited example desperately needs an object to hold the state
of the parse. And for a CSV parser a state machine is clearly the only
way to go. The code may be good from a language lawyer perspective
but the overall organisation and design is just awful. For the sake of C
programmers everywhere I hope the rest of the book isn't anything like it.
[1] http://cm.bell-labs.com/cm/cs/tpop/csvgetline2.c
In the book, Kernighan and Pike go on to improve the final C
version they present (presumably what is at the URL above) into a
C++ implementation that encapsulates the operation of the library
into a class. They also pose an exercise (Exercise 4-8)
encouraging readers to write a C version of the improved C++
code.
I suppose I should have looked at the book more carefully before
suggesting that its code be used as a CSV parser. I wouldn't
recommend the book's final C version as a prepackaged CSV parser.
In my defense, I suspect that I was at home, not in my Stanford
office, when I posted the suggestion; my copy of the book is at
the office.