NCEPLIBS-bufr  12.2.0
All Data Structures Namespaces Files Functions Variables Macros Pages
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(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  ** Copy the input arguments into local variables and check them for validity. This is especially
131  ** important in case either of the arguments was passed in as a string literal by the calling
132  ** program or else doesn't have a trailing NULL character.
133  */
134  for (i = 0; (! isspace(bfl[i]) && ! iscntrl(bfl[i])); i++) {
135  if (i == MXFNLEN) {
136  sprintf(errstr, "BUFRLIB: COBFL - INPUT FILENAME CONTAINS"
137  " MORE THAN %hu CHARACTERS", (unsigned short) MXFNLEN);
138  bort_f(errstr);
139  }
140  lbf[i] = bfl[i];
141  }
142  lbf[i] = '\0';
143 
144  lio = io;
145  if ((foparg[0] = (char) tolower(lio)) == 'r') {
146  j = 0;
147  }
148  else if (foparg[0] == 'w') {
149  j = 1;
150  }
151  else {
152  sprintf(errstr, "BUFRLIB: COBFL - SECOND ARGUMENT WAS (%c),"
153  " WHICH IS AN ILLEGAL VALUE", lio);
154  bort_f(errstr);
155  }
156 
157  /*
158  ** If a file of this type is already open, then close it before opening the new one.
159  */
160  if (pbf[j] != NULL) fclose(pbf[j]);
161 
162  /*
163  ** Open the requested file.
164  */
165  if ((pbf[j] = fopen(lbf, foparg)) == NULL) {
166  sprintf(errstr, "BUFRLIB: COBFL - COULD NOT OPEN FILE %s", lbf);
167  bort_f(errstr);
168  }
169 
170  return;
171 }
172 
193 void
194 crbmg(char *bmg, int mxmb, int *nmb, int *iret)
195 {
196  int wkint[2];
197 
198  char errstr[129];
199  char blanks[5] = " ";
200 
201  /*
202  ** Make sure that a file is open for reading.
203  */
204  if (pbf[0] == NULL) {
205  sprintf(errstr, "BUFRLIB: CRBMG - NO FILE IS OPEN FOR READING");
206  bort_f(errstr);
207  }
208 
209  /*
210  ** Initialize the first 4 characters of the output array to blanks.
211  */
212  if (mxmb < 5) {
213  *iret = 1;
214  return;
215  }
216  strcpy(bmg, blanks);
217 
218  /*
219  ** Look for the start of the next BUFR message.
220  */
221  while (strncmp(BMOSTR, bmg, 4) != 0) {
222  memmove(bmg, &bmg[1], 3);
223  if ((*iret = rbytes(bmg, mxmb, 3, 1)) != 0) return;
224  }
225 
226  /*
227  ** Read the next 4 bytes of the BUFR message and get the length of the message.
228  */
229  if ((*iret = rbytes(bmg, mxmb, 4, 4)) != 0) return;
230  memcpy(wkint, bmg, 8);
231  *nmb = iupbs01_f(wkint, "LENM");
232 
233  /*
234  ** Read the remainder of the BUFR message.
235  */
236  if ((*iret = rbytes(bmg, mxmb, 8, *nmb-8)) != 0) return;
237 
238  /*
239  ** Check that the "7777" is in the expected location.
240  */
241  *iret = ((strncmp(BMCSTR, &bmg[*nmb-4], 4) == 0) ? 0 : 2);
242 
243  return;
244 }
245 
261 void
262 cwbmg(char *bmg, int nmb, int *iret)
263 {
264  char errstr[129];
265 
266  /*
267  ** Make sure that a file is open for writing.
268  */
269  if (pbf[1] == NULL) {
270  sprintf(errstr, "BUFRLIB: CWBMG - NO FILE IS OPEN FOR WRITING");
271  bort_f(errstr);
272  }
273 
274  /*
275  ** Write the BUFR message to the file.
276  */
277  *iret = ((fwrite(bmg, 1, nmb, pbf[1]) == nmb) ? 0 : -1);
278 
279  return;
280 }
281 
288 void
289 ccbfl(void)
290 {
291  unsigned short i;
292 
293  for (i = 0; i < 2; i++) {
294  if (pbf[i] != NULL) fclose(pbf[i]);
295  }
296 
297  return;
298 }
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.
#define BMCSTR
Closing string of a BUFR message.
Definition: bufrlib.h:73
void bort_f(char *errstr)
Log one error message and abort application program.
#define BMOSTR
Opening string of a BUFR message.
Definition: bufrlib.h:70
void cobfl(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(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:262
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:194
void ccbfl(void)
Close all files that were opened via previous calls to function cobfl().
Definition: crwbmg.c:289
#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