source: branches/work_320/AppConfigReader.cpp @ 679

Revision 553, 4.5 KB checked in by jls17, 6 years ago (diff)
  • initialize configFile member var directly
  • added initialization for settingsFoundCount
Line 
1//Copyright (C) 2012 John Luthgers | jls17
2//
3//This file is part of ARInside.
4//
5//    ARInside is free software: you can redistribute it and/or modify
6//    it under the terms of the GNU General Public License as published by
7//    the Free Software Foundation, version 2 of the License.
8//
9//    ARInside is distributed in the hope that it will be useful,
10//    but WITHOUT ANY WARRANTY; without even the implied warranty of
11//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12//    GNU General Public License for more details.
13//
14//    You should have received a copy of the GNU General Public License
15//    along with ARInside.  If not, see <http://www.gnu.org/licenses/>.
16
17#include "stdafx.h"
18#include "AppConfigReader.h"
19#include "AppException.h"
20#include "ConfigFile.h"
21#include "FileSystemUtil.h"
22
23AppConfigReader::AppConfigReader(const std::string &filename)
24: configFile(filename)
25{
26        settingsFoundCount = 0;
27}
28
29AppConfigReader::~AppConfigReader(void)
30{
31}
32
33void AppConfigReader::LoadTo(AppConfig &appConfig)
34{
35        // reset settings-found count incase the LoadTo function is called multiple times
36        settingsFoundCount = 0;
37
38        if (!ConfigFileExistsAndIsReadable())
39                ThrowNotFoundException();
40
41        auto_ptr<ConfigFile> cfg(CreateConfigFileObject());
42
43        if (cfg.get() == NULL)
44                throw AppException("internal error, couldn't create ConfigFile object!", "CFG_OBJ_CREATE_FAILED", "configuration");
45
46        MapConfigFileToAppConfig(*cfg, appConfig);
47
48        if (settingsFoundCount == 0)
49                ThrowInvalidConfigFileException();
50}
51
52bool AppConfigReader::ConfigFileExistsAndIsReadable()
53{
54        return FileSystemUtil::FileExistsAndReadable(configFile);
55}
56
57ConfigFile* AppConfigReader::CreateConfigFileObject()
58{
59        return new ConfigFile(configFile);
60}
61
62void AppConfigReader::MapConfigFileToAppConfig(ConfigFile &config, AppConfig& cfg)
63{
64        IncFound(config.readInto<string>(cfg.userForm, "UserForm", "User"));
65        IncFound(config.readInto<string>(cfg.userQuery, "UserQuery", "1=1"));
66        IncFound(config.readInto<string>(cfg.groupForm, "GroupForm", "Group"));
67        IncFound(config.readInto<string>(cfg.groupQuery, "GroupQuery", "1=1"));
68        IncFound(config.readInto<string>(cfg.roleForm, "RoleForm", "Roles"));
69        IncFound(config.readInto<string>(cfg.roleQuery, "RoleQuery", "1=1"));
70        IncFound(config.readInto<int>(cfg.maxRetrieve, "MaxRetrieve", 0));
71        IncFound(config.readInto<string>(cfg.companyName, "CompanyName", ""));
72        IncFound(config.readInto<string>(cfg.companyUrl, "CompanyUrl", ""));
73        IncFound(config.readInto<string>(cfg.targetFolder, "TargetFolder", ""));
74        IncFound(config.readInto<bool>(cfg.fileMode, "FileMode", false));
75        IncFound(config.readInto<string>(cfg.objListXML, "ObjListXML", ""));
76        IncFound(config.readInto<bool>(cfg.oldNaming, "OldNaming", false));
77        IncFound(config.readInto<string>(cfg.blackList, "BlackList", ""));
78        IncFound(config.readInto<bool>(cfg.bLoadServerInfoList, "LoadServerInfoList", true));
79        IncFound(config.readInto<bool>(cfg.bLoadUserList, "LoadUserList", true));
80        IncFound(config.readInto<bool>(cfg.bLoadGroupList, "LoadGroupList", true));
81        IncFound(config.readInto<bool>(cfg.bLoadRoleList, "LoadRoleList", true));
82        IncFound(config.readInto<bool>(cfg.bUseUtf8, "Utf-8", false));
83        IncFound(config.readInto<bool>(cfg.bCompactFolder, "CompactFolder", false));
84        IncFound(config.readInto<bool>(cfg.bGZCompression, "GZCompression", false));
85        IncFound(config.readInto<bool>(cfg.bDeleteExistingFiles, "DeleteExistingFiles", false));
86        IncFound(config.readInto<string>(cfg.runNotes, "RunNotes", ""));
87        IncFound(config.readInto<string>(cfg.serverName, "ServerName", ""));
88        IncFound(config.readInto<int>(cfg.tcpPort, "TCPPort", 0));
89        IncFound(config.readInto<int>(cfg.rpcPort, "RPCPort", 0));
90        IncFound(config.readInto<string>(cfg.userName, "Username", ""));
91        IncFound(config.readInto<string>(cfg.password, "Password", ""));
92        IncFound(config.readInto<int>(cfg.apiTimeout, "APITimeout", 0));
93        IncFound(config.readInto<string>(cfg.overlayMode, "OverlayMode", "TRUE"));
94}
95
96void AppConfigReader::IncFound(bool wasFound)
97{
98        if (wasFound) settingsFoundCount++;
99}
100
101void AppConfigReader::ThrowNotFoundException()
102{
103        stringstream strm;
104        strm << "File '" << configFile << "' not found.";
105        throw(AppException(strm.str(), "CONFIG_FILE_MISSING", "configuration"));
106}
107
108void AppConfigReader::ThrowInvalidConfigFileException()
109{
110        stringstream strm;
111        strm << "File '" << configFile << "' doesn't look like a valid configuration file!";
112        throw(AppException(strm.str(), "CONFIG_FILE_INVALID", "configuration"));
113}
Note: See TracBrowser for help on using the repository browser.