10 const char *infile
= "object.h";
11 const char *outfile
= "autogen/objnames.cpp";
13 // read data from a file until CR
14 void fgetline(FILE *fp
, char *str
, int maxlen
)
18 fgets(str
, maxlen
- 1, fp
);
20 // trim the CRLF that fgets appends
21 for(k
=strlen(str
)-1;k
>=0;k
--)
23 if (str
[k
] != 13 && str
[k
] != 10) break;
29 bool strbegin(const char *bigstr
, const char *smallstr
)
33 for(i
=0;smallstr
[i
];i
++)
34 if (bigstr
[i
] != smallstr
[i
]) return false;
47 printf("regenerating object-names table...\n");
49 fp
= fopen(infile
, "rb");
52 fprintf(stderr
, "<Cannot open object.h>\n");
56 fpo
= fileopen(outfile
, "wb");
59 fprintf(stderr
, "<Cannot open objnames.cpp>\n");
64 memset(nametable
, 0, sizeof(nametable
));
68 fgetline(fp
, line
, sizeof(line
));
70 if (strbegin(line
, "#define OBJ_"))
72 char *ptr
= strstr(line
, "//");
76 const char *obj_name
= strtok(NULL
, " \t");
77 const char *obj_number_str
= strtok(NULL
, " \t");
80 if (obj_name
&& obj_number_str
&& (obj_number
= atoi(obj_number_str
)))
82 if (!strcmp(obj_name
, "OBJ_LAST"))
84 nobjects
= obj_number
;
88 //printf("obj_name: %s obj_number: %d\n", obj_name, obj_number);
89 nametable
[obj_number
] = strdup(&obj_name
[4]);
98 fprintf(stderr
, "Couldn't find OBJ_LAST\n");
104 fprintf(fpo
, "\n// auto-generated by genobjnametable.cpp\n");
105 fprintf(fpo
, "#include <stdio.h>\n");
106 fprintf(fpo
, "\nconst char *object_names[] = {\n");
108 for(int i
=0;i
<nobjects
;i
++)
112 fprintf(fpo
, "\t\"%s\"", nametable
[i
]);
117 fprintf(fpo
, "\tNULL");
126 fprintf(fpo
, "};\n");
127 printf("wrote %d objects in a space of %d\n", count
, nobjects
);
140 if (stat(outfile
, &outstat
))
142 printf("names table missing\n");
146 if (stat(infile
, &instat
))
148 fprintf(stderr
, "cannot stat '%s': error %d\n", infile
, errno
);
152 if (instat
.st_mtime
>= outstat
.st_mtime
)
157 printf("table up to date\n");