Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
StreamSequenceInput.c
Go to the documentation of this file.
1#include <complex.h>
2#include <stdio.h>
3#include <string.h>
4#include <zlib.h>
5#include <lal/LALStdlib.h>
6#include <lal/AVFactories.h>
7#include <lal/StringInput.h>
8#include <lal/StreamInput.h>
9
10/* Define linked-list of buffers for storing an arbitrary number of
11 arbitrary datatypes. */
12#define BUFFSIZE 1024
13typedef union tagBuffer {
25} Buffer;
26typedef struct tagBufferList {
28 size_t size;
29 struct tagBufferList *next;
31
32/* Define a macro for freeing the linked list. */
33#define FREEBUFFERLIST( headPtr ) \
34if ( headPtr ) { \
35 BufferList *herePtr = headPtr; \
36 while ( herePtr ) { \
37 BufferList *nextPtr = herePtr->next; \
38 LALFree( herePtr ); \
39 herePtr = nextPtr; \
40 } \
41} else (void)(0)
42
43/* static const BufferList empty; */
44
45
46void
47LALCHARReadSequence( LALStatus *stat, CHARSequence **sequence, FILE *stream )
48{
49 BufferList head; /* head of linked list of buffers */
50 BufferList *here; /* pointer to current position in list */
51 CHAR *data; /* pointer to vector data */
52 size_t nTot = 0; /* total number of characters read */
53
54 INITSTATUS(stat);
55 ATTATCHSTATUSPTR( stat );
56
57 /* Check for valid input arguments. */
60 ASSERT( !*sequence, stat, STREAMINPUTH_EOUT, STREAMINPUTH_MSGEOUT );
61
62 /* Read file into linked list of buffers. */
63 here = &head;
64 here->next = NULL;
65 while ( !feof( stream ) ) {
66 size_t n = BUFFSIZE;
67 data = here->buf.CH;
68 while ( !feof( stream ) && n )
69 {
70 *(data++) = (CHAR)getc( stream );
71 n--;
72 }
73 /* The very last value returned by getc() is EOF, which is not a
74 character and should not be stored. */
75 if ( feof( stream ) ) {
76 data--;
77 n++;
78 }
79 here->size = BUFFSIZE - n;
80 nTot += here->size;
81 if ( !feof( stream ) ) {
82 here->next = (BufferList *)LALMalloc( sizeof(BufferList) );
83 if ( !(here->next) ) {
84 FREEBUFFERLIST( head.next );
86 }
87 here = here->next;
88 here->next = NULL;
89 }
90 }
91
92 /* Allocate **sequence, include space for a terminating '\0'. */
93 LALCHARCreateVector( stat->statusPtr, sequence, nTot+1 );
94 BEGINFAIL( stat )
95 FREEBUFFERLIST( head.next );
96 ENDFAIL( stat );
97
98 /* Copy buffer list into (*sequence)->data, adding final '\0'. */
99 here = &head;
100 data = (*sequence)->data;
101 while ( here ) {
102 memcpy( data, here->buf.CH, here->size );
103 data += here->size;
104 here = here->next;
105 }
106 (*sequence)->data[nTot] = '\0';
107
108 /* Free buffer list and exit. */
109 FREEBUFFERLIST( head.next );
110 DETATCHSTATUSPTR( stat );
111 RETURN( stat );
112}
113
114
115
116int
117XLALCHARReadSequence( CHARSequence **sequence, FILE *stream )
118{
119 BufferList head; /* head of linked list of buffers */
120 BufferList *here; /* pointer to current position in list */
121 CHAR *data; /* pointer to vector data */
122 size_t nTot = 0; /* total number of characters read */
123
124 /* Check for valid input arguments. */
125 if ( !stream || !sequence ) {
127 return STREAMINPUTH_ENUL;
128 }
129 if ( *sequence != NULL ) {
131 return STREAMINPUTH_EOUT;
132 }
133
134 /* Re-open file with zlib */
135 gzFile gzstream = gzdopen(dup(fileno(stream)), "rb");
136 if ( !gzstream ) {
138 return STREAMINPUTH_ENUL;
139 }
140
141 /* Read file into linked list of buffers. */
142 here = &head;
143 here->next = NULL;
144 while ( !gzeof( gzstream ) ) {
145 size_t n = BUFFSIZE;
146 data = here->buf.CH;
147 while ( !gzeof( gzstream ) && n )
148 {
149 int thisChar = gzgetc( gzstream );
150 if ( thisChar == -1 ) {
151 break;
152 }
153 *(data++) = thisChar;
154 n--;
155 }
156 here->size = BUFFSIZE - n;
157 nTot += here->size;
158 if ( !gzeof( gzstream ) ) {
159 here->next = (BufferList *)LALMalloc( sizeof(BufferList) );
160 if ( !(here->next) ) {
161 gzclose( gzstream );
162 FREEBUFFERLIST( head.next );
164 return STREAMINPUTH_EMEM;
165 }
166 here = here->next;
167 here->next = NULL;
168 }
169 }
170
171 /* Close zlib stream */
172 gzclose( gzstream );
173
174 /* Allocate **sequence, include space for a terminating '\0'. */
175 *sequence = XLALCreateCHARVector( nTot+1 );
176 if ( ! *sequence ) {
177 FREEBUFFERLIST( head.next );
178 }
179
180 /* Copy buffer list into (*sequence)->data, adding final '\0'. */
181 here = &head;
182 data = (*sequence)->data;
183 while ( here ) {
184 memcpy( data, here->buf.CH, here->size );
185 data += here->size;
186 here = here->next;
187 }
188 (*sequence)->data[nTot] = '\0';
189
190 /* Free buffer list and exit. */
191 FREEBUFFERLIST( head.next );
192
193 return 0;
194}
195
196
197/* tell the GNU compiler to ignore issues with the `ll' length modifier */
198#ifdef __GNUC__
199#define fscanf __extension__ fscanf
200#endif
201
202#define TYPECODE I2
203#define TYPE INT2
204#define SIZE 2
205#include "StreamSequenceInput_source.c"
206#undef TYPECODE
207#undef TYPE
208#undef SIZE
209
210#define TYPECODE I4
211#define TYPE INT4
212#define SIZE 4
213#include "StreamSequenceInput_source.c"
214#undef TYPECODE
215#undef TYPE
216#undef SIZE
217
218#define TYPECODE I8
219#define TYPE INT8
220#define SIZE 8
221#include "StreamSequenceInput_source.c"
222#undef TYPECODE
223#undef TYPE
224#undef SIZE
225
226#define TYPECODE U2
227#define TYPE UINT2
228#define SIZE 2
229#include "StreamSequenceInput_source.c"
230#undef TYPECODE
231#undef TYPE
232#undef SIZE
233
234#define TYPECODE U4
235#define TYPE UINT4
236#define SIZE 4
237#include "StreamSequenceInput_source.c"
238#undef TYPECODE
239#undef TYPE
240#undef SIZE
241
242#define TYPECODE U8
243#define TYPE UINT8
244#define SIZE 8
245#include "StreamSequenceInput_source.c"
246#undef TYPECODE
247#undef TYPE
248#undef SIZE
249
250#define TYPECODE S
251#define TYPE REAL4
252#define SIZE 4
253#include "StreamSequenceInput_source.c"
254#undef TYPECODE
255#undef TYPE
256#undef SIZE
257
258#define TYPECODE D
259#define TYPE REAL8
260#define SIZE 8
261#include "StreamSequenceInput_source.c"
262#undef TYPECODE
263#undef TYPE
264#undef SIZE
265
266#define TYPECODE Z
267#define TYPE COMPLEX16
268#define DATA REAL8
269#define SIZE 16
270#define FORMAT LAL_REAL8_FORMAT
271#include "StreamSequenceInputComplex_source.c"
272#undef TYPECODE
273#undef TYPE
274#undef DATA
275#undef SIZE
276#undef FORMAT
277
278#define TYPECODE C
279#define TYPE COMPLEX8
280#define DATA REAL4
281#define SIZE 8
282#define FORMAT LAL_REAL4_FORMAT
283#include "StreamSequenceInputComplex_source.c"
284#undef TYPECODE
285#undef TYPE
286#undef DATA
287#undef SIZE
288#undef FORMAT
#define LALMalloc(n)
Definition: LALMalloc.h:93
#define ABORT(statusptr, code, mesg)
#define ENDFAIL(statusptr)
#define ATTATCHSTATUSPTR(statusptr)
#define ASSERT(assertion, statusptr, code, mesg)
#define DETATCHSTATUSPTR(statusptr)
#define INITSTATUS(statusptr)
#define RETURN(statusptr)
#define BEGINFAIL(statusptr)
#define STREAMINPUTH_MSGEOUT
Definition: StreamInput.h:94
#define STREAMINPUTH_MSGEMEM
Definition: StreamInput.h:95
#define STREAMINPUTH_MSGENUL
Definition: StreamInput.h:93
#define BUFFSIZE
#define FREEBUFFERLIST(headPtr)
#define fprintf
uint64_t UINT8
Eight-byte unsigned integer; on some platforms this is equivalent to unsigned long int instead.
double complex COMPLEX16
Double-precision floating-point complex number (16 bytes total)
double REAL8
Double precision real floating-point number (8 bytes).
int16_t INT2
Two-byte signed integer.
int64_t INT8
Eight-byte signed integer; on some platforms this is equivalent to long int instead.
uint16_t UINT2
Two-byte unsigned integer.
char CHAR
One-byte signed integer, see Headers LAL(Atomic)Datatypes.h for more details.
uint32_t UINT4
Four-byte unsigned integer.
float complex COMPLEX8
Single-precision floating-point complex number (8 bytes total)
int32_t INT4
Four-byte signed integer.
float REAL4
Single precision real floating-point number (4 bytes).
#define STREAMINPUTH_EOUT
Output handle points to a non-null pointer.
Definition: StreamInput.h:81
#define STREAMINPUTH_ENUL
Unexpected null pointer in arguments.
Definition: StreamInput.h:80
#define STREAMINPUTH_EMEM
Memory allocation error.
Definition: StreamInput.h:82
int XLALCHARReadSequence(CHARSequence **sequence, FILE *stream)
void LALCHARReadSequence(LALStatus *stat, CHARSequence **sequence, FILE *stream)
void LALCHARCreateVector(LALStatus *, CHARVector **, UINT4)
CHARVector * XLALCreateCHARVector(UINT4 length)
struct tagBufferList * next
Vector of type CHAR, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:73
LAL status structure, see The LALStatus structure for more details.
Definition: LALDatatypes.h:947
struct tagLALStatus * statusPtr
Pointer to the next node in the list; NULL if this function is not reporting a subroutine error.
Definition: LALDatatypes.h:954
CHAR CH[BUFFSIZE]