PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wrfv2_fire/external/io_grib1/MEL_grib1/init_gribhdr.c

http://github.com/jbeezley/wrf-fire
C | 290 lines | 111 code | 20 blank | 159 comment | 24 complexity | 303080c86ee5ad77eb2d60bcf038739d MD5 | raw file
Possible License(s): AGPL-1.0
  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
  38. int init_gribhdr ( GRIB_HDR **ppgrib_hdr, char *errmsg)
  39. #else
  40. int init_gribhdr ( ppgrib_hdr, errmsg)
  41. GRIB_HDR **ppgrib_hdr; char *errmsg;
  42. #endif
  43. {
  44. /*
  45. * A.0 DEFAULT to error status
  46. */
  47. char *func= "init_gribhdr";
  48. int stat=1;
  49. DPRINT1 ("Entering %s\n", func);
  50. /*
  51. *
  52. * A.1 ALLOCATE storage for struct GRIB_HDR
  53. * IF (fails) THEN
  54. * RETURN error
  55. * ELSE
  56. * CLEAR out struct GRIB_HDR
  57. * ENDIF
  58. */
  59. *ppgrib_hdr= (GRIB_HDR *)malloc(sizeof(GRIB_HDR));
  60. if (*ppgrib_hdr == NULL) {
  61. DPRINT1 ("%s: failed to create storage for GRIB_HDR\n", func);
  62. sprintf (errmsg, "%s: failed to create storage for GRIB_HDR\n", func);
  63. goto BYE;
  64. }
  65. else memset ((void *)*ppgrib_hdr, '\0', sizeof(GRIB_HDR));
  66. DPRINT2 ("Allocate storage of GRIB_HDR struct, addr=%ld (%ld bytes)\n",
  67. *ppgrib_hdr, sizeof(GRIB_HDR));
  68. /*
  69. *
  70. * A.2 ALLOCATE storage for struct GRIB_HDR's Entire_Msg array
  71. * !size DEF_MSG_LEN bytes as defined in 'grib.h'
  72. * IF (fails) THEN
  73. * FREE Grib Header
  74. * RETURN error
  75. * ELSE
  76. * STORE absolute size of Entire_Msg in header's Abs_Size
  77. * CLEAR out array Entire_Msg of struct
  78. * SET status to good
  79. * ENDIF
  80. */
  81. (*ppgrib_hdr)->entire_msg= (void *)malloc(DEF_MSG_LEN);
  82. if ((*ppgrib_hdr)->entire_msg == NULL) {
  83. DPRINT1 ( "%s: failed to create storage for GRIB_HDR's Msg\n", func);
  84. sprintf (errmsg, "%s: failed to create storage for GRIB_HDR's Msg\n",
  85. func);
  86. free (*ppgrib_hdr);
  87. }
  88. else {
  89. (*ppgrib_hdr)->abs_size = (long)DEF_MSG_LEN;
  90. memset ((void *)(*ppgrib_hdr)->entire_msg, '\0', DEF_MSG_LEN);
  91. DPRINT2 (
  92. "Allocate storage for GRIB_HDR->entire_msg, addr=%ld, sz= %ld bytes \n",
  93. (*ppgrib_hdr)->entire_msg, (*ppgrib_hdr)->abs_size);
  94. stat=0;
  95. }
  96. /*
  97. *
  98. * A.3 RETURN status
  99. */
  100. BYE:
  101. DPRINT2 ("Leaving %s, stat=%d;\n", func,stat);
  102. return (stat);
  103. /*
  104. *
  105. * END OF FUNCTION
  106. *
  107. *
  108. */
  109. }
  110. /*
  111. *
  112. ****************************************************************************
  113. * B. FUNCTION: free_gribhdr
  114. * to free up storage of Grib Header structure and all its attributes.
  115. *
  116. * INTERFACE:
  117. * void free_gribhdr (ppgrib_hdr)
  118. *
  119. * ARGUMENTS (I=input, O=output, I&O=input and output):
  120. * (O) GRIB_HDR **ppgrib_hdr;
  121. * Grib Header structure whose storage is released;
  122. *
  123. * RETURN CODE: none;
  124. ****************************************************************************
  125. */
  126. #if PROTOTYPE_NEEDED
  127. void free_gribhdr ( GRIB_HDR **ppgrib_hdr)
  128. #else
  129. void free_gribhdr ( ppgrib_hdr)
  130. GRIB_HDR **ppgrib_hdr;
  131. #endif
  132. {
  133. char *func="free_gribhdr";
  134. DPRINT1 ("Entering %s\n", func);
  135. /*
  136. *
  137. * B.1 IF (this struct is not null) {
  138. * IF (struct's entire_msg is not null)
  139. * FREE entire msg array
  140. * ENDIF
  141. * FREE struct itself
  142. * SET it to null
  143. * ENDIF
  144. */
  145. if (*ppgrib_hdr != NULL) {
  146. if ((*ppgrib_hdr)->entire_msg != NULL) free((*ppgrib_hdr)->entire_msg);
  147. free (*ppgrib_hdr);
  148. *ppgrib_hdr= NULL;
  149. }
  150. DPRINT1 ("Leaving %s, no return code\n", func);
  151. /*
  152. *
  153. * END OF FUNCTION
  154. *
  155. */
  156. }
  157. /*
  158. ***********************************************************************
  159. * C. FUNCTION: Expand_gribhdr
  160. * to make Grib Header structure 's entire_msg buffer larger
  161. * than its current abs_size.
  162. *
  163. * INTERFACE:
  164. * int Expand_gribhdr (gh, newsize, errmsg)
  165. *
  166. * ARGUMENTS (I=input, O=output, I&O=input and output):
  167. * (I&O) GRIB_HDR *gh;
  168. * Grib Header structure whose buffer is to be expanded;
  169. * (I) long newsize;
  170. * size to expand entire_msg to;
  171. * (O) char *errmsg;
  172. * empty array, returned filled if error occurred;
  173. *
  174. * RETURN CODE:
  175. * 0> newsize is smaller or equal to current size and function
  176. * with return with GRIB header unchanged; OR,
  177. * successful, entire_msg now is larger & abs_size has
  178. * been updated; all of the section pointers are also
  179. * updated to point to correct location within the new
  180. * larger block.
  181. * 1> error occurred, Errmsg filled;
  182. ****************************************************************************
  183. */
  184. #if PROTOTYPE_NEEDED
  185. int Expand_gribhdr (GRIB_HDR *gh, long newsize, char *errmsg)
  186. #else
  187. int Expand_gribhdr (gh, newsize, errmsg)
  188. GRIB_HDR *gh;
  189. long newsize;
  190. char *errmsg;
  191. #endif
  192. {
  193. char *func="Expand_gribhdr";
  194. unsigned char *Buff; /* temp array */
  195. DPRINT1 ("Entering %s\n", func);
  196. /*
  197. * C.0 IF (grib hdr struct pointer or entire_msg is null)
  198. * RETURN with error
  199. * ENDIF
  200. */
  201. if (gh == (GRIB_HDR *)NULL || gh->entire_msg == (unsigned char *)NULL) {
  202. sprintf(errmsg,"%s: either GRIB_HDR or Entire_msg is Null\n",
  203. func);
  204. DPRINT1 ("Leaving %s, with error (NULL Grib Header)\n", func);
  205. return (1);
  206. }
  207. /*
  208. * C.1 IF (new size is smaller than abs_size) THEN
  209. * PRINT warning
  210. * RETURN with no errors
  211. * ENDIF
  212. */
  213. if (newsize <= gh->abs_size) {
  214. fprintf(stdout,
  215. "%s: cannot expand to %ld bytes (must be bigger than abs_size= %ld)\n",
  216. func, newsize, gh->abs_size);
  217. return (0);
  218. }
  219. DPRINT2 ("Require %ld bytes and curr abs_size= %ld\n",
  220. newsize, gh->abs_size);
  221. /*
  222. * C.2 ALLOCATE a new block of 'newsize' bytes
  223. * RETURN on error
  224. */
  225. Buff = (unsigned char *)malloc (newsize);
  226. if (Buff == NULL) {
  227. sprintf(errmsg,"%s: failed to create new array (%d bytes)\n",
  228. func, newsize);
  229. DPRINT1 ("Leaving %s, with Malloc error\n", func);
  230. return (1);
  231. }
  232. /*
  233. * C.3 CLEAR new array out
  234. */
  235. memset ((void*)Buff, '\0', newsize);
  236. /*
  237. * C.4 COPY content of old buffer into new buffer
  238. */
  239. if (gh->msg_length > 0) {
  240. DPRINT1(
  241. "Copy %ld bytes of data from old buffer to new one\n",
  242. gh->msg_length);
  243. memcpy ((void*)Buff, (void*)gh->entire_msg, gh->msg_length);
  244. }
  245. /*
  246. * C.6 UPDATE each Section that's present to point to
  247. * proper location within the new larger buffer
  248. */
  249. if (gh->ids_ptr !=NULL) gh->ids_ptr= Buff + (gh->ids_ptr - gh->entire_msg);
  250. if (gh->pds_ptr !=NULL) gh->pds_ptr= Buff + (gh->pds_ptr - gh->entire_msg);
  251. if (gh->gds_ptr !=NULL) gh->gds_ptr= Buff + (gh->gds_ptr - gh->entire_msg);
  252. if (gh->bms_ptr !=NULL) gh->bms_ptr= Buff + (gh->bms_ptr - gh->entire_msg);
  253. if (gh->bds_ptr !=NULL) gh->bds_ptr= Buff + (gh->bds_ptr - gh->entire_msg);
  254. if (gh->eds_ptr !=NULL) gh->eds_ptr= Buff + (gh->eds_ptr - gh->entire_msg);
  255. /*
  256. * C.5 FREE the old buffer & assign the new one to GRIB_HDR
  257. */
  258. free ((void *) gh->entire_msg);
  259. gh->entire_msg = (unsigned char *)Buff;
  260. /*
  261. * C.6 UPDATE alloc_size of GRIB_HDR
  262. */
  263. gh->abs_size = newsize;
  264. DPRINT1 ("expanded gh->abs_size = %ld\n", gh->abs_size);
  265. DPRINT1 ("Leaving %s, no errors\n", func);
  266. return (0);
  267. /*
  268. * END OF FUNCTION
  269. */
  270. }