RigsofRods
Soft-body Physics Simulation
GenericFileFormat.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2022 Petr Ohlidal
4 
5  For more information, see http://www.rigsofrods.org/
6 
7  Rigs of Rods is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License version 3, as
9  published by the Free Software Foundation.
10 
11  Rigs of Rods is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
33 
34 #pragma once
35 
36 #include "RefCountingObject.h"
37 #include "RefCountingObjectPtr.h"
38 #include "BitFlags.h"
39 #include "Application.h"
40 
41 #include <vector>
42 #include <string>
43 
44 #include <Ogre.h>
45 
46 namespace RoR {
47 
48 enum class TokenType
49 {
50  NONE,
51  LINEBREAK, // Input: LF (CR is ignored); Output: platform-specific.
52  COMMENT, // Line starting with ; (skipping whitespace). Data: offset in string pool.
53  STRING, // Quoted string. Data: offset in string pool.
54  NUMBER, // Float.
55  BOOL, // Lowercase 'true'/'false'. Data: 1.0 for true, 0.0 for false.
56  KEYWORD, // Unquoted string at start of line (skipping whitespace). Data: offset in string pool.
57 };
58 
59 struct Token
60 {
62  float data;
63 };
64 
65 struct GenericDocument: public RefCountingObject<GenericDocument>
66 {
75 
76  virtual ~GenericDocument() {};
77 
78  std::vector<char> string_pool; // Data of COMMENT/KEYWORD/STRING tokens; NUL-terminated strings.
79  std::vector<Token> tokens;
80 
81  virtual void loadFromDataStream(Ogre::DataStreamPtr datastream, BitMask_t options = 0);
82  virtual void saveToDataStream(Ogre::DataStreamPtr datastream);
83 
84  virtual bool loadFromResource(std::string resource_name, std::string resource_group_name, BitMask_t options = 0);
85  virtual bool saveToResource(std::string resource_name, std::string resource_group_name);
86 };
87 
89 
90 struct GenericDocContext: public RefCountingObject<GenericDocContext>
91 {
93  {
94  ROR_ASSERT(doc != nullptr);
95  if (doc == nullptr && AngelScript::asGetActiveContext() != nullptr)
96  {
97  AngelScript::asGetActiveContext()->SetException("Cannot create GenericDocContextClass from null GenericDocument!");
98  }
99  }
100  virtual ~GenericDocContext() {};
101 
103  uint32_t token_pos = 0;
104 
105  // PLEASE maintain the same order as in 'bindings/GenericFileFormatAngelscript.cpp' and 'doc/*/GenericDocContextClass.h'
106 
107  bool moveNext() { token_pos++; return endOfFile(); }
108  uint32_t getPos() const { return token_pos; }
109  bool seekNextLine();
110  int countLineArgs();
111  bool endOfFile(int offset = 0) const { return token_pos + offset >= doc->tokens.size(); }
112  TokenType tokenType(int offset = 0) const { return !endOfFile(offset) ? doc->tokens[token_pos + offset].type : TokenType::NONE; }
113 
114  std::string getTokString(int offset = 0) const { ROR_ASSERT(isTokString(offset)); return getStringData(offset); }
115  float getTokFloat(int offset = 0) const { ROR_ASSERT(isTokFloat(offset)); return getFloatData(offset); }
116  bool getTokBool(int offset = 0) const { ROR_ASSERT(isTokBool(offset)); return getFloatData(offset) == 1.f; }
117  std::string getTokKeyword(int offset = 0) const { ROR_ASSERT(isTokKeyword(offset)); return getStringData(offset); }
118  std::string getTokComment(int offset = 0) const { ROR_ASSERT(isTokComment(offset)); return getStringData(offset); }
119 
120  bool isTokString(int offset = 0) const { return tokenType(offset) == TokenType::STRING; }
121  bool isTokFloat(int offset = 0) const { return tokenType(offset) == TokenType::NUMBER; }
122  bool isTokBool(int offset = 0) const { return tokenType(offset) == TokenType::BOOL; }
123  bool isTokKeyword(int offset = 0) const { return tokenType(offset) == TokenType::KEYWORD; }
124  bool isTokComment(int offset = 0) const { return tokenType(offset) == TokenType::COMMENT; }
125  bool isTokLineBreak(int offset = 0) const { return tokenType(offset) == TokenType::LINEBREAK; }
126 
127  // Editing functions:
128 
129  bool insertToken(int offset = 0);
130  bool eraseToken(int offset = 0);
131 
132  bool setTokString(int offset, const std::string& str) { return setStringData(offset, TokenType::STRING, str); }
133  bool setTokFloat(int offset, float val) { return setFloatData(offset, TokenType::NUMBER, val); }
134  bool setTokBool(int offset, bool val) { return setFloatData(offset, TokenType::BOOL, val); }
135  bool setTokKeyword(int offset, const std::string& str) { return setStringData(offset, TokenType::KEYWORD, str); }
136  bool setTokComment(int offset, const std::string& str) { return setStringData(offset, TokenType::COMMENT, str); }
137  bool setTokLineBreak(int offset) { return setFloatData(offset, TokenType::LINEBREAK, 0.f); }
138 
139  // Not exported to script:
140 
141  const char* getStringData(int offset = 0) const { return !endOfFile(offset) ? (doc->string_pool.data() + (uint32_t)doc->tokens[token_pos + offset].data) : ""; }
142  float getFloatData(int offset = 0) const { return !endOfFile(offset) ? doc->tokens[token_pos + offset].data : 0.f; }
143  bool setStringData(int offset, TokenType type, const std::string& data);
144  bool setFloatData(int offset, TokenType type, float data);
145 };
146 
148 
149 } // namespace RoR
ROR_ASSERT
#define ROR_ASSERT(_EXPR)
Definition: Application.h:40
RoR::GenericDocContext::setFloatData
bool setFloatData(int offset, TokenType type, float data)
Definition: GenericFileFormat.cpp:1178
RoR::GenericDocContext::insertToken
bool insertToken(int offset=0)
Inserts TokenType::NONE;.
Definition: GenericFileFormat.cpp:1143
RoR::GenericDocContext::setTokKeyword
bool setTokKeyword(int offset, const std::string &str)
Definition: GenericFileFormat.h:135
RoR::GenericDocContext::isTokKeyword
bool isTokKeyword(int offset=0) const
Definition: GenericFileFormat.h:123
RoR::GenericDocumentPtr
RefCountingObjectPtr< GenericDocument > GenericDocumentPtr
Definition: GenericFileFormat.h:88
RoR::GenericDocument::OPTION_ALLOW_SEPARATOR_COLON
static const BitMask_t OPTION_ALLOW_SEPARATOR_COLON
Allow ':' as separator between tokens.
Definition: GenericFileFormat.h:70
RoR::Token::data
float data
Definition: GenericFileFormat.h:62
RoR::GenericDocContext::getTokBool
bool getTokBool(int offset=0) const
Definition: GenericFileFormat.h:116
RoR::GenericDocument::OPTION_ALLOW_SLASH_COMMENTS
static const BitMask_t OPTION_ALLOW_SLASH_COMMENTS
Allow comments starting with //.
Definition: GenericFileFormat.h:68
RoR::GenericDocContext::isTokLineBreak
bool isTokLineBreak(int offset=0) const
Definition: GenericFileFormat.h:125
RoR::GenericDocContext::getTokString
std::string getTokString(int offset=0) const
Definition: GenericFileFormat.h:114
RoR::GenericDocContext::setTokComment
bool setTokComment(int offset, const std::string &str)
Definition: GenericFileFormat.h:136
RoR::TokenType::NUMBER
@ NUMBER
RoR::GenericDocContext::tokenType
TokenType tokenType(int offset=0) const
Definition: GenericFileFormat.h:112
RoR::GenericDocContext::isTokFloat
bool isTokFloat(int offset=0) const
Definition: GenericFileFormat.h:121
RoR::GenericDocContext::seekNextLine
bool seekNextLine()
Definition: GenericFileFormat.cpp:1115
RoR::GenericDocument::loadFromResource
virtual bool loadFromResource(std::string resource_name, std::string resource_group_name, BitMask_t options=0)
Definition: GenericFileFormat.cpp:1083
RoR::GenericDocContext::setTokString
bool setTokString(int offset, const std::string &str)
Definition: GenericFileFormat.h:132
RoR::GenericDocContext::getTokFloat
float getTokFloat(int offset=0) const
Definition: GenericFileFormat.h:115
BITMASK
#define BITMASK(OFFSET)
Definition: BitFlags.h:10
RefCountingObjectPtr< GenericDocument >
RoR::GenericDocument::saveToResource
virtual bool saveToResource(std::string resource_name, std::string resource_group_name)
Definition: GenericFileFormat.cpp:1099
RoR::GenericDocument::saveToDataStream
virtual void saveToDataStream(Ogre::DataStreamPtr datastream)
Definition: GenericFileFormat.cpp:1031
RoR::GenericDocContext::isTokString
bool isTokString(int offset=0) const
Definition: GenericFileFormat.h:120
RoR::GenericDocContext::countLineArgs
int countLineArgs()
Definition: GenericFileFormat.cpp:1132
RoR::TokenType::KEYWORD
@ KEYWORD
RoR::Token::type
TokenType type
Definition: GenericFileFormat.h:61
BitFlags.h
Bit operations.
RoR::TokenType::COMMENT
@ COMMENT
RoR::GenericDocContext::getTokComment
std::string getTokComment(int offset=0) const
Definition: GenericFileFormat.h:118
RoR::GenericDocument::tokens
std::vector< Token > tokens
Definition: GenericFileFormat.h:79
RoR::GenericDocContext::doc
GenericDocumentPtr doc
Definition: GenericFileFormat.h:100
RefCountingObject.h
RoR::GenericDocContext::moveNext
bool moveNext()
Definition: GenericFileFormat.h:107
RoR::GenericDocument::OPTION_ALLOW_BRACED_KEYWORDS
static const BitMask_t OPTION_ALLOW_BRACED_KEYWORDS
Allow INI-like '[keyword]' tokens.
Definition: GenericFileFormat.h:72
Application.h
Central state/object manager and communications hub.
RoR::Token
Definition: GenericFileFormat.h:59
RoR::GenericDocument::OPTION_FIRST_LINE_IS_TITLE
static const BitMask_t OPTION_FIRST_LINE_IS_TITLE
First non-empty & non-comment line is a naked string with spaces.
Definition: GenericFileFormat.h:69
RoR::GenericDocument::string_pool
std::vector< char > string_pool
Definition: GenericFileFormat.h:76
RoR::TokenType::NONE
@ NONE
RoR::GenericDocContext::setStringData
bool setStringData(int offset, TokenType type, const std::string &data)
Definition: GenericFileFormat.cpp:1164
RoR::GenericDocument::OPTION_ALLOW_SEPARATOR_EQUALS
static const BitMask_t OPTION_ALLOW_SEPARATOR_EQUALS
Allow '=' as separator between tokens.
Definition: GenericFileFormat.h:73
RoR::GenericDocContext::getStringData
const char * getStringData(int offset=0) const
Definition: GenericFileFormat.h:141
RoR::GenericDocContext::endOfFile
bool endOfFile(int offset=0) const
Definition: GenericFileFormat.h:111
RoR::GenericDocContext::isTokBool
bool isTokBool(int offset=0) const
Definition: GenericFileFormat.h:122
RoR::GenericDocContext::getFloatData
float getFloatData(int offset=0) const
Definition: GenericFileFormat.h:142
RoR::GenericDocContext::eraseToken
bool eraseToken(int offset=0)
Definition: GenericFileFormat.cpp:1152
RoR::TokenType
TokenType
Definition: GenericFileFormat.h:48
RoR::GenericDocument
Definition: GenericFileFormat.h:65
RefCountingObjectPtr.h
RoR::TokenType::STRING
@ STRING
RoR::GenericDocContext::getTokKeyword
std::string getTokKeyword(int offset=0) const
Definition: GenericFileFormat.h:117
BitMask_t
uint32_t BitMask_t
Definition: BitFlags.h:7
RoR::GenericDocContext
Definition: GenericFileFormat.h:90
RoR::GenericDocContext::GenericDocContext
GenericDocContext(GenericDocumentPtr d)
Definition: GenericFileFormat.h:92
RoR::GenericDocument::~GenericDocument
virtual ~GenericDocument()
Definition: GenericFileFormat.h:76
RoR::GenericDocument::loadFromDataStream
virtual void loadFromDataStream(Ogre::DataStreamPtr datastream, BitMask_t options=0)
Definition: GenericFileFormat.cpp:1000
RoR::GenericDocContext::setTokBool
bool setTokBool(int offset, bool val)
Definition: GenericFileFormat.h:134
RoR::GenericDocContext::~GenericDocContext
virtual ~GenericDocContext()
Definition: GenericFileFormat.h:100
RefCountingObject
Self reference-counting objects, as requred by AngelScript garbage collector.
Definition: RefCountingObject.h:26
RoR::GenericDocContext::setTokFloat
bool setTokFloat(int offset, float val)
Definition: GenericFileFormat.h:133
RoR::GenericDocContext::isTokComment
bool isTokComment(int offset=0) const
Definition: GenericFileFormat.h:124
RoR::GenericDocContext::token_pos
uint32_t token_pos
Definition: GenericFileFormat.h:103
RoR::TokenType::LINEBREAK
@ LINEBREAK
RoR::GenericDocument::OPTION_ALLOW_NAKED_STRINGS
static const BitMask_t OPTION_ALLOW_NAKED_STRINGS
Allow strings without quotes, for backwards compatibility.
Definition: GenericFileFormat.h:67
RoR
Definition: AppContext.h:36
RoR::GenericDocument::OPTION_ALLOW_HASH_COMMENTS
static const BitMask_t OPTION_ALLOW_HASH_COMMENTS
Allow comments starting with #.
Definition: GenericFileFormat.h:74
RoR::GenericDocument::OPTION_PARENTHESES_CAPTURE_SPACES
static const BitMask_t OPTION_PARENTHESES_CAPTURE_SPACES
If non-empty NAKED string encounters '(', following spaces will be captured until matching ')' is fou...
Definition: GenericFileFormat.h:71
RoR::GenericDocContext::getPos
uint32_t getPos() const
Definition: GenericFileFormat.h:108
RoR::GenericDocContext::setTokLineBreak
bool setTokLineBreak(int offset)
Definition: GenericFileFormat.h:137
RoR::TokenType::BOOL
@ BOOL
RoR::GenericDocContextPtr
RefCountingObjectPtr< GenericDocContext > GenericDocContextPtr
Definition: GenericFileFormat.h:147