1
0
Fork 0

(svn r1820) Style cleanup:

- indentation
- 0 -> '\0' in string handling
- whitespaces
- space after for, switch, etc.
- don't treat non-boolean values as boolean
- space before and after binary operators
- bracing style
release/0.4.5
tron 2005-02-06 11:23:41 +00:00
parent ccf3c71903
commit 498ccae27a
1 changed files with 117 additions and 56 deletions

127
strings.c
View File

@ -59,15 +59,70 @@ const uint16 _currency_string_list[] = {
}; };
static const uint16 _cargo_string_list[NUM_LANDSCAPE][NUM_CARGO] = { static const uint16 _cargo_string_list[NUM_LANDSCAPE][NUM_CARGO] = {
/* LT_NORMAL */ {STR_PASSENGERS, STR_TONS, STR_BAGS, STR_LITERS, STR_ITEMS, STR_CRATES, STR_TONS, STR_TONS, STR_TONS, STR_TONS, STR_BAGS, STR_RES_OTHER}, { /* LT_NORMAL */
/* LT_HILLY */ {STR_PASSENGERS, STR_TONS, STR_BAGS, STR_LITERS, STR_ITEMS, STR_CRATES, STR_TONS, STR_TONS, STR_RES_OTHER, STR_TONS, STR_BAGS, STR_TONS}, STR_PASSENGERS,
/* LT_DESERT */ {STR_PASSENGERS, STR_LITERS, STR_BAGS, STR_LITERS, STR_TONS, STR_CRATES, STR_TONS, STR_TONS, STR_TONS, STR_LITERS, STR_BAGS, STR_TONS}, STR_TONS,
/* LT_CANDY */ {STR_PASSENGERS, STR_TONS, STR_BAGS, STR_NOTHING, STR_NOTHING, STR_TONS, STR_TONS, STR_LITERS, STR_TONS, STR_NOTHING, STR_LITERS, STR_NOTHING} STR_BAGS,
STR_LITERS,
STR_ITEMS,
STR_CRATES,
STR_TONS,
STR_TONS,
STR_TONS,
STR_TONS,
STR_BAGS,
STR_RES_OTHER
},
{ /* LT_HILLY */
STR_PASSENGERS,
STR_TONS,
STR_BAGS,
STR_LITERS,
STR_ITEMS,
STR_CRATES,
STR_TONS,
STR_TONS,
STR_RES_OTHER,
STR_TONS,
STR_BAGS,
STR_TONS
},
{ /* LT_DESERT */
STR_PASSENGERS,
STR_LITERS,
STR_BAGS,
STR_LITERS,
STR_TONS,
STR_CRATES,
STR_TONS,
STR_TONS,
STR_TONS,
STR_LITERS,
STR_BAGS,
STR_TONS
},
{ /* LT_CANDY */
STR_PASSENGERS,
STR_TONS,
STR_BAGS,
STR_NOTHING,
STR_NOTHING,
STR_TONS,
STR_TONS,
STR_LITERS,
STR_TONS,
STR_NOTHING,
STR_LITERS,
STR_NOTHING
}
}; };
static char *str_cat(char *dst, const char *src) static char *str_cat(char *dst, const char *src)
{ {
while ( (*dst++ = *src++) != 0) {} while ((*dst++ = *src++) != '\0') {}
return dst - 1; return dst - 1;
} }
@ -190,7 +245,7 @@ static char *FormatCommaNumber(char *buff, int32 number)
} }
} }
*buff = 0; *buff = '\0';
return buff; return buff;
} }
@ -222,7 +277,7 @@ static char *FormatNoCommaNumber(char *buff, int32 number)
} }
} }
*buff = 0; *buff = '\0';
return buff; return buff;
} }
@ -235,7 +290,7 @@ static char *FormatYmdString(char *buff, uint16 number)
ConvertDayToYMD(&ymd, number); ConvertDayToYMD(&ymd, number);
for(src = GetStringPtr(ymd.day+STR_01AC_1ST-1); (*buff++=*src++) != 0;) {} for (src = GetStringPtr(ymd.day + STR_01AC_1ST - 1); (*buff++ = *src++) != '\0';) {}
buff[-1] = ' '; buff[-1] = ' ';
memcpy(buff, GetStringPtr(STR_0162_JAN + ymd.month), 4); memcpy(buff, GetStringPtr(STR_0162_JAN + ymd.month), 4);
@ -251,7 +306,7 @@ static char *FormatMonthAndYear(char *buff, uint16 number)
ConvertDayToYMD(&ymd, number); ConvertDayToYMD(&ymd, number);
for(src = GetStringPtr(STR_MONTH_JAN + ymd.month); (*buff++=*src++) != 0;) {} for (src = GetStringPtr(STR_MONTH_JAN + ymd.month); (*buff++ = *src++) != '\0';) {}
buff[-1] = ' '; buff[-1] = ' ';
return FormatNoCommaNumber(buff, ymd.year + MAX_YEAR_BEGIN_REAL); return FormatNoCommaNumber(buff, ymd.year + MAX_YEAR_BEGIN_REAL);
@ -269,7 +324,7 @@ static char *FormatTinyDate(char *buff, uint16 number)
uint GetCurrentCurrencyRate(void) uint GetCurrentCurrencyRate(void)
{ {
return (&_currency_specs[_opt.currency])->rate; return _currency_specs[_opt.currency].rate;
} }
static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, int64 number, bool compact) static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, int64 number, bool compact)
@ -283,11 +338,14 @@ static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, int64 n
number *= spec->rate; number *= spec->rate;
// convert from negative // convert from negative
if (number < 0) { *buff++ = '-'; number = -number; } if (number < 0) {
*buff++ = '-';
number = -number;
}
// add prefix part // add prefix part
s = spec->prefix; s = spec->prefix;
while (s != spec->prefix + lengthof(spec->prefix) && (c=*s++)) *buff++ = c; while (s != spec->prefix + lengthof(spec->prefix) && (c = *s++) != '\0') *buff++ = c;
// for huge numbers, compact the number into k or M // for huge numbers, compact the number into k or M
if (compact) { if (compact) {
@ -305,7 +363,10 @@ static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, int64 n
p = buf; p = buf;
j = 4; j = 4;
do { do {
if (--j == 0) { *p++ = spec->separator; j = 3; } if (--j == 0) {
*p++ = spec->separator;
j = 3;
}
*p++ = '0' + number % 10; *p++ = '0' + number % 10;
} while (number /= 10); } while (number /= 10);
do *buff++ = *--p; while (p != buf); do *buff++ = *--p; while (p != buf);
@ -314,7 +375,7 @@ static char *FormatGenericCurrency(char *buff, const CurrencySpec *spec, int64 n
// add suffix part // add suffix part
s = spec->suffix; s = spec->suffix;
while (s != spec->suffix + lengthof(spec->suffix) && (c=*s++)) *buff++ = c; while (s != spec->suffix + lengthof(spec->suffix) && (c = *s++) != '\0') *buff++ = c;
return buff; return buff;
} }
@ -323,7 +384,7 @@ static char *DecodeString(char *buff, const char *str)
{ {
byte b; byte b;
while((b = *str++) != 0) { while ((b = *str++) != '\0') {
switch (b) { switch (b) {
case 0x1: // {SETX} case 0x1: // {SETX}
*buff++ = b; *buff++ = b;
@ -442,7 +503,7 @@ static char *DecodeString(char *buff, const char *str)
Station *st; Station *st;
InjectDParam(1); InjectDParam(1);
st = GetStation(GetDParam(1)); st = GetStation(GetDParam(1));
if (!st->xy) { // station doesn't exist anymore if (st->xy == 0) { // station doesn't exist anymore
buff = GetString(buff, STR_UNKNOWN_DESTINATION); buff = GetString(buff, STR_UNKNOWN_DESTINATION);
break; break;
} }
@ -500,7 +561,7 @@ static char *DecodeString(char *buff, const char *str)
*buff++ = b; *buff++ = b;
} }
} }
buff[0] = 0; *buff = '\0';
return buff; return buff;
} }
@ -508,21 +569,22 @@ static char *DecodeString(char *buff, const char *str)
static char *StationGetSpecialString(char *buff) static char *StationGetSpecialString(char *buff)
{ {
int x = GetParamInt8(); int x = GetParamInt8();
if (x & 1) *buff++ = 0xB4; if (x & 0x01) *buff++ = '\xB4';
if (x & 2) *buff++ = 0xB5; if (x & 0x02) *buff++ = '\xB5';
if (x & 4) *buff++ = 0xB6; if (x & 0x04) *buff++ = '\xB6';
if (x & 8) *buff++ = 0xB7; if (x & 0x08) *buff++ = '\xB7';
if (x & 16) *buff++ = 0xB8; if (x & 0x10) *buff++ = '\xB8';
*buff = 0; *buff = '\0';
return buff; return buff;
} }
static char *GetSpecialTownNameString(char *buff, int ind) { static char *GetSpecialTownNameString(char *buff, int ind)
{
uint32 x = GetParamInt32(); uint32 x = GetParamInt32();
_town_name_generators[ind](buff, x); _town_name_generators[ind](buff, x);
while (*buff != 0) buff++; while (*buff != '\0') buff++;
return buff; return buff;
} }
@ -666,7 +728,6 @@ static const char * const _song_names[] = {
static char *GetSpecialPlayerNameString(char *buff, int ind) static char *GetSpecialPlayerNameString(char *buff, int ind)
{ {
switch (ind) { switch (ind) {
// not used // not used
case 1: { case 1: {
int i = GetParamInt32() & 0xFFFF; int i = GetParamInt32() & 0xFFFF;
@ -710,7 +771,6 @@ static char *GetSpecialPlayerNameString(char *buff, int ind)
return str_cat(buff, GetScreenshotFormatDesc(i)); return str_cat(buff, GetScreenshotFormatDesc(i));
} }
assert(0); assert(0);
return NULL; return NULL;
} }
@ -729,7 +789,8 @@ StringID RemapOldStringID(StringID s)
return s; return s;
} }
bool ReadLanguagePack(int lang_index) { bool ReadLanguagePack(int lang_index)
{
int tot_count, i; int tot_count, i;
char *lang_pack; char *lang_pack;
size_t len; size_t len;
@ -772,8 +833,8 @@ bool ReadLanguagePack(int lang_index) {
s = lang_pack + sizeof(LanguagePackHeader); s = lang_pack + sizeof(LanguagePackHeader);
for (i = 0; i != tot_count; i++) { for (i = 0; i != tot_count; i++) {
len = (byte)*s; len = (byte)*s;
*s++ = 0; // zero terminate the string before. *s++ = '\0'; // zero terminate the string before.
if (len >= 0xC0) { len = ((len & 0x3F) << 8) + (byte)*s++; } if (len >= 0xC0) len = ((len & 0x3F) << 8) + (byte)*s++;
langpack_offs[i] = s; langpack_offs[i] = s;
s += len; s += len;
} }
@ -809,7 +870,7 @@ void InitializeLanguagePacks(void)
char *s = str_fmt("%s%s", _path.lang_dir, files[i]); char *s = str_fmt("%s%s", _path.lang_dir, files[i]);
in = fopen(s, "rb"); in = fopen(s, "rb");
free(s); free(s);
if (!in || if (in == NULL ||
(j = fread(&hdr, sizeof(hdr), 1, in), fclose(in), j) != 1 || (j = fread(&hdr, sizeof(hdr), 1, in), fclose(in), j) != 1 ||
hdr.ident != TO_LE32(LANGUAGE_PACK_IDENT) || hdr.ident != TO_LE32(LANGUAGE_PACK_IDENT) ||
hdr.version != TO_LE32(LANGUAGE_PACK_VERSION)) { hdr.version != TO_LE32(LANGUAGE_PACK_VERSION)) {
@ -820,7 +881,7 @@ void InitializeLanguagePacks(void)
dl->ent[m].file = files[i]; dl->ent[m].file = files[i];
dl->ent[m].name = strdup(hdr.name); dl->ent[m].name = strdup(hdr.name);
if (!strcmp(hdr.name, "English")) def = m; if (strcmp(hdr.name, "English") == 0) def = m;
m++; m++;
} }
@ -834,7 +895,7 @@ void InitializeLanguagePacks(void)
dl->dropdown[i] = INVALID_STRING_ID; dl->dropdown[i] = INVALID_STRING_ID;
for (i = 0; i != dl->num; i++) for (i = 0; i != dl->num; i++)
if (!strcmp(dl->ent[i].file, dl->curr_file)) { if (strcmp(dl->ent[i].file, dl->curr_file) == 0) {
def = i; def = i;
break; break;
} }