| 1 | /* An input parsing abstraction: an input buffer (file, stream, etc.) |
| 2 | */ |
| 3 | #ifndef eslBUFFER_INCLUDED |
| 4 | #define eslBUFFER_INCLUDED |
| 5 | #include <esl_config.h> |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | |
| 9 | #define eslBUFFER_PAGESIZE 4096 /* default for b->pagesize */ |
| 10 | #define eslBUFFER_SLURPSIZE 4194304 /* switchover from slurping whole file to mmap() */ |
| 11 | |
| 12 | enum esl_buffer_mode_e { |
| 13 | eslBUFFER_UNSET = 0, |
| 14 | eslBUFFER_STREAM = 1, /* chunk in mem[0..n-1] = input[baseoffset..baseoffset-n-1]; balloc>0; offset>=0; fp open */ |
| 15 | eslBUFFER_CMDPIPE = 2, /* chunk in mem[0..n-1] = input[baseoffset..baseoffset-n-1]; balloc>0; offset>=0; fp open */ |
| 16 | eslBUFFER_FILE = 3, /* chunk in mem[0..n-1] = input[baseoffset..baseoffset-n-1]; balloc>0; offset>=0; fp open */ |
| 17 | eslBUFFER_ALLFILE = 4, /* whole file in mem[0..n-1]; balloc=0; offset=0; fp=NULL */ |
| 18 | eslBUFFER_MMAP = 5, /* whole file in mem[0..n-1]; balloc=0; offset=0; fp=NULL */ |
| 19 | eslBUFFER_STRING = 6 /* whole str in mem[0..n-1]; balloc=0; offset=0; fp=NULL */ |
| 20 | }; |
| 21 | |
| 22 | typedef struct { |
| 23 | char *mem; /* the buffer */ |
| 24 | esl_pos_t n; /* curr buf length; mem[0..n-1] contains valid bytes */ |
| 25 | esl_pos_t balloc; /* curr buf alloc; mem[0..balloc-1] may be used */ |
| 26 | esl_pos_t pos; /* curr buf parse position; n-pos = # of parseable bytes */ |
| 27 | esl_pos_t baseoffset; /* offset of byte mem[0] in the stream */ |
| 28 | |
| 29 | esl_pos_t anchor; /* buf[anchor..n-1] safe from overwrite [-1=unset] */ |
| 30 | int nanchor; /* number of anchors set at <anchor> */ |
| 31 | |
| 32 | FILE *fp; /* open stream; NULL if already entirely in memory */ |
| 33 | char *filename; /* for diagnostics. filename; or NULL (stdin, string) */ |
| 34 | char *cmdline; /* for diagnostics. NULL, or cmd for CMDPIPE */ |
| 35 | |
| 36 | esl_pos_t pagesize; /* size of new <fp> reads. Guarantee: n-pos >= pagesize */ |
| 37 | |
| 38 | char errmsg[eslERRBUFSIZE]; /* error message storage */ |
| 39 | enum esl_buffer_mode_e mode_is; /* mode (stdin, cmdpipe, file, allfile, mmap, string) */ |
| 40 | } ESL_BUFFER; |
| 41 | |
| 42 | |
| 43 | /* 1. The ESL_BUFFER object: opening/closing. */ |
| 44 | extern int esl_buffer_Open (const char *filename, const char *envvar, ESL_BUFFER **ret_bf); |
| 45 | extern int esl_buffer_OpenFile (const char *filename, ESL_BUFFER **ret_bf); |
| 46 | extern int esl_buffer_OpenPipe (const char *filename, const char *cmdfmt, ESL_BUFFER **ret_bf); |
| 47 | extern int esl_buffer_OpenMem (const char *p, esl_pos_t n, ESL_BUFFER **ret_bf); |
| 48 | extern int esl_buffer_OpenStream(FILE *fp, ESL_BUFFER **ret_bf); |
| 49 | extern int esl_buffer_Close(ESL_BUFFER *bf); |
| 50 | |
| 51 | /* 2. Positioning and anchoring an ESL_BUFFER. */ |
| 52 | extern esl_pos_t esl_buffer_GetOffset (ESL_BUFFER *bf); |
| 53 | extern int esl_buffer_SetOffset (ESL_BUFFER *bf, esl_pos_t offset); |
| 54 | extern int esl_buffer_SetAnchor (ESL_BUFFER *bf, esl_pos_t offset); |
| 55 | extern int esl_buffer_SetStableAnchor(ESL_BUFFER *bf, esl_pos_t offset); |
| 56 | extern int esl_buffer_RaiseAnchor (ESL_BUFFER *bf, esl_pos_t offset); |
| 57 | |
| 58 | /* 3. Raw access to the buffer */ |
| 59 | extern int esl_buffer_Get(ESL_BUFFER *bf, char **ret_p, esl_pos_t *ret_n); |
| 60 | extern int esl_buffer_Set(ESL_BUFFER *bf, char *p, esl_pos_t nused); |
| 61 | |
| 62 | /* 4. Line-based parsing */ |
| 63 | extern int esl_buffer_GetLine (ESL_BUFFER *bf, char **opt_p, esl_pos_t *opt_n); |
| 64 | extern int esl_buffer_FetchLine (ESL_BUFFER *bf, char **opt_p, esl_pos_t *opt_n); |
| 65 | extern int esl_buffer_FetchLineAsStr(ESL_BUFFER *bf, char **opt_s, esl_pos_t *opt_n); |
| 66 | |
| 67 | /* 5. Token-based parsing */ |
| 68 | extern int esl_buffer_GetToken (ESL_BUFFER *bf, const char *sep, char **opt_p, esl_pos_t *opt_n); |
| 69 | extern int esl_buffer_FetchToken (ESL_BUFFER *bf, const char *sep, char **opt_p, esl_pos_t *opt_n); |
| 70 | extern int esl_buffer_FetchTokenAsStr(ESL_BUFFER *bf, const char *sep, char **opt_p, esl_pos_t *opt_n); |
| 71 | |
| 72 | /* 6. Binary (fread-like) parsing */ |
| 73 | extern int esl_buffer_Read(ESL_BUFFER *bf, size_t nbytes, void *p); |
| 74 | |
| 75 | |
| 76 | |
| 77 | /* Replaces functionality of esl_fileparser module as follows |
| 78 | * |
| 79 | * esl_fileparser_Open() -> esl_buffer_Open() |
| 80 | * esl_fileparser_Create() -> esl_buffer_OpenStream() |
| 81 | * esl_fileparser_CreateMapped() -> esl_buffer_OpenMem() |
| 82 | * esl_fileparser_SetCommentChar() -> esl_buffer_SetCommentChar() |
| 83 | * esl_fileparser_GetToken() -> esl_buffer_GetToken() |
| 84 | * esl_fileparser_NextLine() -> do { esl_buffer_GetLine() } while esl_line_IsBlank(); |
| 85 | * esl_fileparser_NextLinePeeked() -> unneeded. esl_buffer_Peek*() functionality, syntax different |
| 86 | * esl_fileparser_GetTokenOnLine() -> unneeded. esl_buffer_GetToken() has an idiom. |
| 87 | * esl_fileparser_GetRemainingLine() -> esl_buffer_GetLine() |
| 88 | * esl_fileparser_Destroy() -> esl_buffer_Close() |
| 89 | * esl_fileparser_Close() -> esl_buffer_Close() |
| 90 | */ |
| 91 | |
| 92 | /* Replaces functionality of esl_recorder module as follows: |
| 93 | * |
| 94 | * esl_recorder_Create() -> esl_buffer_OpenStream() |
| 95 | * esl_recorder_ResizeTo() |
| 96 | * esl_recorder_GetFirst() -> |
| 97 | * esl_recorder_GetLast() -> |
| 98 | * esl_recorder_GetCurrent() -> |
| 99 | * esl_recorder_GetNext() -> |
| 100 | * esl_recorder_Destroy() -> esl_buffer_Close() |
| 101 | * esl_recorder_Read() -> esl_buffer_GetLine() |
| 102 | * esl_recorder_Position() |
| 103 | * esl_recorder_MarkBlock() |
| 104 | * esl_recorder_UnmarkBlock() |
| 105 | * esl_recorder_GetBlock() |
| 106 | * |
| 107 | */ |
| 108 | |
| 109 | #endif /*eslBUFFER_INCLUDED*/ |