source: trunk/WRF.COMMON/WRFV3/external/io_grib1/MEL_grib1/init_gribhdr.c

Last change on this file was 2759, checked in by aslmd, 2 years ago

adding unmodified code from WRFV3.0.1.1, expurged from useless data +1M size

File size: 8.3 KB
Line 
1/*  File:  init_gribhdr.c               Alice T. Nakajima, SAIC, 10/96
2    funcs to make storage and free up storage for Grib header struct
3*/
4#include <stdio.h>
5#include <stdlib.h>
6#include "dprints.h"            /* for dprints */
7#include "gribfuncs.h"          /* prototypes */
8/*
9*
10****************************************************************************
11* A. FUNCTION  init_gribhdr
12*       Allocates storage for Grib Header and its entire_msg and initialize
13*       every of its attributes.
14*
15*    INTERFACE:
16*       int     init_gribhdr (ppgrib_hdr, errmsg)
17*
18*    ARGUMENTS (I=input, O=output, I&O=input and output):
19*      (O)  GRIB_HDR **ppgrib_hdr;
20*           Grib Header structure, Null upon entry;  Returns pointing to a
21*           newly created storage.  Its attribute 'entire_msg' will point
22*           to a block of size indicated in 'abs_size' (initially set to
23*           DEF_MSG_LEN bytes, see grib.h).  'entire_msg' may later be
24*           expanded by other functions if required, but 'abs_size' must
25*           be updated to the expanded byte length.
26*      (O)  char *errmsg;
27*           empty array, returned filled if error occurred;
28*
29*    RETURNS: 
30*     0>  no error; storage for grib header and its entire_msg array created
31*         and cleared;  msg_length and all section lengths are set to zero,
32*         all section pointers are Null; abs_size is set to DEF_MSG_LEN;
33*         'shuffled' flag is set to zero;
34*     1>  failed, see errmsg;
35****************************************************************************
36*/
37#if PROTOTYPE_NEEDED
38int  init_gribhdr ( GRIB_HDR **ppgrib_hdr, char *errmsg)
39#else
40int  init_gribhdr ( ppgrib_hdr, errmsg)
41                GRIB_HDR **ppgrib_hdr; char *errmsg;
42#endif
43{
44/*
45* A.0       DEFAULT to error status
46*/
47char *func= "init_gribhdr";
48int     stat=1;
49
50   DPRINT1 ("Entering %s\n", func);
51/*
52*
53* A.1       ALLOCATE storage for struct GRIB_HDR
54*           IF (fails) THEN
55*               RETURN  error
56*           ELSE
57*               CLEAR out struct GRIB_HDR
58*           ENDIF
59*/
60   *ppgrib_hdr= (GRIB_HDR *)malloc(sizeof(GRIB_HDR));
61   if (*ppgrib_hdr == NULL) {
62
63        DPRINT1 ("%s:  failed to create storage for GRIB_HDR\n", func);
64        sprintf (errmsg, "%s:  failed to create storage for GRIB_HDR\n", func);
65        goto BYE;
66        }
67   else memset ((void *)*ppgrib_hdr, '\0', sizeof(GRIB_HDR));
68   DPRINT2 ("Allocate storage of GRIB_HDR struct, addr=%ld (%ld bytes)\n", 
69   *ppgrib_hdr, sizeof(GRIB_HDR));
70
71/*
72*
73* A.2       ALLOCATE storage for struct GRIB_HDR's Entire_Msg array
74*           !size DEF_MSG_LEN bytes as defined in 'grib.h'
75*           IF (fails) THEN
76*               FREE Grib Header
77*               RETURN error
78*           ELSE
79*               STORE absolute size of Entire_Msg in header's Abs_Size
80*               CLEAR out array Entire_Msg of struct
81*               SET status to good
82*           ENDIF
83*/
84   
85   (*ppgrib_hdr)->entire_msg=  (void *)malloc(DEF_MSG_LEN);
86   if ((*ppgrib_hdr)->entire_msg == NULL) {
87        DPRINT1 ( "%s:  failed to create storage for GRIB_HDR's Msg\n", func);
88        sprintf (errmsg, "%s:  failed to create storage for GRIB_HDR's Msg\n", 
89        func);
90        free (*ppgrib_hdr);
91        }
92   else {
93        (*ppgrib_hdr)->abs_size = (long)DEF_MSG_LEN;
94        memset ((void *)(*ppgrib_hdr)->entire_msg, '\0', DEF_MSG_LEN);
95        DPRINT2 (
96        "Allocate storage for GRIB_HDR->entire_msg, addr=%ld, sz= %ld bytes \n",
97        (*ppgrib_hdr)->entire_msg, (*ppgrib_hdr)->abs_size);
98        stat=0;
99        }
100
101
102/*
103*
104* A.3       RETURN status
105*/
106BYE:
107   DPRINT2 ("Leaving %s,  stat=%d;\n", func,stat);
108   return (stat);
109
110/*
111*
112* END OF FUNCTION
113*
114*
115*/
116}
117
118/*
119*
120****************************************************************************
121* B. FUNCTION:  free_gribhdr
122*      to free up storage of Grib Header structure and all its attributes.
123*
124*    INTERFACE:
125*      void    free_gribhdr (ppgrib_hdr)
126*
127*    ARGUMENTS (I=input, O=output, I&O=input and output):
128*      (O)  GRIB_HDR **ppgrib_hdr;
129*           Grib Header structure whose storage is released;
130*
131*    RETURN CODE:  none;
132****************************************************************************
133*/
134#if PROTOTYPE_NEEDED
135void    free_gribhdr ( GRIB_HDR **ppgrib_hdr)
136#else
137void    free_gribhdr ( ppgrib_hdr)
138                        GRIB_HDR **ppgrib_hdr;
139#endif
140{
141   char *func="free_gribhdr";
142   DPRINT1 ("Entering %s\n", func);
143/*
144*
145* B.1       IF (this struct is not null) {
146*               IF (struct's entire_msg is not null)
147*                   FREE entire msg array
148*               ENDIF
149*               FREE struct itself
150*               SET it to null
151*           ENDIF
152*/
153   if (*ppgrib_hdr != NULL) {
154        if ((*ppgrib_hdr)->entire_msg != NULL) free((*ppgrib_hdr)->entire_msg);
155        free (*ppgrib_hdr);
156        *ppgrib_hdr= NULL;
157   }
158   DPRINT1 ("Leaving %s, no return code\n", func);
159/*
160*
161* END OF FUNCTION
162*
163*/
164}
165
166/*
167***********************************************************************
168* C. FUNCTION:  Expand_gribhdr
169*      to make Grib Header structure 's entire_msg buffer larger
170*      than its current abs_size.
171*
172*    INTERFACE:
173*      int   Expand_gribhdr (gh, newsize, errmsg)
174*
175*    ARGUMENTS (I=input, O=output, I&O=input and output):
176*    (I&O)  GRIB_HDR *gh;
177*           Grib Header structure whose buffer is to be expanded;
178*      (I)  long newsize;
179*           size to expand entire_msg to;
180*      (O)  char *errmsg;
181*           empty array, returned filled if error occurred;
182*
183*    RETURN CODE: 
184*      0>  newsize is smaller or equal to current size and function
185*          with return with GRIB header unchanged;  OR,
186*          successful, entire_msg now is larger & abs_size has
187*          been updated;  all of the section pointers are also
188*          updated to point to correct location within the new
189*          larger block.
190*      1>  error occurred, Errmsg filled;
191****************************************************************************
192*/
193#if PROTOTYPE_NEEDED
194int     Expand_gribhdr (GRIB_HDR *gh, long newsize, char *errmsg)
195#else
196int     Expand_gribhdr (gh, newsize, errmsg)
197GRIB_HDR        *gh;
198long            newsize;
199char            *errmsg;
200#endif
201{
202   char *func="Expand_gribhdr";
203   unsigned char  *Buff;                        /* temp array */
204
205   DPRINT1 ("Entering %s\n", func);
206/*
207* C.0       IF (grib hdr struct pointer or entire_msg is null)
208*              RETURN with error
209*           ENDIF
210*/
211   if (gh == (GRIB_HDR *)NULL || gh->entire_msg == (unsigned char *)NULL)  {
212        sprintf(errmsg,"%s: either GRIB_HDR or Entire_msg is Null\n",
213        func);
214        DPRINT1 ("Leaving %s, with error (NULL Grib Header)\n", func);
215        return (1);
216   }
217
218/*
219* C.1       IF (new size is smaller than abs_size) THEN
220*              PRINT warning
221*              RETURN with no errors
222*           ENDIF
223*/
224   if (newsize <= gh->abs_size) {
225        fprintf(stdout,
226        "%s:  cannot expand to %ld bytes (must be bigger than abs_size= %ld)\n",
227        func, newsize, gh->abs_size);
228        return (0);
229   }
230
231   DPRINT2 ("Require %ld bytes and curr abs_size= %ld\n", 
232   newsize, gh->abs_size);
233
234/*
235* C.2       ALLOCATE a new block of 'newsize' bytes
236*           RETURN on error
237*/
238   Buff =  (unsigned char *)malloc (newsize);
239   if (Buff == NULL) {
240        sprintf(errmsg,"%s: failed to create new array (%d bytes)\n",
241        func, newsize);
242        DPRINT1 ("Leaving %s, with Malloc error\n", func);
243        return (1);
244   } 
245
246/*
247* C.3       CLEAR new array out
248*/
249   memset ((void*)Buff, '\0', newsize);
250
251/*
252* C.4       COPY content of old buffer into new buffer
253*/
254   if (gh->msg_length > 0) {
255        DPRINT1(
256        "Copy %ld bytes of data from old buffer to new one\n",
257        gh->msg_length);
258
259        memcpy ((void*)Buff, (void*)gh->entire_msg, gh->msg_length);
260   }
261
262/*
263* C.6       UPDATE each Section that's present to point to
264*           proper location within the new larger buffer
265*/
266   if (gh->ids_ptr !=NULL) gh->ids_ptr= Buff + (gh->ids_ptr - gh->entire_msg);
267   if (gh->pds_ptr !=NULL) gh->pds_ptr= Buff + (gh->pds_ptr - gh->entire_msg);
268   if (gh->gds_ptr !=NULL) gh->gds_ptr= Buff + (gh->gds_ptr - gh->entire_msg);
269   if (gh->bms_ptr !=NULL) gh->bms_ptr= Buff + (gh->bms_ptr - gh->entire_msg);
270   if (gh->bds_ptr !=NULL) gh->bds_ptr= Buff + (gh->bds_ptr - gh->entire_msg);
271   if (gh->eds_ptr !=NULL) gh->eds_ptr= Buff + (gh->eds_ptr - gh->entire_msg);
272
273/*
274* C.5       FREE the old buffer & assign the new one to GRIB_HDR
275*/
276   free ((void *) gh->entire_msg);
277   gh->entire_msg = (unsigned char *)Buff;
278   
279/*
280* C.6       UPDATE alloc_size of GRIB_HDR
281*/
282   gh->abs_size = newsize;
283   DPRINT1 ("expanded  gh->abs_size = %ld\n", gh->abs_size);
284
285   DPRINT1 ("Leaving %s, no errors\n", func);
286   return (0);
287/*
288* END OF FUNCTION
289*/
290}
Note: See TracBrowser for help on using the repository browser.