NCEPLIBS-g2c 2.3.0
Loading...
Searching...
No Matches
util.c
Go to the documentation of this file.
1
6
7#include "grib2_int.h"
8#include <stdarg.h>
9
13
40int
41g2c_check_msg(unsigned char *cgrib, g2int *lencurr, int verbose, int from)
42{
43 unsigned char G = 0x47; /* 'G' */
44 unsigned char R = 0x52; /* 'R' */
45 unsigned char I = 0x49; /* 'I' */
46 unsigned char B = 0x42; /* 'B' */
47 unsigned char seven = 0x37; /* '7' */
48
49 int iret = 0;
50 static int last_from = 0; /* Keep last value of from arg */
51
52 assert(cgrib && lencurr);
53
54 /* Check to see if beginning of GRIB message exists. */
55 if (cgrib[0] != G || cgrib[1] != R || cgrib[2] != I || cgrib[3] != B)
56 {
57 if (verbose)
58 printf("GRIB not found in given message. A call to routine g2_create() "
59 "is required to to initialize GRIB messge.\n");
60 /* Update last_from. */
61 MUTEX_LOCK(m);
62 last_from = from;
63 MUTEX_UNLOCK(m);
64 return G2C_ENOTGRIB;
65 }
66
67 /* Get current length of GRIB message. */
68 gbit(cgrib, lencurr, 96, 32);
69
70 /* Check to see if GRIB message is already complete. */
71 if (cgrib[*lencurr - 4] == seven && cgrib[*lencurr - 3] == seven &&
72 cgrib[*lencurr - 2] == seven && cgrib[*lencurr - 1] == seven)
73 {
74 /* Allow this to occur if this is called from g2_gribend (from = 4)
75 * and previous call was from g2_addfield where last_from = 3. */
76 if (from == 4 && last_from == 3)
77 {
78 iret = G2C_NOERROR;
79 }
80 else
81 {
82 if (verbose)
83 printf("GRIB message already complete. Cannot add new section.\n");
84 iret = G2C_EMSGCOMPLETE;
85 }
86 }
87
88 /* Update last_from. */
89 MUTEX_LOCK(m);
90 last_from = from;
91 MUTEX_UNLOCK(m);
92
93 return iret;
94}
95
96#ifdef LOGGING
97/* This is the severity level of messages which will be logged. Use
98 severity 0 for errors, 1 for important log messages, 2 for less
99 important, etc. */
100int g2_log_level = -1;
101
102/* This function prints out a message, if the severity of
103 * the message is lower than the global g2_log_level. To use it, do
104 * something like this:
105 *
106 * g2_log(0, "this computer will explode in %d seconds", i);
107 *
108 * After the first arg (the severity), use the rest like a normal
109 * printf statement. Output will appear on stderr.
110 *
111 * This function is not included in the build unless NCEPLIBS-g2c was
112 * built with -DLOGGING.
113 *
114 * Ed Hartnett
115 */
116void
117g2_log(int severity, const char *fmt, ...)
118{
119 va_list argp;
120 int t;
121 FILE *f = stderr;
122
123 /* If the severity is greater than the log level, we don't print
124 * this message. */
125 if (severity > g2_log_level)
126 return;
127
128 /* If the severity is zero, this is an error. Otherwise insert that
129 many tabs before the message. */
130 if (!severity)
131 fprintf(f, "ERROR: ");
132 for (t = 0; t < severity; t++)
133 fprintf(f, "\t");
134
135 /* Print out the variable list of args with vprintf. */
136 va_start(argp, fmt);
137 vfprintf(f, fmt, argp);
138 va_end(argp);
139
140 /* Put on a final linefeed. */
141 fprintf(f, "\n");
142 fflush(f);
143}
144#endif /* LOGGING */
145
164int
165g2c_set_log_level(int new_level)
166{
167#ifdef LOGGING
168 /* Remember the new level. */
169 g2_log_level = new_level;
170
171 LOG((1, "log_level changed to %d", g2_log_level));
172#endif
173 return G2C_NOERROR;
174}
void gbit(unsigned char *in, g2int *iout, g2int iskip, g2int nbits)
Get arbitrary size values from a packed bit string, right justifying each value in the unpacked iout ...
Definition gbits.c:20
#define G2C_ENOTGRIB
GRIB header not found.
Definition grib2.h:492
#define G2C_EMSGCOMPLETE
GRIB message already complete.
Definition grib2.h:493
int64_t g2int
Long integer type.
Definition grib2.h:32
#define G2C_NOERROR
No error.
Definition grib2.h:490
Header file with internal function prototypes NCEPLIBS-g2c library.
#define MUTEX_UNLOCK(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:119
#define LOG(e)
Ignore logging to stdout.
Definition grib2_int.h:428
#define MUTEX_LOCK(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:116
#define EXTERN_MUTEX(m)
Pthreads not enabled, so do nothing.
Definition grib2_int.h:113
int g2c_set_log_level(int new_level)
Use this to set the global log level.
Definition util.c:165
int g2c_check_msg(unsigned char *cgrib, g2int *lencurr, int verbose, int from)
Check for 'GRIB' at the beginning of a GRIB message, and check to see if the message is already termi...
Definition util.c:41