Changeset 474

Show
Ignore:
Timestamp:
08/09/07 10:53:42 (1 year ago)
Author:
takkaria
Message:

Document the new z-file code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/z-file.c

    r469 r474  
    698698 
    699699/* 
    700  * Opens a directory handle. 
    701  *  
    702  * `dirname` must be a system-specific pathname to the directory 
    703  * you want scanned. 
    704  * 
    705  * Returns a valid directory handle on success, NULL otherwise. 
    706  */ 
    707 ang_dir *my_dopen(const char *dirname); 
    708  
    709  
    710 /* 
    711  * Reads a directory entry. 
    712  * 
    713  * `dir` must point to a directory handle previously returned by my_dopen(). 
    714  * `fname` must be a pointer to a writeable chunk of memory `len` long. 
    715  * 
    716  * Returns TRUE on successful reading, FALSE otherwise. 
    717  * (FALSE generally indicates that there are no more files to be read.) 
    718  */ 
    719 bool my_dread(ang_dir *dir, char *fname, size_t len); 
    720  
    721  
    722 /* 
    723  * Close a directory handle. 
    724  */ 
    725 void my_dclose(ang_dir *dir); 
    726  
    727  
    728  
     700 * For information on what these are meant to do, please read the header file. 
     701 */ 
    729702 
    730703#ifdef WINDOWS 
     
    738711}; 
    739712 
    740 /* Specified above */ 
    741713ang_dir *my_dopen(const char *dirname) 
    742714{ 
     
    761733} 
    762734 
    763 /* Specified above */ 
    764735bool my_dread(ang_dir *dir, char *fname, size_t len) 
    765736{ 
     
    823794}; 
    824795 
    825 /* Specified above */ 
    826796ang_dir *my_dopen(const char *dirname) 
    827797{ 
     
    849819} 
    850820 
    851 /* Specified above */ 
    852821bool my_dread(ang_dir *dir, char *fname, size_t len) 
    853822{ 
  • trunk/src/z-file.h

    r469 r474  
    44#include "h-basic.h" 
    55 
     6/*** Permissions code ***/ 
     7 
     8/*  
     9 * Player's user ID and group ID, respectively. 
     10 * 
     11 * Only relevant to POSIX systems that use main.c, and set there. 
     12 */ 
    613extern int player_uid; 
    714extern int player_egid; 
    815 
     16/* 
     17 * Drop or grab privileges. 
     18 * 
     19 * This is used on multiuser systems, where the game wants to gain access to 
     20 * system-wide files like the scores, raw files, or savefiles.  Reading from 
     21 * these locations is permitted by anyone, but writing to them requires a call 
     22 * to safe_setuid_grab() before opening the file for writing. 
     23 * 
     24 * safe_setuid_drop() should be called immediately after the file has been 
     25 * opened, to prevent security risks, and restores the game's rights so that it 
     26 * cannot write to the system-wide files. 
     27 */ 
     28void safe_setuid_grab(void); 
    929void safe_setuid_drop(void); 
    10 void safe_setuid_grab(void); 
    11  
     30 
     31 
     32/*** Path building code ***/ 
     33 
     34/* 
     35 * Concatenates "leaf" onto the end of "base", using system-specific path 
     36 * separators, and places the result in buf[], truncated to "len" bytes. 
     37 * 
     38 * On Unixes, deals with the tilde as representing home directories. 
     39 */ 
    1240size_t path_build(char *buf, size_t len, const char *base, const char *leaf); 
    1341 
    14 /* File code */ 
     42 
     43 
     44/*** File access code ***/ 
     45 
     46/** Data types **/ 
     47 
     48/* 
     49 * An opaque file handle for Angband file handling. 
     50 */ 
    1551typedef struct ang_file ang_file; 
    1652 
     53/* 
     54 * Specifies what kind of access is required to a file.  See file_open(). 
     55 */ 
    1756typedef enum 
    1857{ 
     
    2261} file_mode; 
    2362 
     63/* 
     64 * Specified what kind of thing a file is, when writing.  See file_open(). 
     65 */ 
    2466typedef enum 
    2567{ 
    26         FTYPE_TEXT = 1, /* -> FILE_TYPE_TEXT */ 
     68        FTYPE_TEXT = 1, 
    2769        FTYPE_SAVE, 
    28         FTYPE_RAW,             /* -> FILE_TYPE_DATA */ 
     70        FTYPE_RAW, 
    2971        FTYPE_HTML 
    3072} file_type; 
    3173 
     74 
     75/** Utility functions **/ 
     76 
     77/* 
     78 * Returns TRUE if `fname` exists (and is a file), FALSE otherwise. 
     79 */ 
    3280bool file_exists(const char *fname); 
     81 
     82/* 
     83 * Tries to delete `fname`. 
     84 * 
     85 * Returns TRUE if successful, FALSE otherwise. 
     86 */ 
    3387bool file_delete(const char *fname); 
     88 
     89/* 
     90 * Moves the file `fname` to `newname`. 
     91 * 
     92 * Returns TRUE if successful, FALSE otherwise. 
     93 */ 
    3494bool file_move(const char *fname, const char *newname); 
     95 
     96/* 
     97 * Returns TRUE if the file `first` is newer than `second`. 
     98 */ 
    3599bool file_newer(const char *first, const char *second); 
    36100 
     101 
     102/** File handle creation **/ 
     103 
     104/* 
     105 * Open file `buf`, returning a a file handling representing that file. 
     106 * 
     107 * The file mode specifies what kind of access is required to the file: 
     108 *  - MODE_WRITE will overwrite the current contents of the file 
     109 *  - MODE_READ will allow read-only access to the file 
     110 *  - MODE_APPEND will allow write-only access, but will not overwrite the 
     111 *    current contents of the file. 
     112 * 
     113 * The file type is specified to allow systems which don't use file extensions 
     114 * to set the type of the file appropriately.  When reading, pass -1 as ftype; 
     115 * when writing, use whichever filetype seems most appropriate. 
     116 * 
     117 * On any kind of error, this function returns NULL. 
     118 */ 
    37119ang_file *file_open(const char *buf, file_mode mode, file_type ftype); 
    38 ang_file *file_temp(); 
     120 
     121/* 
     122 * Attempt to close the file handle `f`. 
     123 * 
     124 * Returns TRUE if successful, FALSE otherwise. 
     125 */ 
    39126bool file_close(ang_file *f); 
    40127 
     128 
     129/** File locking **/ 
     130 
     131/* 
     132 * Lock or unlock the file represented by `f` for writing. 
     133 * If the file is not open for writing, this call will fail. 
     134 * 
     135 * If `f` is closed, the file is automatically unlocked. 
     136 */ 
    41137void file_lock(ang_file *f); 
    42138void file_unlock(ang_file *f); 
    43139 
     140 
     141/** Line-based IO **/ 
     142 
     143/* 
     144 * Get a line of text from the file represented by `f`, placing it into `buf` 
     145 * to a maximum length of `n`. 
     146 * 
     147 * This expands tabs, replaces non-printables with '?', and deals with differing 
     148 * line endings. 
     149 * 
     150 * Returns TRUE when data is returned; FALSE otherwise. 
     151 */ 
    44152bool file_getl(ang_file *f, char *buf, size_t n); 
     153 
     154/* 
     155 * Write the string pointed to by `buf` to the file represented by `f`. 
     156 * 
     157 * Returns TRUE if successful, FALSE otherwise. 
     158 */ 
    45159bool file_put(ang_file *f, const char *buf); 
     160 
     161/* 
     162 * Format (using strnfmt) the given args, and then call file_put(). 
     163 */ 
    46164bool file_putf(ang_file *f, const char *fmt, ...); 
    47165 
     166 
     167/** Byte-based IO */ 
     168 
     169/* 
     170 * Seek to position `pos` in the file represented by `f`. 
     171 * 
     172 * Returns TRUE if successful, FALSE otherwise.  
     173 */ 
    48174bool file_seek(ang_file *f, u32b pos); 
     175 
     176/* 
     177 * Reads `n` bytes from the file represented by `f` into the buffer `buf`. 
     178 * Do not mix with calls to file_readc(). 
     179 * 
     180 * Returns the number of bytes read.  
     181 */ 
    49182size_t file_read(ang_file *f, char *buf, size_t n); 
     183 
     184/* 
     185 * Write the first `n` bytes following the pointer `buf` to the file represented 
     186 * by `f`.  Do not mix with calls to file_writec(). 
     187 * 
     188 * Returns TRUE if successful, FALSE otherwise. 
     189 */ 
    50190bool file_write(ang_file *f, const char *buf, size_t n); 
     191 
     192/* 
     193 * Read a byte from the file represented by `f` and place it at the location 
     194 * specified by 'b'. 
     195 * 
     196 * Returns TRUE if successful, FALSE otherwise. 
     197 */ 
    51198bool file_readc(ang_file *f, byte *b); 
     199 
     200/* 
     201 * Write the byte `b` to the file represented by `f`. 
     202 * 
     203 * Returns TRUE if successful, FALSE otherwise. 
     204 */ 
    52205bool file_writec(ang_file *f, byte b); 
    53206 
    54207 
    55208 
    56 /* Directory code */ 
     209/*** Directory code ***/ 
     210 
     211/* 
     212 * An opaque file handle for Angband directory handling. 
     213 */ 
    57214typedef struct ang_dir ang_dir; 
    58215 
     216 
     217/* 
     218 * Opens a directory handle. 
     219 * 
     220 * `dirname` must be a system-specific pathname to the directory 
     221 * you want scanned. 
     222 * 
     223 * Returns a valid directory handle on success, NULL otherwise. 
     224 */ 
    59225ang_dir *my_dopen(const char *dirname); 
     226 
     227/* 
     228 * Reads a directory entry. 
     229 * 
     230 * `dir` must point to a directory handle previously returned by my_dopen(). 
     231 * `fname` must be a pointer to a writeable chunk of memory `len` long. 
     232 * 
     233 * Returns TRUE on successful reading, FALSE otherwise. 
     234 * (FALSE generally indicates that there are no more files to be read.) 
     235 */ 
    60236bool my_dread(ang_dir *dir, char *fname, size_t len); 
     237 
     238/* 
     239 * Close a directory handle. 
     240 */ 
    61241void my_dclose(ang_dir *dir); 
    62242