static int fillTeamFromMsg(flib_team *team, char **parts) { team->name = flib_strdupnull(parts[0]); team->grave = flib_strdupnull(parts[1]); team->fort = flib_strdupnull(parts[2]); team->voicepack = flib_strdupnull(parts[3]); team->flag = flib_strdupnull(parts[4]); team->ownerName = flib_strdupnull(parts[5]); if(!team->name || !team->grave || !team->fort || !team->voicepack || !team->flag || !team->ownerName) { return -1; } errno = 0; long difficulty = strtol(parts[6], NULL, 10); if(errno) { return -1; } for(int i=0; i<HEDGEHOGS_PER_TEAM; i++) { flib_hog *hog = &team->hogs[i]; hog->difficulty = difficulty; hog->name = flib_strdupnull(parts[7+2*i]); hog->hat = flib_strdupnull(parts[8+2*i]); if(!hog->name || !hog->hat) { return -1; } } // Set some default assumptions as well team->colorIndex = DEFAULT_COLOR_INDEX; team->hogsInGame = DEFAULT_HEDGEHOG_COUNT; team->remoteDriven = true; return 0; }
static char *strdupWithError(const char *in, bool *error) { char *out = flib_strdupnull(in); if(in && !out) { *error = true; } return out; }
flib_room *flib_room_from_netmsg(char **params) { flib_room *result = NULL; flib_room *tmpRoom = flib_calloc(1, sizeof(flib_room)); if(tmpRoom) { tmpRoom->inProgress = !strcmp(params[0], "True"); tmpRoom->name = flib_strdupnull(params[1]); tmpRoom->playerCount = atoi(params[2]); tmpRoom->teamCount = atoi(params[3]); tmpRoom->owner = flib_strdupnull(params[4]); tmpRoom->map = flib_strdupnull(params[5]); tmpRoom->scheme = flib_strdupnull(params[6]); tmpRoom->weapons = flib_strdupnull(params[7]); if(tmpRoom->name && tmpRoom->owner && tmpRoom->map && tmpRoom->scheme && tmpRoom->weapons) { result = tmpRoom; tmpRoom = NULL; } } flib_room_destroy(tmpRoom); return result; }
char *flib_ini_get_keyname(flib_ini *ini, int number) { char *result = NULL; if(!log_badargs_if3(ini==NULL, ini->currentSection==NULL, number<0)) { int keyCount = iniparser_getsecnkeys(ini->inidict, ini->currentSection); char **keys = iniparser_getseckeys(ini->inidict, ini->currentSection); if(keys && keyCount>number) { // The keys are in the format section:key, so we have to skip the section and colon. result = flib_strdupnull(keys[number]+strlen(ini->currentSection)+1); } free(keys); } return result; }
flib_map *flib_map_copy(const flib_map *map) { flib_map *result = NULL; if(map) { flib_map *newmap = flib_calloc(1, sizeof(flib_map)); if(newmap) { newmap->mapgen = map->mapgen; newmap->drawDataSize = map->drawDataSize; newmap->drawData = flib_bufdupnull(map->drawData, map->drawDataSize); newmap->mazeSize = map->mazeSize; newmap->name = flib_strdupnull(map->name); newmap->seed = flib_strdupnull(map->seed); newmap->templateFilter = map->templateFilter; newmap->theme = flib_strdupnull(map->theme); if((newmap->drawData || !map->drawData) && (newmap->name || !map->name) && (newmap->seed || !map->seed) && (newmap->theme || !map->theme)) { result = newmap; newmap = NULL; } } flib_map_destroy(newmap); } return result; }
int flib_ini_get_str_opt(flib_ini *ini, char **outVar, const char *key, const char *def) { int result = INI_ERROR_OTHER; if(!log_badargs_if4(ini==NULL, ini->currentSection==NULL, outVar==NULL, key==NULL)) { const char *value = findValue(ini->inidict, ini->currentSection, key); if(!value) { value = def; } char *valueDup = flib_strdupnull(value); if(valueDup || !def) { *outVar = valueDup; result = 0; } } return result; }
flib_weaponset *flib_weaponset_create(const char *name) { flib_weaponset *result = NULL; if(!log_badargs_if(name==NULL)) { flib_weaponset *newSet = flib_calloc(1, sizeof(flib_weaponset)); if(newSet) { newSet->name = flib_strdupnull(name); if(newSet->name) { setField(newSet->loadout, "", 0, false); setField(newSet->crateprob, "", 0, false); setField(newSet->crateammo, "", 0, false); setField(newSet->delay, "", 0, false); result = newSet; newSet = NULL; } } flib_weaponset_destroy(newSet); } return result; }
flib_scheme *flib_scheme_create(const char *schemeName) { flib_scheme *result = flib_calloc(1, sizeof(flib_scheme)); if(log_badargs_if(schemeName==NULL) || result==NULL) { return NULL; } result->name = flib_strdupnull(schemeName); result->mods = flib_calloc(flib_meta.modCount, sizeof(*result->mods)); result->settings = flib_calloc(flib_meta.settingCount, sizeof(*result->settings)); if(!result->mods || !result->settings || !result->name) { flib_scheme_destroy(result); return NULL; } for(int i=0; i<flib_meta.settingCount; i++) { result->settings[i] = flib_meta.settings[i].def; } return result; }
int flib_ini_enter_section(flib_ini *ini, const char *section) { int result = INI_ERROR_OTHER; if(ini) { free(ini->currentSection); ini->currentSection = NULL; } if(!log_badargs_if2(ini==NULL, section==NULL)) { if(!iniparser_find_entry(ini->inidict, section)) { flib_log_d("Ini section %s not found", section); result = INI_ERROR_NOTFOUND; } else { ini->currentSection = flib_strdupnull(section); if(ini->currentSection) { // Usually iniparser ignores case, but some section-handling functions don't, // so we set it to lowercase manually strToLower(ini->currentSection); result = 0; } } } return result; }
int flib_ipc_append_style(flib_vector *vec, const char *style) { int result = -1; char *copy = flib_strdupnull(style); if(!log_badargs_if(vec==NULL) && copy) { if(!strcmp("Normal", copy)) { // "Normal" means no gametype script // TODO if an empty script called "Normal" is added to the scripts directory this can be removed result = 0; } else { size_t len = strlen(copy); for(size_t i=0; i<len; i++) { if(copy[i] == ' ') { copy[i] = '_'; } } result = flib_ipc_append_message(vec, "escript %s%s.lua", MULTIPLAYER_SCRIPT_PATH, copy); } } free(copy); return result; }
char *flib_ini_get_sectionname(flib_ini *ini, int number) { if(!log_badargs_if2(ini==NULL, number<0)) { return flib_strdupnull(iniparser_getsecname(ini->inidict, number)); } return NULL; }