Esempio n. 1
0
static void test_findrace(CuTest *tc) {
    const char * data = "{\"races\": { \"dwarf\": {} }, \"strings\": { \"de\" : { \"race::dwarf\" : \"Zwerg\" } } }";
    cJSON *json = cJSON_Parse(data);
    const struct locale *lang;
    const race *rc;

    CuAssertPtrNotNull(tc, json);
    test_cleanup();
    lang = get_or_create_locale("de");
    CuAssertPtrEquals(tc, 0, (void *)findrace("Zwerg", lang));

    json_config(json);
    rc = findrace("Zwerg", lang);
    CuAssertPtrNotNull(tc, rc);
    CuAssertStrEquals(tc, "dwarf", rc->_name);
}
Esempio n. 2
0
newfaction *read_newfactions(const char *filename)
{
    newfaction *newfactions = NULL;
    FILE *F = fopen(filename, "r");
    char buf[1024];

    if (F == NULL)
        return NULL;
    for (;;) {
        faction *f;
        char race[20], email[64], lang[8], password[16];
        newfaction *nf, **nfi;
        int bonus = 0, subscription = 0;
        int alliance = 0;

        if (fgets(buf, sizeof(buf), F) == NULL)
            break;

        email[0] = '\0';
        password[0] = '\0';

        if (sscanf(buf, "%54s %20s %8s %d %d %16s %d", email, race, lang, &bonus,
            &subscription, password, &alliance) < 3)
            break;
        if (email[0] == '\0')
            break;
        if (password[0] == '\0') {
            size_t sz;
            sz = strlcpy(password, itoa36(rng_int()), sizeof(password));
            sz += strlcat(password, itoa36(rng_int()), sizeof(password));
        }
        for (f = factions; f; f = f->next) {
            if (strcmp(f->email, email) == 0 && f->subscription
                && f->age < MINAGE_MULTI)
                break;
        }
        if (f && f->units)
            continue;                 /* skip the ones we've already got */
        for (nf = newfactions; nf; nf = nf->next) {
            if (strcmp(nf->email, email) == 0)
                break;
        }
        if (nf)
            continue;
        nf = calloc(sizeof(newfaction), 1);
        if (set_email(&nf->email, email) != 0) {
            log_error("Invalid email address for subscription %s: %s\n", itoa36(subscription), email);
            continue;
        }
        nf->password = _strdup(password);
        nf->race = rc_find(race);
        nf->subscription = subscription;
        if (alliances != NULL) {
            struct alliance *al = findalliance(alliance);
            if (al == NULL) {
                char zText[64];
                sprintf(zText, "Allianz %d", alliance);
                al = makealliance(alliance, zText);
            }
            nf->allies = al;
        }
        else {
            nf->allies = NULL;
        }
        if (nf->race == NULL) {
            /* if the script didn't supply the race as a token, then it gives us a
             * race in the default locale (which means that itis a UTF8 string) */
            nf->race = findrace(race, default_locale);
            if (nf->race == NULL) {
                char buffer[32];
                size_t outbytes = sizeof(buffer) - 1;
                size_t inbytes = strlen(race);
                unicode_latin1_to_utf8(buffer, &outbytes, race, &inbytes);
                buffer[outbytes] = 0;
                nf->race = findrace(buffer, default_locale);
                if (nf->race == NULL) {
                    log_error("new faction has unknown race '%s'.\n", race);
                    free(nf);
                    continue;
                }
            }
        }
        nf->lang = get_locale(lang);
        nf->bonus = bonus;
        assert(nf->race && nf->email && nf->lang);
        nfi = &newfactions;
        while (*nfi) {
            if ((*nfi)->race == nf->race)
                break;
            nfi = &(*nfi)->next;
        }
        nf->next = *nfi;
        *nfi = nf;
    }
    fclose(F);
    return newfactions;
}