Colobot
parser.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 #include "common/make_unique.h"
28 
29 #include "level/level_category.h"
30 #include "level/robotmain.h"
31 
35 
36 #include <string>
37 #include <vector>
38 #include <memory>
39 
41 {
42 public:
44  CLevelParser();
46  CLevelParser(std::string filename);
48 
49  CLevelParser(LevelCategory category, int chapter, int rank);
50  CLevelParser(std::string category, int chapter, int rank);
52 
54 
55  static std::string BuildCategoryPath(LevelCategory category);
56  static std::string BuildCategoryPath(std::string category);
58 
60  static std::string BuildScenePath(LevelCategory category, int chapter, int rank, bool sceneFile = true);
61  static std::string BuildScenePath(std::string category, int chapter, int rank, bool sceneFile = true);
63 
65  bool Exists();
67  void Load();
69  void Save();
70 
72  void SetLevelPaths(LevelCategory category, int chapter = 0, int rank = 0);
74  std::string InjectLevelPaths(const std::string& path, const std::string& defaultDir = "");
75 
77  const std::string& GetFilename();
78 
80  inline const std::vector<CLevelParserLineUPtr>& GetLines()
81  {
82  return m_lines;
83  }
84 
86  void AddLine(CLevelParserLineUPtr line);
87 
89  CLevelParserLine* Get(const std::string& command);
90 
92  CLevelParserLine* GetIfDefined(const std::string &command);
93 
95  int CountLines(const std::string& command);
96 
97 private:
98  std::string m_filename;
99  std::vector<CLevelParserLineUPtr> m_lines;
100 
101  std::string m_pathCat;
102  std::string m_pathChap;
103  std::string m_pathLvl;
104 };
105 
106 inline std::string InjectLevelPathsForCurrentLevel(const std::string& path, const std::string& defaultDir = "")
107 {
108  CRobotMain* main = CRobotMain::GetInstancePointer();
109  auto levelParser = MakeUnique<CLevelParser>();
110  levelParser->SetLevelPaths(main->GetLevelCategory(), main->GetLevelChap(), main->GetLevelRank());
111  return levelParser->InjectLevelPaths(path, defaultDir);
112 }
Value of command argument in level file.
Exceptions that could be thrown in level parser.
CLevelParser()
Create an empty level file.
Definition: parser.cpp:45
CRobotMain - main class of Colobot game engine.
const std::string & GetFilename()
Get filename.
Definition: parser.cpp:349
Definition: parserline.h:37
void Save()
Save file.
Definition: parser.cpp:302
Definition: robotmain.h:159
CLevelParserLine * Get(const std::string &command)
Find first line with given command.
Definition: parser.cpp:360
int CountLines(const std::string &command)
Count lines with given command.
Definition: parser.cpp:378
bool Exists()
Check if level file exists.
Definition: parser.cpp:151
void Load()
Load file.
Definition: parser.cpp:156
std::string InjectLevelPaths(const std::string &path, const std::string &defaultDir="")
Inject something% paths.
Definition: parser.cpp:324
static std::string BuildScenePath(LevelCategory category, int chapter, int rank, bool sceneFile=true)
Build level filename.
Definition: parser.cpp:146
const std::vector< CLevelParserLineUPtr > & GetLines()
Get all lines from file.
Definition: parser.h:80
Definition: parser.h:40
CLevelParserLine * GetIfDefined(const std::string &command)
Find first line with given command, null if doesn&#39;t exist.
Definition: parser.cpp:368
static std::string BuildCategoryPath(LevelCategory category)
Build category path.
Definition: parser.cpp:85
void SetLevelPaths(LevelCategory category, int chapter=0, int rank=0)
Configure level paths for the given level.
Definition: parser.cpp:317
Class for one line from level file.
int main(int argc, char *argv[])
Entry point to the program.
Definition: main.cpp:93
void AddLine(CLevelParserLineUPtr line)
Insert new line to file.
Definition: parser.cpp:354