Changeset 499

Show
Ignore:
Timestamp:
08/11/07 17:26:21 (1 year ago)
Author:
takkaria
Message:

Document z-blockfile, in the processing adding const-correctness.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/loadsave.c

    r497 r499  
    106106        safe_setuid_grab(); 
    107107        file_delete(new_name); 
    108         bf = bf_open(new_name, BF_WRITE); 
     108        bf = bf_open(new_name, BF_WRITE | BF_SAVE); 
    109109        safe_setuid_drop(); 
    110110 
     
    217217        u32b len = 0; 
    218218 
    219         char *data = bf_nextrecord(block, &len); 
     219        const char *data = bf_nextrecord(block, &len); 
    220220        if (!data) return NULL; 
    221221 
     
    502502{ 
    503503        block_t *global_block; 
    504         void *rec; 
     504        const void *rec; 
    505505        u32b len = 0; 
    506506 
     
    509509        global_block = bf_findblock(bf, "system"); 
    510510        rec = bf_nextrecord(global_block, &len); 
    511         global_smap = smap_fromstring((char *)rec, len); 
     511        global_smap = smap_fromstring(rec, len); 
    512512 
    513513        sf_saves = smap_get_u16b(global_smap, "past_saves"); 
  • trunk/src/z-blockfile.c

    r498 r499  
    1818#include "z-blockfile.h" 
    1919 
    20 void bf_load(blockfile_t *bf); 
     20static void bf_load(blockfile_t *bf); 
    2121void bf_loadblock(blockfile_t *bf); 
    2222void bf_loadrecord(blockfile_t *bf, block_t *bl); 
     
    2626 
    2727/* Public interface */ 
    28 blockfile_t *bf_open(char *name, u32b flags) 
     28blockfile_t *bf_open(const char *name, u32b flags) 
    2929{ 
    3030        blockfile_t *bf = ZNEW(blockfile_t); 
     
    5151} 
    5252 
    53 block_t *bf_createblock(blockfile_t *bf, char *name) 
     53block_t *bf_createblock(blockfile_t *bf, const char *name) 
    5454{ 
    5555        block_t *block = ZNEW(block_t); 
     
    7373} 
    7474 
    75 block_t *bf_findblock(blockfile_t *bf, char *name) 
     75block_t *bf_findblock(blockfile_t *bf, const char *name) 
    7676{ 
    7777        block_t *tmp = bf->block_head; 
     
    9898} 
    9999 
    100 void bf_rewindblock(blockfile_t *bf) 
     100void bf_rewind(blockfile_t *bf) 
    101101{ 
    102102        bf->block_curr = bf->block_head; 
     
    113113} 
    114114 
    115 char *bf_name(block_t *block) 
     115const char *bf_name(block_t *block) 
    116116{ 
    117117        return block->name; 
     
    158158} 
    159159 
    160 void *bf_nextrecord(block_t *block, u32b *len) 
     160const void *bf_nextrecord(block_t *block, u32b *len) 
    161161{ 
    162162        record_t *rec = block->record_curr; 
     
    174174} 
    175175 
    176 void bf_eachrecord(block_t *block, void (*fn)(record_t *)) 
     176void bf_eachrecord(block_t *block, void (*fn)(const void *, u32b)) 
    177177{ 
    178178        record_t *rec = block->record_head; 
    179179        while (rec) 
    180180        { 
    181                 fn(rec); 
     181                fn(rec->data, rec->len); 
    182182                rec = rec->next; 
    183183        } 
     
    218218} 
    219219 
    220 void bf_load(blockfile_t *bf) 
     220static void bf_load(blockfile_t *bf) 
    221221{ 
    222222        u32b blockno = 0; 
  • trunk/src/z-blockfile.h

    r498 r499  
    55#include "z-file.h" 
    66 
     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 */ 
    714#define BF_WRITE        0x00000001 
    815#define BF_READ         0x00000002 
    916#define BF_SAVE         0x00000004 
    1017 
    11 typedef struct _record_t 
     18 
     19/*** Data types ***/ 
     20 
     21/* (Forward declarations) */ 
     22typedef struct _record_t record_t; 
     23typedef struct _block_t block_t; 
     24typedef 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 */ 
     33struct _blockfile_t 
    1234{ 
    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 */ 
     49struct _block_t 
    1950{ 
    2051        u32b namelen; 
     
    2556        record_t *record_tail; 
    2657        struct _block_t *next; 
    27 } block_t; 
    28  
    29 typedef struct _blockfile_t 
     58}; 
     59 
     60/* 
     61 * A record in a block. 
     62 */ 
     63struct _record_t 
    3064{ 
    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 */ 
     86blockfile_t *bf_open(const char *name, u32b flags); 
     87 
     88/* 
     89 * Save all currently held data to the blockfile. 
     90 */ 
     91void bf_save(blockfile_t *bf); 
     92 
     93/* 
     94 * Close a blockfile handle. 
     95 */ 
     96void 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 */ 
    40104u32b bf_nrblocks(blockfile_t *bf); 
    41105 
    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 */ 
     117block_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 */ 
     128block_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 */ 
    44139block_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 */ 
     146void 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 */ 
    46153void 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 */ 
     161const char *bf_name(block_t *block); 
     162 
     163/* 
     164 * Returns the size of block `block`. 
     165 */ 
     166u32b bf_blocksize(block_t *block); 
     167 
     168/* 
     169 * Returns the number of records stored within the block `block`. 
     170 */ 
    48171u32b 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 */ 
    51183void 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 */ 
     195const 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 */ 
    53202void 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 */ 
     207void bf_eachrecord(block_t *block, void (*fn)(const void *rec, u32b len)); 
     208 
    58209 
    59210#endif /* !INCLUDED_BLOCKFILE_H */ 
  • trunk/src/z-smap.c

    r493 r499  
    314314 
    315315 
    316 smap_t *smap_fromstring(char *string, u32b length) 
     316smap_t *smap_fromstring(const char *string, u32b length) 
    317317{ 
    318318        smap_t *smap = smap_new(); 
  • trunk/src/z-smap.h

    r495 r499  
    5252 
    5353char *smap_tostring(smap_t *smap, u32b *length); 
    54 smap_t *smap_fromstring(char *string, u32b length); 
     54smap_t *smap_fromstring(const char *string, u32b length); 
    5555 
    5656void smap_foreach(smap_t *smap, void (*fn)(sentry_t *se));