BStreamFileToolkit.h
1 // Copyright (c) Tech Soft 3D
2 //
3 // The information contained herein is confidential and proprietary to Tech Soft 3D, Inc.,
4 // and considered a trade secret as defined under civil and criminal statutes.
5 // Tech Soft 3D, Inc. shall pursue its civil and criminal remedies in the event of
6 // unauthorized use or misappropriation of its trade secrets. Use of this information
7 // by anyone other than authorized employees of Tech Soft 3D, Inc. is granted only under
8 // a written non-disclosure agreement, expressly prescribing the scope and manner of such use.
9 
10 #ifndef BBINFILETK_TOOLKIT
11 #define BBINFILETK_TOOLKIT
12 
13 #ifndef _MSC_VER
14 # define __wchar_t wchar_t
15 #else
16 # ifdef _DEBUG
17 # include <crtdbg.h>
18 # endif
19 #endif
20 
21 #if defined(WIN64) || defined(_M_X64) || defined(_WIN64)
22 # define HLONG __int64
23 #else
24 # define HLONG long
25 #endif
26 
27 #include "BStreamMemory.h"
28 
34 class BBINFILETK_API2 BControlledMemoryObject {
35  public:
36  void * operator new (size_t size);
37  void operator delete (void * p);
38 };
39 
40 class BBaseOpcodeHandler;
41 
42 
43 #ifndef DOXYGEN_SHOULD_SKIP_THIS
44 
45 /*
46  Buffers data so basic handlers can deal with simple Get/Put calls of basic data types (and arrays of those types).
47  Handles both reading and writing, ZLIB compressed or uncompressed.
48 */
49 class BBINFILETK_API2 Internal_Data_Accumulator
50 #ifdef HPS_CORE_BUILD
51  : public CMO
52 #else
54 #endif
55 {
56  private:
57  char * m_pending_buffer; /*< The whole burrito. */
58  int m_pending_buffer_allocated; /*< How big the buffer is (so we know when to reallocate) */
59  char * m_pending_position; /*< Start of actual data to be processed.*/
60  int m_pending_size; /*< Size of actual data. */
61 
62  char * m_buffer_data; /*< The buffer shown to the user (i.e. passed to GenerateBuffer or ParseBuffer) */
63  int m_buffer_size; /*< The size of the user's buffer */
64 
65  int m_failed_size; /*< When a read fails because of insufficient data, how much space would have been required for success? */
66 
67  int m_generated; /*< How much has been written into the user's buffer (after considering compression). */
68 
69  struct z_stream_s * m_z_stream; /*< The control structure for ZLIB */
70  bool m_compressed; /*< are we in compressed mode? */
71  bool m_writing; /*< are we compressing or decompressing? */
72  int m_original_size; /*< size of buffer originally passed in */
73 
74  public:
75  Internal_Data_Accumulator () : m_pending_buffer (0), m_pending_buffer_allocated (0),
76  m_pending_position (0), m_pending_size (0),
77  m_failed_size (0), m_generated (0),
78  m_z_stream (0), m_compressed (false), m_writing (false) {}
79  ~Internal_Data_Accumulator ();
80 
81  void set_data (char * b, int s) { m_buffer_data = b; m_original_size = m_buffer_size = s; }
82  void save ();
83  TK_Status consume ();
84 
85  TK_Status read (char * b, int s);
86  TK_Status write (char const * b, int s);
87  TK_Status lookat (char & b);
88 
89  int get_original_buffer_size () const {return m_original_size; }
90 
91  void restart ();
92  void clean ();
93  int unused () const { return m_buffer_size; }
94  int generated () const { return m_generated; }
95 
96  TK_Status start_compression ();
97  TK_Status stop_compression (bool flush);
98  TK_Status start_decompression ();
99  TK_Status stop_decompression (bool force);
100  bool compressed () const { return m_compressed; }
101 
102  TK_Status error (char const * msg = 0) const;
103 };
104 
105 
106 /*
107  Provides index <-> key translation, plus storage of additional item-specific data, such as
108  file offset and size and bounding volume.
109 */
110 struct IT_Index_Key_Extra {
111  int m_variants[8][2];
112  int m_options;
113  float m_bounds[6];
114 };
115 
116 class BBINFILETK_API2 Internal_Translator
117 #ifdef HPS_CORE_BUILD
118  : public CMO
119 #else
120  : public BControlledMemoryObject
121 #endif
122 {
123  friend class TK_Dictionary; // note, dictionary writer tied closely to this implementation
124  private:
125  // array -- index to key is trivial
126  int m_size;
127  int m_used;
128 
129  struct Index_Key_Pair {
130  int m_index;
131  ID_Key m_key;
132  IT_Index_Key_Extra *m_extra;
133  } * m_pairs;
134 
135  enum Options {
136  Bounds_Valid = 0x0001,
137 
138  Extended = 0x0080 // reserved
139  };
140 
141  // hash of key to locate potential indices for key to index lookup
142  struct Hash_Block {
143  Hash_Block * m_next;
144  int m_used;
145  int m_indices[32];
146  } * m_blocks[1024];
147 
148 
149  public:
150  Internal_Translator () : m_size (0), m_used (0), m_pairs (0) { memset (m_blocks, 0, 1024*sizeof(void *)); }
151  ~Internal_Translator ();
152 
153  TK_Status add_pair (int index, ID_Key key);
154  TK_Status add_variant (ID_Key key, int variant, int value1, int value2 = 0);
155  TK_Status add_bounds (ID_Key key, float const bounds[]);
156  TK_Status index_to_key (int index, ID_Key & key) const;
157  TK_Status key_to_index (ID_Key key, int & index) const;
158  TK_Status key_variant_offset (ID_Key key, int variant,
159  int & offset, int & length, int & index) const;
160  TK_Status key_bounds (ID_Key key, float bounds[]) const;
161  int used () const { return m_used; }
162 
163  void clean ();
164 
165  // older forms:
166  TK_Status key_variant_offset (ID_Key key, int variant, int & offset) const {
167  int length, index;
168  return key_variant_offset (key, variant, offset, length, index);
169  }
170  TK_Status key_variant_offset (ID_Key key, int variant, int & offset, int & length) const {
171  int index;
172  return key_variant_offset (key, variant, offset, length, index);
173  }
174 
175 };
176 
177 
178 class BBINFILETK_API2 Internal_Key_Record
179 #ifdef HPS_CORE_BUILD
180  : public CMO
181 #else
182  : public BControlledMemoryObject
183 #endif
184 {
185  private:
186  // hash of key to list of recorded segments
187  struct Hash_Block {
188  Hash_Block * m_next;
189  int m_used;
190  ID_Key m_keys[32];
191  } * m_blocks[1024];
192 
193 
194  public:
195  Internal_Key_Record () { memset (m_blocks, 0, 1024*sizeof(void *)); }
196  ~Internal_Key_Record ();
197 
198  TK_Status add_key (ID_Key key);
199  TK_Status find_key (ID_Key key) const;
200 
201  void clean ();
202 };
203 
204 
205 
206 // control memory on these objects, not sure who might be creating/destroying them
207 
208 class Internal_Segment_List
209 #ifdef HPS_CORE_BUILD
210  : public CMO
211 #else
212  : public BControlledMemoryObject
213 #endif
214 {
215  public:
216  Internal_Segment_List * m_next;
217  ID_Key m_key;
218 
219  public:
220  Internal_Segment_List (ID_Key k) : m_next (0), m_key (k) {}
221 
222  ID_Key key () const { return m_key; }
223 };
224 
225 class Internal_Revisit_Item
226 #ifdef HPS_CORE_BUILD
227  : public CMO
228 #else
229  : public BControlledMemoryObject
230 #endif
231 {
232  public:
233  Internal_Revisit_Item * m_next;
234  ID_Key m_key;
235  ID_Key m_owner;
236  int m_lod;
237  float m_priority;
238  char m_opcode;
239  bool m_force_context;
240 };
241 
242 
243 class Internal_ExRef_List
244 #ifdef HPS_CORE_BUILD
245  : public CMO
246 #else
247  : public BControlledMemoryObject
248 #endif
249 {
250  public:
251  Internal_ExRef_List * m_next;
252  Internal_ExRef_List * m_caller;
253  wchar_t * m_ref;
254  wchar_t * m_actual;
255  ID_Key m_context;
256 
257  public:
258  Internal_ExRef_List (char const * ref, ID_Key context, Internal_ExRef_List * caller);
259  Internal_ExRef_List (__wchar_t const * ref, ID_Key context, Internal_ExRef_List * caller);
260 #ifdef _MSC_VER
261  Internal_ExRef_List (unsigned short const * ref, ID_Key context, Internal_ExRef_List * caller);
262 #endif
263  ~Internal_ExRef_List ();
264 
265  wchar_t const * Reference () const { return m_ref; }
266  wchar_t const * Actual () const { return m_actual; }
267  ID_Key Context () const { return m_context; }
268  Internal_ExRef_List * Caller () const { return m_caller; }
269  void SetActual (__wchar_t const * actual);
270 #ifdef _MSC_VER
271  void SetActual (unsigned short const * actual);
272 #endif
273 };
274 
275 
276 class BBINFILETK_API Recorded_Instance
277 #ifdef HPS_CORE_BUILD
278  : public CMO
279 #else
280  : public BControlledMemoryObject
281 #endif
282 {
283  public:
284  Recorded_Instance * m_next;
285 
286  ID_Key m_key;
287  int m_variant;
288  int m_values[3];
289 
290  float m_local_basis[16]; // matrix from our basis to identity
291  int m_basis_indices[4];
292  float m_arbitrary_point[3];
293  int m_arbitrary_index;
294  bool m_basis_valid;
295 
296  unsigned char m_opcode;
297 
298 #ifdef _DEBUG
299  int m_times_used;
300 #endif
301 
302  Recorded_Instance (ID_Key key, int variant, unsigned char op, int val1, int val2, int val3)
303  : m_next (0), m_key (key), m_variant (variant), m_basis_valid (false), m_opcode (op) {
304  m_values[0] = val1; m_values[1] = val2; m_values[2] = val3;
305 #ifdef _DEBUG
306  m_times_used = 0;
307 #endif
308  }
309 
310  bool basis_valid () const { return m_basis_valid; }
311  bool generate_basis (int count, float const points[]);
312 };
313 
314 class Internal_Segment_Processor;
315 
316 /*
317  Callback which can be used for reporting progress during writing/reading.
318  During writing:
319  so_far is number of "objects" written, expected is the number the toolkit will likely write.
320  Note: the toolkit won't know about objects in pre/post-walk handlers or extra objects due to
321  overloading the default classes, so "so_far" may exceed "expected"
322  During reading:
323  so_far is the number of bytes processed, expected is the file size.
324  user_data allows the application to pass any necessary values through.
325  returns a flag which indicates whether the processing should continue.
326 */
327 typedef bool (*TK_Progress_Callback) (unsigned HLONG so_far, unsigned HLONG expected, void * user_data);
328 
329 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
330 
331 
332 
334 
367 class BBINFILETK_API2 BStreamFileToolkit
368 #ifdef HPS_CORE_BUILD
369  : public CMO
370 #else
371  : public BControlledMemoryObject
372 #endif
373 {
374  friend class BBaseOpcodeHandler;
375  friend class TK_Default;
376  friend class TK_Comment;
377  friend class TK_Header;
378  friend class TK_Compression;
379  friend class TK_Dictionary;
380  friend class Internal_Segment_Processor;
381  friend class TK_Shell;
382 
383  friend class TK_Tag;
384  friend class TK_Instance;
385  friend class HTK_Reference;
386  protected:
387 
388 #ifndef DOXYGEN_SHOULD_SKIP_THIS
389 
390  Internal_Data_Accumulator m_accumulator;
391  Internal_Translator m_translator;
393  BBaseOpcodeHandler * m_objects[256];
394  BBaseOpcodeHandler * m_default_object;
395  int m_prewalk_count;
396  int m_postwalk_count;
397  BBaseOpcodeHandler ** m_prewalk;
398  BBaseOpcodeHandler ** m_postwalk;
399  BBaseOpcodeHandler * m_current_object;
400  Internal_Segment_List * m_active_segments;
401  Internal_Key_Record m_visited_items;
402  ID_Key m_context_key;
403  ID_Key * m_last_keys;
404  int m_last_keys_used;
405  int m_last_keys_allocated;
406  Internal_Revisit_Item * m_revisit;
407  Internal_Revisit_Item * m_revisit_working;
408  int m_stage;
409  int m_substage;
410  int m_pass;
411  int m_tag_count;
412  int m_position;
413  unsigned int m_offset;
414  int m_unused;
415  int m_write_flags;
416  int m_read_flags;
417  int m_num_normal_bits;
418  int m_num_vertex_bits;
419  int m_num_parameter_bits;
420  int m_num_color_bits;
421  int m_num_index_bits;
422  int m_file_version;
423  int m_target_version;
424  bool m_header_comment_seen;
425  char * m_log_file;
426  FILE * m_log_fp;
427  bool m_logging;
428  unsigned int m_logging_options;
429  mutable unsigned int m_log_line_length;
430  unsigned int m_opcode_sequence;
431  unsigned int m_objects_written;
432  TK_Progress_Callback m_progress_callback;
433  void * m_progress_value;
434  int m_buffer_limit;
435  int m_nesting_level;
436  int m_dictionary_format;
437  int m_dictionary_options;
438  int m_dictionary_size;
439  int m_dictionary_offset;
440  Recorded_Instance * m_instance_hash[256];
441  int m_jpeg_quality;
442  int * m_pause_table;
443  int m_pause_table_size;
444  unsigned short m_num_pauses;
445  float * m_world_bounding;
446  Internal_ExRef_List * m_external_references;
447  Internal_ExRef_List * m_external_ref_tail;
448  Internal_ExRef_List * m_previous_exrefs;
449  bool m_suppress_errors;
451  wchar_t ** m_file_names;
452  int * m_file_indices;
453  int m_file_count;
454  int m_files_allocated;
455  wchar_t * m_current_filename;
456  int m_index_base;
457  float m_quantization_error;
459  int m_save_write_flags;
460  wchar_t * m_wfilename;
461  bool m_geometry_open;
463  bool m_is_ascii;
464  int m_num_tabs;
466  /* Internal use.
467  * A quick utility to verify that an array is in fact sorted.
468  * FOR DEBUGGING ONLY
469  */
470  bool issorted_revisit(Internal_Revisit_Item *array[], int count);
471 
476  void qsort_revisit(Internal_Revisit_Item **, Internal_Revisit_Item **);
477  TK_Status sort_revisit();
478 
479  virtual void read_completed ();
480 
481  bool add_exref (Internal_ExRef_List * exref);
482 
483 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
484 
485 
486  protected:
487 
489  void * m_file;
490 
491  public:
492 
497  virtual ~BStreamFileToolkit ();
498 
502  static bool SupportsAsciiMode() {
503 #ifndef BSTREAM_DISABLE_ASCII
504  return true;
505 #else
506  return false;
507 #endif
508  }
509 
513  TK_Status SetAsciiMode(bool whether);
514 
518  bool GetAsciiMode();
519 
520  void SetTabs(int);
521  int GetTabs();
522 
528  static int ParseVersion (char const * block);
529 
563  TK_Status ParseBuffer (char const * b, int s, TK_Status mode = TK_Normal);
564 
565 
571  TK_Status PrepareBuffer(char * b, int s);
572 
574  int CurrentBufferLength() {return m_accumulator.get_original_buffer_size() -
575  m_accumulator.unused();}
576 
582  virtual void ActivateContext (ID_Key key) { (void)key; }
583 
589  virtual void DeactivateContext (ID_Key key) { (void)key; }
590 
598  virtual void NewFileContext (ID_Key key) { (void)key; }
599 
605  int GeneratedSoFar () const { return m_accumulator.generated(); }
606 
611  unsigned int ObjectsSoFar () const { return m_objects_written; }
612 
619  void SetOpcodeHandler (int which, BBaseOpcodeHandler * handler);
620 
628  void SetPrewalkHandler (BBaseOpcodeHandler * handler);
629 
630 
638  void SetPostwalkHandler (BBaseOpcodeHandler * handler);
639 
641  BBaseOpcodeHandler * GetOpcodeHandler (int which) const { return m_objects[which]; }
642 
648  virtual void Restart ();
649 
650  // access to index <-> key tranlation
657  TK_Status IndexToKey (int index, ID_Key & key) const;
658 
665  TK_Status KeyToIndex (ID_Key key, int & index) const;
666 
674  { return m_translator.add_pair (index, key); }
675 
684  TK_Status AddVariant (ID_Key key, int variant, int value1, int value2 = -1)
685  { return m_translator.add_variant (key, variant, value1, value2); }
686 
693  TK_Status AddBounds (ID_Key key, float const bounds[])
694  { return m_translator.add_bounds (key, bounds); }
695 
703  TK_Status GetOffset (ID_Key key, int variant, int & offset) const
704  { return m_translator.key_variant_offset (key, variant, offset); }
705 
714  TK_Status GetOffset (ID_Key key, int variant, int & offset, int & length) const
715  { return m_translator.key_variant_offset (key, variant, offset, length); }
716 
726  TK_Status GetOffset (ID_Key key, int variant,
727  int & offset, int & length, __wchar_t const *& filename) const;
728 #ifdef _MSC_VER
729  TK_Status GetOffset (ID_Key key, int variant,
730  int & offset, int & length, unsigned short const *& filename) const;
731 #endif
732 
738  TK_Status GetBounds (ID_Key key, float bounds[]) const
739  { return m_translator.key_bounds (key, bounds); }
740 
745  int NextTagIndex () { return m_tag_count++; }
750  int PeekTagIndex () const { return m_tag_count; }
751 
757 #ifndef SWIG
758  void SetFilename (char const * name);
759 #endif
760 
765  void SetFilename (__wchar_t const * name);
766 #ifdef _MSC_VER
767  void SetFilename (unsigned short const * name);
768 #endif
769 
773  TK_Status Read_Stream_File ();
774 
780 #ifndef SWIG
781  void SetNewFile (char const * name);
782 #endif
783 
788  void SetNewFile (__wchar_t const * name);
789 #ifdef _MSC_VER
790  void SetNewFile (unsigned short const * name);
791 #endif
792 
796  __wchar_t const * GetCurrentFile () const { return (__wchar_t const *)m_current_filename; }
801  void GetCurrentFile (__wchar_t const *& filename) const { filename = (__wchar_t const *)m_current_filename; }
802 #ifdef _MSC_VER
803  void GetCurrentFile (unsigned short const *& filename) const { filename = (unsigned short const *)m_current_filename; }
804 #endif
805 
806 
807 
814 #ifndef SWIG
815  TK_Status SelectFile (char const * name);
816 #endif
817 
823  TK_Status SelectFile (__wchar_t const * name);
824 #ifdef _MSC_VER
825  TK_Status SelectFile (unsigned short const * name);
826 #endif
827 
834 #ifndef SWIG
835  virtual TK_Status OpenFile (char const * name, bool write = false);
836 #endif
837 
844  virtual TK_Status OpenFile (__wchar_t const * name, bool write = false);
845 #ifdef _MSC_VER
846  virtual TK_Status OpenFile (unsigned short const * name, bool write = false);
847 #endif
848 
852  virtual TK_Status CloseFile ();
853 
863  virtual TK_Status ReadBuffer (char * buffer, int size, int & amount_read);
864 
873  virtual TK_Status WriteBuffer (char * buffer, int size);
874 
884  virtual TK_Status PositionFile (int offset);
885 
894  virtual TK_Status GetFileSize (unsigned HLONG & size);
895 
896 
908  virtual TK_Status LocateDictionary ();
909 
920  virtual TK_Status LocateEntity (ID_Key key, int variant);
921 
922  int GetFlags () const { return GetWriteFlags(); }
923  void SetFlags (int flags) { SetWriteFlags(flags); }
927  void SetWriteFlags (int flags) { m_write_flags = flags; }
933  int GetWriteFlags (int mask = ~0) const { return m_write_flags & mask; }
934 
936  void SetReadFlags (int flags) { m_read_flags = flags; }
942  int GetReadFlags (int mask = ~0) const { return m_read_flags & mask; }
943 
944 
945  int GetNumNormalBits () const { return m_num_normal_bits; }
951  void SetNumNormalBits (int numbits) { m_num_normal_bits = (numbits<=72) ? numbits : 72; }
952 
953  int GetNumVertexBits () const { return m_num_vertex_bits; }
955  void SetNumVertexBits (int numbits) { m_num_vertex_bits = (numbits<=72) ? numbits : 72; }
956  int GetNumParameterBits () const { return m_num_parameter_bits; }
961  void SetNumParameterBits (int numbits) { m_num_parameter_bits = (numbits<=72) ? numbits : 72; }
962  int GetNumColorBits () const { return m_num_color_bits; }
963  void SetNumColorBits (int numbits) { m_num_color_bits = (numbits<=72) ? numbits : 72; }
964  int GetNumIndexBits () const { return m_num_index_bits; }
965  void SetNumIndexBits (int numbits) { m_num_index_bits = (numbits<=24) ? numbits : 24; }
968  void SetJpegQuality (int quality = 75) { m_jpeg_quality = quality; }
970  int GetJpegQuality () const { return m_jpeg_quality; }
971 
972  int GetVersion () const { return m_file_version; }
973  void SetReadVersion (int version) { m_file_version = version; m_header_comment_seen = true; }
981  void SetTargetVersion (int version) {
982  m_target_version = version;
983  if ((m_target_version < 1) || (m_target_version > TK_File_Format_Version))
984  m_target_version = TK_File_Format_Version;
985  }
986  int GetTargetVersion () const { return m_target_version; }
988  unsigned int GetFileOffset () const { return m_offset; }
991  void SetFileOffset (unsigned int offset) { m_offset = offset; }
993  int Unused () const { return m_unused; }
997  virtual TK_Status Error(char const * msg = 0) const;
998 
1000  char const *GetLogFile () const { return m_log_file; }
1002  void SetLogFile (char const * filename = 0);
1003 
1005  bool GetLogging () const { return m_logging; }
1009  void SetLogging (bool setting) { m_logging = setting; }
1010 
1012  unsigned int GetLoggingOptions (unsigned int mask = ~0) const
1013  { return m_logging_options & mask; }
1015  void SetLoggingOptions (unsigned int options = ~0)
1016  { m_logging_options = options; }
1017 
1022  TK_Status OpenLogFile (char const * filename, char const * mode);
1024 #ifndef SWIG
1025  void LogEntry (char const * string) const;
1026 #endif
1027 
1028  void LogEntry (__wchar_t const * string) const;
1029 #ifdef _MSC_VER
1030  void LogEntry (unsigned short const * string) const;
1031 #endif
1032 
1033  void CloseLogFile ();
1034 
1036  unsigned int NextOpcodeSequence () { return ++m_opcode_sequence; }
1038  void SetOpcodeSequence (unsigned int seq=0) { m_opcode_sequence = seq; }
1039 
1041  bool HeaderCommentSeen() const { return m_header_comment_seen; }
1042 
1044  TK_Progress_Callback GetProgressCallback () const { return m_progress_callback; }
1046  void SetProgressCallback (TK_Progress_Callback cb = 0) { m_progress_callback = cb; }
1047 
1049  void * GetProgressValue () const { return m_progress_value; }
1051  void SetProgressValue (void * value) { m_progress_value = value; }
1052 
1054  int GetBufferLimit () const { return m_buffer_limit; }
1056  void SetBufferLimit (int limit) {
1057  m_buffer_limit = (0 < limit && limit < TK_DEFAULT_BUFFER_SIZE) ?
1058  limit : TK_DEFAULT_BUFFER_SIZE;
1059  }
1060 
1062  void SetLastKey (ID_Key key);
1064  TK_Status AppendLastKey (ID_Key key);
1066  void ClearLastKey ();
1068  TK_Status GetLastKey (ID_Key &key) const;
1069 
1075  void SetDictionaryFormat (int format = 3, int options = TK_Dictionary_Bounding_Volumes)
1076  { m_dictionary_format = format; m_dictionary_options = options; }
1081  int GetDictionaryFormat () const { return m_dictionary_format; }
1086  int GetDictionaryOptions () const { return m_dictionary_options; }
1091  void SetDictionaryOffset (int offset) { m_dictionary_offset = offset; }
1096  int GetDictionaryOffset () const { return m_dictionary_offset; }
1101  void SetDictionarySize (int size) { m_dictionary_size = size; }
1106  int GetDictionarySize () const { return m_dictionary_size; }
1107 
1112  void RecordPause (int offset);
1116  void ClearPauses () { m_num_pauses = 0; }
1121  int GetPauseCount () const { return m_num_pauses; }
1126  int const * GetPauseTable () const { return m_pause_table; }
1127 
1129  void SetFirstPause (int offset) { if (GetPauseCount() == 0) RecordPause (offset);
1130  else m_pause_table[0] = offset; }
1132  int GetFirstPause () const { return (m_num_pauses > 0) ? m_pause_table[0] : 0; }
1133 
1137  int GetPosition () const { return m_position; }
1138 
1143  void SetWorldBounding (float const bbox[]);
1144 
1150  void SetWorldBoundingBySphere (float const pt[], float radius);
1151 
1153  float const * GetWorldBounding () const { return m_world_bounding; }
1154 
1155 
1161 #ifndef SWIG
1162  bool AddExternalReference (char const * ref, ID_Key context);
1163 #endif
1164 
1169  bool AddExternalReference (__wchar_t const * ref, ID_Key context);
1170 #ifdef _MSC_VER
1171  bool AddExternalReference (unsigned short const * ref, ID_Key context);
1172 #endif
1173 
1176  bool NextExternalReference ();
1180  wchar_t const * GetExternalReference () const {
1181  return (m_external_references != 0) ? (wchar_t const*)m_external_references->Reference() : 0;
1182  }
1186  void GetExternalReference (__wchar_t const *& exref) const {
1187  exref = (m_external_references != 0) ? (__wchar_t const *)m_external_references->Reference() : 0;
1188  }
1189 #ifdef _MSC_VER
1190  void GetExternalReference (unsigned short const *& exref) const {
1191  exref = (m_external_references != 0) ? (unsigned short const *)m_external_references->Reference() : 0;
1192  }
1193 #endif
1194 
1199  return (m_external_references != 0) ? m_external_references->m_context : -1;
1200  }
1201 
1205  virtual bool MatchPreviousExRef () const { return false; }
1206 
1210  void AddSegment (ID_Key key);
1214  ID_Key RemoveSegment ();
1218  ID_Key CurrentSegment () { return (m_active_segments != 0) ? m_active_segments->key() : -1; }
1220  void ResetQuantizationError() { m_quantization_error = 0; }
1224  void ReportQuantizationError(float error) { if (error > m_quantization_error) m_quantization_error = error; };
1228  void ReportQuantizationError(int bits_per_sample, float const *bounding, int num_dimensions = 3);
1232  float GetQuantizationError() const { return m_quantization_error; }
1233 
1238  if (m_geometry_open)
1239  return Error ("recursive geometry open");
1240  m_geometry_open = true;
1241  return TK_Normal;
1242  }
1247  if (!m_geometry_open)
1248  return Error ("no geometry open");
1249  m_geometry_open = false;
1250  return TK_Normal;
1251  }
1252 
1256  bool GeometryIsOpen () const { return m_geometry_open; }
1257 
1258  ID_Key RevisitKey () const { return m_revisit_working ? m_revisit_working->m_key : -1; }
1259  ID_Key RevisitOwner () const { return m_revisit_working ? m_revisit_working->m_owner : -1; }
1260 
1261  protected:
1262 
1263 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1264 
1265  virtual void FileDone() { ; };
1266 
1267  // normal data access for objects
1268  TK_Status read (char * b, int n) { return m_accumulator.read (b, n); }
1269  TK_Status write (char const * b, int n) { return m_accumulator.write (b, n); }
1270  TK_Status lookat (char & b) { return m_accumulator.lookat (b); }
1271 
1272  // used by segment handlers to make sure we know which segment should be "open"
1273  void add_segment (ID_Key key) { AddSegment (key); }
1274  ID_Key remove_segment () { return RemoveSegment(); }
1275 
1276  // used by renumber key, maybe by lookup functions (to be implemented...)
1278  void set_last_key (ID_Key key) { SetLastKey(key); }
1279  ID_Key context_key () const { return m_context_key; }
1280  void set_context_key (ID_Key key);
1282  ID_Key last_key () const {
1283  if(m_last_keys_used == 1)
1284  return m_last_keys[0];
1285  else
1286  return -1;
1287  }
1288 
1289  // keep track of visited segments (for things like include)
1290  void remember_item (ID_Key key);
1291  bool find_item (ID_Key key) const;
1292 
1294  BBaseOpcodeHandler * opcode_handler (int index) const { return m_objects[index]; }
1295 
1296  void adjust_written (int count) { m_objects_written += count; }
1297 
1298  int pass () const { return m_pass; }
1299 
1301  TK_Status revisit (unsigned char opcode, float priority, int lod=0);
1302 
1304  void record_instance (ID_Key key, int variant, BBaseOpcodeHandler const * object,
1305  int val1, int val2 = 0, int val3 = 0);
1306 
1308  bool find_instance (BBaseOpcodeHandler * object, int val1, int val2 = 0, int val3 = 0) const;
1309 
1311  int position () const { return m_position; }
1312  void set_position (int pos) { m_position = pos; }
1313  void mark_position () { set_position (GeneratedSoFar()); }
1314 
1319  virtual TK_Status tag (int variant);
1320 
1321  void increase_nesting (int amount = 1) { m_nesting_level += amount; }
1322  void decrease_nesting (int amount = 1) { m_nesting_level -= amount; }
1323 
1324  // utility
1325  TK_Status start_compression () { return m_accumulator.start_compression(); }
1326  TK_Status stop_compression () { return m_accumulator.stop_compression(true); }
1327  TK_Status start_decompression () { return m_accumulator.start_decompression(); }
1328  TK_Status stop_decompression (bool force = false) { return m_accumulator.stop_decompression(force); }
1329  virtual void empty_lists ();
1330 
1331 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
1332 
1333 };
1334 
1335 //TODO Documentation
1336 
1337 #ifndef BSTREAM_DISABLE_ASCII
1338 class PutTab
1339 {
1340 public:
1341  PutTab(BStreamFileToolkit* tk) : m_tk(tk)
1342  {
1343  int n_tabs = m_tk->GetTabs();
1344  m_tk->SetTabs(++n_tabs);
1345  }
1346 
1347  ~PutTab()
1348  {
1349  int n_tabs = m_tk->GetTabs();
1350  m_tk->SetTabs(--n_tabs);
1351  }
1352 
1353 private:
1354  BStreamFileToolkit* m_tk;
1355 };
1356 class Outdent
1357 {
1358 public:
1359  Outdent(BStreamFileToolkit* tk, int n_tabs = 1) : m_tk(tk)
1360  {
1361  int cur_tabs = m_tk->GetTabs();
1362  if(cur_tabs >= n_tabs)
1363  {
1364  m_tabs = n_tabs;
1365  m_tk->SetTabs(cur_tabs-n_tabs);
1366  }
1367  else
1368  {
1369  m_tabs = cur_tabs;
1370  m_tk->SetTabs(0);
1371  }
1372  }
1373 
1374  ~Outdent()
1375  {
1376  int cur_tabs = m_tk->GetTabs();
1377  m_tk->SetTabs(cur_tabs+m_tabs);
1378  }
1379 
1380 private:
1381  BStreamFileToolkit* m_tk;
1382  int m_tabs;
1383 };
1384 
1385 #endif //BSTREAM_DISABLE_ASCII
1386 
1387 #endif //BBINFILETK_TOOLKIT
Definition: BStreamFileToolkit.h:1338
Handles the TKE_Shell opcode.
Definition: BOpcodeShell.h:25
char const * GetLogFile() const
Definition: BStreamFileToolkit.h:1000
bool GetLogging() const
Definition: BStreamFileToolkit.h:1005
int GetFlags() const
Definition: BStreamFileToolkit.h:922
unsigned int NextOpcodeSequence()
Definition: BStreamFileToolkit.h:1036
void GetCurrentFile(__wchar_t const *&filename) const
Definition: BStreamFileToolkit.h:801
#define TK_DEFAULT_BUFFER_SIZE
default amount of the internal memory buffer used for file processing
Definition: BStream.h:183
int GetDictionarySize() const
Definition: BStreamFileToolkit.h:1106
__wchar_t const * GetCurrentFile() const
Definition: BStreamFileToolkit.h:796
virtual void NewFileContext(ID_Key key)
Definition: BStreamFileToolkit.h:598
void SetJpegQuality(int quality=75)
Definition: BStreamFileToolkit.h:968
int GetBufferLimit() const
Definition: BStreamFileToolkit.h:1054
int GetNumColorBits() const
Definition: BStreamFileToolkit.h:962
TK_Progress_Callback GetProgressCallback() const
Definition: BStreamFileToolkit.h:1044
Handles the TKE_Comment opcode.
Definition: BOpcodeHandler.h:1073
The BStreamFileToolkit class provides support for importing/exporting HOOPS Stream File information...
Definition: BStreamFileToolkit.h:367
TK_Status CloseGeometry()
Definition: BStreamFileToolkit.h:1246
void SetReadVersion(int version)
Definition: BStreamFileToolkit.h:973
Handles the TKE_Tag opcode.
Definition: BOpcodeHandler.h:1923
Definition: BStreamFileToolkit.h:34
void SetOpcodeSequence(unsigned int seq=0)
Definition: BStreamFileToolkit.h:1038
void SetTargetVersion(int version)
Definition: BStreamFileToolkit.h:981
void SetReadFlags(int flags)
Definition: BStreamFileToolkit.h:936
void SetFlags(int flags)
Definition: BStreamFileToolkit.h:923
int NextTagIndex()
Definition: BStreamFileToolkit.h:745
#define TK_File_Format_Version
Definition: BStream.h:70
void SetFirstPause(int offset)
Definition: BStreamFileToolkit.h:1129
void SetBufferLimit(int limit)
Definition: BStreamFileToolkit.h:1056
void SetLoggingOptions(unsigned int options=~0)
Definition: BStreamFileToolkit.h:1015
void * m_file
Definition: BStreamFileToolkit.h:489
void SetLogging(bool setting)
Definition: BStreamFileToolkit.h:1009
Definition: BOpcodeHandler.h:964
static bool SupportsAsciiMode()
Definition: BStreamFileToolkit.h:502
int const * GetPauseTable() const
Definition: BStreamFileToolkit.h:1126
int GetFirstPause() const
Definition: BStreamFileToolkit.h:1132
int GetWriteFlags(int mask=~0) const
Definition: BStreamFileToolkit.h:933
int GetTargetVersion() const
Definition: BStreamFileToolkit.h:986
wchar_t const * GetExternalReference() const
Definition: BStreamFileToolkit.h:1180
Handles the TKE_Repeat_Object opcode.
Definition: BOpcodeHandler.h:1765
int Unused() const
Definition: BStreamFileToolkit.h:993
int GetPosition() const
Definition: BStreamFileToolkit.h:1137
TK_Status GetOffset(ID_Key key, int variant, int &offset, int &length) const
Definition: BStreamFileToolkit.h:714
int GetDictionaryOptions() const
Definition: BStreamFileToolkit.h:1086
void GetExternalReference(__wchar_t const *&exref) const
Definition: BStreamFileToolkit.h:1186
int GetReadFlags(int mask=~0) const
Definition: BStreamFileToolkit.h:942
void SetFileOffset(unsigned int offset)
Sets the file offset, a displacement to be added to positions used in the dictionary (for example...
Definition: BStreamFileToolkit.h:991
TK_Status AddVariant(ID_Key key, int variant, int value1, int value2=-1)
Definition: BStreamFileToolkit.h:684
Handles the TKE_Dictionary opcode.
Definition: BOpcodeHandler.h:1952
Definition: BStream.h:254
BBaseOpcodeHandler * GetOpcodeHandler(int which) const
Definition: BStreamFileToolkit.h:641
float GetQuantizationError() const
Definition: BStreamFileToolkit.h:1232
float const * GetWorldBounding() const
Definition: BStreamFileToolkit.h:1153
void SetNumColorBits(int numbits)
Definition: BStreamFileToolkit.h:963
void ReportQuantizationError(float error)
Definition: BStreamFileToolkit.h:1224
TK_Status GetOffset(ID_Key key, int variant, int &offset) const
Definition: BStreamFileToolkit.h:703
TK_Status AddBounds(ID_Key key, float const bounds[])
Definition: BStreamFileToolkit.h:693
ID_Key GetExternalReferenceContext() const
Definition: BStreamFileToolkit.h:1198
int GetDictionaryOffset() const
Definition: BStreamFileToolkit.h:1096
TK_Status OpenGeometry()
Definition: BStreamFileToolkit.h:1237
void ResetQuantizationError()
Definition: BStreamFileToolkit.h:1220
void SetDictionarySize(int size)
Definition: BStreamFileToolkit.h:1101
int CurrentBufferLength()
Definition: BStreamFileToolkit.h:574
int PeekTagIndex() const
Definition: BStreamFileToolkit.h:750
TK_Status AddIndexKeyPair(int index, ID_Key key)
Definition: BStreamFileToolkit.h:673
unsigned int GetLoggingOptions(unsigned int mask=~0) const
Definition: BStreamFileToolkit.h:1012
bool GeometryIsOpen() const
Definition: BStreamFileToolkit.h:1256
The BBaseOpcodeHandler abstract class is used as a base for derived classes which manage logical piec...
Definition: BOpcodeHandler.h:53
void SetNumNormalBits(int numbits)
Definition: BStreamFileToolkit.h:951
TK_Status
Codes which can be either passed to various toolkit functions, or indicate the result of a toolkit fu...
Definition: BStream.h:253
bool HeaderCommentSeen() const
Definition: BStreamFileToolkit.h:1041
void SetWriteFlags(int flags)
Definition: BStreamFileToolkit.h:927
int GetNumVertexBits() const
Definition: BStreamFileToolkit.h:953
int GetNumParameterBits() const
Definition: BStreamFileToolkit.h:956
void SetDictionaryOffset(int offset)
Definition: BStreamFileToolkit.h:1091
dictionary entries include bounding volume info
Definition: BStream.h:302
int GetPauseCount() const
Definition: BStreamFileToolkit.h:1121
void SetNumParameterBits(int numbits)
Definition: BStreamFileToolkit.h:961
unsigned int ObjectsSoFar() const
Definition: BStreamFileToolkit.h:611
#define ID_Key
Definition: BStream.h:229
int GeneratedSoFar() const
Definition: BStreamFileToolkit.h:605
Utility class for managing HSF header information.
Definition: BOpcodeHandler.h:1006
void SetProgressCallback(TK_Progress_Callback cb=0)
Definition: BStreamFileToolkit.h:1046
int GetNumIndexBits() const
Definition: BStreamFileToolkit.h:964
void SetNumVertexBits(int numbits)
Definition: BStreamFileToolkit.h:955
Handles the TKE_Start_Compression and TKE_Stop_Compression opcodes.
Definition: BOpcodeHandler.h:1169
void SetDictionaryFormat(int format=3, int options=TK_Dictionary_Bounding_Volumes)
Definition: BStreamFileToolkit.h:1075
void SetNumIndexBits(int numbits)
Definition: BStreamFileToolkit.h:965
virtual void DeactivateContext(ID_Key key)
Definition: BStreamFileToolkit.h:589
Definition: BStreamFileToolkit.h:1356
int GetDictionaryFormat() const
Definition: BStreamFileToolkit.h:1081
void SetProgressValue(void *value)
Definition: BStreamFileToolkit.h:1051
ID_Key CurrentSegment()
Definition: BStreamFileToolkit.h:1218
void ClearPauses()
Definition: BStreamFileToolkit.h:1116
void * GetProgressValue() const
Definition: BStreamFileToolkit.h:1049
TK_Status GetBounds(ID_Key key, float bounds[]) const
Definition: BStreamFileToolkit.h:738
int GetJpegQuality() const
Definition: BStreamFileToolkit.h:970
int GetNumNormalBits() const
Definition: BStreamFileToolkit.h:945
virtual bool MatchPreviousExRef() const
Definition: BStreamFileToolkit.h:1205
int GetVersion() const
Definition: BStreamFileToolkit.h:972
virtual void ActivateContext(ID_Key key)
Definition: BStreamFileToolkit.h:582