[Xmltools-development] xmltools2/xmltools/tests persist.c persist.h racing.h
Status: Beta
Brought to you by:
nik_89
|
From: um, r. <sm...@us...> - 2004-11-14 12:02:25
|
Update of /cvsroot/xmltools/xmltools2/xmltools/tests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv878 Modified Files: racing.h Added Files: persist.c persist.h Log Message: put all the xml stuff in another file so users aren't bound to it Index: racing.h =================================================================== RCS file: /cvsroot/xmltools/xmltools2/xmltools/tests/racing.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** racing.h 16 Oct 2004 04:34:58 -0000 1.2 --- racing.h 14 Nov 2004 12:02:00 -0000 1.3 *************** *** 5,8 **** --- 5,12 ---- */ + /* whether or not to save and load the track between runs. toggle this from + * the Makefile */ + /* #define CFG_PERSISTENT_TRACK */ + #include "track.h" #include "graphics.h" *************** *** 11,12 **** --- 15,20 ---- #include "cars.h" + #ifdef CFG_PERSISTENT_TRACK + # include "persist.h" + #endif + --- NEW FILE: persist.c --- /* persist.c * * saves and loads stuf between execution * */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "xmltools/xmltools.h" #include "racing.h" int Persist_LoadTrack() { NODE_MAP *template; NODE_MAP *n_map; char *x, *y, *type; int i, fd; char num[5]; /* check to see if save_racetrack.xml exists(?) * note: whoever wrote this doesn't know about stat()? */ fd = open(SAVEFILE, 0644); if (fd == -1) return 1; close(fd); Xmltool_GetXmlDesc(SAVEFILE); template = Nmap_GetData(); for (i = 0; i < TRACK_WIDTH * TRACK_HEIGHT; i++) { memset(num, '\0', 5); snprintf(num, 5, "%d", i); n_map = (NODE_MAP *)Xmltool_MReadXml(SAVEFILE, template->parent_name, "num", num); x = (char *)Nmap_GetContent("x"); y = (char *)Nmap_GetContent("y"); type = (char *)Nmap_GetContent("type"); Track_SetTile(atoi(x), atoi(y), type[0] - 'A'); } /* delete the file? someone comment this please */ fd = open(SAVEFILE, 0644); if (fd != -1) unlink(SAVEFILE); close(fd); /* Hmm, you open an xml file, get pointers to some strings using * Nmap_GetContent(), but you don't call anything to free the Content? * The strings Nmap_GetContent returns are probably allocated in memory * somewhere, mem leak? */ return 0; } int Persist_SaveTrack() { char *savefile = SAVEFILE; int x = 0; int y = 0, id = 0; char strx[11], stry[11], type[2], num[11]; type[1] = '\0'; Nmap_Clear(); for (y = 0; y < TRACK_HEIGHT; y++) { for (x = 0; x < TRACK_WIDTH; x++) { printf("here\n"); snprintf(strx, 11, "%d", x); snprintf(stry, 11, "%d", y); snprintf(num, 11, "%d", id); *type = 'A' + Track_GetTile(x, y); /* printf("num %s type %s\n x-y(%s-%s)\n", num, type, strx, stry); */ Nmap_Add("track", "num", num); Nmap_Add("track", "type", type); Nmap_Add("track", "x", strx); Nmap_Add("track", "y", stry); Xmltool_MAddXml(savefile); id++; } } printf("saved racetrack successfully\n"); return 0; } --- NEW FILE: persist.h --- /* persist.h * * saves and loads stuf between execution * */ #ifndef __PERSIST_H #define __PERSIST_H #define SAVEFILE "save_racetrack.xml" /* these return 0 on success and 1 on failure */ extern int Persist_LoadTrack(void); extern int Persist_SaveTrack(void); #endif |