1
0
Fork 0

(svn r16591) -Codechange: some coding style in strgen

release/1.0
rubidium 2009-06-18 11:17:55 +00:00
parent 98b4f1fce2
commit cdc0c092f1
1 changed files with 57 additions and 80 deletions

View File

@ -238,7 +238,7 @@ static void EmitSetX(char *buf, int value)
{ {
char *err; char *err;
int x = strtol(buf, &err, 0); int x = strtol(buf, &err, 0);
if (*err != 0) error("SetX param invalid"); if (*err != '\0') error("SetX param invalid");
PutUtf8(SCC_SETX); PutUtf8(SCC_SETX);
PutByte((byte)x); PutByte((byte)x);
} }
@ -247,12 +247,10 @@ static void EmitSetX(char *buf, int value)
static void EmitSetXY(char *buf, int value) static void EmitSetXY(char *buf, int value)
{ {
char *err; char *err;
int x;
int y;
x = strtol(buf, &err, 0); int x = strtol(buf, &err, 0);
if (*err != ' ') error("SetXY param invalid"); if (*err != ' ') error("SetXY param invalid");
y = strtol(err + 1, &err, 0); int y = strtol(err + 1, &err, 0);
if (*err != 0) error("SetXY param invalid"); if (*err != 0) error("SetXY param invalid");
PutUtf8(SCC_SETXY); PutUtf8(SCC_SETXY);
@ -271,14 +269,13 @@ bool ParseRelNum(char **buf, int *value)
const char *s = *buf; const char *s = *buf;
char *end; char *end;
bool rel = false; bool rel = false;
int v;
while (*s == ' ' || *s == '\t') s++; while (*s == ' ' || *s == '\t') s++;
if (*s == '+') { if (*s == '+') {
rel = true; rel = true;
s++; s++;
} }
v = strtol(s, &end, 0); int v = strtol(s, &end, 0);
if (end == s) return false; if (end == s) return false;
if (rel || v < 0) { if (rel || v < 0) {
*value += v; *value += v;
@ -329,13 +326,10 @@ static int TranslateArgumentIdx(int arg);
static void EmitWordList(const char * const *words, uint nw) static void EmitWordList(const char * const *words, uint nw)
{ {
uint i;
uint j;
PutByte(nw); PutByte(nw);
for (i = 0; i < nw; i++) PutByte(strlen(words[i])); for (uint i = 0; i < nw; i++) PutByte(strlen(words[i]));
for (i = 0; i < nw; i++) { for (uint i = 0; i < nw; i++) {
for (j = 0; words[i][j] != '\0'; j++) PutByte(words[i][j]); for (uint j = 0; words[i][j] != '\0'; j++) PutByte(words[i][j]);
} }
} }
@ -354,8 +348,9 @@ static void EmitPlural(char *buf, int value)
if (words[nw] == NULL) break; if (words[nw] == NULL) break;
} }
if (nw == 0) if (nw == 0) {
error("%s: No plural words", _cur_ident); error("%s: No plural words", _cur_ident);
}
if (_plural_forms[_lang_pluralform].plural_count != nw) { if (_plural_forms[_lang_pluralform].plural_count != nw) {
if (_translated) { if (_translated) {
@ -389,20 +384,20 @@ static void EmitGender(char *buf, int value)
/* This is a {G=DER} command */ /* This is a {G=DER} command */
for (nw = 0; ; nw++) { for (nw = 0; ; nw++) {
if (nw >= 8) error("G argument '%s' invalid", buf); if (nw >= MAX_NUM_GENDER) error("G argument '%s' invalid", buf);
if (strcmp(buf, _genders[nw]) == 0) break; if (strcmp(buf, _genders[nw]) == 0) break;
} }
/* now nw contains the gender index */ /* now nw contains the gender index */
PutUtf8(SCC_GENDER_INDEX); PutUtf8(SCC_GENDER_INDEX);
PutByte(nw); PutByte(nw);
} else { } else {
const char *words[8]; const char *words[MAX_NUM_GENDER];
/* This is a {G 0 foo bar two} command. /* This is a {G 0 foo bar two} command.
* If no relative number exists, default to +0 */ * If no relative number exists, default to +0 */
if (!ParseRelNum(&buf, &argidx)) {} if (!ParseRelNum(&buf, &argidx)) {}
for (nw = 0; nw < 8; nw++) { for (nw = 0; nw < MAX_NUM_GENDER; nw++) {
words[nw] = ParseWord(&buf); words[nw] = ParseWord(&buf);
if (words[nw] == NULL) break; if (words[nw] == NULL) break;
} }
@ -415,9 +410,7 @@ static void EmitGender(char *buf, int value)
static const CmdStruct *FindCmd(const char *s, int len) static const CmdStruct *FindCmd(const char *s, int len)
{ {
const CmdStruct *cs; for (const CmdStruct *cs = _cmd_structs; cs != endof(_cmd_structs); cs++) {
for (cs = _cmd_structs; cs != endof(_cmd_structs); cs++) {
if (strncmp(cs->cmd, s, len) == 0 && cs->cmd[len] == '\0') return cs; if (strncmp(cs->cmd, s, len) == 0 && cs->cmd[len] == '\0') return cs;
} }
return NULL; return NULL;
@ -425,9 +418,7 @@ static const CmdStruct *FindCmd(const char *s, int len)
static uint ResolveCaseName(const char *str, uint len) static uint ResolveCaseName(const char *str, uint len)
{ {
uint i; for (uint i = 0; i < MAX_NUM_CASES; i++) {
for (i = 0; i < MAX_NUM_CASES; i++) {
if (memcmp(_cases[i], str, len) == 0 && _cases[i][len] == 0) return i + 1; if (memcmp(_cases[i], str, len) == 0 && _cases[i][len] == 0) return i + 1;
} }
error("Invalid case-name '%s'", str); error("Invalid case-name '%s'", str);
@ -439,8 +430,7 @@ static uint ResolveCaseName(const char *str, uint len)
static const CmdStruct *ParseCommandString(const char **str, char *param, int *argno, int *casei) static const CmdStruct *ParseCommandString(const char **str, char *param, int *argno, int *casei)
{ {
const char *s = *str, *start; const char *s = *str, *start;
const CmdStruct *cmd; char c;
byte c;
*argno = -1; *argno = -1;
*casei = -1; *casei = -1;
@ -465,7 +455,7 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a
c = *s++; c = *s++;
} while (c != '}' && c != ' ' && c != '=' && c != '.' && c != 0); } while (c != '}' && c != ' ' && c != '=' && c != '.' && c != 0);
cmd = FindCmd(start, s - start - 1); const CmdStruct *cmd = FindCmd(start, s - start - 1);
if (cmd == NULL) { if (cmd == NULL) {
strgen_error("Undefined command '%.*s'", (int)(s - start - 1), start); strgen_error("Undefined command '%.*s'", (int)(s - start - 1), start);
return NULL; return NULL;
@ -474,10 +464,13 @@ static const CmdStruct *ParseCommandString(const char **str, char *param, int *a
if (c == '.') { if (c == '.') {
const char *casep = s; const char *casep = s;
if (!(cmd->flags & C_CASE)) if (!(cmd->flags & C_CASE)) {
error("Command '%s' can't have a case", cmd->cmd); error("Command '%s' can't have a case", cmd->cmd);
}
do c = *s++; while (c != '}' && c != ' ' && c != '\0'); do {
c = *s++;
} while (c != '}' && c != ' ' && c != '\0');
*casei = ResolveCaseName(casep, s - casep - 1); *casei = ResolveCaseName(casep, s - casep - 1);
} }
@ -636,7 +629,6 @@ static bool CheckCommandsMatch(char *a, char *b, const char *name)
{ {
ParsedCommandStruct templ; ParsedCommandStruct templ;
ParsedCommandStruct lang; ParsedCommandStruct lang;
uint i, j;
bool result = true; bool result = true;
ExtractCommandString(&templ, b, true); ExtractCommandString(&templ, b, true);
@ -648,10 +640,10 @@ static bool CheckCommandsMatch(char *a, char *b, const char *name)
result = false; result = false;
} }
for (i = 0; i < templ.np; i++) { for (uint i = 0; i < templ.np; i++) {
/* see if we find it in lang, and zero it out */ /* see if we find it in lang, and zero it out */
bool found = false; bool found = false;
for (j = 0; j < lang.np; j++) { for (uint j = 0; j < lang.np; j++) {
if (templ.pairs[i].a == lang.pairs[j].a && if (templ.pairs[i].a == lang.pairs[j].a &&
strcmp(templ.pairs[i].v, lang.pairs[j].v) == 0) { strcmp(templ.pairs[i].v, lang.pairs[j].v) == 0) {
/* it was found in both. zero it out from lang so we don't find it again */ /* it was found in both. zero it out from lang so we don't find it again */
@ -669,7 +661,7 @@ static bool CheckCommandsMatch(char *a, char *b, const char *name)
/* if we reach here, all non consumer commands match up. /* if we reach here, all non consumer commands match up.
* Check if the non consumer commands match up also. */ * Check if the non consumer commands match up also. */
for (i = 0; i < lengthof(templ.cmd); i++) { for (uint i = 0; i < lengthof(templ.cmd); i++) {
if (TranslateCmdForCompare(templ.cmd[i]) != TranslateCmdForCompare(lang.cmd[i])) { if (TranslateCmdForCompare(templ.cmd[i]) != TranslateCmdForCompare(lang.cmd[i])) {
strgen_warning("%s: Param idx #%d '%s' doesn't match with template command '%s'", name, i, strgen_warning("%s: Param idx #%d '%s' doesn't match with template command '%s'", name, i,
lang.cmd[i] == NULL ? "<empty>" : lang.cmd[i]->cmd, lang.cmd[i] == NULL ? "<empty>" : lang.cmd[i]->cmd,
@ -683,10 +675,6 @@ static bool CheckCommandsMatch(char *a, char *b, const char *name)
static void HandleString(char *str, bool master) static void HandleString(char *str, bool master)
{ {
char *s, *t;
LangString *ent;
char *casep;
if (*str == '#') { if (*str == '#') {
if (str[1] == '#' && str[2] != '#') HandlePragma(str + 2); if (str[1] == '#' && str[2] != '#') HandlePragma(str + 2);
return; return;
@ -695,35 +683,34 @@ static void HandleString(char *str, bool master)
/* Ignore comments & blank lines */ /* Ignore comments & blank lines */
if (*str == ';' || *str == ' ' || *str == '\0') return; if (*str == ';' || *str == ' ' || *str == '\0') return;
s = strchr(str, ':'); char *s = strchr(str, ':');
if (s == NULL) { if (s == NULL) {
strgen_error("Line has no ':' delimiter"); strgen_error("Line has no ':' delimiter");
return; return;
} }
char *t;
/* Trim spaces. /* Trim spaces.
* After this str points to the command name, and s points to the command contents */ * After this str points to the command name, and s points to the command contents */
for (t = s; t > str && (t[-1] == ' ' || t[-1] == '\t'); t--); for (t = s; t > str && (t[-1] == ' ' || t[-1] == '\t'); t--) {}
*t = 0; *t = 0;
s++; s++;
/* Check string is valid UTF-8 */ /* Check string is valid UTF-8 */
{ const char *tmp;
const char *tmp; for (tmp = s; *tmp != '\0';) {
for (tmp = s; *tmp != '\0';) { size_t len = Utf8Validate(tmp);
size_t len = Utf8Validate(tmp); if (len == 0) error("Invalid UTF-8 sequence in '%s'", s);
if (len == 0) error("Invalid UTF-8 sequence in '%s'", s); tmp += len;
tmp += len;
}
} }
/* Check if the string has a case.. /* Check if the string has a case..
* The syntax for cases is IDENTNAME.case */ * The syntax for cases is IDENTNAME.case */
casep = strchr(str, '.'); char *casep = strchr(str, '.');
if (casep) *casep++ = 0; if (casep) *casep++ = '\0';
/* Check if this string already exists.. */ /* Check if this string already exists.. */
ent = HashFind(str); LangString *ent = HashFind(str);
if (master) { if (master) {
if (ent != NULL && casep == NULL) { if (ent != NULL && casep == NULL) {
@ -897,18 +884,16 @@ static uint CountInUse(uint grp)
bool CompareFiles(const char *n1, const char *n2) bool CompareFiles(const char *n1, const char *n2)
{ {
FILE *f1, *f2; FILE *f2 = fopen(n2, "rb");
char b1[4096];
char b2[4096];
size_t l1, l2;
f2 = fopen(n2, "rb");
if (f2 == NULL) return false; if (f2 == NULL) return false;
f1 = fopen(n1, "rb"); FILE *f1 = fopen(n1, "rb");
if (f1 == NULL) error("can't open %s", n1); if (f1 == NULL) error("can't open %s", n1);
size_t l1, l2;
do { do {
char b1[4096];
char b2[4096];
l1 = fread(b1, 1, sizeof(b1), f1); l1 = fread(b1, 1, sizeof(b1), f1);
l2 = fread(b2, 1, sizeof(b2), f2); l2 = fread(b2, 1, sizeof(b2), f2);
@ -927,18 +912,16 @@ bool CompareFiles(const char *n1, const char *n2)
static void WriteStringsH(const char *filename) static void WriteStringsH(const char *filename)
{ {
FILE *out;
int i;
int next = -1; int next = -1;
out = fopen("tmp.xxx", "w"); FILE *out = fopen("tmp.xxx", "w");
if (out == NULL) error("can't open tmp.xxx"); if (out == NULL) error("can't open tmp.xxx");
fprintf(out, "/* This file is automatically generated. Do not modify */\n\n"); fprintf(out, "/* This file is automatically generated. Do not modify */\n\n");
fprintf(out, "#ifndef TABLE_STRINGS_H\n"); fprintf(out, "#ifndef TABLE_STRINGS_H\n");
fprintf(out, "#define TABLE_STRINGS_H\n"); fprintf(out, "#define TABLE_STRINGS_H\n");
for (i = 0; i != lengthof(_strings); i++) { for (int i = 0; i != lengthof(_strings); i++) {
if (_strings[i] != NULL) { if (_strings[i] != NULL) {
if (next != i) fprintf(out, "\n"); if (next != i) fprintf(out, "\n");
fprintf(out, "static const StringID %s = 0x%X;\n", _strings[i]->name, i); fprintf(out, "static const StringID %s = 0x%X;\n", _strings[i]->name, i);
@ -973,12 +956,13 @@ static void WriteStringsH(const char *filename)
static int TranslateArgumentIdx(int argidx) static int TranslateArgumentIdx(int argidx)
{ {
int i, sum; int sum;
if (argidx < 0 || (uint)argidx >= lengthof(_cur_pcs.cmd)) if (argidx < 0 || (uint)argidx >= lengthof(_cur_pcs.cmd)) {
error("invalid argidx %d", argidx); error("invalid argidx %d", argidx);
}
for (i = sum = 0; i < argidx; i++) { for (int i = sum = 0; i < argidx; i++) {
const CmdStruct *cs = _cur_pcs.cmd[i]; const CmdStruct *cs = _cur_pcs.cmd[i];
sum += (cs != NULL) ? cs->consumes : 1; sum += (cs != NULL) ? cs->consumes : 1;
} }
@ -995,11 +979,6 @@ static void PutArgidxCommand()
static void PutCommandString(const char *str) static void PutCommandString(const char *str)
{ {
const CmdStruct *cs;
char param[256];
int argno;
int casei;
_cur_argidx = 0; _cur_argidx = 0;
while (*str != '\0') { while (*str != '\0') {
@ -1008,7 +987,11 @@ static void PutCommandString(const char *str)
PutByte(*str++); PutByte(*str++);
continue; continue;
} }
cs = ParseCommandString(&str, param, &argno, &casei);
char param[256];
int argno;
int casei;
const CmdStruct *cs = ParseCommandString(&str, param, &argno, &casei);
if (cs == NULL) break; if (cs == NULL) break;
if (casei != -1) { if (casei != -1) {
@ -1050,17 +1033,14 @@ static void WriteLength(FILE *f, uint length)
static void WriteLangfile(const char *filename) static void WriteLangfile(const char *filename)
{ {
FILE *f;
uint in_use[32]; uint in_use[32];
LanguagePackHeader hdr; LanguagePackHeader hdr;
uint i;
uint j;
f = fopen(filename, "wb"); FILE *f = fopen(filename, "wb");
if (f == NULL) error("can't open %s", filename); if (f == NULL) error("can't open %s", filename);
memset(&hdr, 0, sizeof(hdr)); memset(&hdr, 0, sizeof(hdr));
for (i = 0; i != 32; i++) { for (int i = 0; i != 32; i++) {
uint n = CountInUse(i); uint n = CountInUse(i);
in_use[i] = n; in_use[i] = n;
@ -1082,8 +1062,8 @@ static void WriteLangfile(const char *filename)
fwrite(&hdr, sizeof(hdr), 1, f); fwrite(&hdr, sizeof(hdr), 1, f);
for (i = 0; i != 32; i++) { for (int i = 0; i != 32; i++) {
for (j = 0; j != in_use[i]; j++) { for (int j = 0; j != in_use[i]; j++) {
const LangString *ls = _strings[(i << 11) + j]; const LangString *ls = _strings[(i << 11) + j];
const Case *casep; const Case *casep;
const char *cmdp; const char *cmdp;
@ -1179,10 +1159,9 @@ static inline void ottd_mkdir(const char *directory)
* does not already end with a seperator */ * does not already end with a seperator */
static inline char *mkpath(char *buf, size_t buflen, const char *path, const char *file) static inline char *mkpath(char *buf, size_t buflen, const char *path, const char *file)
{ {
char *p;
ttd_strlcpy(buf, path, buflen); // copy directory into buffer ttd_strlcpy(buf, path, buflen); // copy directory into buffer
p = strchr(buf, '\0'); // add path seperator if necessary char *p = strchr(buf, '\0'); // add path seperator if necessary
if (p[-1] != PATHSEPCHAR && (size_t)(p - buf) + 1 < buflen) *p++ = PATHSEPCHAR; if (p[-1] != PATHSEPCHAR && (size_t)(p - buf) + 1 < buflen) *p++ = PATHSEPCHAR;
ttd_strlcpy(p, file, buflen - (size_t)(p - buf)); // catenate filename at end of buffer ttd_strlcpy(p, file, buflen - (size_t)(p - buf)); // catenate filename at end of buffer
return buf; return buf;
@ -1196,9 +1175,7 @@ static inline char *mkpath(char *buf, size_t buflen, const char *path, const cha
*/ */
static inline char *replace_pathsep(char *s) static inline char *replace_pathsep(char *s)
{ {
char *c; for (char *c = s; *c != '\0'; c++) if (*c == '/') *c = '\\';
for (c = s; *c != '\0'; c++) if (*c == '/') *c = '\\';
return s; return s;
} }
#else #else