SimpleConfigParser.cpp
1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "json/document.h"
#include "json/filestream.h"
#include "json/stringbuffer.h"
#include "json/writer.h"
#include "SimpleConfigParser.h"
// SimpleConfigParser
SimpleConfigParser *SimpleConfigParser::s_sharedSimpleConfigParserInstance = NULL;
SimpleConfigParser *SimpleConfigParser::getInstance(void)
{
if (!s_sharedSimpleConfigParserInstance)
{
s_sharedSimpleConfigParserInstance = new SimpleConfigParser();
s_sharedSimpleConfigParserInstance->readConfig();
}
return s_sharedSimpleConfigParserInstance;
}
void SimpleConfigParser::purge()
{
CC_SAFE_DELETE(s_sharedSimpleConfigParserInstance);
}
void SimpleConfigParser::readConfig(const string &filepath)
{
string fullPathFile = filepath;
// read config file
if (fullPathFile.empty())
{
fullPathFile = FileUtils::getInstance()->fullPathForFilename(CONFIG_FILE);
}
string fileContent = FileUtils::getInstance()->getStringFromFile(fullPathFile);
if(fileContent.empty())
return;
if (_docRootjson.Parse<0>(fileContent.c_str()).HasParseError()) {
cocos2d::log("read json file %s failed because of %d", fullPathFile.c_str(), _docRootjson.GetParseError());
return;
}
if (_docRootjson.HasMember("init_cfg"))
{
if(_docRootjson["init_cfg"].IsObject())
{
const rapidjson::Value& objectInitView = _docRootjson["init_cfg"];
if (objectInitView.HasMember("isLandscape") && objectInitView["isLandscape"].IsBool())
{
_isLandscape = objectInitView["isLandscape"].GetBool();
}
}
}
}
SimpleConfigParser::SimpleConfigParser(void) :
_isLandscape(true)
{
}
rapidjson::Document& SimpleConfigParser::getConfigJsonRoot()
{
return _docRootjson;
}
bool SimpleConfigParser::isLanscape()
{
return _isLandscape;
}