NCEPLIBS-bufr  12.0.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  /*
172  ** Call wrdlen to initialize some important information about the local machine, just in case
173  ** it hasn't already been called.
174  */
175  wrdlen_f();
176 
177  return;
178 }
179 
200 void
201 crbmg(char *bmg, int mxmb, int *nmb, int *iret)
202 {
203  int wkint[2];
204 
205  char errstr[129];
206  char blanks[5] = " ";
207 
208  /*
209  ** Make sure that a file is open for reading.
210  */
211  if (pbf[0] == NULL) {
212  sprintf(errstr, "BUFRLIB: CRBMG - NO FILE IS OPEN FOR READING");
213  bort_f(errstr);
214  }
215 
216  /*
217  ** Initialize the first 4 characters of the output array to blanks.
218  */
219  if (mxmb < 5) {
220  *iret = 1;
221  return;
222  }
223  strcpy(bmg, blanks);
224 
225  /*
226  ** Look for the start of the next BUFR message.
227  */
228  while (strncmp("BUFR", bmg, 4) != 0) {
229  memmove(bmg, &bmg[1], 3);
230  if ((*iret = rbytes(bmg, mxmb, 3, 1)) != 0) return;
231  }
232 
233  /*
234  ** Read the next 4 bytes of the BUFR message and get the length of the message.
235  */
236  if ((*iret = rbytes(bmg, mxmb, 4, 4)) != 0) return;
237  memcpy(wkint, bmg, 8);
238  *nmb = iupbs01_f(wkint, "LENM");
239 
240  /*
241  ** Read the remainder of the BUFR message.
242  */
243  if ((*iret = rbytes(bmg, mxmb, 8, *nmb-8)) != 0) return;
244 
245  /*
246  ** Check that the "7777" is in the expected location.
247  */
248  *iret = ((strncmp("7777", &bmg[*nmb-4], 4) == 0) ? 0 : 2);
249 
250  return;
251 }
252 
268 void
269 cwbmg(char *bmg, int nmb, int *iret)
270 {
271  char errstr[129];
272 
273  /*
274  ** Make sure that a file is open for writing.
275  */
276  if (pbf[1] == NULL) {
277  sprintf(errstr, "BUFRLIB: CWBMG - NO FILE IS OPEN FOR WRITING");
278  bort_f(errstr);
279  }
280 
281  /*
282  ** Write the BUFR message to the file.
283  */
284  *iret = ((fwrite(bmg, 1, nmb, pbf[1]) == nmb) ? 0 : -1);
285 
286  return;
287 }
288 
295 void
296 ccbfl(void)
297 {
298  unsigned short i;
299 
300  for (i = 0; i < 2; i++) {
301  if (pbf[i] != NULL) fclose(pbf[i]);
302  }
303 
304  return;
305 }
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 wrdlen_f(void)
Determine important information about the local machine.
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:269
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:201
void ccbfl(void)
Close all files that were opened via previous calls to function cobfl().
Definition: crwbmg.c:296
#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