GameKit  0.0.1a
C++ gamedev tools
XMLFile.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: XMLFile.cpp
5  *
6  * Description:
7  *
8  * Created: 17/01/2018 19:22:02
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include "gk/core/XMLFile.hpp"
15 #include "gk/core/Exception.hpp"
16 
17 namespace gk {
18 
19 XMLFile::XMLFile(const std::string &filename) {
20  load(filename);
21 }
22 
23 void XMLFile::load(const std::string &filename) {
24  int code = m_xml.LoadFile(filename.c_str());
25 
26  if(code != 0) {
27  std::string errorString;
28 
29  switch(code) {
30  case tinyxml2::XML_ERROR_FILE_NOT_FOUND:
31  errorString = "File not found.";
32  break;
33  case tinyxml2::XML_ERROR_FILE_COULD_NOT_BE_OPENED:
34  errorString = "File couldn't be opened.";
35  break;
36  // case tinyxml2::XML_ERROR_FILE_READ_ERROR:
37  // errorString = "File read error.";
38  // break;
39  case tinyxml2::XML_ERROR_PARSING_ELEMENT:
40  errorString = "Error while parsing element.";
41  break;
42  case tinyxml2::XML_ERROR_PARSING_ATTRIBUTE:
43  errorString = "Error while parsing attribute.";
44  break;
45  case tinyxml2::XML_ERROR_PARSING_TEXT:
46  errorString = "Error while parsing text.";
47  break;
48  case tinyxml2::XML_ERROR_PARSING_CDATA:
49  errorString = "Error while parsing cdata.";
50  break;
51  case tinyxml2::XML_ERROR_PARSING_COMMENT:
52  errorString = "Error while parsing comment.";
53  break;
54  case tinyxml2::XML_ERROR_PARSING_DECLARATION:
55  errorString = "Error while parsing declaration.";
56  break;
57  case tinyxml2::XML_ERROR_PARSING_UNKNOWN:
58  errorString = "Parsing error: Unknown object.";
59  break;
60  case tinyxml2::XML_ERROR_EMPTY_DOCUMENT:
61  errorString = "Empty document.";
62  break;
63  case tinyxml2::XML_ERROR_MISMATCHED_ELEMENT:
64  errorString = "Element mismatched.";
65  break;
66  case tinyxml2::XML_ERROR_PARSING:
67  errorString = "Parsing error.";
68  break;
69  default:
70  errorString = "Unknown error.";
71  break;
72  }
73 
74  throw EXCEPTION("Failed to load", filename, "\nError", code, ":", errorString);
75  }
76 }
77 
78 } // namespace gk
79 
#define EXCEPTION(args...)
Definition: Exception.hpp:22
XMLFile()=default
tinyxml2::XMLDocument m_xml
Definition: XMLFile.hpp:33
void load(const std::string &filename)
Definition: XMLFile.cpp:23