File: ConfigLocater.cpp

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (93 lines) | stat: -rw-r--r-- 2,516 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef WIN32
	#include <unistd.h>
	#include <sys/stat.h>
	#include <sys/types.h>
#else
	#include <io.h>
	#include <direct.h>
	#include <windows.h>
#endif
#include <stdlib.h>

#include "ConfigLocater.h"
#include "Game/GameVersion.h"
#include "System/Exceptions.h"
#include "System/FileSystem/DataDirLocater.h"
#include "System/FileSystem/FileSystem.h"
#include "System/Platform/Misc.h"
#include "System/Platform/Win/win32.h"
#include <boost/foreach.hpp>


using std::string;
using std::vector;


static void AddCfgFile(vector<string>& locations, const  std::string& filepath)
{
	BOOST_FOREACH(std::string& fp, locations) {
		if (FileSystem::ComparePaths(fp, filepath)) {
			return;
		}
	}
	locations.push_back(filepath);
}


static void LoadCfgs(vector<string>& locations, const std::string& defCfg, const std::string& verCfg)
{
	// lets see if the file exists & is writable
	// (otherwise it can fail/segfault/end up in virtualstore...)

	if (locations.empty()) {
		// add the config file we write to
		AddCfgFile(locations, defCfg);

		FileSystem::TouchFile(defCfg); // create file if doesn't exists
		if (access(defCfg.c_str(), R_OK | W_OK) == -1) {
			throw content_error(std::string("config file not writeable: \"") + defCfg + "\"");
		}
	}

	if (access(verCfg.c_str(), R_OK) != -1) {
		AddCfgFile(locations, verCfg);
	}
	if (access(defCfg.c_str(), R_OK) != -1) {
		AddCfgFile(locations, defCfg);
	}
}


static void LoadCfgsInFolder(vector<string>& locations, const std::string& path, const bool hidden = false)
{
	// all platforms: springsettings.cfg
	const string defCfg = path + "springsettings.cfg";
	const string verCfg = path + "springsettings-" + SpringVersion::Get() + ".cfg";
	LoadCfgs(locations, defCfg, verCfg);

#ifndef _WIN32
	// unix only: (.)springrc (lower priority than springsettings.cfg!)
	const string base = (hidden) ? ".springrc" : "springrc";
	const string unixDefCfg = path + base;
	const string unixVerCfg = unixDefCfg + "-" + SpringVersion::Get();
	LoadCfgs(locations, unixDefCfg, unixVerCfg);
#endif
}



/**
 * @brief Get the names of the default configuration files
 */
void ConfigLocater::GetDefaultLocations(vector<string>& locations)
{
	// first, add writeable config file
	LoadCfgsInFolder(locations, dataDirLocater.GetWriteDirPath(), false);

	// add additional readonly config files
	BOOST_FOREACH(std::string path, dataDirLocater.GetDataDirPaths()) {
		LoadCfgsInFolder(locations, path);
	}
}