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-2015, 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 "level/level_category.h"
28 
29 #include "common/make_unique.h"
30 
31 #include "level/robotmain.h"
32 
36 
37 #include <string>
38 #include <vector>
39 #include <memory>
40 
42 {
43 public:
45  CLevelParser();
47  CLevelParser(std::string filename);
49 
50  CLevelParser(LevelCategory category, int chapter, int rank);
51  CLevelParser(std::string category, int chapter, int rank);
53 
55 
56  static std::string BuildCategoryPath(LevelCategory category);
57  static std::string BuildCategoryPath(std::string category);
59 
61  static std::string BuildScenePath(LevelCategory category, int chapter, int rank, bool sceneFile = true);
62  static std::string BuildScenePath(std::string category, int chapter, int rank, bool sceneFile = true);
64 
66  bool Exists();
68  void Load();
70  void Save();
71 
73  void SetLevelPaths(LevelCategory category, int chapter = 0, int rank = 0);
75  std::string InjectLevelPaths(const std::string& path, const std::string& defaultDir = "");
76 
78  const std::string& GetFilename();
79 
81  inline const std::vector<CLevelParserLineUPtr>& GetLines()
82  {
83  return m_lines;
84  }
85 
87  void AddLine(CLevelParserLineUPtr line);
88 
90  CLevelParserLine* Get(const std::string& command);
91 
93  int CountLines(const std::string& command);
94 
95 private:
96  std::string m_filename;
97  std::vector<CLevelParserLineUPtr> m_lines;
98 
99  std::string m_pathCat;
100  std::string m_pathChap;
101  std::string m_pathLvl;
102 };
103 
104 inline std::string InjectLevelPathsForCurrentLevel(const std::string& path, const std::string& defaultDir = "")
105 {
106  CRobotMain* main = CRobotMain::GetInstancePointer();
107  auto levelParser = MakeUnique<CLevelParser>();
108  levelParser->SetLevelPaths(main->GetLevelCategory(), main->GetLevelChap(), main->GetLevelRank());
109  return levelParser->InjectLevelPaths(path, defaultDir);
110 }
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:151
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:370
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:81
Definition: parser.h:41
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.
void AddLine(CLevelParserLineUPtr line)
Insert new line to file.
Definition: parser.cpp:354