1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUF_SIZE 256
struct ctx {
int foo;
};
static void init_context(struct ctx *ctx)
{
ctx->foo = 0;
return;
}
static int parse(const char *s, struct ctx *ctx)
{
char *string;
char *ptr, *tmp, *save;
if ((string = strdup(s)) == NULL) {
/* FIXME */
return 1;
}
for (tmp = string; (ptr = strtok_r(tmp, " \n", &save)); tmp = NULL) {
float f;
printf("token '%s'\n", ptr);
if (strcmp(ptr, "DUP") == 0) {
/* FIXME */
} else if (strcmp(ptr, "PUSH") == 0) {
/* FIXME */
} else if (strcmp(ptr, "+") == 0) {
/* FIXME */
} else if (strcmp(ptr, "-") == 0) {
/* FIXME */
} else if (strcmp(ptr, "*") == 0) {
/* FIXME */
} else if (strcmp(ptr, "/") == 0) {
/* FIXME */
} else if (sscanf(ptr, "%f", &f) == 1) {
/* FIXME */
printf("found float: %f\n", f);
} else {
printf("invalid token %s\n", ptr);
/* FIXME */
}
}
free(string);
return 0;
}
static void test_fgets(struct ctx *ctx)
{
char buffer[BUF_SIZE];
while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
if (parse(buffer, ctx) != 0) {
/* FIXME */
}
}
/* FIXME */
return ;
}
int main (void)
{
struct ctx ctx;
init_context(&ctx);
test_fgets(&ctx);
return 0;
} |
Partager