LAL  7.5.0.1-b72065a
StreamVectorInput.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include <lal/LALStdlib.h>
5 #include <lal/AVFactories.h>
6 #include <lal/StringInput.h>
7 #include <lal/StreamInput.h>
8 
9 /* Define linked-list of buffers for storing an arbitrary number of
10  arbitrary datatypes. */
11 #define BUFFSIZE 16
12 typedef union tagBuffer {
13  CHAR CH[BUFFSIZE];
14  INT2 I2[BUFFSIZE/2];
15  INT4 I4[BUFFSIZE/4];
16  INT8 I8[BUFFSIZE/8];
17  UINT2 U2[BUFFSIZE/2];
18  UINT4 U4[BUFFSIZE/4];
19  UINT8 U8[BUFFSIZE/8];
20  REAL4 S[BUFFSIZE/4];
21  REAL8 D[BUFFSIZE/8];
22 } Buffer;
23 typedef struct tagBufferList {
24  Buffer buf;
25  size_t size;
26  struct tagBufferList *next;
27 } BufferList;
28 
29 /* Define a macro for freeing the linked list. */
30 #define FREEBUFFERLIST( headPtr ) \
31 if ( headPtr ) { \
32  BufferList *herePtr = headPtr; \
33  while ( herePtr ) { \
34  BufferList *nextPtr = herePtr->next; \
35  LALFree( herePtr ); \
36  herePtr = nextPtr; \
37  } \
38 } else (void)(0)
39 
40 static const BufferList empty;
41 
42 
43 void
44 LALCHARReadVector( LALStatus *stat, CHARVector **vector, FILE *stream )
45 {
46  BufferList head = empty; /* head of linked list of buffers */
47  BufferList *here; /* pointer to current position in list */
48  CHAR *data; /* pointer to vector data */
49  BOOLEAN done = 0; /* whether or not to read more buffers */
50  size_t nTot; /* total number of characters read */
51 
52  INITSTATUS(stat);
53  ATTATCHSTATUSPTR( stat );
54 
55  /* Check for valid input arguments. */
58  ASSERT( !*vector, stat, STREAMINPUTH_EOUT, STREAMINPUTH_MSGEOUT );
59 
60  /* Read into the first buffer at the head of the list, and see if
61  more needs to be read. */
62  if ( !( fgets( head.buf.CH, BUFFSIZE, stream ) ) )
63  done = 1;
64  nTot = head.size = strlen( head.buf.CH );
65  done |= ( head.size < BUFFSIZE - 1 );
66  done |= ( head.buf.CH[BUFFSIZE-2] == '\n' );
67  here = &head;
68 
69  /* If we haven't yet reached the end of the line or file... */
70  while ( !done ) {
71 
72  /* Allocate next buffer. */
73  here->next = (BufferList *)LALMalloc( sizeof(BufferList) );
74  here = here->next;
75  if ( !here ) {
76  FREEBUFFERLIST( head.next );
78  }
79  memset( here, 0, sizeof(BufferList) );
80 
81  /* Read into the next buffer, and see if more needs to be read. */
82  if ( !( fgets( here->buf.CH, BUFFSIZE, stream ) ) )
83  done = 1;
84  fflush( stream );
85  nTot += here->size = strlen( here->buf.CH );
86  done |= ( here->size < BUFFSIZE - 1 );
87  done |= ( here->buf.CH[BUFFSIZE-2] == '\n' );
88  }
89 
90  /* Finished reading the line. Now allocate **vector. Include space
91  for a terminating '\0'. */
92  LALCHARCreateVector( stat->statusPtr, vector, nTot+1 );
93  BEGINFAIL( stat )
94  FREEBUFFERLIST( head.next );
95  ENDFAIL( stat );
96 
97  /* Copy buffer list into (*vector)->data, adding final '\0'. */
98  here = &head;
99  data = (*vector)->data;
100  while ( here ) {
101  memcpy( data, here->buf.CH, here->size );
102  data += here->size;
103  here = here->next;
104  }
105  (*vector)->data[nTot] = '\0';
106 
107  /* Free buffer list and exit. */
108  FREEBUFFERLIST( head.next );
109  DETATCHSTATUSPTR( stat );
110  RETURN( stat );
111 }
112 
113 #define TYPECODE I2
114 #define TYPE INT2
115 #define SIZE 2
116 #include "StreamVectorInput_source.c"
117 #undef TYPECODE
118 #undef TYPE
119 #undef SIZE
120 
121 #define TYPECODE I4
122 #define TYPE INT4
123 #define SIZE 4
124 #include "StreamVectorInput_source.c"
125 #undef TYPECODE
126 #undef TYPE
127 #undef SIZE
128 
129 #define TYPECODE I8
130 #define TYPE INT8
131 #define SIZE 8
132 #include "StreamVectorInput_source.c"
133 #undef TYPECODE
134 #undef TYPE
135 #undef SIZE
136 
137 #define TYPECODE U2
138 #define TYPE UINT2
139 #define SIZE 2
140 #include "StreamVectorInput_source.c"
141 #undef TYPECODE
142 #undef TYPE
143 #undef SIZE
144 
145 #define TYPECODE U4
146 #define TYPE UINT4
147 #define SIZE 4
148 #include "StreamVectorInput_source.c"
149 #undef TYPECODE
150 #undef TYPE
151 #undef SIZE
152 
153 #define TYPECODE U8
154 #define TYPE UINT8
155 #define SIZE 8
156 #include "StreamVectorInput_source.c"
157 #undef TYPECODE
158 #undef TYPE
159 #undef SIZE
160 
161 #define TYPECODE S
162 #define TYPE REAL4
163 #define SIZE 4
164 #include "StreamVectorInput_source.c"
165 #undef TYPECODE
166 #undef TYPE
167 #undef SIZE
168 
169 #define TYPECODE D
170 #define TYPE REAL8
171 #define SIZE 8
172 #include "StreamVectorInput_source.c"
173 #undef TYPECODE
174 #undef TYPE
175 #undef SIZE
#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)
static const BufferList empty
unsigned char BOOLEAN
Boolean logical type, see Headers LAL(Atomic)Datatypes.h for more details.
uint64_t UINT8
Eight-byte unsigned integer; on some platforms this is equivalent to unsigned long int instead.
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.
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
void LALCHARReadVector(LALStatus *stat, CHARVector **vector, FILE *stream)
void LALCHARCreateVector(LALStatus *, CHARVector **, UINT4)
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]