lwio.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * LWSDK Header File
  3. * Copyright 1999, NewTek, Inc.
  4. *
  5. * LWIO.H -- LightWave Save and Load State
  6. *
  7. * This header contains the definition of the structures needed to
  8. * perform I/O from a LightWave server.
  9. */
  10. #ifndef LWSDK_IO_H
  11. #define LWSDK_IO_H
  12. #include <lwtypes.h>
  13. /*
  14. * Data may be loaded and saved in binary or text files. Text files
  15. * are often scene files, and binary files are often object files.
  16. *
  17. * In BINARY mode, the external form contains raw bytes having any
  18. * value from 0 to 255. Reads and writes are entirely based on the
  19. * number of bytes requested.
  20. *
  21. * In ASCII mode, all data bytes must be in the extended ASCII range
  22. * of 32 to 255. Values outside this range are ignored or undefined.
  23. */
  24. #define LWIO_BINARY 0
  25. #define LWIO_ASCII 1
  26. #define LWIO_OBJECT LWIO_BINARY
  27. #define LWIO_SCENE LWIO_ASCII
  28. /*
  29. * Blocks of data can be marked with identifiers. In BINARY mode the
  30. * four-byte ID code is used, and in ASCII mode the string token (which
  31. * should not contain spaces) is used.
  32. */
  33. typedef struct st_LWBlockIdent {
  34. LWID id;
  35. const char *token;
  36. } LWBlockIdent;
  37. /*
  38. * This structure is used when the server is writing its state to
  39. * the external store. The 'writeData' is the first argument to all
  40. * the callback functions.
  41. *
  42. * write execute a raw write to the store. In BINARY mode, the
  43. * number of bytes indicated by 'len' is written directly to
  44. * the output. In ASCII mode the input buffer is treated as
  45. * a null-terminated string and the length is computed from
  46. * that. The string is written with a newline at the end.
  47. *
  48. * writeI1 write an array of different sized and typed integers to
  49. * writeI2 the store. In BINARY mode the 'n' numbers are written as
  50. * writeI4 1, 2 or 4 btype elements in "big-endian" (Motorola)
  51. * writeU1 format. In ASCII mode, all 'n' numbers are written to a
  52. * writeU2 single line, the signed values in decimal and the unsigned
  53. * writeU4 values in hexadecimal. A newline is written after the
  54. * list of numbers unless the writing is done inside a leaf
  55. * block.
  56. *
  57. * writeStr write a null-terminated string to the store. In ASCII
  58. * mode the string may be contained in double quotes.
  59. *
  60. * writeID write an identifier token to the store. In BINARY mode
  61. * this is the four-byte ID code, while in ASCII mode this
  62. * is the string token.
  63. *
  64. * beginBlk start a nested data block. The block is identified by
  65. * the ID codes defined in the LWBlockIdent, and the 'leaf'
  66. * flag is true if this block will not contain other blocks.
  67. *
  68. * endBlk end the current data block.
  69. *
  70. * depth return the current block nesting level, where zero means
  71. * we've entered no blocks.
  72. */
  73. typedef struct st_LWSaveState {
  74. int ioMode;
  75. void *writeData;
  76. void (*write) (void *data, const char *, int len);
  77. void (*writeI1) (void *data, const char *, int n);
  78. void (*writeI2) (void *data, const short *, int n);
  79. void (*writeI4) (void *data, const long *, int n);
  80. void (*writeU1) (void *data, const unsigned char *, int n);
  81. void (*writeU2) (void *data, const unsigned short *, int n);
  82. void (*writeU4) (void *data, const unsigned long *, int n);
  83. void (*writeFP) (void *data, const float *, int n);
  84. void (*writeStr) (void *data, const char *);
  85. void (*writeID) (void *data, const LWBlockIdent *);
  86. void (*beginBlk) (void *data, const LWBlockIdent *, int leaf);
  87. void (*endBlk) (void *data);
  88. int (*depth) (void *data);
  89. } LWSaveState;
  90. /*
  91. * This structure is used when the server is reading its state from
  92. * the external store. The 'readData' is the first argument to all
  93. * the callback functions.
  94. *
  95. * read execute a raw read of the store. In binary mode, 'len'
  96. * bytes are read directly from the store. In ASCII mode, up
  97. * to 'len' bytes of the current line are read from the
  98. * store, perhaps leaving more bytes to be read later. The
  99. * return value is the number of bytes read on the current
  100. * line (which may be zero), or -1 for end of data.
  101. *
  102. * readI1 read an array of integers of various types and formats,
  103. * readI2 returning the number of integers successfully read. In
  104. * readI4 BINARY mode a sequence of 'n' single, double or quad bytes
  105. * readU1 are read from the stream and interpreted as "big-endian"
  106. * readU2 (Motorola) format words. In ASCII mode, the numbers are
  107. * readU4 read from the current line, in decimal for signed values
  108. * and in hexadecimal for unsigned values.
  109. *
  110. * readFP read an array of floating point numbers from the store.
  111. * The return value is the number of values read.
  112. *
  113. * readStr read a string from the store. In ASCII mode double quotes
  114. * will be removed.
  115. *
  116. * readID read an identifier token from the store. In BINARY this is
  117. * just a four-byte code value, but in ASCII this in the string
  118. * token which will be matched with its longword code.
  119. *
  120. * findBlk read ahead looking for the next block. The array of block
  121. * identifiers includes all the blocks that could be expected
  122. * and is terminated with a null ID code. If a reconginized
  123. * block is found in the file, it's ID code is returned. A
  124. * zero return indicates no more valid blocks.
  125. *
  126. * endBlk complete reading the current open block.
  127. *
  128. * depth return the current block nesting level, where zero means
  129. * we've entered no blocks.
  130. */
  131. typedef struct st_LWLoadState {
  132. int ioMode;
  133. void *readData;
  134. int (*read) (void *data, char *, int len);
  135. int (*readI1) (void *data, char *, int n);
  136. int (*readI2) (void *data, short *, int n);
  137. int (*readI4) (void *data, long *, int n);
  138. int (*readU1) (void *data, unsigned char *, int n);
  139. int (*readU2) (void *data, unsigned short *, int n);
  140. int (*readU4) (void *data, unsigned long *, int n);
  141. int (*readFP) (void *data, float *, int n);
  142. int (*readStr) (void *data, char *, int max);
  143. LWID (*readID) (void *data, const LWBlockIdent *);
  144. LWID (*findBlk) (void *data, const LWBlockIdent *);
  145. void (*endBlk) (void *data);
  146. int (*depth) (void *data);
  147. } LWLoadState;
  148. /*
  149. * Macros make the call format easier to write.
  150. */
  151. #define LWSAVE_I1(lwss,p,n) (*(lwss)->writeI1)((lwss)->writeData,p,n)
  152. #define LWSAVE_I2(lwss,p,n) (*(lwss)->writeI2)((lwss)->writeData,p,n)
  153. #define LWSAVE_I4(lwss,p,n) (*(lwss)->writeI4)((lwss)->writeData,p,n)
  154. #define LWSAVE_U1(lwss,p,n) (*(lwss)->writeU1)((lwss)->writeData,p,n)
  155. #define LWSAVE_U2(lwss,p,n) (*(lwss)->writeU2)((lwss)->writeData,p,n)
  156. #define LWSAVE_U4(lwss,p,n) (*(lwss)->writeU4)((lwss)->writeData,p,n)
  157. #define LWSAVE_FP(lwss,p,n) (*(lwss)->writeFP)((lwss)->writeData,p,n)
  158. #define LWSAVE_STR(lwss,p) (*(lwss)->writeStr)((lwss)->writeData,p)
  159. #define LWSAVE_ID(lwss,b) (*(lwss)->writeID)((lwss)->writeData,b)
  160. #define LWSAVE_BEGIN(lwss,b,l) (*(lwss)->beginBlk)((lwss)->writeData,b,l)
  161. #define LWSAVE_END(lwss) (*(lwss)->endBlk)((lwss)->writeData)
  162. #define LWSAVE_DEPTH(lwss) (*(lwss)->depth)((lwss)->writeData)
  163. #define LWLOAD_I1(lwls,p,n) (*(lwls)->readI1)((lwls)->readData,p,n)
  164. #define LWLOAD_I2(lwls,p,n) (*(lwls)->readI2)((lwls)->readData,p,n)
  165. #define LWLOAD_I4(lwls,p,n) (*(lwls)->readI4)((lwls)->readData,p,n)
  166. #define LWLOAD_U1(lwls,p,n) (*(lwls)->readU1)((lwls)->readData,p,n)
  167. #define LWLOAD_U2(lwls,p,n) (*(lwls)->readU2)((lwls)->readData,p,n)
  168. #define LWLOAD_U4(lwls,p,n) (*(lwls)->readU4)((lwls)->readData,p,n)
  169. #define LWLOAD_FP(lwls,p,n) (*(lwls)->readFP)((lwls)->readData,p,n)
  170. #define LWLOAD_STR(lwls,p,n) (*(lwls)->readStr)((lwls)->readData,p,n)
  171. #define LWLOAD_ID(lwls,b) (*(lwls)->readID)((lwls)->readData,b)
  172. #define LWLOAD_FIND(lwls,b) (*(lwls)->findBlk)((lwls)->readData,b)
  173. #define LWLOAD_END(lwls) (*(lwls)->endBlk)((lwls)->readData)
  174. #define LWLOAD_DEPTH(lwls) (*(lwls)->depth)((lwls)->readData)
  175. #define LWFILEIOFUNCS_GLOBAL "File IO"
  176. #define LWIO_BINARY_IFF 0x8C0000
  177. typedef struct st_LWFileIOFuncs {
  178. LWSaveState * (*openSave) (const char *name, int ioMode);
  179. void (*closeSave)(LWSaveState *save);
  180. LWLoadState * (*openLoad) (const char *name, int ioMode);
  181. void (*closeLoad)(LWLoadState *load);
  182. } LWFileIOFuncs;
  183. #endif