retvalue space_prepare(struct devices **devices, enum spacecheckmode mode, off_t reservedfordb, off_t reservedforothers) { struct devices *n; struct device *d; struct stat s; int ret; retvalue r; if (mode == scm_NONE) { *devices = NULL; return RET_OK; } assert (mode == scm_FULL); n = NEW(struct devices); if (FAILEDTOALLOC(n)) return RET_ERROR_OOM; n->root = NULL; n->reserved = reservedforothers; ret = stat(global.dbdir, &s); if (ret != 0) { int e = errno; fprintf(stderr, "Error stat'ing %s: %d=%s\n", global.dbdir, e, strerror(e)); free(n); return RET_ERRNO(e); } r = device_find_or_create(n, s.st_dev, global.dbdir, &d); if (RET_WAS_ERROR(r)) { space_free(n); return r; } d->reserved += reservedfordb/d->blocksize+1; *devices = n; return RET_OK; }
void world_free(World *w){ int i; assert(w!=NULL); for(i=0; i<numobj(w);i++) object_free(obj(w)[i]); free(obj(w)); for(i=0; i<numspaces(w);i++) space_free(spaces(w)[i]); player_free(player(w)); free(spaces(w)); free(w); return; }
Space * spacefromfile(FILE * f) { assert(f != NULL); char buff[MAX_L_DESC]; int aux, i, ndoors, x, y, nx, ny; FILE *file; Space *s; s = space_ini(); if (!s) return NULL; fscanf(f, "%d\n", &aux); if (space_setId(s, aux) == ERROR) { space_free(s); return NULL; } fscanf(f, "%d\n", &aux); if (space_setNDoors(s, aux) == ERROR) { space_free(s); return NULL; } ndoors = aux; for (i = 0; i < ndoors; i++) { fscanf(f, "%d %d %d %d %d\n", &y, &x, &aux, &ny, &nx); if (space_setDoor(s, i, x, y, aux, nx, ny) == ERROR) { space_free(s); return NULL; } } fscanf(f, "%1000[^\n]\n", buff); if (space_setSDesc(s, buff) == ERROR) { space_free(s); return NULL; } fscanf(f, "%2000[^\n]\n", buff); if (space_setLDesc(s, buff) == ERROR) { space_free(s); return NULL; } fscanf(f, "%d\n", &aux); if (space_setLight(s, aux) == ERROR) { space_free(s); return NULL; } fscanf(f, "%d\n", &aux); if (space_setLock(s, aux) == ERROR) { space_free(s); return NULL; } fscanf(f, "%s\n", buff); file = fopen(buff, "r"); if (!file) { space_free(s); return NULL; } fscanf(file, "%d ", &aux); if (space_setNRows(s, aux) == ERROR) { space_free(s); return NULL; } fscanf(file, "%d\n", &aux); if (space_setNCols(s, aux) == ERROR) { space_free(s); return NULL; } if (space_setMap(s, mapfromfile(file, space_getNRows(s), space_getNCols(s))) == ERROR) { space_free(s); fclose(file); return NULL; } fclose(file); return s; }
static void space_node_free(RBNode *n) { RSpace *s = container_of (n, RSpace, rb); space_free (s); }
World *worldfromfile(char *file){ assert(file!=NULL); FILE *f; int i,j; int numspaces, numobjects; Space **spaces; Player *player; Object **objects; World *w; f=fopen(file,"r"); if(f==NULL) return NULL; fscanf(f,"%d\n",&numspaces); spaces=(Space **)malloc(sizeof(Space *)*numspaces); if(spaces==NULL){ fclose(f); return NULL; } for(i=0;i<numspaces;i++){ spaces[i]=spacefromfile(f); if(spaces[i]==NULL){ for(j=i;j>=0;j--){ space_free(spaces[j]); } free(spaces); fclose(f); return NULL; } } player=playerfromfile(f); if(player==NULL){/*if theres no memory free everything and return NULL*/ for(j=0;j<numspaces;j++){ space_free(spaces[j]); } free(spaces); fclose(f); return NULL; } fscanf(f,"%d\n",&numobjects); objects=(Object **)malloc(sizeof(Object *)*numobjects); if(objects==NULL){ for(j=0;j<numspaces;j++){ space_free(spaces[j]); } free(spaces); player_free(player); fclose(f); return NULL; } for(i=0;i<numobjects;i++){ objects[i]=objectfromfile(f); if(objects[i]==NULL){ for(j=0;j<numspaces;j++){ space_free(spaces[j]); } free(spaces); player_free(player); for(j=i-1;j>=0;j--){ object_free(objects[i]); } free(objects); fclose(f); return NULL; } } w=world_ini(objects,player,spaces,numobjects,numspaces); if(w==NULL){ for(j=0;j<numspaces;j++){ space_free(spaces[j]); } free(spaces); player_free(player); for(j=0;j<numobjects;j++){ object_free(objects[i]); } fclose(f); free(objects); return NULL; } fclose(f); return w; }