File: dataStore.cxx

package info (click to toggle)
flightgear 1%3A2020.3.19%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 51,068 kB
  • sloc: cpp: 245,588; ansic: 181,318; sh: 13,686; perl: 4,475; python: 3,139; xml: 899; asm: 642; makefile: 347; java: 314
file content (135 lines) | stat: -rw-r--r-- 4,264 bytes parent folder | download | duplicates (4)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2018 James Turner
 * Copyright (C) 2018 Edward d'Auvergne
 *
 * This file is part of the program FlightGear.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <iostream>

#include <simgear/misc/strutils.hxx>

#include <Main/fg_init.hxx>

#include <Include/version.h>
#include "config.h"

#include "dataStore.hxx"


// Sanity check.
bool looksLikeFGData(const SGPath& path)
{
    return (path / "defaults.xml").exists();
}


// Set up the path to FGData.
int DataStore::findFGRoot(const std::string& fgRootCmdLineOpt, bool debug)
{
    SGPath fgRoot;

    // Command line supplied path.
    if (!fgRootCmdLineOpt.empty()) {
        fgRoot = SGPath::fromUtf8(fgRootCmdLineOpt);
        if (looksLikeFGData(fgRoot)) {
            if (debug)
                std::cerr << "FGdata from the command line: " << fgRoot << std::endl;
            _fgRootPath = fgRoot;
            return 0;

        // Fail if an invalid path is supplied.
        } else {
            std::cerr << "The supplied path \"" << fgRootCmdLineOpt << "\" is not a valid FGData path." << std::endl;
            return 1;
        }
    }

    // The FG_DATA_DIR CMake option.
    fgRoot = SGPath::fromUtf8(PKGLIBDIR);
    if (looksLikeFGData(fgRoot)) {
        if (debug)
            std::cerr << "FGdata found via $PKGLIBDIR: " << fgRoot << std::endl;
        _fgRootPath = fgRoot;
        return 0;
    }

    // The FG_ROOT environmental variable.
    if (std::getenv("FG_ROOT")) {
        fgRoot = SGPath::fromEnv("FG_ROOT");
        if (looksLikeFGData(fgRoot)) {
            if (debug)
                std::cerr << "FGdata found via $FG_ROOT: " << fgRoot << std::endl;
            _fgRootPath = fgRoot;
            return 0;
        }
    }

    // Try in ../fgdata.
    fgRoot = SGPath::fromUtf8(FGSRCDIR) / ".." / "fgdata";
    if (looksLikeFGData(fgRoot)) {
        if (debug)
            std::cerr << "FGdata found at \"$FGSRCDIR/../fgdata\": " << fgRoot << std::endl;
        _fgRootPath = fgRoot;
        return 0;
    }

    // Try in ../data.
    fgRoot = SGPath::fromUtf8(FGSRCDIR) / ".." / "data";
    if (looksLikeFGData(fgRoot)) {
        if (debug)
            std::cerr << "FGdata found at \"$FGSRCDIR/../data\": " << std::endl;
        _fgRootPath = fgRoot;
        return 0;
    }

    // No FGData.
    std::cerr << "FGData not found." << std::endl;
    return 1;
}

// Get the path to FGData.
SGPath DataStore::getFGRoot()
{
    return _fgRootPath;
}

// Perform a version validation of FGData, returning zero if ok.
int DataStore::validateFGRoot()
{
    // The FGData version.
    SGPath root = getFGRoot();
    std::string base_version = fgBasePackageVersion(root);
    if (base_version.empty()) {
        std::cout << "Base package not found." << std::endl;
        std::cout << "Required FGData files not found, please check your configuration." << std::endl;
        std::cout << "Looking for FGData files in '" << root.str() << "'." << std::endl;
        std::cout.flush();
        return 1;
    }

    // Comparison of only the major and minor version numbers.
    const int versionComp = simgear::strutils::compare_versions(FLIGHTGEAR_VERSION, base_version, 2);
    if (versionComp != 0) {
        std::cout << "Base package version mismatch." << std::endl;
        std::cout << "Version check failed, please check your configuration." << std::endl;
        std::cout << "The FGData version '" << base_version << "' in '" << root.str();
        std::cout << "does not match the FlightGear version '" << std::string(FLIGHTGEAR_VERSION);
        std::cout << "'." << std::endl;
        std::cout.flush();
    }
    return versionComp;
}