bufr2synop 0.24.0
bufrnoaa_utils.c
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2013-2017 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 bufrnoaa_utils.c
22 \brief File with some routines for bufrnoaa binary
23*/
24#include "bufrnoaa.h"
25
26/*!
27 \fn int is_bufr(unsigned char *b)
28 \brief checks if an unsigned char from an array is the first char of 'BUFR'
29 \param b pointer to unsigned char to check
30
31 Returns 1 if success, 0 othewise
32*/
33int is_bufr ( unsigned char *b )
34{
35 if ( b[0] == 'R' && b[1] == 'F' && b[2] == 'U' && b[3] == 'B' )
36 return 1;
37 return 0;
38}
39
40/*!
41 \fn int is_head(unsigned char *b)
42 \brief checks if an unsigned char from an array is the first char of '****'
43 \param b pointer to unsigned char to check
44
45 Returns 1 if success, 0 othewise
46*/
47int is_head ( unsigned char *b )
48{
49 if ( b[0] == '*' && b[1] == '*' && b[2] == '*' && b[3] == '*' )
50 return 1;
51 return 0;
52}
53
54/*!
55 \fn int is_head_custom(unsigned char *b, char mark)
56 \brief checks if an unsigned char from an array is the first char of repeated mark char four times
57 \param b pointer to unsigned char to check
58 \param mark char to be found repeated four times since \a b
59 Returns 1 if success, 0 othewise
60*/
61int is_head_custom ( unsigned char *b, char mark )
62{
63 if ( b[0] == mark && b[1] == mark && b[2] == mark && b[3] == mark )
64 return 1;
65 return 0;
66}
67
68
69/*!
70 \fn int is_endb(unsigned char *b)
71 \brief checks if an unsigned char from an array is the first char of '7777'
72 \param b pointer to unsigned char to check
73
74 Returns 1 if success, 0 othewise
75*/
76int is_endb ( unsigned char *b )
77{
78 if ( b[0] == '7' && b[1] == '7' && b[2] == '7' && b[3] == '7' )
79 return 1;
80 return 0;
81}
82
83/*!
84 \fn int timeval_substract (struct timeval *result, struct timeval *x, struct timeval *y)
85 \brief Subtract the `struct timeval' values X and Y, storing the result in RESULT.
86 \param result pointer to a struct timeval where to set the result
87 \param x pointer to struct timeval as X in (X - Y)
88 \param y pointer to struct timeval as Y in (X - Y)
89
90 Return 1 if the difference is negative, otherwise 0.
91*/
92
93int timeval_substract ( struct timeval *result, struct timeval *x,
94 struct timeval *y )
95{
96 /* Perform the carry for the later subtraction by updating Y. */
97 if ( x->tv_usec < y->tv_usec )
98 {
99 int nsec = ( y->tv_usec - x->tv_usec ) / 1000000 + 1;
100 y->tv_usec -= 1000000 * nsec;
101 y->tv_sec += nsec;
102 }
103 if ( x->tv_usec - y->tv_usec > 1000000 )
104 {
105 int nsec = ( x->tv_usec - y->tv_usec ) / 1000000;
106 y->tv_usec += 1000000 * nsec;
107 y->tv_sec -= nsec;
108 }
109
110 /* Compute the time remaining to wait.
111 `tv_usec' is certainly positive. */
112 result->tv_sec = x->tv_sec - y->tv_sec;
113 result->tv_usec = x->tv_usec - y->tv_usec;
114
115 /* Return 1 if result is negative. */
116 return x->tv_sec < y->tv_sec;
117}
118
119/*!
120 \fn int bufr_is_selected(char *name)
121 \brief returns 1 if selected message 0 otherwise
122 \param name string with name to check
123*/
124int bufr_is_selected ( char *name )
125{
126 if ( strlen ( SEL ) == 0 )
127 return 0;
128 if ( strchr ( SEL, name[1] ) == NULL )
129 return 0;
130 if ( name[1] == 'S' )
131 {
132 if ( strlen ( SELS ) == 0 )
133 return 1;
134 else if ( strchr ( SELS, name[2] ) == NULL )
135 return 0;
136 }
137
138 if ( name[1] == 'O' )
139 {
140 if ( strlen ( SELO ) == 0 )
141 return 1;
142 else if ( strchr ( SELO, name[2] ) == NULL )
143 return 0;
144 }
145
146 if ( name[1] == 'U' )
147 {
148 if ( strlen ( SELU ) == 0 )
149 return 1;
150 else if ( strchr ( SELU, name[2] ) == NULL )
151 return 0;
152 }
153
154 return 1;
155}
156
157/*!
158 \fn nt date_mtime_from_stat(char *date, struct stat *st)
159 \brief get a string with date and time from a struct stat
160 \param date string wuth the result
161 \param st pointer to a strucr stat
162*/
163int date_mtime_from_stat ( char *date, struct stat *st )
164{
165 struct tm tim;
166 memset ( &tim, 0, sizeof ( tim ) );
167 gmtime_r ( & ( st->st_mtime ), &tim );
168 strftime ( date, 16, "%Y%m%d%H%M%S", &tim );
169 return 1;
170}
171
172/*!
173 \fn int mtime_from_stat(char *filename, struct stat *st)
174 \brief modifies a file update time from a struct stat
175 \param filename string with the pathname of file to modify
176 \param st pointer to a struct stat as reference
177*/
178int mtime_from_stat ( char *filename, struct stat *st )
179{
180 struct utimbuf ut;
181 ut.modtime = st->st_mtime;
182 ut.actime = st->st_mtime;
183 utime ( filename, &ut );
184 return 1;
185}
char SELU[64]
Definition: bufrnoaa.c:199
char SELS[64]
Definition: bufrnoaa.c:198
char SELO[64]
Definition: bufrnoaa.c:200
inclusion file for binary bufrnoaa
int bufr_is_selected(char *name)
returns 1 if selected message 0 otherwise
int is_head_custom(unsigned char *b, char mark)
checks if an unsigned char from an array is the first char of repeated mark char four times
int is_endb(unsigned char *b)
checks if an unsigned char from an array is the first char of '7777'
int mtime_from_stat(char *filename, struct stat *st)
modifies a file update time from a struct stat
int is_head(unsigned char *b)
checks if an unsigned char from an array is the first char of '****'
int is_bufr(unsigned char *b)
checks if an unsigned char from an array is the first char of 'BUFR'
int date_mtime_from_stat(char *date, struct stat *st)
get a string with date and time from a struct stat
int timeval_substract(struct timeval *result, struct timeval *x, struct timeval *y)
Subtract the ‘struct timeval’ values X and Y, storing the result in RESULT.