NCEPLIBS-bufr  12.1.0
crwbmg.c
Go to the documentation of this file.
1 
6 #include "bufrlib.h"
7 
10 #define MXFNLEN 200
11 
14 FILE *pbf[2];
15 
37 int
38 rbytes(char *bmg, int mxmb, int isloc, int newbytes)
39 {
40  int iret;
41 
42  if ((isloc + newbytes) > mxmb) {
43  iret = 1;
44  }
45  else if (fread(&bmg[isloc], 1, newbytes, pbf[0]) != newbytes) {
46  iret = (feof(pbf[0]) ? -1 : -2);
47  }
48  else {
49  iret = 0;
50  }
51 
52  return iret;
53 }
54 
119 void
120 cobfl(char *bfl, char io)
121 {
122  char lbf[MXFNLEN+1];
123  char lio;
124 
125  char errstr[MXFNLEN+50];
126 
127  char foparg[3] = " b"; /* 3rd character will automatically initialize to NULL */
128  unsigned short i, j;
129 
130  /*
131  ** Copy the input arguments into local variables and check them for validity. This is especially
132  ** important in case either of the arguments was passed in as a string literal by the calling
133  ** program or else doesn't have a trailing NULL character.
134  */
135  for (i = 0; (! isspace(bfl[i]) && ! iscntrl(bfl[i])); i++) {
136  if (i == MXFNLEN) {
137  sprintf(errstr, "BUFRLIB: COBFL - INPUT FILENAME CONTAINS"
138  " MORE THAN %hu CHARACTERS", (unsigned short) MXFNLEN);
139  bort_f(errstr);
140  }
141  lbf[i] = bfl[i];
142  }
143  lbf[i] = '\0';
144 
145  lio = io;
146  if ((foparg[0] = (char) tolower(lio)) == 'r') {
147  j = 0;
148  }
149  else if (foparg[0] == 'w') {
150  j = 1;
151  }
152  else {
153  sprintf(errstr, "BUFRLIB: COBFL - SECOND ARGUMENT WAS (%c),"
154  " WHICH IS AN ILLEGAL VALUE", lio);
155  bort_f(errstr);
156  }
157 
158  /*
159  ** If a file of this type is already open, then close it before opening the new one.
160  */
161  if (pbf[j] != NULL) fclose(pbf[j]);
162 
163  /*
164  ** Open the requested file.
165  */
166  if ((pbf[j] = fopen(lbf, foparg)) == NULL) {
167  sprintf(errstr, "BUFRLIB: COBFL - COULD NOT OPEN FILE %s", lbf);
168  bort_f(errstr);
169  }
170 
171  return;
172 }
173 
194 void
195 crbmg(char *bmg, int mxmb, int *nmb, int *iret)
196 {
197  int wkint[2];
198 
199  char errstr[129];
200  char blanks[5] = " ";
201 
202  /*
203  ** Make sure that a file is open for reading.
204  */
205  if (pbf[0] == NULL) {
206  sprintf(errstr, "BUFRLIB: CRBMG - NO FILE IS OPEN FOR READING");
207  bort_f(errstr);
208  }
209 
210  /*
211  ** Initialize the first 4 characters of the output array to blanks.
212  */
213  if (mxmb < 5) {
214  *iret = 1;
215  return;
216  }
217  strcpy(bmg, blanks);
218 
219  /*
220  ** Look for the start of the next BUFR message.
221  */
222  while (strncmp("BUFR", bmg, 4) != 0) {
223  memmove(bmg, &bmg[1], 3);
224  if ((*iret = rbytes(bmg, mxmb, 3, 1)) != 0) return;
225  }
226 
227  /*
228  ** Read the next 4 bytes of the BUFR message and get the length of the message.
229  */
230  if ((*iret = rbytes(bmg, mxmb, 4, 4)) != 0) return;
231  memcpy(wkint, bmg, 8);
232  *nmb = iupbs01_f(wkint, "LENM");
233 
234  /*
235  ** Read the remainder of the BUFR message.
236  */
237  if ((*iret = rbytes(bmg, mxmb, 8, *nmb-8)) != 0) return;
238 
239  /*
240  ** Check that the "7777" is in the expected location.
241  */
242  *iret = ((strncmp("7777", &bmg[*nmb-4], 4) == 0) ? 0 : 2);
243 
244  return;
245 }
246 
262 void
263 cwbmg(char *bmg, int nmb, int *iret)
264 {
265  char errstr[129];
266 
267  /*
268  ** Make sure that a file is open for writing.
269  */
270  if (pbf[1] == NULL) {
271  sprintf(errstr, "BUFRLIB: CWBMG - NO FILE IS OPEN FOR WRITING");
272  bort_f(errstr);
273  }
274 
275  /*
276  ** Write the BUFR message to the file.
277  */
278  *iret = ((fwrite(bmg, 1, nmb, pbf[1]) == nmb) ? 0 : -1);
279 
280  return;
281 }
282 
289 void
290 ccbfl(void)
291 {
292  unsigned short i;
293 
294  for (i = 0; i < 2; i++) {
295  if (pbf[i] != NULL) fclose(pbf[i]);
296  }
297 
298  return;
299 }
int iupbs01_f(int *bufr, char *mnemonic)
Read a data value from Section 0 or Section 1 of a BUFR message.
Enable a number of NCEPLIBS-bufr subprograms to be called from within the C part of the library.
void bort_f(char *errstr)
Log one error message and abort application program.
void cobfl(char *bfl, char io)
Open a new file for reading or writing BUFR messages via a C language interface.
Definition: crwbmg.c:120
void cwbmg(char *bmg, int nmb, int *iret)
Write a BUFR message to the file that was opened via the most recent call to function cobfl() with io...
Definition: crwbmg.c:263
int rbytes(char *bmg, int mxmb, int isloc, int newbytes)
Read a specified number of bytes from the file that was opened via the most recent call to function c...
Definition: crwbmg.c:38
void crbmg(char *bmg, int mxmb, int *nmb, int *iret)
Read the next BUFR message from the file that was opened via the most recent call to function cobfl()...
Definition: crwbmg.c:195
void ccbfl(void)
Close all files that were opened via previous calls to function cobfl().
Definition: crwbmg.c:290
#define MXFNLEN
Maximum length of a filename, including any directory prefixes or other local filesystem notation.
Definition: crwbmg.c:10
FILE * pbf[2]
File pointers; each element will automatically initialize to NULL.
Definition: crwbmg.c:14