Changeset 499
- Timestamp:
- 08/11/07 17:26:21 (1 year ago)
- Files:
-
- trunk/src/loadsave.c (modified) (4 diffs)
- trunk/src/z-blockfile.c (modified) (9 diffs)
- trunk/src/z-blockfile.h (modified) (2 diffs)
- trunk/src/z-smap.c (modified) (1 diff)
- trunk/src/z-smap.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/loadsave.c
r497 r499 106 106 safe_setuid_grab(); 107 107 file_delete(new_name); 108 bf = bf_open(new_name, BF_WRITE );108 bf = bf_open(new_name, BF_WRITE | BF_SAVE); 109 109 safe_setuid_drop(); 110 110 … … 217 217 u32b len = 0; 218 218 219 c har *data = bf_nextrecord(block, &len);219 const char *data = bf_nextrecord(block, &len); 220 220 if (!data) return NULL; 221 221 … … 502 502 { 503 503 block_t *global_block; 504 void *rec;504 const void *rec; 505 505 u32b len = 0; 506 506 … … 509 509 global_block = bf_findblock(bf, "system"); 510 510 rec = bf_nextrecord(global_block, &len); 511 global_smap = smap_fromstring( (char *)rec, len);511 global_smap = smap_fromstring(rec, len); 512 512 513 513 sf_saves = smap_get_u16b(global_smap, "past_saves"); trunk/src/z-blockfile.c
r498 r499 18 18 #include "z-blockfile.h" 19 19 20 void bf_load(blockfile_t *bf);20 static void bf_load(blockfile_t *bf); 21 21 void bf_loadblock(blockfile_t *bf); 22 22 void bf_loadrecord(blockfile_t *bf, block_t *bl); … … 26 26 27 27 /* Public interface */ 28 blockfile_t *bf_open(c har *name, u32b flags)28 blockfile_t *bf_open(const char *name, u32b flags) 29 29 { 30 30 blockfile_t *bf = ZNEW(blockfile_t); … … 51 51 } 52 52 53 block_t *bf_createblock(blockfile_t *bf, c har *name)53 block_t *bf_createblock(blockfile_t *bf, const char *name) 54 54 { 55 55 block_t *block = ZNEW(block_t); … … 73 73 } 74 74 75 block_t *bf_findblock(blockfile_t *bf, c har *name)75 block_t *bf_findblock(blockfile_t *bf, const char *name) 76 76 { 77 77 block_t *tmp = bf->block_head; … … 98 98 } 99 99 100 void bf_rewind block(blockfile_t *bf)100 void bf_rewind(blockfile_t *bf) 101 101 { 102 102 bf->block_curr = bf->block_head; … … 113 113 } 114 114 115 c har *bf_name(block_t *block)115 const char *bf_name(block_t *block) 116 116 { 117 117 return block->name; … … 158 158 } 159 159 160 void *bf_nextrecord(block_t *block, u32b *len)160 const void *bf_nextrecord(block_t *block, u32b *len) 161 161 { 162 162 record_t *rec = block->record_curr; … … 174 174 } 175 175 176 void bf_eachrecord(block_t *block, void (*fn)( record_t *))176 void bf_eachrecord(block_t *block, void (*fn)(const void *, u32b)) 177 177 { 178 178 record_t *rec = block->record_head; 179 179 while (rec) 180 180 { 181 fn(rec );181 fn(rec->data, rec->len); 182 182 rec = rec->next; 183 183 } … … 218 218 } 219 219 220 void bf_load(blockfile_t *bf)220 static void bf_load(blockfile_t *bf) 221 221 { 222 222 u32b blockno = 0; trunk/src/z-blockfile.h
r498 r499 5 5 #include "z-file.h" 6 6 7 8 /*** Constants ***/ 9 10 /* 11 * For use with bf_open(). Indicate mode of file opening, and when "BF_SAVE" 12 * is set, the file will be marked as a savefile on participating OSes. 13 */ 7 14 #define BF_WRITE 0x00000001 8 15 #define BF_READ 0x00000002 9 16 #define BF_SAVE 0x00000004 10 17 11 typedef struct _record_t 18 19 /*** Data types ***/ 20 21 /* (Forward declarations) */ 22 typedef struct _record_t record_t; 23 typedef struct _block_t block_t; 24 typedef struct _blockfile_t blockfile_t; 25 26 27 /* 28 * The basic "blockfile" type. 29 * 30 * A blockfile contains a given number of blocks and is associated with a file 31 * handle. Each block is named, and is of type block_t. 32 */ 33 struct _blockfile_t 12 34 { 13 u32b len; 14 void *data; 15 struct _record_t *next; 16 } record_t; 17 18 typedef struct _block_t 35 ang_file *fh; 36 u32b nr_blocks; 37 u32b flags; 38 block_t *block_curr; 39 block_t *block_head; 40 block_t *block_tail; 41 }; 42 43 /* 44 * A block, stored in a blockfile 45 * 46 * Many blocks make up a blockfile, and each block contains one or more 47 * records, which are of type record_t. 48 */ 49 struct _block_t 19 50 { 20 51 u32b namelen; … … 25 56 record_t *record_tail; 26 57 struct _block_t *next; 27 } block_t; 28 29 typedef struct _blockfile_t 58 }; 59 60 /* 61 * A record in a block. 62 */ 63 struct _record_t 30 64 { 31 ang_file *fh; 32 u32b nr_blocks; 33 u32b flags; 34 block_t *block_curr; 35 block_t *block_head; 36 block_t *block_tail; 37 } blockfile_t; 38 39 blockfile_t *bf_open(char *name, u32b flags); 65 u32b len; 66 void *data; 67 struct _record_t *next; 68 }; 69 70 71 /*** Functions ***/ 72 73 /** Opening/saving/closing **/ 74 75 /* 76 * Open a new blockfile, with the filename "name" and flags as follows: 77 * 78 * - BF_READ indicates opening an already-existing file, and loading data from 79 * it into memory 80 * - BF_WRITE indicates overwriting any existing file with a clean structure, 81 * ready for writing new data 82 * - BF_SAVE indicates that this file is a savefile. 83 * 84 * Returns a blockfile handle if successful, or NULL if unsucessful. 85 */ 86 blockfile_t *bf_open(const char *name, u32b flags); 87 88 /* 89 * Save all currently held data to the blockfile. 90 */ 91 void bf_save(blockfile_t *bf); 92 93 /* 94 * Close a blockfile handle. 95 */ 96 void bf_close(blockfile_t *bf); 97 98 99 /** Block info **/ 100 101 /* 102 * Returns the number of blocks in the blockfile represented by `bf`. 103 */ 40 104 u32b bf_nrblocks(blockfile_t *bf); 41 105 42 block_t *bf_createblock(blockfile_t *bf, char *name); 43 block_t *bf_findblock(blockfile_t *bf, char *name); 106 107 /** Block creation **/ 108 109 /* 110 * Create a new block in the blockfile `bf` with name `name`. 111 * 112 * Returns the block when successful, NULL otherwise. 113 * 114 * When you have created a block, you should use bf_createrecord() to fill it 115 * with data. 116 */ 117 block_t *bf_createblock(blockfile_t *bf, const char *name); 118 119 120 /** Finding blocks **/ 121 122 /* 123 * Finds a block in the blockfile `bf` with the name `name`. 124 * 125 * If the block cannot be found, NULL is returned; otherwise, a pointer to the 126 * block is returned. 127 */ 128 block_t *bf_findblock(blockfile_t *bf, const char *name); 129 130 /* 131 * Return the next block in the blockfile `bf`. 132 * 133 * On the first call, this will return the first block written to the file; the 134 * second call will return the second block, etc. If there is no "next block" 135 * in the file, NULL will be returned. 136 * 137 * If you want to iterate over all the blocks again, use bf_rewind(). 138 */ 44 139 block_t *bf_nextblock(blockfile_t *bf); 45 void bf_rewindblock(blockfile_t *bf); 140 141 /* 142 * Reset the current block in the blockfile `bf`. This is only useful if, 143 * having once iterated through some blocks in the file, you want to return to 144 * the beginning to go again. 145 */ 146 void bf_rewind(blockfile_t *bf); 147 148 149 /* 150 * Calls the provided function for each block in the file `bf`, in the order in 151 * which they were written. 152 */ 46 153 void bf_eachblock(blockfile_t *bf, void (*fn)(block_t *block)); 47 char *bf_name(block_t *block); 154 155 156 /** Block information **/ 157 158 /* 159 * Returns the name of block `block`. 160 */ 161 const char *bf_name(block_t *block); 162 163 /* 164 * Returns the size of block `block`. 165 */ 166 u32b bf_blocksize(block_t *block); 167 168 /* 169 * Returns the number of records stored within the block `block`. 170 */ 48 171 u32b bf_nrrecords(block_t *block); 49 u32b bf_blocksize(block_t *block); 50 172 173 174 175 /** Record creation and reading **/ 176 177 /* 178 * Create a new record in the block `block`, with data `data` of length `len`. 179 * 180 * Much like a blockfile contains multiple blocks, each block contains one or 181 * more records. These are read back using bf_nextrecord() or bf_eachrecord(). 182 */ 51 183 void bf_createrecord(block_t *block, void *data, u32b len); 52 void *bf_nextrecord(block_t *block, u32b *len); 184 185 /* 186 * Return the data of the next record in the block `block`, and place the 187 * length of this data in *`len`. 188 * 189 * On the first call, this will return the first record written to the file; 190 * the second call will return the second, etc. If there is no "next record" 191 * in the file, NULL will be returned. 192 * 193 * If you want to iterate over all the records again, use bf_rewindrecord(). 194 */ 195 const void *bf_nextrecord(block_t *block, u32b *len); 196 197 /* 198 * Reset the current record in the block `block`. This is only useful if, 199 * having once iterated through some records in the block, you want to return 200 * to the beginning to go again. 201 */ 53 202 void bf_rewindrecord(block_t *block); 54 void bf_eachrecord(block_t *block, void (*fn)(record_t *rec)); 55 56 void bf_save(blockfile_t *bf); 57 void bf_close(blockfile_t *bf); 203 204 /* 205 * Calls the supplied `fn` to iterate over each record in the block `block`. 206 */ 207 void bf_eachrecord(block_t *block, void (*fn)(const void *rec, u32b len)); 208 58 209 59 210 #endif /* !INCLUDED_BLOCKFILE_H */ trunk/src/z-smap.c
r493 r499 314 314 315 315 316 smap_t *smap_fromstring(c har *string, u32b length)316 smap_t *smap_fromstring(const char *string, u32b length) 317 317 { 318 318 smap_t *smap = smap_new(); trunk/src/z-smap.h
r495 r499 52 52 53 53 char *smap_tostring(smap_t *smap, u32b *length); 54 smap_t *smap_fromstring(c har *string, u32b length);54 smap_t *smap_fromstring(const char *string, u32b length); 55 55 56 56 void smap_foreach(smap_t *smap, void (*fn)(sentry_t *se));
