NCEPLIBS-bufr  12.3.0
crwbmg.c
Go to the documentation of this file.
1 
6 #include "bufrlib.h"
7 
10 #define MXFNLEN 200
11 
13 FILE *pbf[2];
14 
36 int
37 rbytes(char *bmg, int mxmb, int isloc, int newbytes)
38 {
39  int iret;
40 
41  if ((isloc + newbytes) > mxmb) {
42  iret = 1;
43  }
44  else if (fread(&bmg[isloc], 1, newbytes, pbf[0]) != newbytes) {
45  iret = (feof(pbf[0]) ? -1 : -2);
46  }
47  else {
48  iret = 0;
49  }
50 
51  return iret;
52 }
53 
118 void
119 cobfl(const char *bfl, char io)
120 {
121  char lbf[MXFNLEN+1];
122  char lio;
123 
124  char errstr[MXFNLEN+50];
125 
126  char foparg[3] = " b"; /* 3rd character will automatically initialize to NULL */
127  unsigned short i, j;
128 
129  /*
130  ** If we're catching bort errors, set a target return location if one doesn't already exist.
131  */
132  if (bort_target_set_f() == 1) {
133  catch_bort_cobfl(bfl, io);
135  return;
136  }
137 
138  /*
139  ** Copy the input arguments into local variables and check them for validity. This is especially
140  ** important in case either of the arguments was passed in as a string literal by the calling
141  ** program or else doesn't have a trailing NULL character.
142  */
143  for (i = 0; (! isspace(bfl[i]) && ! iscntrl(bfl[i])); i++) {
144  if (i == MXFNLEN) {
145  sprintf(errstr, "BUFRLIB: COBFL - INPUT FILENAME CONTAINS"
146  " MORE THAN %hu CHARACTERS", (unsigned short) MXFNLEN);
147  bort_f(errstr);
148  }
149  lbf[i] = bfl[i];
150  }
151  lbf[i] = '\0';
152 
153  lio = io;
154  if ((foparg[0] = (char) tolower(lio)) == 'r') {
155  j = 0;
156  }
157  else if (foparg[0] == 'w') {
158  j = 1;
159  }
160  else {
161  sprintf(errstr, "BUFRLIB: COBFL - SECOND ARGUMENT WAS (%c),"
162  " WHICH IS AN ILLEGAL VALUE", lio);
163  bort_f(errstr);
164  }
165 
166  /*
167  ** If a file of this type is already open, then close it before opening the new one.
168  */
169  if (pbf[j] != NULL) fclose(pbf[j]);
170 
171  /*
172  ** Open the requested file.
173  */
174  if ((pbf[j] = fopen(lbf, foparg)) == NULL) {
175  sprintf(errstr, "BUFRLIB: COBFL - COULD NOT OPEN FILE %s", lbf);
176  bort_f(errstr);
177  }
178 
179  return;
180 }
181 
202 void
203 crbmg(char *bmg, int mxmb, int *nmb, int *iret)
204 {
205  int wkint[2];
206 
207  char errstr[129];
208  char blanks[5] = " ";
209 
210  /*
211  ** If we're catching bort errors, set a target return location if one doesn't already exist.
212  */
213  if (bort_target_set_f() == 1) {
214  catch_bort_crbmg(bmg, mxmb, nmb, iret);
216  return;
217  }
218 
219  /*
220  ** Make sure that a file is open for reading.
221  */
222  if (pbf[0] == NULL) {
223  sprintf(errstr, "BUFRLIB: CRBMG - NO FILE IS OPEN FOR READING");
224  bort_f(errstr);
225  }
226 
227  /*
228  ** Initialize the first 4 characters of the output array to blanks.
229  */
230  if (mxmb < 5) {
231  *iret = 1;
232  return;
233  }
234  strcpy(bmg, blanks);
235 
236  /*
237  ** Look for the start of the next BUFR message.
238  */
239  while (strncmp(BMOSTR, bmg, 4) != 0) {
240  memmove(bmg, &bmg[1], 3);
241  if ((*iret = rbytes(bmg, mxmb, 3, 1)) != 0) return;
242  }
243 
244  /*
245  ** Read the next 4 bytes of the BUFR message and get the length of the message.
246  */
247  if ((*iret = rbytes(bmg, mxmb, 4, 4)) != 0) return;
248  memcpy(wkint, bmg, 8);
249  *nmb = iupbs01_f(wkint, "LENM");
250 
251  /*
252  ** Read the remainder of the BUFR message.
253  */
254  if ((*iret = rbytes(bmg, mxmb, 8, *nmb-8)) != 0) return;
255 
256  /*
257  ** Check that the "7777" is in the expected location.
258  */
259  *iret = ((strncmp(BMCSTR, &bmg[*nmb-4], 4) == 0) ? 0 : 2);
260 
261  return;
262 }
263 
279 void
280 cwbmg(const char *bmg, int nmb, int *iret)
281 {
282  char errstr[129];
283 
284  /*
285  ** If we're catching bort errors, set a target return location if one doesn't already exist.
286  */
287  if (bort_target_set_f() == 1) {
288  catch_bort_cwbmg(bmg, nmb, iret);
290  return;
291  }
292 
293  /*
294  ** Make sure that a file is open for writing.
295  */
296  if (pbf[1] == NULL) {
297  sprintf(errstr, "BUFRLIB: CWBMG - NO FILE IS OPEN FOR WRITING");
298  bort_f(errstr);
299  }
300 
301  /*
302  ** Write the BUFR message to the file.
303  */
304  *iret = ((fwrite(bmg, 1, nmb, pbf[1]) == nmb) ? 0 : -1);
305 
306  return;
307 }
308 
315 void
316 ccbfl(void)
317 {
318  unsigned short i;
319 
320  for (i = 0; i < 2; i++) {
321  if (pbf[i] != NULL) fclose(pbf[i]);
322  }
323 
324  return;
325 }
void catch_bort_crbmg(char *bmg, int mxmb, int *nmb, int *iret)
Catch any bort error inside of function crbmg().
Definition: borts.c:625
void catch_bort_cwbmg(const char *bmg, int nmb, int *iret)
Catch any bort error inside of function cwbmg().
Definition: borts.c:645
void catch_bort_cobfl(const char *bfl, char io)
Catch any bort error inside of function cobfl().
Definition: borts.c:604
int iupbs01_f(int *bufr, const 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.
#define BMCSTR
Closing string of a BUFR message.
Definition: bufrlib.h:76
void bort_target_unset_f(void)
Clear any existing bort target.
void bort_f(const char *errstr)
Log one error message and abort application program.
int bort_target_set_f(void)
Sets a new bort target, if bort catching is enabled and such a target doesn't already exist.
#define BMOSTR
Opening string of a BUFR message.
Definition: bufrlib.h:73
void cobfl(const char *bfl, char io)
Open a new file for reading or writing BUFR messages via a C language interface.
Definition: crwbmg.c:119
void cwbmg(const 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:280
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:37
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:203
void ccbfl(void)
Close all files that were opened via previous calls to function cobfl().
Definition: crwbmg.c:316
#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:13