File: ConfigLocater.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (86 lines) | stat: -rwxr-xr-x 2,448 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
/* 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/FileSystem/DataDirLocater.h"
#include "System/FileSystem/FileSystem.h"
#include "System/Platform/Misc.h"


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

static void GetPortableLocations(vector<string>& locations)
{
#ifndef _WIN32
	const std::string cfgName = ".springrc";
#else
	const std::string cfgName = "springsettings.cfg";
#endif

	if (dataDirLocater.IsIsolationMode()) {
		// Search in IsolatedModeDir
		const std::string confPath = FileSystem::EnsurePathSepAtEnd(dataDirLocater.GetIsolationModeDir()) + cfgName;
		locations.push_back(confPath);
	} else {
		// Search in binary dir
		const std::string confPath = FileSystem::EnsurePathSepAtEnd(Platform::GetProcessExecutablePath()) + cfgName;

		// lets see if the file exists & is writable
		// (otherwise it can fail/segfault/end up in virtualstore...)
		// _access modes: 0 - exists; 2 - write; 4 - read; 6 - r/w
		// doesn't work on directories (precisely, mode is always 0)
		if (access(confPath.c_str(), 6) != -1) {
			locations.push_back(confPath);
		}
	}
}

static void GetPlatformLocations(vector<string>& locations)
{
#ifndef _WIN32
	const string base = ".springrc";
	const string home = FileSystem::EnsurePathSepAtEnd(Platform::GetUserDir());

	const string defCfg = home + base;
	const string verCfg = defCfg + "-" + SpringVersion::Get();
#else
	// e.g. "C:\Users\USER\AppData\Local"
	const string userDir = FileSystem::EnsurePathSepAtEnd(Platform::GetUserDir());

	const string defCfg = userDir + "springsettings.cfg";
	const string verCfg = userDir + "springsettings-" + SpringVersion::Get() + ".cfg";
#endif

	if (access(verCfg.c_str(), 6) != -1) { // check for read & write access
		locations.push_back(verCfg);
	}

	locations.push_back(defCfg);
}

/**
 * @brief Get the names of the default configuration files
 */
void ConfigLocater::GetDefaultLocations(vector<string>& locations)
{
	// check if there exists a config file in the same directory as the exe
	GetPortableLocations(locations);

	// portable implies isolated:
	// no extra sources when a portable source has been found!
	if (locations.empty()) {
		GetPlatformLocations(locations);
	}
}