10 static int cacheRead_readFun(
int tileIndex,
void *tileBuf,
void *closure)
12 G3D_Map *map = closure;
14 if (!
G3d_readTile(map, tileIndex, tileBuf, map->typeIntern)) {
15 G3d_error(
"cacheRead_readFun: error in G3d_readTile");
23 static int initCacheRead(G3D_Map * map,
int nCached)
26 map->tileSize * map->numLengthIntern,
27 map->nTiles, cacheRead_readFun, map);
28 if (map->cache ==
NULL) {
29 G3d_error(
"initCacheRead: error in G3d_cache_new_read");
56 static int cacheWrite_readFun(
int tileIndex,
void *tileBuf,
void *closure)
58 G3D_Map *map = closure;
60 long pos, offs, offsLast;
62 pos = map->index[tileIndex];
74 nBytes = map->tileSize * map->numLengthIntern;
75 offs = pos * (nBytes +
sizeof(
int));
79 if (lseek(map->cacheFD, offs, SEEK_SET) == -1) {
80 G3d_error(
"cacheWrite_readFun: can't position file");
83 if (read(map->cacheFD, tileBuf, nBytes) != nBytes) {
84 G3d_error(
"cacheWrite_readFun: can't read file");
90 map->index[tileIndex] = -1;
95 if (map->cachePosLast == pos) {
103 offsLast = map->cachePosLast * (nBytes +
sizeof(
int));
105 if (lseek(map->cacheFD, offsLast, SEEK_SET) == -1) {
106 G3d_error(
"cacheWrite_readFun: can't position file");
109 if (read(map->cacheFD,
xdr, nBytes +
sizeof(
int)) != nBytes +
sizeof(
int)) {
110 G3d_error(
"cacheWrite_readFun: can't read file");
114 if (lseek(map->cacheFD, offs, SEEK_SET) == -1) {
115 G3d_error(
"cacheWrite_readFun: can't position file");
118 if (write(map->cacheFD,
xdr, nBytes +
sizeof(
int)) !=
119 nBytes +
sizeof(
int)) {
120 G3d_error(
"cacheWrite_readFun: can't write file");
124 index = *((
int *)((
unsigned char *)
xdr + nBytes));
125 map->index[index] = -pos - 2;
135 cacheWrite_writeFun(
int tileIndex,
const void *tileBuf,
void *closure)
137 G3D_Map *map = closure;
141 if (map->index[tileIndex] != -1)
145 nBytes = map->tileSize * map->numLengthIntern;
146 offs = map->cachePosLast * (nBytes +
sizeof(
int));
148 if (lseek(map->cacheFD, offs, SEEK_SET) == -1) {
149 G3d_error(
"cacheWrite_writeFun: can't position file");
152 if (write(map->cacheFD, tileBuf, nBytes) != nBytes) {
153 G3d_error(
"cacheWrite_writeFun: can't write file");
156 if (write(map->cacheFD, &tileIndex,
sizeof(
int)) !=
sizeof(
int)) {
157 G3d_error(
"cacheWrite_writeFun: can't write file");
161 map->index[tileIndex] = -map->cachePosLast - 2;
168 static int disposeCacheWrite(G3D_Map * map)
170 if (map->cacheFD >= 0) {
171 if (close(map->cacheFD) != 0) {
172 G3d_error(
"disposeCacheWrite: could not close file");
175 remove(map->cacheFileName);
186 static int initCacheWrite(G3D_Map * map,
int nCached)
189 map->cacheFD = open(map->cacheFileName, O_RDWR | O_CREAT | O_TRUNC, 0666);
191 if (map->cacheFD < 0) {
192 G3d_error(
"initCacheWrite: could not open file");
196 map->cachePosLast = -1;
199 map->tileSize * map->numLengthIntern,
201 cacheWrite_writeFun, map,
202 cacheWrite_readFun, map);
204 if (map->cache ==
NULL) {
205 disposeCacheWrite(map);
206 G3d_error(
"initCacheWrite: error in G3d_cache_new");
218 if (!initCacheRead(map, nCached)) {
219 G3d_error(
"G3d_initCache: error in initCacheRead");
225 if (!initCacheWrite(map, nCached)) {
226 G3d_error(
"G3d_initCache: error in initCacheWrite");
235 static int disposeCacheRead(G3D_Map * map)
246 if (!disposeCacheRead(map)) {
247 G3d_error(
"G3d_disposeCache: error in disposeCacheRead");
253 if (!disposeCacheWrite(map)) {
254 G3d_error(
"G3d_disposeCache: error in disposeCacheWrite");
264 static int cacheFlushFun(
int tileIndex,
const void *tileBuf,
void *closure)
266 G3D_Map *map = closure;
268 if (!
G3d_writeTile(map, tileIndex, tileBuf, map->typeIntern)) {
269 G3d_error(
"cacheFlushFun: error in G3d_writeTile");
280 int tileIndex, nBytes;
285 G3d_error(
"G3d_flushAllTiles: error in G3d_cache_remove_all");
296 nBytes = map->tileSize * map->numLengthIntern;
298 while (map->cachePosLast >= 0) {
299 offs = map->cachePosLast * (nBytes +
sizeof(
int)) + nBytes;
301 if (lseek(map->cacheFD, offs, SEEK_SET) == -1) {
302 G3d_error(
"G3d_flushAllTiles: can't position file");
305 if (read(map->cacheFD, &tileIndex,
sizeof(
int)) !=
sizeof(
int)) {
306 G3d_error(
"G3d_flushAllTiles: can't read file");
311 G3d_error(
"G3d_flushAllTiles: error in G3d_cache_load");
315 G3d_error(
"G3d_flushAllTiles: error in G3d_cache_flush");
322 G3d_error(
"G3d_flushAllTiles: error in G3d_cache_flush_all");
int G3d_writeTile(G3D_Map *map, int tileIndex, const void *tile, int type)
Writes tile with index tileIndex to the file corresponding to map. It is assumed that the cells in ti...
void * G3d_cache_new(int nofElts, int sizeOfElts, int nofNames, int(*eltRemoveFun)(), void *eltRemoveFunData, int(*eltLoadFun)(), void *eltLoadFunData)
int G3d_cache_remove_all(G3D_cache *c)
int G3d_initCache(G3D_Map *map, int nCached)
int G3d_disposeCache(G3D_Map *map)
void G3d_free(void *buf)
Same as free (ptr).
void G3d_error(const char *msg,...)
int G3d_flushAllTiles(G3D_Map *map)
int G3d_cache_load(G3D_cache *c, int name)
char * G_tempfile(void)
Returns a temporary file name.
int G3d_cache_flush(G3D_cache *c, int name)
void G3d_cache_dispose(G3D_cache *c)
int G3d_cache_flush_all(G3D_cache *c)
void * G3d_cache_new_read(int nofElts, int sizeOfElts, int nofNames, read_fn *eltLoadFun, void *eltLoadFunData)
void G3d_cache_set_removeFun(G3D_cache *c, int(*eltRemoveFun)(), void *eltRemoveFunData)
int G3d_readTile(G3D_Map *map, int tileIndex, void *tile, int type)
Reads tile with index tileIndex into the tile buffer. The cells are stored with type type which must ...