mngtree.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* ************************************************************************** */
  2. /* * * */
  3. /* * COPYRIGHT NOTICE: * */
  4. /* * * */
  5. /* * Copyright (c) 2000 Gerard Juyn ([email protected]) * */
  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.cpp copyright (c) 2000 G.Juyn * */
  45. /* * version : 1.0.0 * */
  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.3 - 06/26/2000 - G.Juyn * */
  57. /* * - changed userdata variable to mng_ptr * */
  58. /* * 0.5.3 - 06/28/2000 - G.Juyn * */
  59. /* * - changed memory allocation size parameters to mng_size_t * */
  60. /* * * */
  61. /* ************************************************************************** */
  62. #pragma hdrstop
  63. #include <condefs.h>
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <mem.h>
  67. #include "libmng.h"
  68. /* ************************************************************************** */
  69. USERES("mngtree.res");
  70. USELIB("..\win32dll\libmng.lib");
  71. //---------------------------------------------------------------------------
  72. /* ************************************************************************** */
  73. typedef struct user_struct {
  74. FILE *hFile; /* file handle */
  75. int iIndent; /* for nice indented formatting */
  76. } userdata;
  77. typedef userdata * userdatap;
  78. /* ************************************************************************** */
  79. #define MY_DECL __stdcall /* get the right callback convention */
  80. /* ************************************************************************** */
  81. mng_ptr MY_DECL myalloc (mng_size_t iSize)
  82. { /* duh! */
  83. return (mng_ptr)calloc (1, (size_t)iSize);
  84. }
  85. /* ************************************************************************** */
  86. #pragma argsused
  87. void MY_DECL myfree (mng_ptr pPtr, mng_size_t iSize)
  88. {
  89. free (pPtr); /* duh! */
  90. return;
  91. }
  92. /* ************************************************************************** */
  93. #pragma argsused
  94. mng_bool MY_DECL myopenstream (mng_handle hMNG)
  95. {
  96. return MNG_TRUE; /* already opened in main function */
  97. }
  98. /* ************************************************************************** */
  99. #pragma argsused
  100. mng_bool MY_DECL myclosestream (mng_handle hMNG)
  101. {
  102. return MNG_TRUE; /* gets closed in main function */
  103. }
  104. /* ************************************************************************** */
  105. mng_bool MY_DECL myreaddata (mng_handle hMNG,
  106. mng_ptr pBuf,
  107. mng_uint32 iSize,
  108. mng_uint32 *iRead)
  109. { /* get to my file handle */
  110. userdatap pMydata = (userdatap)mng_get_userdata (hMNG);
  111. /* read it */
  112. *iRead = fread (pBuf, 1, iSize, pMydata->hFile);
  113. /* iRead will indicate EOF */
  114. return MNG_TRUE;
  115. }
  116. /* ************************************************************************** */
  117. #pragma argsused
  118. mng_bool MY_DECL myiterchunk (mng_handle hMNG,
  119. mng_handle hChunk,
  120. mng_chunkid iChunktype,
  121. mng_uint32 iChunkseq)
  122. { /* get to my file handle */
  123. userdatap pMydata = (userdatap)mng_get_userdata (hMNG);
  124. char aCh[4];
  125. char zIndent[80];
  126. int iX;
  127. /* decode the chunkname */
  128. aCh[0] = (char)((iChunktype >> 24) & 0xFF);
  129. aCh[1] = (char)((iChunktype >> 16) & 0xFF);
  130. aCh[2] = (char)((iChunktype >> 8) & 0xFF);
  131. aCh[3] = (char)((iChunktype ) & 0xFF);
  132. /* indent less ? */
  133. if ( (iChunktype == MNG_UINT_MEND) || (iChunktype == MNG_UINT_IEND) ||
  134. (iChunktype == MNG_UINT_ENDL) )
  135. pMydata->iIndent -= 2;
  136. /* this looks ugly; but I haven't
  137. figured out how to do it prettier */
  138. for (iX = 0; iX < pMydata->iIndent; iX++)
  139. zIndent[iX] = ' ';
  140. zIndent[pMydata->iIndent] = '\0';
  141. /* print a nicely indented line */
  142. printf ("%s%c%c%c%c\n", &zIndent, aCh[0], aCh[1], aCh[2], aCh[3]);
  143. /* indent more ? */
  144. if ( (iChunktype == MNG_UINT_MHDR) || (iChunktype == MNG_UINT_IHDR) ||
  145. (iChunktype == MNG_UINT_JHDR) || (iChunktype == MNG_UINT_DHDR) ||
  146. (iChunktype == MNG_UINT_BASI) || (iChunktype == MNG_UINT_LOOP) )
  147. pMydata->iIndent += 2;
  148. return MNG_TRUE; /* keep'm coming... */
  149. }
  150. /* ************************************************************************** */
  151. int dumptree (char * zFilename)
  152. {
  153. userdatap pMydata;
  154. mng_handle hMNG;
  155. mng_retcode iRC;
  156. /* get a data buffer */
  157. pMydata = (userdatap)calloc (1, sizeof (userdata));
  158. if (pMydata == NULL) /* oke ? */
  159. {
  160. fprintf (stderr, "Cannot allocate a data buffer.\n");
  161. return 1;
  162. }
  163. /* can we open the file ? */
  164. if ((pMydata->hFile = fopen (zFilename, "rb")) == NULL)
  165. { /* error out if we can't */
  166. fprintf (stderr, "Cannot open input file %s.\n", zFilename);
  167. return 1;
  168. }
  169. /* let's initialize the library */
  170. hMNG = mng_initialize ((mng_ptr)pMydata, myalloc, myfree, MNG_NULL);
  171. if (!hMNG) /* did that work out ? */
  172. {
  173. fprintf (stderr, "Cannot initialize libmng.\n");
  174. iRC = 1;
  175. }
  176. else
  177. { /* setup callbacks */
  178. if ( ((iRC = mng_setcb_openstream (hMNG, myopenstream )) != 0) ||
  179. ((iRC = mng_setcb_closestream (hMNG, myclosestream)) != 0) ||
  180. ((iRC = mng_setcb_readdata (hMNG, myreaddata )) != 0) )
  181. fprintf (stderr, "Cannot set callbacks for libmng.\n");
  182. else
  183. { /* reaad the file into memory */
  184. if ((iRC = mng_read (hMNG)) != 0)
  185. fprintf (stderr, "Cannot read the file.\n");
  186. else
  187. {
  188. pMydata->iIndent = 2; /* start of the indenting at a nice level */
  189. printf ("Starting dump of %s.\n\n", zFilename);
  190. /* run through the chunk list */
  191. if ((iRC = mng_iterate_chunks (hMNG, 0, myiterchunk)) != 0)
  192. fprintf (stderr, "Cannot iterate the chunks.\n");
  193. printf ("\nDone.\n");
  194. }
  195. }
  196. mng_cleanup (&hMNG); /* cleanup the library */
  197. }
  198. fclose (pMydata->hFile); /* cleanup */
  199. free (pMydata);
  200. return iRC;
  201. }
  202. /* ************************************************************************** */
  203. #pragma argsused
  204. int main(int argc, char *argv[])
  205. {
  206. if (argc > 1) /* need that first parameter ! */
  207. return dumptree (argv[1]);
  208. else
  209. fprintf (stdout, "\nUsage: mngtree <file.mng>\n\n");
  210. return 0;
  211. }
  212. /* ************************************************************************** */