diff --git a/src/creport.c b/src/creport.c index dfcc94f63..278c96bd7 100644 --- a/src/creport.c +++ b/src/creport.c @@ -490,11 +490,12 @@ static void report_crtypes(FILE * F, const struct locale *lang) for (kmt = mtypehash[i]; kmt; kmt = kmt->nexthash) { const struct nrmessage_type *nrt = nrt_find(lang, kmt->mtype); if (nrt) { + char buffer[DISPLAYSIZE]; unsigned int hash = kmt->mtype->key; assert(hash > 0); fprintf(F, "MESSAGETYPE %u\n", hash); fputc('\"', F); - fputs(escape_string(nrt_string(nrt), NULL, 0), F); + fputs(escape_string(nrt_string(nrt), buffer, sizeof(buffer)), F); fputs("\";text\n", F); fprintf(F, "\"%s\";section\n", nrt_section(nrt)); } diff --git a/src/kernel/config.c b/src/kernel/config.c index b6fab5f59..90cf0765d 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -314,7 +314,7 @@ helpmode helpmodes[] = { */ const char *dbrace(const struct race *rc) { - static char zText[32]; + static char zText[32]; // FIXME: static return value char *zPtr = zText; /* the english names are all in ASCII, so we don't need to worry about UTF8 */ @@ -974,7 +974,7 @@ int modifier) #ifndef NDEBUG const char *strcheck(const char *s, size_t maxlen) { - static char buffer[16 * 1024]; + static char buffer[16 * 1024]; // FIXME: static return value if (strlen(s) > maxlen) { assert(maxlen < 16 * 1024); log_warning("[strcheck] String wurde auf %d Zeichen verkürzt:\n%s\n", (int)maxlen, s); @@ -1911,7 +1911,7 @@ int check_param(const struct param *p, const char *key, const char *searchvalue) static const char *g_datadir; const char *datapath(void) { - static char zText[MAX_PATH]; + static char zText[MAX_PATH]; // FIXME: static return value if (g_datadir) return g_datadir; return strcat(strcpy(zText, basepath()), "/data"); @@ -1925,7 +1925,7 @@ void set_datapath(const char *path) static const char *g_reportdir; const char *reportpath(void) { - static char zText[MAX_PATH]; + static char zText[MAX_PATH]; // FIXME: static return value if (g_reportdir) return g_reportdir; return strcat(strcpy(zText, basepath()), "/reports"); diff --git a/src/kernel/names.c b/src/kernel/names.c index 02a2216a5..cba36539c 100644 --- a/src/kernel/names.c +++ b/src/kernel/names.c @@ -54,7 +54,7 @@ static const char *make_names(const char *monster, int *num_postfix, int pprefix, int *num_name, int *num_prefix, int ppostfix) { int uv, uu, un; - static char name[NAMESIZE + 1]; + static char name[NAMESIZE + 1]; // FIXME: static return value char zText[32]; const char *str; @@ -222,7 +222,7 @@ static const char *generic_name(const unit * u) static const char *dragon_name(const unit * u) { - static char name[NAMESIZE + 1]; + static char name[NAMESIZE + 1]; // FIXME: static return value int rnd, ter = 0; int anzahl = 1; static int num_postfix; @@ -355,7 +355,7 @@ static const char *drac_suf[DRAC_SUF] = { static const char *dracoid_name(const unit * u) { - static char name[NAMESIZE + 1]; + static char name[NAMESIZE + 1]; // FIXME: static return value int mid_syllabels; u = u; diff --git a/src/kernel/race.c b/src/kernel/race.c index c6325a9bc..e424e5915 100644 --- a/src/kernel/race.c +++ b/src/kernel/race.c @@ -1,7 +1,7 @@ /* Copyright (c) 1998-2010, Enno Rehling - Katja Zedel +Katja Zedel Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -93,7 +93,8 @@ struct race *get_race(race_t rt) { cache = cache_breaker; memset(race_cache, 0, sizeof(race_cache)); return race_cache[rt] = rc_get_or_create(name); - } else { + } + else { result = race_cache[rt]; if (!result) { result = race_cache[rt] = rc_get_or_create(name); @@ -104,57 +105,57 @@ struct race *get_race(race_t rt) { race_list *get_familiarraces(void) { - static int init = 0; - static race_list *familiarraces; + static int init = 0; + static race_list *familiarraces; - if (!init) { - race *rc = races; - for (; rc != NULL; rc = rc->next) { - if (rc->init_familiar != NULL) { - racelist_insert(&familiarraces, rc); - } + if (!init) { + race *rc = races; + for (; rc != NULL; rc = rc->next) { + if (rc->init_familiar != NULL) { + racelist_insert(&familiarraces, rc); + } + } + init = false; } - init = false; - } - return familiarraces; + return familiarraces; } void racelist_clear(struct race_list **rl) { - while (*rl) { - race_list *rl2 = (*rl)->next; - free(*rl); - *rl = rl2; - } + while (*rl) { + race_list *rl2 = (*rl)->next; + free(*rl); + *rl = rl2; + } } void racelist_insert(struct race_list **rl, const struct race *r) { - race_list *rl2 = (race_list *) malloc(sizeof(race_list)); + race_list *rl2 = (race_list *)malloc(sizeof(race_list)); - rl2->data = r; - rl2->next = *rl; + rl2->data = r; + rl2->next = *rl; - *rl = rl2; + *rl = rl2; } void free_races(void) { while (races) { race * rc = races->next; free(races); - races =rc; + races = rc; } } static race *rc_find_i(const char *name) { - const char *rname = name; - race *rc = races; + const char *rname = name; + race *rc = races; - while (rc && !strcmp(rname, rc->_name) == 0) { - rc = rc->next; - } - return rc; + while (rc && !strcmp(rname, rc->_name) == 0) { + rc = rc->next; + } + return rc; } const race * rc_find(const char *name) { @@ -168,7 +169,7 @@ race *rc_get_or_create(const char *zName) assert(zName); rc = rc_find_i(zName); if (!rc) { - char zBuffer[80]; + char zBuffer[80]; rc = (race *)calloc(sizeof(race), 1); rc->hitpoints = 1; @@ -193,30 +194,30 @@ race *rc_get_or_create(const char *zName) /** dragon movement **/ bool allowed_dragon(const region * src, const region * target) { - if (fval(src->terrain, ARCTIC_REGION) && fval(target->terrain, SEA_REGION)) - return false; - return allowed_fly(src, target); + if (fval(src->terrain, ARCTIC_REGION) && fval(target->terrain, SEA_REGION)) + return false; + return allowed_fly(src, target); } char **race_prefixes = NULL; extern void add_raceprefix(const char *prefix) { - static size_t size = 4; - static unsigned int next = 0; - if (race_prefixes == NULL) - race_prefixes = malloc(size * sizeof(char *)); - if (next + 1 == size) { - size *= 2; - race_prefixes = realloc(race_prefixes, size * sizeof(char *)); - } - race_prefixes[next++] = _strdup(prefix); - race_prefixes[next] = NULL; + static size_t size = 4; + static unsigned int next = 0; + if (race_prefixes == NULL) + race_prefixes = malloc(size * sizeof(char *)); + if (next + 1 == size) { + size *= 2; + race_prefixes = realloc(race_prefixes, size * sizeof(char *)); + } + race_prefixes[next++] = _strdup(prefix); + race_prefixes[next] = NULL; } bool r_insectstalled(const region * r) { - return fval(r->terrain, ARCTIC_REGION); + return fval(r->terrain, ARCTIC_REGION); } const char *rc_name(const race * rc, name_t n) @@ -233,7 +234,7 @@ const char *rc_name(const race * rc, name_t n) default: assert(!"invalid name_t enum in rc_name"); } if (postfix) { - static char name[64]; // FIXME: static variable return + static char name[64]; // FIXME: static return value sprintf(name, "race::%s%s", rc->_name, postfix); return name; } @@ -242,97 +243,98 @@ const char *rc_name(const race * rc, name_t n) const char *raceprefix(const unit * u) { - const attrib *asource = u->faction->attribs; + const attrib *asource = u->faction->attribs; - if (fval(u, UFL_GROUP)) { - const attrib *agroup = agroup = a_findc(u->attribs, &at_group); - if (agroup != NULL) - asource = ((const group *)(agroup->data.v))->attribs; - } - return get_prefix(asource); + if (fval(u, UFL_GROUP)) { + const attrib *agroup = agroup = a_findc(u->attribs, &at_group); + if (agroup != NULL) + asource = ((const group *)(agroup->data.v))->attribs; + } + return get_prefix(asource); } const char *racename(const struct locale *loc, const unit * u, const race * rc) { - const char *str, *prefix = raceprefix(u); + const char *str, *prefix = raceprefix(u); - if (prefix != NULL) { - static char lbuf[80]; - char *bufp = lbuf; - size_t size = sizeof(lbuf) - 1; - int ch, bytes; + if (prefix != NULL) { + static char lbuf[80]; // FIXME: static return value + char *bufp = lbuf; + size_t size = sizeof(lbuf) - 1; + int ch, bytes; - bytes = (int)strlcpy(bufp, LOC(loc, mkname("prefix", prefix)), size); - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); + bytes = (int)strlcpy(bufp, LOC(loc, mkname("prefix", prefix)), size); + if (wrptr(&bufp, &size, bytes) != 0) + WARN_STATIC_BUFFER(); - bytes = (int)strlcpy(bufp, LOC(loc, rc_name(rc, u->number != 1)), size); - assert(~bufp[0] & 0x80 || !"unicode/not implemented"); - ch = tolower(*(unsigned char *)bufp); - bufp[0] = (char)ch; - if (wrptr(&bufp, &size, bytes) != 0) - WARN_STATIC_BUFFER(); - *bufp = 0; + bytes = (int)strlcpy(bufp, LOC(loc, rc_name(rc, u->number != 1)), size); + assert(~bufp[0] & 0x80 || !"unicode/not implemented"); + ch = tolower(*(unsigned char *)bufp); + bufp[0] = (char)ch; + if (wrptr(&bufp, &size, bytes) != 0) + WARN_STATIC_BUFFER(); + *bufp = 0; - return lbuf; - } - str = LOC(loc, rc_name(rc, (u->number == 1) ? NAME_SINGULAR : NAME_PLURAL)); - return str ? str : rc->_name; + return lbuf; + } + str = LOC(loc, rc_name(rc, (u->number == 1) ? NAME_SINGULAR : NAME_PLURAL)); + return str ? str : rc->_name; } int rc_specialdamage(const race * ar, const race * dr, - const struct weapon_type *wtype) +const struct weapon_type *wtype) { - race_t art = old_race(ar); - int m, modifier = 0; + race_t art = old_race(ar); + int m, modifier = 0; - if (wtype != NULL && wtype->modifiers != NULL) - for (m = 0; wtype->modifiers[m].value; ++m) { - /* weapon damage for this weapon, possibly by race */ - if (wtype->modifiers[m].flags & WMF_DAMAGE) { - race_list *rlist = wtype->modifiers[m].races; - if (rlist != NULL) { - while (rlist) { - if (rlist->data == ar) - break; - rlist = rlist->next; - } - if (rlist == NULL) - continue; + if (wtype != NULL && wtype->modifiers != NULL) + for (m = 0; wtype->modifiers[m].value; ++m) { + /* weapon damage for this weapon, possibly by race */ + if (wtype->modifiers[m].flags & WMF_DAMAGE) { + race_list *rlist = wtype->modifiers[m].races; + if (rlist != NULL) { + while (rlist) { + if (rlist->data == ar) + break; + rlist = rlist->next; + } + if (rlist == NULL) + continue; + } + modifier += wtype->modifiers[m].value; } - modifier += wtype->modifiers[m].value; - } - } - switch (art) { + } + switch (art) { case RC_HALFLING: - if (wtype != NULL && dragonrace(dr)) { - modifier += 5; - } - break; + if (wtype != NULL && dragonrace(dr)) { + modifier += 5; + } + break; default: - break; - } - return modifier; + break; + } + return modifier; } void write_race_reference(const race * rc, struct storage *store) { - WRITE_TOK(store, rc ? rc->_name : "none"); + WRITE_TOK(store, rc ? rc->_name : "none"); } variant read_race_reference(struct storage *store) { - variant result; - char zName[20]; - READ_TOK(store, zName, sizeof(zName)); + variant result; + char zName[20]; + READ_TOK(store, zName, sizeof(zName)); - if (strcmp(zName, "none") == 0) { - result.v = NULL; + if (strcmp(zName, "none") == 0) { + result.v = NULL; + return result; + } + else { + result.v = rc_find_i(zName); + } + assert(result.v != NULL); return result; - } else { - result.v = rc_find_i(zName); - } - assert(result.v != NULL); - return result; } diff --git a/src/kernel/reports.c b/src/kernel/reports.c index 68283a1f6..5abd71394 100644 --- a/src/kernel/reports.c +++ b/src/kernel/reports.c @@ -107,7 +107,7 @@ const char *combatstatus[] = { const char *report_kampfstatus(const unit * u, const struct locale *lang) { - static char fsbuf[64]; + static char fsbuf[64]; // FIXME: static return value strlcpy(fsbuf, LOC(lang, combatstatus[u->status]), sizeof(fsbuf)); if (fval(u, UFL_NOAID)) { @@ -1928,7 +1928,7 @@ f_regionid(const region * r, const faction * f, char *buffer, size_t size) static char *f_regionid_s(const region * r, const faction * f) { static int i = 0; - static char bufs[4][NAMESIZE + 20]; + static char bufs[4][NAMESIZE + 20]; // FIXME: static return value char *buf = bufs[(++i) % 4]; f_regionid(r, f, buf, NAMESIZE + 20); @@ -2177,7 +2177,7 @@ static void eval_race(struct opstack **stack, const void *userdata) static void eval_order(struct opstack **stack, const void *userdata) { /* order -> string */ const struct order *ord = (const struct order *)opop(stack).v; - static char buf[512]; + char buf[512]; size_t len; variant var; @@ -2193,7 +2193,7 @@ static void eval_resources(struct opstack **stack, const void *userdata) const faction *report = (const faction *)userdata; const struct locale *lang = report ? report->locale : default_locale; const struct resource *res = (const struct resource *)opop(stack).v; - static char buf[1024]; /* but we only use about half of this */ + char buf[1024]; /* but we only use about half of this */ size_t size = sizeof(buf) - 1; variant var; @@ -2225,7 +2225,7 @@ static void eval_regions(struct opstack **stack, const void *userdata) int i = opop(stack).i; int end, begin = opop(stack).i; const arg_regions *regions = (const arg_regions *)opop(stack).v; - static char buf[256]; + char buf[256]; size_t size = sizeof(buf) - 1; variant var; char *bufp = buf; @@ -2262,7 +2262,7 @@ static void eval_trail(struct opstack **stack, const void *userdata) const struct locale *lang = report ? report->locale : default_locale; int i, end = 0, begin = 0; const arg_regions *regions = (const arg_regions *)opop(stack).v; - static char buf[512]; + char buf[512]; size_t size = sizeof(buf) - 1; variant var; char *bufp = buf; diff --git a/src/kernel/save.c b/src/kernel/save.c index 092a00d12..0cac1954b 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -241,7 +241,7 @@ static faction *factionorders(void) } else { - log_warning("orders for invalid faction %s\n", itoa36(fid)); + log_debug("orders for invalid faction %s\n", itoa36(fid)); } return f; } diff --git a/src/keyword.c b/src/keyword.c index 026a765be..23f55a609 100644 --- a/src/keyword.c +++ b/src/keyword.c @@ -12,7 +12,7 @@ const char * keyword(keyword_t kwd) { - static char result[KEYWORDSIZE]; + static char result[KEYWORDSIZE]; // FIXME: static return value if (!result[0]) { strcpy(result, "keyword::"); } diff --git a/src/report.c b/src/report.c index 03de0e889..96a29cbf9 100644 --- a/src/report.c +++ b/src/report.c @@ -100,7 +100,7 @@ extern int months_per_year; static char *gamedate_season(const struct locale *lang) { - static char buf[256]; + static char buf[256]; // FIXME: static return value gamedate gd; get_gamedate(turn, &gd); @@ -517,7 +517,7 @@ void sparagraph(strlist ** SP, const char *s, int indent, char mark) int i, j, width; int firstline; - static char buf[REPORTWIDTH + 1]; + static char buf[REPORTWIDTH + 1]; // FIXME: static return value width = REPORTWIDTH - indent; firstline = 1; diff --git a/src/test_eressea.c b/src/test_eressea.c index 85a00bf64..7a1e83d48 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -34,6 +34,7 @@ int RunAllTests(void) ADD_TESTS(suite, bsdstring); ADD_TESTS(suite, functions); ADD_TESTS(suite, umlaut); + ADD_TESTS(suite, strings); /* kernel */ ADD_TESTS(suite, faction); ADD_TESTS(suite, build); diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 6a8afb09b..c4b51250c 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -2,6 +2,7 @@ project(util C) SET(_TEST_FILES base36.test.c +strings.test.c bsdstring.test.c functions.test.c umlaut.test.c diff --git a/src/util/base36.c b/src/util/base36.c index 5b867a9d7..8407c3fc4 100644 --- a/src/util/base36.c +++ b/src/util/base36.c @@ -56,7 +56,7 @@ int atoi36(const char *str) const char *itoab(int i, int base) { - static char **as = NULL; /* STATIC_RETURN: used for return, not across calls */ + static char **as = NULL; // FIXME: static return value char *s, *dst; static int index = 0; /* STATIC_XCALL: used across calls */ int neg = 0; diff --git a/src/util/language.c b/src/util/language.c index c55e1c838..6c7afdba4 100644 --- a/src/util/language.c +++ b/src/util/language.c @@ -1,7 +1,7 @@ /* Copyright (c) 1998-2010, Enno Rehling - Katja Zedel +Katja Zedel Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -36,17 +36,17 @@ locale *locales; unsigned int locale_index(const locale * lang) { - assert(lang); - return lang->index; + assert(lang); + return lang->index; } locale *get_locale(const char *name) { - unsigned int hkey = hashstring(name); - locale *l = locales; - while (l && l->hashkey != hkey) - l = l->next; - return l; + unsigned int hkey = hashstring(name); + locale *l = locales; + while (l && l->hashkey != hkey) + l = l->next; + return l; } static unsigned int nextlocaleindex = 0; @@ -59,7 +59,8 @@ locale *get_or_create_locale(const char *name) if (!locales) { nextlocaleindex = 0; - } else { + } + else { while (*lp && (*lp)->hashkey != hkey) lp = &(*lp)->next; if (*lp) { return *lp; @@ -81,172 +82,174 @@ locale *get_or_create_locale(const char *name) */ void make_locales(const char *str) { - const char *tok = str; - while (*tok) { - char zText[32]; - while (*tok && *tok != ',') - ++tok; - strncpy(zText, str, tok - str); - zText[tok - str] = 0; - get_or_create_locale(zText); - if (*tok) { - str = ++tok; + const char *tok = str; + while (*tok) { + char zText[32]; + while (*tok && *tok != ',') + ++tok; + strncpy(zText, str, tok - str); + zText[tok - str] = 0; + get_or_create_locale(zText); + if (*tok) { + str = ++tok; + } } - } } const char *locale_getstring(const locale * lang, const char *key) { - unsigned int hkey = hashstring(key); - unsigned int id = hkey & (SMAXHASH - 1); - const struct locale_str *find; + unsigned int hkey = hashstring(key); + unsigned int id = hkey & (SMAXHASH - 1); + const struct locale_str *find; - assert(lang); - if (key == NULL || *key == 0) - return NULL; - find = lang->strings[id]; - while (find) { - if (find->hashkey == hkey) { - if (find->nexthash == NULL) { - /* if this is the only entry with this hash, fine. */ - assert(strcmp(key, find->key) == 0); - return find->str; - } - if (strcmp(key, find->key) == 0) { - return find->str; - } + assert(lang); + if (key == NULL || *key == 0) + return NULL; + find = lang->strings[id]; + while (find) { + if (find->hashkey == hkey) { + if (find->nexthash == NULL) { + /* if this is the only entry with this hash, fine. */ + assert(strcmp(key, find->key) == 0); + return find->str; + } + if (strcmp(key, find->key) == 0) { + return find->str; + } + } + find = find->nexthash; } - find = find->nexthash; - } - return NULL; + return NULL; } const char *locale_string(const locale * lang, const char *key) { - assert(lang); - assert(key); + assert(lang); + assert(key); - if (key != NULL) { - unsigned int hkey = hashstring(key); - unsigned int id = hkey & (SMAXHASH - 1); - struct locale_str *find; + if (key != NULL) { + unsigned int hkey = hashstring(key); + unsigned int id = hkey & (SMAXHASH - 1); + struct locale_str *find; - if (*key == 0) return 0; - find = lang->strings[id]; - while (find) { - if (find->hashkey == hkey) { - if (!find->nexthash) { - /* if this is the only entry with this hash, fine. */ - assert(strcmp(key, find->key) == 0); - break; + if (*key == 0) return 0; + find = lang->strings[id]; + while (find) { + if (find->hashkey == hkey) { + if (!find->nexthash) { + /* if this is the only entry with this hash, fine. */ + assert(strcmp(key, find->key) == 0); + break; + } + if (strcmp(key, find->key) == 0) + break; + } + find = find->nexthash; } - if (strcmp(key, find->key) == 0) - break; - } - find = find->nexthash; + if (!find) { + log_warning("missing translation for \"%s\" in locale %s\n", key, lang->name); + if (lang->fallback) { + return locale_string(lang->fallback, key); + } + return 0; + } + return find->str; } - if (!find) { - log_warning("missing translation for \"%s\" in locale %s\n", key, lang->name); - if (lang->fallback) { - return locale_string(lang->fallback, key); - } - return 0; - } - return find->str; - } - return 0; + return 0; } void locale_setstring(locale * lang, const char *key, const char *value) { - unsigned int hkey = hashstring(key); - unsigned int id = hkey & (SMAXHASH - 1); - struct locale_str *find; - if (!lang) { - lang = default_locale; - } - assert(lang); - find = lang->strings[id]; - while (find) { - if (find->hashkey == hkey && strcmp(key, find->key) == 0) - break; - find = find->nexthash; - } - if (!find) { - find = calloc(1, sizeof(struct locale_str)); - find->nexthash = lang->strings[id]; - lang->strings[id] = find; - find->hashkey = hkey; - find->key = _strdup(key); - find->str = _strdup(value); - } else { - if (strcmp(find->str, value) != 0) { - log_warning("multiple translations for key %s\n", key); + unsigned int hkey = hashstring(key); + unsigned int id = hkey & (SMAXHASH - 1); + struct locale_str *find; + if (!lang) { + lang = default_locale; + } + assert(lang); + find = lang->strings[id]; + while (find) { + if (find->hashkey == hkey && strcmp(key, find->key) == 0) + break; + find = find->nexthash; + } + if (!find) { + find = calloc(1, sizeof(struct locale_str)); + find->nexthash = lang->strings[id]; + lang->strings[id] = find; + find->hashkey = hkey; + find->key = _strdup(key); + find->str = _strdup(value); + } + else { + if (strcmp(find->str, value) != 0) { + log_warning("multiple translations for key %s\n", key); + } + free(find->str); + find->str = _strdup(value); } - free(find->str); - find->str = _strdup(value); - } } const char *locale_name(const locale * lang) { - return lang ? lang->name : "(null)"; + return lang ? lang->name : "(null)"; } char *mkname_buf(const char *space, const char *name, char *buffer) { - if (space && *space) { - sprintf(buffer, "%s::%s", space, name); - } else { - strcpy(buffer, name); - } - return buffer; + if (space && *space) { + sprintf(buffer, "%s::%s", space, name); + } + else { + strcpy(buffer, name); + } + return buffer; } const char *mkname(const char *space, const char *name) { - static char zBuffer[128]; /* STATIC_RESULT: used for return, not across calls */ - return mkname_buf(space, name, zBuffer); + static char zBuffer[128]; // FIXME: static return value + return mkname_buf(space, name, zBuffer); } locale *nextlocale(const struct locale * lang) { - return lang->next; + return lang->next; } typedef struct lstr { - void * tokens[UT_MAX]; + void * tokens[UT_MAX]; } lstr; static lstr lstrs[MAXLOCALES]; void ** get_translations(const struct locale *lang, int index) { - assert(lang); - assert(lang->index < MAXLOCALES - || "you have to increase MAXLOCALES and recompile"); - if (lang->index < MAXLOCALES) { - return lstrs[lang->index].tokens + index; - } - return lstrs[0].tokens + index; + assert(lang); + assert(lang->index < MAXLOCALES + || "you have to increase MAXLOCALES and recompile"); + if (lang->index < MAXLOCALES) { + return lstrs[lang->index].tokens + index; + } + return lstrs[0].tokens + index; } void free_locales(void) { - while (locales) { - int i; - locale * next = locales->next; + while (locales) { + int i; + locale * next = locales->next; - for (i=0; i!=SMAXHASH; ++i) { - while (locales->strings[i]) { - struct locale_str * strings = locales->strings[i]; - free(strings->key); - free(strings->str); - locales->strings[i] = strings->nexthash; - free(strings); - } + for (i = 0; i != SMAXHASH; ++i) { + while (locales->strings[i]) { + struct locale_str * strings = locales->strings[i]; + free(strings->key); + free(strings->str); + locales->strings[i] = strings->nexthash; + free(strings); + } + } + free(locales); + locales = next; } - free(locales); - locales = next; - } } \ No newline at end of file diff --git a/src/util/strings.c b/src/util/strings.c index a50199ddf..a2e137042 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -1,7 +1,7 @@ /* Copyright (c) 1998-2010, Enno Rehling - Katja Zedel +Katja Zedel Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -20,73 +20,72 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. /* libc includes */ #include +#include unsigned int hashstring(const char *s) { - unsigned int key = 0; - while (*s) { - key = key * 37 + *s++; - } - return key & 0x7FFFFFFF; + unsigned int key = 0; + while (*s) { + key = key * 37 + *s++; + } + return key & 0x7FFFFFFF; } const char *escape_string(const char *str, char *buffer, - unsigned int len) + unsigned int len) { - const char *start = strchr(str, '\"'); - if (start) { - static char s_buffer[4096]; /* STATIC_RESULT: used for return, not across calls */ - const char *p; - char *o; - size_t skip = start - str; + const char *start = strchr(str, '\"'); + if (!start) start = strchr(str, '\\'); + assert(buffer); + if (start) { + const char *p; + char *o; + size_t skip = start - str; - if (buffer == NULL) { - buffer = s_buffer; - len = sizeof(s_buffer); + memcpy(buffer, str, skip); + o = buffer + skip; + p = str + skip; + do { + if (*p == '\"' || *p == '\\') { + if (len < 2) { + *o = '\0'; + break; + } + (*o++) = '\\'; + len -= 2; + } + else { + if (len < 1) { + *o = '\0'; + break; + } + --len; + } + (*o++) = (*p); + } while (*p++); + return buffer; } - memcpy(buffer, str, skip); - o = buffer + skip; - p = str + skip; - do { - if (*p == '\"' || *p == '\\') { - if (len < 2) { - *o = '\0'; - break; - } - (*o++) = '\\'; - len -= 2; - } else { - if (len < 1) { - *o = '\0'; - break; - } - --len; - } - (*o++) = (*p); - } while (*p++); - return buffer; - } - return str; + return str; } unsigned int jenkins_hash(unsigned int a) { - a = (a + 0x7ed55d16) + (a << 12); - a = (a ^ 0xc761c23c) ^ (a >> 19); - a = (a + 0x165667b1) + (a << 5); - a = (a + 0xd3a2646c) ^ (a << 9); - a = (a + 0xfd7046c5) + (a << 3); - a = (a ^ 0xb55a4f09) ^ (a >> 16); - return a; + a = (a + 0x7ed55d16) + (a << 12); + a = (a ^ 0xc761c23c) ^ (a >> 19); + a = (a + 0x165667b1) + (a << 5); + a = (a + 0xd3a2646c) ^ (a << 9); + a = (a + 0xfd7046c5) + (a << 3); + a = (a ^ 0xb55a4f09) ^ (a >> 16); + return a; } unsigned int wang_hash(unsigned int a) { - a = ~a + (a << 15); /* a = (a << 15) - a - 1; */ - a = a ^ (a >> 12); - a = a + (a << 2); - a = a ^ (a >> 4); - a = a * 2057; /* a = (a + (a << 3)) + (a << 11); */ - a = a ^ (a >> 16); - return a; + a = ~a + (a << 15); /* a = (a << 15) - a - 1; */ + a = a ^ (a >> 12); + a = a + (a << 2); + a = a ^ (a >> 4); + a = a * 2057; /* a = (a + (a << 3)) + (a << 11); */ + a = a ^ (a >> 16); + return a; } diff --git a/src/util/strings.test.c b/src/util/strings.test.c new file mode 100644 index 000000000..ada78d206 --- /dev/null +++ b/src/util/strings.test.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include +#include "goodies.h" + +static void test_escape_string(CuTest * tc) +{ + char scratch[64]; + CuAssertStrEquals(tc, "hello world", escape_string("hello world", scratch, sizeof(scratch))); + CuAssertStrEquals(tc, "hello \\\"world\\\"", escape_string("hello \"world\"", scratch, sizeof(scratch))); + CuAssertStrEquals(tc, "\\\"\\\\", escape_string("\"\\", scratch, sizeof(scratch))); + CuAssertStrEquals(tc, "\\\\", escape_string("\\", scratch, sizeof(scratch))); +} + +CuSuite *get_strings_suite(void) +{ + CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_escape_string); + return suite; +} diff --git a/src/util/xml.c b/src/util/xml.c index 6b7fc3e67..7150641fd 100644 --- a/src/util/xml.c +++ b/src/util/xml.c @@ -30,7 +30,7 @@ #ifdef USE_LIBXML2 const xmlChar *xml_i(double number) { - static char buffer[128]; + static char buffer[128]; // FIXME: static return value _snprintf(buffer, sizeof(buffer), "%.0f", number); return (const xmlChar *)buffer; }