BStreamFileToolkit.h
1 // Copyright (c) Tech Soft 3D, Inc.
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  BBaseOpcodeHandler * m_must_delete;
401  Internal_Segment_List * m_active_segments;
402  Internal_Key_Record m_visited_items;
403  ID_Key m_context_key;
404  ID_Key * m_last_keys;
405  int m_last_keys_used;
406  int m_last_keys_allocated;
407  Internal_Revisit_Item * m_revisit;
408  Internal_Revisit_Item * m_revisit_working;
409  int m_stage;
410  int m_substage;
411  int m_pass;
412  int m_tag_count;
413  int m_position;
414  unsigned int m_offset;
415  int m_unused;
416  int m_write_flags;
417  int m_read_flags;
418  int m_num_normal_bits;
419  int m_num_vertex_bits;
420  int m_num_parameter_bits;
421  int m_num_color_bits;
422  int m_num_index_bits;
423  int m_file_version;
424  int m_target_version;
425  bool m_header_comment_seen;
426  char * m_log_file;
427  FILE * m_log_fp;
428  bool m_logging;
429  unsigned int m_logging_options;
430  mutable unsigned int m_log_line_length;
431  unsigned int m_opcode_sequence;
432  unsigned int m_objects_written;
433  TK_Progress_Callback m_progress_callback;
434  void * m_progress_value;
435  int m_buffer_limit;
436  int m_nesting_level;
437  int m_dictionary_format;
438  int m_dictionary_options;
439  int m_dictionary_size;
440  int m_dictionary_offset;
441  Recorded_Instance * m_instance_hash[256];
442  int m_jpeg_quality;
443  int * m_pause_table;
444  int m_pause_table_size;
445  unsigned short m_num_pauses;
446  float * m_world_bounding;
447  Internal_ExRef_List * m_external_references;
448  Internal_ExRef_List * m_external_ref_tail;
449  Internal_ExRef_List * m_previous_exrefs;
450  bool m_suppress_errors;
452  wchar_t ** m_file_names;
453  int * m_file_indices;
454  int m_file_count;
455  int m_files_allocated;
456  wchar_t * m_current_filename;
457  int m_index_base;
458  float m_quantization_error;
460  int m_save_write_flags;
461  wchar_t * m_wfilename;
462  bool m_geometry_open;
464  bool m_is_ascii;
465  int m_num_tabs;
467  /* Internal use.
468  * A quick utility to verify that an array is in fact sorted.
469  * FOR DEBUGGING ONLY
470  */
471  bool issorted_revisit(Internal_Revisit_Item *array[], int count);
472 
477  void qsort_revisit(Internal_Revisit_Item **, Internal_Revisit_Item **);
478  TK_Status sort_revisit();
479 
480  virtual void read_completed ();
481 
482  bool add_exref (Internal_ExRef_List * exref);
483 
484 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
485 
486 
487  protected:
488 
490  void * m_file;
491 
492  public:
493 
498  virtual ~BStreamFileToolkit ();
499 
503  static bool SupportsAsciiMode() {
504 #ifndef BSTREAM_DISABLE_ASCII
505  return true;
506 #else
507  return false;
508 #endif
509  }
510 
514  TK_Status SetAsciiMode(bool whether);
515 
519  bool GetAsciiMode();
520 
521  void SetTabs(int);
522  int GetTabs();
523 
529  static int ParseVersion (char const * block);
530 
564  virtual TK_Status ParseBuffer (char const * b, int s, TK_Status mode = TK_Normal);
565 
566 
572  TK_Status PrepareBuffer(char * b, int s);
573 
575  int CurrentBufferLength() {return m_accumulator.get_original_buffer_size() -
576  m_accumulator.unused();}
577 
583  virtual void ActivateContext (ID_Key key) { (void)key; }
584 
590  virtual void DeactivateContext (ID_Key key) { (void)key; }
591 
599  virtual void NewFileContext (ID_Key key) { (void)key; }
600 
606  int GeneratedSoFar () const { return m_accumulator.generated(); }
607 
612  unsigned int ObjectsSoFar () const { return m_objects_written; }
613 
620  void SetOpcodeHandler (int which, BBaseOpcodeHandler * handler);
621 
629  void SetPrewalkHandler (BBaseOpcodeHandler * handler);
630 
631 
639  void SetPostwalkHandler (BBaseOpcodeHandler * handler);
640 
642  BBaseOpcodeHandler * GetOpcodeHandler (int which) const { return m_objects[which]; }
643 
649  virtual void Restart ();
650 
651  // access to index <-> key tranlation
658  TK_Status IndexToKey (int index, ID_Key & key) const;
659 
666  TK_Status KeyToIndex (ID_Key key, int & index) const;
667 
675  { return m_translator.add_pair (index, key); }
676 
685  TK_Status AddVariant (ID_Key key, int variant, int value1, int value2 = -1)
686  { return m_translator.add_variant (key, variant, value1, value2); }
687 
694  TK_Status AddBounds (ID_Key key, float const bounds[])
695  { return m_translator.add_bounds (key, bounds); }
696 
704  TK_Status GetOffset (ID_Key key, int variant, int & offset) const
705  { return m_translator.key_variant_offset (key, variant, offset); }
706 
715  TK_Status GetOffset (ID_Key key, int variant, int & offset, int & length) const
716  { return m_translator.key_variant_offset (key, variant, offset, length); }
717 
727  TK_Status GetOffset (ID_Key key, int variant,
728  int & offset, int & length, __wchar_t const *& filename) const;
729 #ifdef _MSC_VER
730  TK_Status GetOffset (ID_Key key, int variant,
731  int & offset, int & length, unsigned short const *& filename) const;
732 #endif
733 
739  TK_Status GetBounds (ID_Key key, float bounds[]) const
740  { return m_translator.key_bounds (key, bounds); }
741 
746  int NextTagIndex () { return m_tag_count++; }
751  int PeekTagIndex () const { return m_tag_count; }
752 
758 #ifndef SWIG
759  void SetFilename (char const * name);
760 #endif
761 
766  void SetFilename (__wchar_t const * name);
767 #ifdef _MSC_VER
768  void SetFilename (unsigned short const * name);
769 #endif
770 
774  TK_Status Read_Stream_File ();
775 
781 #ifndef SWIG
782  void SetNewFile (char const * name);
783 #endif
784 
789  void SetNewFile (__wchar_t const * name);
790 #ifdef _MSC_VER
791  void SetNewFile (unsigned short const * name);
792 #endif
793 
797  __wchar_t const * GetCurrentFile () const { return (__wchar_t const *)m_current_filename; }
802  void GetCurrentFile (__wchar_t const *& filename) const { filename = (__wchar_t const *)m_current_filename; }
803 #ifdef _MSC_VER
804  void GetCurrentFile (unsigned short const *& filename) const { filename = (unsigned short const *)m_current_filename; }
805 #endif
806 
807 
808 
815 #ifndef SWIG
816  TK_Status SelectFile (char const * name);
817 #endif
818 
824  TK_Status SelectFile (__wchar_t const * name);
825 #ifdef _MSC_VER
826  TK_Status SelectFile (unsigned short const * name);
827 #endif
828 
835 #ifndef SWIG
836  virtual TK_Status OpenFile (char const * name, bool write = false);
837 #endif
838 
845  virtual TK_Status OpenFile (__wchar_t const * name, bool write = false);
846 #ifdef _MSC_VER
847  virtual TK_Status OpenFile (unsigned short const * name, bool write = false);
848 #endif
849 
853  virtual TK_Status CloseFile ();
854 
864  virtual TK_Status ReadBuffer (char * buffer, int size, int & amount_read);
865 
874  virtual TK_Status WriteBuffer (char * buffer, int size);
875 
885  virtual TK_Status PositionFile (int offset);
886 
895  virtual TK_Status GetFileSize (unsigned HLONG & size);
896 
897 
909  virtual TK_Status LocateDictionary ();
910 
921  virtual TK_Status LocateEntity (ID_Key key, int variant);
922 
923  int GetFlags () const { return GetWriteFlags(); }
924  void SetFlags (int flags) { SetWriteFlags(flags); }
928  void SetWriteFlags (int flags) { m_write_flags = flags; }
934  int GetWriteFlags (int mask = ~0) const { return m_write_flags & mask; }
935 
937  void SetReadFlags (int flags) { m_read_flags = flags; }
943  int GetReadFlags (int mask = ~0) const { return m_read_flags & mask; }
944 
945 
946  int GetNumNormalBits () const { return m_num_normal_bits; }
952  void SetNumNormalBits (int numbits) { m_num_normal_bits = (numbits<=72) ? numbits : 72; }
953 
954  int GetNumVertexBits () const { return m_num_vertex_bits; }
956  void SetNumVertexBits (int numbits) { m_num_vertex_bits = (numbits<=72) ? numbits : 72; }
957  int GetNumParameterBits () const { return m_num_parameter_bits; }
962  void SetNumParameterBits (int numbits) { m_num_parameter_bits = (numbits<=72) ? numbits : 72; }
963  int GetNumColorBits () const { return m_num_color_bits; }
964  void SetNumColorBits (int numbits) { m_num_color_bits = (numbits<=72) ? numbits : 72; }
965  int GetNumIndexBits () const { return m_num_index_bits; }
966  void SetNumIndexBits (int numbits) { m_num_index_bits = (numbits<=24) ? numbits : 24; }
969  void SetJpegQuality (int quality = 75) { m_jpeg_quality = quality; }
971  int GetJpegQuality () const { return m_jpeg_quality; }
972 
973  int GetVersion () const { return m_file_version; }
974  void SetReadVersion (int version) { m_file_version = version; m_header_comment_seen = true; }
982  void SetTargetVersion (int version) {
983  m_target_version = version;
984  if ((m_target_version < 1) || (m_target_version > TK_File_Format_Version))
985  m_target_version = TK_File_Format_Version;
986  }
987  int GetTargetVersion () const { return m_target_version; }
989  unsigned int GetFileOffset () const { return m_offset; }
992  void SetFileOffset (unsigned int offset) { m_offset = offset; }
994  int Unused () const { return m_unused; }
998  virtual TK_Status Error(char const * msg = 0) const;
999 
1001  char const *GetLogFile () const { return m_log_file; }
1003  void SetLogFile (char const * filename = 0);
1004 
1006  bool GetLogging () const { return m_logging; }
1010  void SetLogging (bool setting) { m_logging = setting; }
1011 
1013  unsigned int GetLoggingOptions (unsigned int mask = ~0) const
1014  { return m_logging_options & mask; }
1016  void SetLoggingOptions (unsigned int options = ~0)
1017  { m_logging_options = options; }
1018 
1023  TK_Status OpenLogFile (char const * filename, char const * mode);
1025 #ifndef SWIG
1026  void LogEntry (char const * string) const;
1027 #endif
1028 
1029  void LogEntry (__wchar_t const * string) const;
1030 #ifdef _MSC_VER
1031  void LogEntry (unsigned short const * string) const;
1032 #endif
1033 
1034  void CloseLogFile ();
1035 
1037  unsigned int NextOpcodeSequence () { return ++m_opcode_sequence; }
1039  void SetOpcodeSequence (unsigned int seq=0) { m_opcode_sequence = seq; }
1040 
1042  bool HeaderCommentSeen() const { return m_header_comment_seen; }
1043 
1045  TK_Progress_Callback GetProgressCallback () const { return m_progress_callback; }
1047  void SetProgressCallback (TK_Progress_Callback cb = 0) { m_progress_callback = cb; }
1048 
1050  void * GetProgressValue () const { return m_progress_value; }
1052  void SetProgressValue (void * value) { m_progress_value = value; }
1053 
1055  int GetBufferLimit () const { return m_buffer_limit; }
1057  void SetBufferLimit (int limit) {
1058  m_buffer_limit = (0 < limit && limit < TK_DEFAULT_BUFFER_SIZE) ?
1059  limit : TK_DEFAULT_BUFFER_SIZE;
1060  }
1061 
1063  void SetLastKey (ID_Key key);
1065  TK_Status AppendLastKey (ID_Key key);
1067  void ClearLastKey ();
1069  TK_Status GetLastKey (ID_Key &key) const;
1070 
1076  void SetDictionaryFormat (int format = 3, int options = TK_Dictionary_Bounding_Volumes)
1077  { m_dictionary_format = format; m_dictionary_options = options; }
1082  int GetDictionaryFormat () const { return m_dictionary_format; }
1087  int GetDictionaryOptions () const { return m_dictionary_options; }
1092  void SetDictionaryOffset (int offset) { m_dictionary_offset = offset; }
1097  int GetDictionaryOffset () const { return m_dictionary_offset; }
1102  void SetDictionarySize (int size) { m_dictionary_size = size; }
1107  int GetDictionarySize () const { return m_dictionary_size; }
1108 
1113  void RecordPause (int offset);
1117  void ClearPauses () { m_num_pauses = 0; }
1122  int GetPauseCount () const { return m_num_pauses; }
1127  int const * GetPauseTable () const { return m_pause_table; }
1128 
1130  void SetFirstPause (int offset) { if (GetPauseCount() == 0) RecordPause (offset);
1131  else m_pause_table[0] = offset; }
1133  int GetFirstPause () const { return (m_num_pauses > 0) ? m_pause_table[0] : 0; }
1134 
1138  int GetPosition () const { return m_position; }
1139 
1144  void SetWorldBounding (float const bbox[]);
1145 
1151  void SetWorldBoundingBySphere (float const pt[], float radius);
1152 
1154  float const * GetWorldBounding () const { return m_world_bounding; }
1155 
1156 
1162 #ifndef SWIG
1163  bool AddExternalReference (char const * ref, ID_Key context);
1164 #endif
1165 
1170  bool AddExternalReference (__wchar_t const * ref, ID_Key context);
1171 #ifdef _MSC_VER
1172  bool AddExternalReference (unsigned short const * ref, ID_Key context);
1173 #endif
1174 
1177  bool NextExternalReference ();
1181  wchar_t const * GetExternalReference () const {
1182  return (m_external_references != 0) ? (wchar_t const*)m_external_references->Reference() : 0;
1183  }
1187  void GetExternalReference (__wchar_t const *& exref) const {
1188  exref = (m_external_references != 0) ? (__wchar_t const *)m_external_references->Reference() : 0;
1189  }
1190 #ifdef _MSC_VER
1191  void GetExternalReference (unsigned short const *& exref) const {
1192  exref = (m_external_references != 0) ? (unsigned short const *)m_external_references->Reference() : 0;
1193  }
1194 #endif
1195 
1200  return (m_external_references != 0) ? m_external_references->m_context : -1;
1201  }
1202 
1206  virtual bool MatchPreviousExRef () const { return false; }
1207 
1211  void AddSegment (ID_Key key);
1215  ID_Key RemoveSegment ();
1219  ID_Key CurrentSegment () { return (m_active_segments != 0) ? m_active_segments->key() : -1; }
1221  void ResetQuantizationError() { m_quantization_error = 0; }
1225  void ReportQuantizationError(float error) { if (error > m_quantization_error) m_quantization_error = error; };
1229  void ReportQuantizationError(int bits_per_sample, float const *bounding, int num_dimensions = 3);
1233  float GetQuantizationError() const { return m_quantization_error; }
1234 
1239  if (m_geometry_open)
1240  return Error ("recursive geometry open");
1241  m_geometry_open = true;
1242  return TK_Normal;
1243  }
1248  if (!m_geometry_open)
1249  return Error ("no geometry open");
1250  m_geometry_open = false;
1251  return TK_Normal;
1252  }
1253 
1257  bool GeometryIsOpen () const { return m_geometry_open; }
1258 
1259  ID_Key RevisitKey () const { return m_revisit_working ? m_revisit_working->m_key : -1; }
1260  ID_Key RevisitOwner () const { return m_revisit_working ? m_revisit_working->m_owner : -1; }
1261 
1262  protected:
1263 
1264 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1265 
1266  virtual void FileDone() { ; };
1267 
1268  // normal data access for objects
1269  TK_Status read (char * b, int n) { return m_accumulator.read (b, n); }
1270  TK_Status write (char const * b, int n) { return m_accumulator.write (b, n); }
1271  TK_Status lookat (char & b) { return m_accumulator.lookat (b); }
1272 
1273  // used by segment handlers to make sure we know which segment should be "open"
1274  void add_segment (ID_Key key) { AddSegment (key); }
1275  ID_Key remove_segment () { return RemoveSegment(); }
1276 
1277  // used by renumber key, maybe by lookup functions (to be implemented...)
1279  void set_last_key (ID_Key key) { SetLastKey(key); }
1280  ID_Key context_key () const { return m_context_key; }
1281  void set_context_key (ID_Key key);
1283  ID_Key last_key () const {
1284  if(m_last_keys_used == 1)
1285  return m_last_keys[0];
1286  else
1287  return -1;
1288  }
1289 
1290  // keep track of visited segments (for things like include)
1291  void remember_item (ID_Key key);
1292  bool find_item (ID_Key key) const;
1293 
1295  BBaseOpcodeHandler * opcode_handler (int index) const { return m_objects[index]; }
1296 
1297  void adjust_written (int count) { m_objects_written += count; }
1298 
1299  int pass () const { return m_pass; }
1300 
1302  TK_Status revisit (unsigned char opcode, float priority, int lod=0);
1303 
1305  void record_instance (ID_Key key, int variant, BBaseOpcodeHandler const * object,
1306  int val1, int val2 = 0, int val3 = 0);
1307 
1309  bool find_instance (BBaseOpcodeHandler * object, int val1, int val2 = 0, int val3 = 0) const;
1310 
1312  int position () const { return m_position; }
1313  void set_position (int pos) { m_position = pos; }
1314  void mark_position () { set_position (GeneratedSoFar()); }
1315 
1320  virtual TK_Status tag (int variant);
1321 
1322  void increase_nesting (int amount = 1) { m_nesting_level += amount; }
1323  void decrease_nesting (int amount = 1) { m_nesting_level -= amount; }
1324 
1325  // utility
1326  TK_Status start_compression () { return m_accumulator.start_compression(); }
1327  TK_Status stop_compression () { return m_accumulator.stop_compression(true); }
1328  TK_Status start_decompression () { return m_accumulator.start_decompression(); }
1329  TK_Status stop_decompression (bool force = false) { return m_accumulator.stop_decompression(force); }
1330  virtual void empty_lists ();
1331 
1332 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
1333 
1334 };
1335 
1336 //TODO Documentation
1337 
1338 #ifndef BSTREAM_DISABLE_ASCII
1339 class PutTab
1340 {
1341 public:
1342  PutTab(BStreamFileToolkit* tk) : m_tk(tk)
1343  {
1344  int n_tabs = m_tk->GetTabs();
1345  m_tk->SetTabs(++n_tabs);
1346  }
1347 
1348  ~PutTab()
1349  {
1350  int n_tabs = m_tk->GetTabs();
1351  m_tk->SetTabs(--n_tabs);
1352  }
1353 
1354 private:
1355  BStreamFileToolkit* m_tk;
1356 };
1357 class Outdent
1358 {
1359 public:
1360  Outdent(BStreamFileToolkit* tk, int n_tabs = 1) : m_tk(tk)
1361  {
1362  int cur_tabs = m_tk->GetTabs();
1363  if(cur_tabs >= n_tabs)
1364  {
1365  m_tabs = n_tabs;
1366  m_tk->SetTabs(cur_tabs-n_tabs);
1367  }
1368  else
1369  {
1370  m_tabs = cur_tabs;
1371  m_tk->SetTabs(0);
1372  }
1373  }
1374 
1375  ~Outdent()
1376  {
1377  int cur_tabs = m_tk->GetTabs();
1378  m_tk->SetTabs(cur_tabs+m_tabs);
1379  }
1380 
1381 private:
1382  BStreamFileToolkit* m_tk;
1383  int m_tabs;
1384 };
1385 
1386 #endif //BSTREAM_DISABLE_ASCII
1387 
1388 #endif //BBINFILETK_TOOLKIT
Definition: BStreamFileToolkit.h:1339
Handles the TKE_Shell opcode.
Definition: BOpcodeShell.h:25
int GetDictionaryOffset() const
Definition: BStreamFileToolkit.h:1097
ID_Key GetExternalReferenceContext() const
Definition: BStreamFileToolkit.h:1199
unsigned int NextOpcodeSequence()
Definition: BStreamFileToolkit.h:1037
#define TK_DEFAULT_BUFFER_SIZE
default amount of the internal memory buffer used for file processing
Definition: BStream.h:195
virtual void NewFileContext(ID_Key key)
Definition: BStreamFileToolkit.h:599
void SetJpegQuality(int quality=75)
Definition: BStreamFileToolkit.h:969
int GetFlags() const
Definition: BStreamFileToolkit.h:923
void increase_nesting(BStreamFileToolkit &tk, int amount=1)
for internal use only
Definition: BOpcodeHandler.h:658
void * GetProgressValue() const
Definition: BStreamFileToolkit.h:1050
virtual TK_Status CloseGeometry()
Definition: BStreamFileToolkit.h:1247
void GetCurrentFile(__wchar_t const *&filename) const
Definition: BStreamFileToolkit.h:802
int PeekTagIndex() const
Definition: BStreamFileToolkit.h:751
int GetDictionaryFormat() const
Definition: BStreamFileToolkit.h:1082
void decrease_nesting(BStreamFileToolkit &tk, int amount=1)
for internal use only
Definition: BOpcodeHandler.h:660
void GetExternalReference(__wchar_t const *&exref) const
Definition: BStreamFileToolkit.h:1187
Handles the TKE_Comment opcode.
Definition: BOpcodeHandler.h:938
The BStreamFileToolkit class provides support for importing/exporting HOOPS Stream File information...
Definition: BStreamFileToolkit.h:367
int const * GetPauseTable() const
Definition: BStreamFileToolkit.h:1127
void SetReadVersion(int version)
Definition: BStreamFileToolkit.h:974
Handles the TKE_Tag opcode.
Definition: BOpcodeHandler.h:1607
Definition: BStreamFileToolkit.h:34
void SetOpcodeSequence(unsigned int seq=0)
Definition: BStreamFileToolkit.h:1039
void SetTargetVersion(int version)
Definition: BStreamFileToolkit.h:982
void SetReadFlags(int flags)
Definition: BStreamFileToolkit.h:937
void SetFlags(int flags)
Definition: BStreamFileToolkit.h:924
int NextTagIndex()
Definition: BStreamFileToolkit.h:746
int GetVersion() const
Definition: BStreamFileToolkit.h:973
#define TK_File_Format_Version
Definition: BStream.h:66
TK_Status GetOffset(ID_Key key, int variant, int &offset) const
Definition: BStreamFileToolkit.h:704
int GetNumVertexBits() const
Definition: BStreamFileToolkit.h:954
void SetFirstPause(int offset)
Definition: BStreamFileToolkit.h:1130
float const * GetWorldBounding() const
Definition: BStreamFileToolkit.h:1154
void SetBufferLimit(int limit)
Definition: BStreamFileToolkit.h:1057
void SetLoggingOptions(unsigned int options=~0)
Definition: BStreamFileToolkit.h:1016
void * m_file
Definition: BStreamFileToolkit.h:490
char const * GetLogFile() const
Definition: BStreamFileToolkit.h:1001
int GetDictionaryOptions() const
Definition: BStreamFileToolkit.h:1087
void SetLogging(bool setting)
Definition: BStreamFileToolkit.h:1010
int GetNumColorBits() const
Definition: BStreamFileToolkit.h:963
Definition: BOpcodeHandler.h:829
static bool SupportsAsciiMode()
Definition: BStreamFileToolkit.h:503
int Unused() const
Definition: BStreamFileToolkit.h:994
unsigned int GetLoggingOptions(unsigned int mask=~0) const
Definition: BStreamFileToolkit.h:1013
int GetNumParameterBits() const
Definition: BStreamFileToolkit.h:957
wchar_t const * GetExternalReference() const
Definition: BStreamFileToolkit.h:1181
bool GeometryIsOpen() const
Definition: BStreamFileToolkit.h:1257
ID_Key remove_segment(BStreamFileToolkit &tk)
for internal use only
Definition: BOpcodeHandler.h:645
Handles the TKE_Repeat_Object opcode.
Definition: BOpcodeHandler.h:1449
virtual bool MatchPreviousExRef() const
Definition: BStreamFileToolkit.h:1206
int GetDictionarySize() const
Definition: BStreamFileToolkit.h:1107
TK_Status GetOffset(ID_Key key, int variant, int &offset, int &length) const
Definition: BStreamFileToolkit.h:715
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:992
TK_Status AddVariant(ID_Key key, int variant, int value1, int value2=-1)
Definition: BStreamFileToolkit.h:685
Handles the TKE_Dictionary opcode.
Definition: BOpcodeHandler.h:1636
int GeneratedSoFar() const
Definition: BStreamFileToolkit.h:606
Definition: BStream.h:277
void SetNumColorBits(int numbits)
Definition: BStreamFileToolkit.h:964
void ReportQuantizationError(float error)
Definition: BStreamFileToolkit.h:1225
void add_segment(BStreamFileToolkit &tk, ID_Key key)
for internal use only
Definition: BOpcodeHandler.h:643
TK_Status AddBounds(ID_Key key, float const bounds[])
Definition: BStreamFileToolkit.h:694
int m_stage
The writing stage.
Definition: BOpcodeHandler.h:63
void ResetQuantizationError()
Definition: BStreamFileToolkit.h:1221
void SetDictionarySize(int size)
Definition: BStreamFileToolkit.h:1102
int CurrentBufferLength()
Definition: BStreamFileToolkit.h:575
bool HeaderCommentSeen() const
Definition: BStreamFileToolkit.h:1042
int GetNumIndexBits() const
Definition: BStreamFileToolkit.h:965
void set_last_key(BStreamFileToolkit &tk, ID_Key key)
sets the given key as "most recent" on the toolkit for the purposes of associating keys with indices ...
Definition: BOpcodeHandler.h:647
TK_Status AddIndexKeyPair(int index, ID_Key key)
Definition: BStreamFileToolkit.h:674
The BBaseOpcodeHandler abstract class is used as a base for derived classes which manage logical piec...
Definition: BOpcodeHandler.h:55
void SetNumNormalBits(int numbits)
Definition: BStreamFileToolkit.h:952
int GetFirstPause() const
Definition: BStreamFileToolkit.h:1133
TK_Status
Codes which can be either passed to various toolkit functions, or indicate the result of a toolkit fu...
Definition: BStream.h:276
void SetWriteFlags(int flags)
Definition: BStreamFileToolkit.h:928
void SetDictionaryOffset(int offset)
Definition: BStreamFileToolkit.h:1092
dictionary entries include bounding volume info
Definition: BStream.h:325
BBaseOpcodeHandler * GetOpcodeHandler(int which) const
Definition: BStreamFileToolkit.h:642
void SetNumParameterBits(int numbits)
Definition: BStreamFileToolkit.h:962
#define ID_Key
Definition: BStream.h:252
Utility class for managing HSF header information.
Definition: BOpcodeHandler.h:871
int GetReadFlags(int mask=~0) const
Definition: BStreamFileToolkit.h:943
int GetWriteFlags(int mask=~0) const
Definition: BStreamFileToolkit.h:934
virtual TK_Status OpenGeometry()
Definition: BStreamFileToolkit.h:1238
void SetProgressCallback(TK_Progress_Callback cb=0)
Definition: BStreamFileToolkit.h:1047
void SetNumVertexBits(int numbits)
Definition: BStreamFileToolkit.h:956
int GetJpegQuality() const
Definition: BStreamFileToolkit.h:971
int GetTargetVersion() const
Definition: BStreamFileToolkit.h:987
Handles the TKE_Start_Compression and TKE_Stop_Compression opcodes.
Definition: BOpcodeHandler.h:1034
unsigned int ObjectsSoFar() const
Definition: BStreamFileToolkit.h:612
int GetNumNormalBits() const
Definition: BStreamFileToolkit.h:946
TK_Progress_Callback GetProgressCallback() const
Definition: BStreamFileToolkit.h:1045
float GetQuantizationError() const
Definition: BStreamFileToolkit.h:1233
int GetBufferLimit() const
Definition: BStreamFileToolkit.h:1055
void SetDictionaryFormat(int format=3, int options=TK_Dictionary_Bounding_Volumes)
Definition: BStreamFileToolkit.h:1076
void SetNumIndexBits(int numbits)
Definition: BStreamFileToolkit.h:966
TK_Status GetBounds(ID_Key key, float bounds[]) const
Definition: BStreamFileToolkit.h:739
virtual void DeactivateContext(ID_Key key)
Definition: BStreamFileToolkit.h:590
Definition: BStreamFileToolkit.h:1357
void SetProgressValue(void *value)
Definition: BStreamFileToolkit.h:1052
ID_Key CurrentSegment()
Definition: BStreamFileToolkit.h:1219
void ClearPauses()
Definition: BStreamFileToolkit.h:1117
void adjust_written(BStreamFileToolkit &tk, int count)
for internal use only
Definition: BOpcodeHandler.h:656
ID_Key last_key(BStreamFileToolkit &tk) const
obsolete
Definition: BOpcodeHandler.h:649
bool GetLogging() const
Definition: BStreamFileToolkit.h:1006
Provides HOOPS/3dGS-specific handling of the TKE_Reference opcode.
Definition: HOpcodeHandler.h:140
int GetPauseCount() const
Definition: BStreamFileToolkit.h:1122
int GetPosition() const
Definition: BStreamFileToolkit.h:1138
virtual void ActivateContext(ID_Key key)
Definition: BStreamFileToolkit.h:583