home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Game_Audio / audio_sdk / src / AudioScript / TokenFile.h < prev   
Encoding:
C/C++ Source or Header  |  2002-05-27  |  3.6 KB  |  127 lines

  1. /***********************************************************\
  2. Copyright (C) James Boer, 2002. 
  3. All rights reserved worldwide.
  4.  
  5. This software is provided "as is" without express or implied
  6. warranties. You may freely copy and compile this source into
  7. applications you distribute provided that the copyright text
  8. below is included in the resulting source code, for example:
  9. "Portions Copyright (C) James Boer, 2002"
  10. \***********************************************************/
  11.  
  12. /*
  13.  
  14. Some general notes and limitations to be aware of...
  15.  
  16. In order to minimize file size, and thus loading times, I've made certain
  17. optimizations to the data storage.  The biggest and most significant optimization
  18. is the use of a string table at the beginning of the file to store keywords, 
  19. operators, and variables.  These are stored in string format in the tokens, and
  20. since they are often repeating, it makes sense to use a lookup table for huge
  21. space savings.  Each token type is stored in it's own table, and the tokens are
  22. stored as indices into those tables instead of storing the actual strings.  A
  23. further optimization is made if there are fewer than 256 entries in a particular
  24. table - the index value is stored as a byte instead of a word.
  25.  
  26. Note that since all the index values are stored at a maximum value of a word, the
  27. maximum allowed number of unique keywords, operators, or variables in a single
  28. compiled file is 65535.  If this ends up being a limitation, the code can easily
  29. be adjusted to switch to storing the value in a dword instead of word.  Or, better
  30. yet, make it a three-way switch to determine the size of the storage chunk of
  31. the indices.
  32.  
  33. Strings are also stored as a length, followed by a sequence of bytes.  The index is
  34. a word, so strings are also limited to 65K characters.
  35.  
  36. */
  37.  
  38.  
  39. #pragma once
  40.  
  41. #ifndef __TOKEN_FILE_H
  42. #define __TOKEN_FILE_H
  43.  
  44. #include "Token.h"
  45.  
  46. #define TF_CASE_SENSITIVE    0x0001
  47.  
  48. namespace Audio
  49. {
  50.  
  51. typedef std::queue<unsigned char> ByteQueue;
  52.  
  53. class TokenFile 
  54. {
  55. public:
  56.     TokenFile()                {  Clear();  }
  57.     virtual ~TokenFile()    {  Term();  }
  58.  
  59.     void Clear();
  60.     void Term();
  61.  
  62.     bool Write(std::ostream &Output, TokenList* pTokenList, DWORD dwFlags = 0);
  63.     bool Write(std::string sFilename, TokenList* pTokenList, DWORD dwFlags = 0);
  64.  
  65.     bool Read(std::istream &Input, TokenList* pTokenList);
  66.     bool Read(std::string sFilename, TokenList* pTokenList);
  67.  
  68. // Internal functions
  69. private:
  70.  
  71.     // Output functions
  72.  
  73.     // Write the header containing the file id and version information
  74.     void WriteHeader();
  75.     // Iterate through the list and pre-generate string-based tokens (except strings)
  76.     void GenerateTables();
  77.     // Output the string-based tables
  78.     void WriteTables();
  79.     // Output the token stream
  80.     void WriteTokens();
  81.  
  82.     // Basic types
  83.     void WriteString(std::string sData);
  84.     void WriteDword(DWORD dwData);
  85.     void WriteWord(WORD wData);
  86.     void WriteByte(BYTE byteData);
  87.     void WriteDouble(double dData);
  88.     void WriteVector(TokenVector3D &vVector);
  89.     void WriteGuid(GUID &guidData);
  90.  
  91.     // Input functions
  92.     bool ReadHeader();
  93.     bool ReadTables();
  94.     bool ReadTokens();
  95.  
  96.     // Basic types
  97.     std::string ReadString();
  98.     DWORD ReadDword();
  99.     WORD ReadWord();
  100.     BYTE ReadByte();
  101.     double ReadDouble();
  102.     TokenVector3D ReadVector();
  103.     GUID ReadGuid();
  104.  
  105.  
  106. // Member data
  107. private:
  108.     
  109.     TokenList*        m_pTokenList;
  110.  
  111.     StringVector    m_vecKeyword;
  112.     StringVector    m_vecOperator;
  113.     StringVector    m_vecVariable;
  114.  
  115.     ByteQueue        m_queBuffer;
  116.  
  117.     bool            m_bCaseSensitive;
  118.  
  119.     bool            m_bWordKeywordIndex;
  120.     bool            m_bWordOperatorIndex;
  121.     bool            m_bWordVariableIndex;
  122.  
  123. };
  124.  
  125. }; // namespace Audio
  126.  
  127. #endif // __TOKEN_FILE_H