sourcepawn/compiler/sp_file.h
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:07:12 2009 -0500 (2009-02-25)
changeset 2607 f3b8bbe17f85
parent 1952 b7aa2b626807
permissions -rw-r--r--
Backed out changeset fa286305bd3f
     1 #ifndef _INCLUDE_SPFILE_H
     2 #define _INCLUDE_SPFILE_H
     3 
     4 #include "sp_file_headers.h"
     5 
     6 /** 
     7  * Used for overwriting writing routines.
     8  */
     9 typedef struct sp_writefuncs_s
    10 {
    11 	void *(*fnOpen)(const char *);	/* filename, returns handle */
    12 	void (*fnClose)(void *);			/* handle */
    13 	/* buffer, size, count, handle, returns count written */
    14 	size_t (*fnWrite)(const void *, size_t, size_t, void *);	
    15 	/* buffer, size, count, handle, returns count read */
    16 	size_t (*fnRead)(void *, size_t, size_t, void *);
    17 	/* returns current position from start */
    18 	size_t (*fnGetPos)(void *);
    19 	/* sets current position from start, return 0 for success, nonzero for error */
    20 	int (*fnSetPos)(void *, size_t);
    21 } sp_writefuncs_t;
    22 
    23 typedef struct sp_file_s
    24 {
    25 	sp_file_hdr_t header;
    26 	sp_file_section_t *sections;
    27 	size_t *offsets;
    28 	sp_writefuncs_t funcs;
    29 	size_t lastsection;
    30 	size_t curoffs;
    31 	void *handle;
    32 	int state;
    33 	char *nametab;
    34 	size_t nametab_idx;
    35 } sp_file_t;
    36 
    37 /**
    38  * Creates a new SourcePawn binary file.  
    39  * You may optionally specify alternative writing functions.
    40  */
    41 sp_file_t *spfw_create(const char *name, sp_writefuncs_t *optfuncs);
    42 
    43 /**
    44  * Closes file handle and frees memory.
    45  */
    46 void spfw_destroy(sp_file_t *spf);
    47 
    48 /**
    49  * Adds a section name to the header. 
    50  * Only valid BEFORE finalization.
    51  * Returns the number of sections, or 0 on failure.
    52  */
    53 uint8_t spfw_add_section(sp_file_t *spf, const char *name);
    54 
    55 /**
    56  * Finalizes the section header.
    57  * This means no more sections can be added after this call.
    58  * Also, aligns the writer to the first section.
    59  * Returns 0 on success, nonzero on error.
    60  */
    61 int spfw_finalize_header(sp_file_t *spf);
    62 
    63 /**
    64  * Finalizes the current section and advances to the next.
    65  * In order for this to be accurate, the file pointer must
    66  *  reside at the end before calling this, because the size
    67  *  is calculated by differencing with the last known offset.
    68  * Returns 1 if there are more sections left, 0 otherwise.
    69  * Returns -1 if the file state is wrong.
    70  */
    71 int spfw_next_section(sp_file_t *spf);
    72 
    73 /**
    74  * Finalizes all sections.
    75  * Cannot be called until all sections are used.
    76  * Must be called with the file pointer at the end.
    77  * Also does compression!
    78  */
    79 int spfw_finalize_all(sp_file_t *spf);
    80 
    81 #endif //_INCLUDE_SPFILE_H