Sujet : Re: Command line globber/tokenizer library for C?
De : already5chosen (at) *nospam* yahoo.com (Michael S)
Groupes : comp.lang.cDate : 12. Sep 2024, 20:38:28
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240912223828.00005c10@yahoo.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12
User-Agent : Claws Mail 4.1.1 (GTK 3.24.34; x86_64-w64-mingw32)
On Thu, 12 Sep 2024 19:02:35 +0200
Bonita Montero <
Bonita.Montero@gmail.com> wrote:
Am 12.09.2024 um 18:28 schrieb Bart:
BM's C++ version doesn't handle embedded quotes or single quotes
either.
> Neither expand wildcards into sequences of filename arguments.
Ok, that must be impossible with C++.
I just wanted to show how to do it basically and what are the
advantages: no intermediate data structure through functional
progtamming and debug iterators.
Callback is as easy in C as in C++.
Debug iterators not needed in such simple program. At least, I don't
need them.
Here is an equivalent of your parser written in C. It does not look 5
times longer.
Attention! That is an equivalent of Bonita's code, no more and
hopefully no less. The routine does not fulfill requirements of OP!
#include <stddef.h>
void parse(const char* src,
void (*OnToken)(const char* beg, size_t len, void* context),
void* context) {
char c0 = ' ', c1 = '\t';
const char* beg = 0;
for (;;src++) {
char c = *src;
if (c == c0 || c == c1 || c == 0) {
if (beg) {
OnToken(beg, src-beg, context);
c0 = ' ', c1 = '\t';
beg = 0;
}
if (c == 0)
break;
} else if (!beg) {
beg = src;
if (c == '"') {
c0 = c1 = c;
++beg;
}
}
}
}