mngtree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* ************************************************************************** */
  2. /* * * */
  3. /* * COPYRIGHT NOTICE: * */
  4. /* * * */
  5. /* * Copyright (c) 2000 Gerard Juyn (gerard :at: libmng.com) * */
  6. /* * [You may insert additional notices after this sentence if you modify * */
  7. /* * this source] * */
  8. /* * * */
  9. /* * For the purposes of this copyright and license, "Contributing Authors" * */
  10. /* * is defined as the following set of individuals: * */
  11. /* * * */
  12. /* * Gerard Juyn * */
  13. /* * (hopefully some more to come...) * */
  14. /* * * */
  15. /* * The MNG Library is supplied "AS IS". The Contributing Authors * */
  16. /* * disclaim all warranties, expressed or implied, including, without * */
  17. /* * limitation, the warranties of merchantability and of fitness for any * */
  18. /* * purpose. The Contributing Authors assume no liability for direct, * */
  19. /* * indirect, incidental, special, exemplary, or consequential damages, * */
  20. /* * which may result from the use of the MNG Library, even if advised of * */
  21. /* * the possibility of such damage. * */
  22. /* * * */
  23. /* * Permission is hereby granted to use, copy, modify, and distribute this * */
  24. /* * source code, or portions hereof, for any purpose, without fee, subject * */
  25. /* * to the following restrictions: * */
  26. /* * * */
  27. /* * 1. The origin of this source code must not be misrepresented; * */
  28. /* * you must not claim that you wrote the original software. * */
  29. /* * * */
  30. /* * 2. Altered versions must be plainly marked as such and must not be * */
  31. /* * misrepresented as being the original source. * */
  32. /* * * */
  33. /* * 3. This Copyright notice may not be removed or altered from any source * */
  34. /* * or altered source distribution. * */
  35. /* * * */
  36. /* * The Contributing Authors specifically permit, without fee, and * */
  37. /* * encourage the use of this source code as a component to supporting * */
  38. /* * the MNG and JNG file format in commercial products. If you use this * */
  39. /* * source code in a product, acknowledgment would be highly appreciated. * */
  40. /* * * */
  41. /* ************************************************************************** */
  42. /* * * */
  43. /* * project : mngtree * */
  44. /* * file : mngtree.c copyright (c) 2000 G.Juyn * */
  45. /* * version : 1.0.1 * */
  46. /* * * */
  47. /* * purpose : main project file * */
  48. /* * * */
  49. /* * author : G.Juyn * */
  50. /* * web : http://www.3-t.com * */
  51. /* * email : mailto:[email protected] * */
  52. /* * * */
  53. /* * comment : mngtree simply dumps the chunk-structure of the supplied * */
  54. /* * first parameter to stdout (should be a xNG-file) * */
  55. /* * * */
  56. /* * changes : 0.5.2 - 06/03/2000 - G.Juyn * */
  57. /* * - fixed for compilation under Linux * */
  58. /* * 0.5.3 - 06/26/2000 - G.Juyn * */
  59. /* * - changed definition of userdata to mng_ptr * */
  60. /* * 0.5.3 - 06/28/2000 - G.Juyn * */
  61. /* * - changed memory allocation size parameters to mng_size_t * */
  62. /* * * */
  63. /* * 1.0.1 - 12/07/2003 - G.Juyn * */
  64. /* * - fixed inclusion of libmng.h (Thanks Raphael) * */
  65. /* * * */
  66. /* ************************************************************************** */
  67. #include <stdio.h>
  68. #include <stdlib.h>
  69. #include <strings.h>
  70. #include "../../../libmng.h"
  71. /* ************************************************************************** */
  72. typedef struct user_struct {
  73. FILE *hFile; /* file handle */
  74. int iIndent; /* for nice indented formatting */
  75. } userdata;
  76. typedef userdata * userdatap;
  77. /* ************************************************************************** */
  78. mng_ptr myalloc (mng_size_t iSize)
  79. { /* duh! */
  80. return (mng_ptr)calloc (1, (size_t)iSize);
  81. }
  82. /* ************************************************************************** */
  83. void myfree (mng_ptr pPtr, mng_size_t iSize)
  84. {
  85. free (pPtr); /* duh! */
  86. return;
  87. }
  88. /* ************************************************************************** */
  89. mng_bool myopenstream (mng_handle hMNG)
  90. {
  91. return MNG_TRUE; /* already opened in main function */
  92. }
  93. /* ************************************************************************** */
  94. mng_bool myclosestream (mng_handle hMNG)
  95. {
  96. return MNG_TRUE; /* gets closed in main function */
  97. }
  98. /* ************************************************************************** */
  99. mng_bool myreaddata (mng_handle hMNG,
  100. mng_ptr pBuf,
  101. mng_uint32 iSize,
  102. mng_uint32 *iRead)
  103. { /* get to my file handle */
  104. userdatap pMydata = (userdatap)mng_get_userdata (hMNG);
  105. /* read it */
  106. *iRead = fread (pBuf, 1, iSize, pMydata->hFile);
  107. /* iRead will indicate EOF */
  108. return MNG_TRUE;
  109. }
  110. /* ************************************************************************** */
  111. mng_bool myiterchunk (mng_handle hMNG,
  112. mng_handle hChunk,
  113. mng_chunkid iChunktype,
  114. mng_uint32 iChunkseq)
  115. { /* get to my file handle */
  116. userdatap pMydata = (userdatap)mng_get_userdata (hMNG);
  117. char aCh[4];
  118. char zIndent[80];
  119. int iX;
  120. /* decode the chunkname */
  121. aCh[0] = (char)((iChunktype >> 24) & 0xFF);
  122. aCh[1] = (char)((iChunktype >> 16) & 0xFF);
  123. aCh[2] = (char)((iChunktype >> 8) & 0xFF);
  124. aCh[3] = (char)((iChunktype ) & 0xFF);
  125. /* indent less ? */
  126. if ( (iChunktype == MNG_UINT_MEND) || (iChunktype == MNG_UINT_IEND) ||
  127. (iChunktype == MNG_UINT_ENDL) )
  128. pMydata->iIndent -= 2;
  129. /* this looks ugly; but I haven't
  130. figured out how to do it prettier */
  131. for (iX = 0; iX < pMydata->iIndent; iX++)
  132. zIndent[iX] = ' ';
  133. zIndent[pMydata->iIndent] = '\0';
  134. /* print a nicely indented line */
  135. printf ("%s%c%c%c%c\n", zIndent, aCh[0], aCh[1], aCh[2], aCh[3]);
  136. /* indent more ? */
  137. if ( (iChunktype == MNG_UINT_MHDR) || (iChunktype == MNG_UINT_IHDR) ||
  138. (iChunktype == MNG_UINT_JHDR) || (iChunktype == MNG_UINT_DHDR) ||
  139. (iChunktype == MNG_UINT_BASI) || (iChunktype == MNG_UINT_LOOP) )
  140. pMydata->iIndent += 2;
  141. return MNG_TRUE; /* keep'm coming... */
  142. }
  143. /* ************************************************************************** */
  144. int dumptree (char * zFilename)
  145. {
  146. userdatap pMydata;
  147. mng_handle hMNG;
  148. mng_retcode iRC;
  149. /* get a data buffer */
  150. pMydata = (userdatap)calloc (1, sizeof (userdata));
  151. if (pMydata == NULL) /* oke ? */
  152. {
  153. fprintf (stderr, "Cannot allocate a data buffer.\n");
  154. return 1;
  155. }
  156. /* can we open the file ? */
  157. if ((pMydata->hFile = fopen (zFilename, "rb")) == NULL)
  158. { /* error out if we can't */
  159. fprintf (stderr, "Cannot open input file %s.\n", zFilename);
  160. return 1;
  161. }
  162. /* let's initialize the library */
  163. hMNG = mng_initialize ((mng_ptr)pMydata, myalloc, myfree, MNG_NULL);
  164. if (!hMNG) /* did that work out ? */
  165. {
  166. fprintf (stderr, "Cannot initialize libmng.\n");
  167. iRC = 1;
  168. }
  169. else
  170. { /* setup callbacks */
  171. if ( ((iRC = mng_setcb_openstream (hMNG, myopenstream )) != 0) ||
  172. ((iRC = mng_setcb_closestream (hMNG, myclosestream)) != 0) ||
  173. ((iRC = mng_setcb_readdata (hMNG, myreaddata )) != 0) )
  174. fprintf (stderr, "Cannot set callbacks for libmng.\n");
  175. else
  176. { /* read the file into memory */
  177. if ((iRC = mng_read (hMNG)) != 0)
  178. fprintf (stderr, "Cannot read the file.\n");
  179. else
  180. {
  181. pMydata->iIndent = 2; /* start of the indenting at a nice level */
  182. printf ("Starting dump of %s.\n\n", zFilename);
  183. /* run through the chunk list */
  184. if ((iRC = mng_iterate_chunks (hMNG, 0, myiterchunk)) != 0)
  185. fprintf (stderr, "Cannot iterate the chunks.\n");
  186. printf ("\nDone.\n");
  187. }
  188. }
  189. mng_cleanup (&hMNG); /* cleanup the library */
  190. }
  191. fclose (pMydata->hFile); /* cleanup */
  192. free (pMydata);
  193. return iRC;
  194. }
  195. /* ************************************************************************** */
  196. int main(int argc, char *argv[])
  197. {
  198. if (argc > 1) /* need that first parameter ! */
  199. return dumptree (argv[1]);
  200. else
  201. printf ("\nUsage: mngtree <file.mng>\n\n");
  202. return 0;
  203. }
  204. /* ************************************************************************** */