Advertisement
snake5

EntityScript header file [rediscovered]

Oct 25th, 2013
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. #pragma once
  2.  
  3. /*
  4.  
  5. *   ENTITY   SCRIPT   DOCUMENTATION
  6.        
  7.     1. SYNTAX
  8.         Variable names can contain these characters: 0-9, a-z, A-Z, _(underscore), they start and end with ""'s.
  9.         Variable names cannot have spaces!!!
  10.         Variable values have strings assigned, those are separated from other things by ""'s.
  11.         End of variable definition is marked with ";".
  12.         Strings are one-lined.
  13.         Operator between labels: "." (like Object1.XPosition).
  14.         Assignment operator: "=".
  15.         Blocks have the same naming as variables, but they don't need the ""'s.
  16.         Block beginnings and endings: { and }, respectively.
  17.  
  18.     2. SYNTAX EXAMPLE
  19.         Player
  20.         {
  21.             Pistol
  22.             {
  23.                 "id" = "1";
  24.                 "bullet_count" = "32";
  25.             }
  26.             SMG
  27.             {
  28.                 "id" = "2";
  29.                 "bullet_count" = "20";
  30.             }
  31.             "action" = "0";
  32.             "last_sentence" = "Awesome!";
  33.         }
  34.         "ambient_color" = "10897245";
  35.  
  36. */
  37. #define ESC_ERRVAL "\n";
  38.  
  39. #include <vector>
  40. #include <map>
  41. #include <string>
  42.  
  43. typedef std::map< std::string, std::string > CStrParamMap;
  44. struct SParamBlock;
  45. typedef std::map< std::string, SParamBlock* > CParamBlockMap;
  46.  
  47. void EntityScriptError( const char* strError );
  48.  
  49. struct SParamBlock
  50. {
  51.     CStrParamMap m_params;
  52.     CParamBlockMap m_blocks;
  53.     SParamBlock* m_pParent;
  54.  
  55.     ~SParamBlock();
  56.  
  57.     std::string GetValue( const char* strKey );
  58.     SParamBlock* GetBlock( const char* strName );
  59.  
  60.     std::string GetBlockName( SParamBlock* pBlock );
  61.     std::string GetBlockFullName();
  62.  
  63.     SParamBlock* AddBlock( const char* strName );
  64.     void RemoveBlock( const char* strName );
  65.  
  66.     void SetValue( const char* strKey, const char* strValue );
  67.     void RemoveValue( const char* strValue );
  68. };
  69.  
  70.  
  71. class CEntityScript
  72. {
  73. public:
  74.     CEntityScript( char* strData, unsigned int uLength );
  75.  
  76.     std::string Read( const char* strVariable );
  77.  
  78. protected:
  79.     SParamBlock m_mainBlock;
  80.  
  81.     std::vector< std::string > m_strTemp;
  82.  
  83. };
  84.  
  85.  
  86. inline
  87. SParamBlock::~SParamBlock()
  88. {
  89.     CParamBlockMap::iterator it = m_blocks.begin(), itend = m_blocks.end();
  90.     while( it != itend )
  91.     {
  92.         delete it->second;
  93.         it++;
  94.     }
  95. }
  96.  
  97. inline
  98. std::string SParamBlock::GetValue(const char *strKey)
  99. {
  100.     if( m_params.find( strKey ) == m_params.end() )
  101.         return ESC_ERRVAL;
  102.  
  103.     return m_params[ strKey ];
  104. }
  105.  
  106. inline
  107. SParamBlock* SParamBlock::GetBlock(const char *strName)
  108. {
  109.     if( m_blocks.find( strName ) == m_blocks.end() )
  110.         return NULL;
  111.  
  112.     return m_blocks[ strName ];
  113. }
  114.  
  115. inline
  116. std::string SParamBlock::GetBlockName(SParamBlock *pBlock)
  117. {
  118.     CParamBlockMap::iterator it = m_blocks.begin(), itend = m_blocks.end();
  119.     while( it != itend )
  120.     {
  121.         if( it->second == pBlock )
  122.             return it->first;
  123.         it++;
  124.     }
  125.     return ESC_ERRVAL;
  126. }
  127.  
  128. inline
  129. void SParamBlock::SetValue(const char *strKey, const char *strValue)
  130. {
  131.     m_params[ strKey ] = strValue;
  132. }
  133.  
  134. inline
  135. void SParamBlock::RemoveValue(const char *strValue)
  136. {
  137.     CStrParamMap::iterator it = m_params.find( strValue );
  138.     if( it == m_params.end() )
  139.     {
  140.         std::string sError = "Value \"";
  141.         sError += GetBlockFullName();
  142.         sError += ".";
  143.         sError += strValue;
  144.         sError += " \" not found! (";
  145.         EntityScriptError( sError.c_str() );
  146.        
  147.         return;
  148.     }
  149.  
  150.     m_params.erase( it );
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement