bufr2synop 0.24.0
bufr2tac_io.c
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2013-2018 by Guillermo Ballester Valor *
3 * gbv@ogimet.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20/*!
21 \file bufr2tac_io.c
22 \brief file with the i/o code for binary bufr2tac
23 */
24
25#include "bufr2tac.h"
26
27/*!
28 \fn int read_bufr(unsigned char *bufr, char *filename, int *length)
29 \brief read a bufr file as an array of unsigned chars
30 \param bufr pointer to an array of unsigned chars. On output it will contain the bufr
31 \param filename string with complete pathname of bufr file to read
32 \param length On input max length allocated by caller. On output real length of bufr
33*/
34int read_bufr ( unsigned char *bufr, char *filename, int *length )
35{
36 int aux;
37 size_t n = 0;
38 FILE *fp;
39
40 /* Open input file */
41 if ( ( fp = fopen ( filename, "r" ) ) == NULL )
42 {
43 fprintf ( stderr, "cannot open file '%s'\n", filename );
44 exit ( EXIT_FAILURE );
45 }
46
47 while ( ( aux = fgetc ( fp ) ) != EOF && ( int ) n < *length )
48 bufr[n++] = aux;
49 *length = n;
50
51 // close the file
52 fclose ( fp );
53
54 if ( n < 8 ) // we need at least 8 bytes
55 return -4;
56
57 // check if begins with BUFR
58 if ( bufr[0] != 'B' || bufr[1] != 'U' || bufr[2] != 'F' || bufr[3] != 'R' )
59 return -1;
60
61 // check if end with '7777'
62 if ( bufr[n - 4] != '7' || bufr[n - 3] != '7' || bufr[n - 2] != '7'
63 || bufr[n - 1] != '7' )
64 return -2;
65
66 //printf("%d %d\n", n, three_bytes_to_uint(bufr + 4));
67 // check about the expected size
68 if ( n != three_bytes_to_uint ( bufr + 4 ) )
69 return -3;
70
71 return 0;
72}
Include header file for binary bufr2tac.
unsigned int three_bytes_to_uint(const unsigned char *source)
returns the integer value from an array of three bytes, most significant first
int read_bufr(unsigned char *bufr, char *filename, int *length)
read a bufr file as an array of unsigned chars
Definition: bufr2tac_io.c:34