File: Path.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (137 lines) | stat: -rw-r--r-- 3,653 bytes parent folder | download | duplicates (2)
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
136
137
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Util/Path.cpp
//! @brief     Implements Helpers functions.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/Model/Util/Path.h"
#include "BAVersion.h"
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QMap>
#include <QStandardPaths>

namespace {

QMap<QString, QString> initializeCharacterMap()
{
    QMap<QString, QString> result;
    result["\\"] = "_backslash_";
    result["/"] = "_slash_";
    result["\""] = "_quote_";
    result["<"] = "_lessthan_";
    result[">"] = "_greaterthan_";
    result["|"] = "_pipe_";
    result["?"] = "_questionmark_";
    return result;
}

const QMap<QString, QString> invalidCharacterMap = initializeCharacterMap();

} // namespace


QString GUI::Path::withTildeHomePath(const QString& path)
{
#ifdef Q_OS_WIN
    return path;
#endif

    static const QString homePath = QDir::homePath();

    QFileInfo fi(QDir::cleanPath(path));
    QString outPath = fi.absoluteFilePath();
    if (outPath.startsWith(homePath))
        return "~" + outPath.mid(homePath.size());
    return path;
}

QString GUI::Path::getBornAgainVersionString()
{
    return QString::fromStdString(BornAgain::version);
}

//! Returns valid file name to be saved on disk. This name is constructed from proposed_name
//! by replacing all special characters with text representation
//! \ backslash
//! / slash
//! " quote
//! < lessthan
//! > greaterthan
//! | pipe
//! ? questionmark
QString GUI::Path::getValidFileName(const QString& proposed_name)
{
    QString result = proposed_name;
    for (auto it = invalidCharacterMap.begin(); it != invalidCharacterMap.end(); ++it)
        result.replace(it.key(), it.value());
    return result;
}

//! Constructs the name of the file for intensity data.
QString GUI::Path::intensityDataFileName(const QString& itemName, const QString& prefix)
{
    return prefix + "_" + getValidFileName(itemName) + ".int";
}


//! parses version string into 2 numbers, returns true in the case of success
bool GUI::Path::parseVersion(const QString& version, int& major_num, int& minor_num)
{
    major_num = 0;
    minor_num = 0;
    bool success(true);
    QStringList nums = version.split(".");
    if (nums.size() != 2)
        return false;

    bool ok(false);
    major_num = nums.at(0).toInt(&ok);
    success &= ok;
    minor_num = nums.at(1).toInt(&ok);
    success &= ok;

    return success;
}

int GUI::Path::versionCode(const QString& version)
{
    int ba_major;
    int ba_minor;
    if (!parseVersion(version, ba_major, ba_minor))
        return -1;

    return ba_major * 100 + ba_minor;
}

//! Returns true if current BornAgain version match minimal required version
bool GUI::Path::isVersionMatchMinimal(const QString& version, const QString& minimal_version)
{
    return versionCode(version) >= versionCode(minimal_version);
}

//! Returns file directory from the full file path
QString GUI::Path::fileDir(const QString& fname)
{
    QFileInfo info(fname);
    if (info.exists())
        return info.dir().path();
    return "";
}

//! Returns base name of file.

QString GUI::Path::baseName(const QString& fname)
{
    QFileInfo info(fname);
    return info.baseName();
}