File: fileName.cpp

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (174 lines) | stat: -rw-r--r-- 4,198 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/***********************************************/
/**
* @file fileName.cpp
*
* @brief File names
*
* @author Torsten Mayer-Guerr
* @date 2008-07-28
*
*/
/***********************************************/

#include "base/importStd.h"
#include "base/string.h"
#include "parser/stringParser.h"
#include "fileName.h"

/*************************************************/

FileName::FileName() : resolved(TRUE) {}

/*************************************************/

FileName::FileName(const std::string &name)
  : nameUnparsed(name), nameParsed(name), resolved(TRUE) {}

/*************************************************/

FileName::FileName(const std::string &name, const VariableList &varList)
  : nameUnparsed(name), resolved(FALSE), varList(varList) {}

/*************************************************/

void FileName::resolve() const
{
  try
  {
    if(resolved)
      return;
    nameParsed = StringParser::parse(nameUnparsed, varList);
    resolved = TRUE;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/*************************************************/

FileName FileName::append(const FileName &fileName) const
{
  resolve();
  if(empty())
    return fileName;
  std::string separator;
  if((nameParsed.back() != '/') && (nameParsed.back() != '\\'))
#ifdef _WIN32
    separator = '\\'; // = std::filesystem::path::preferred_separator;
#else
    separator = '/';
#endif
  return FileName(nameParsed+separator+fileName.str());
}

/*************************************************/

FileName FileName::fullExtension() const
{
  resolve();
  auto pos = nameParsed.rfind('.');
  if((pos == std::string::npos) || (pos+1 == nameParsed.size()))
    return FileName();
  std::string ext = String::upperCase(nameParsed.substr(pos+1));
  if((pos > 0) && (ext == "GZ" || ext == "Z"))
  {
    auto posNew = nameParsed.rfind('.', pos-1);
    if(posNew != std::string::npos)
      pos = posNew;
  }
  return FileName(nameParsed.substr(pos+1));
}

/*************************************************/

FileName FileName::typeExtension() const
{
  resolve();
  std::string ext = fullExtension();
  return FileName(ext.substr(0, ext.find('.')));
}

/*************************************************/

FileName FileName::packExtension() const
{
  resolve();
  std::string ext = fullExtension();
  auto pos = ext.find('.');
  if(pos == std::string::npos)
    return FileName();
  return FileName(ext.substr(pos+1));
}

/*************************************************/

FileName FileName::stripFullExtension() const
{
  resolve();
  auto pos = nameParsed.rfind("."+fullExtension().str());
  if(pos == std::string::npos)
    return *this;
  return FileName(nameParsed.substr(0, pos));
}

/*************************************************/

FileName FileName::replaceFullExtension(const std::string &text) const
{
  resolve();
  return FileName(stripFullExtension().str() + ((text.empty() || String::startsWith(text, ".")) ? "" : ".") + text);
}

/*************************************************/

FileName FileName::directory() const
{
  resolve();
  auto pos = nameParsed.find_last_of("/\\");
  if(pos == std::string::npos)
    return FileName();
  return FileName(nameParsed.substr(0, pos+1));
}

/*************************************************/

FileName FileName::stripDirectory() const
{
  resolve();
  std::string::size_type pos = nameParsed.find_last_of("/\\");
  if(pos == std::string::npos)
    return *this;
  return FileName(nameParsed.substr(pos+1));
}

/*************************************************/

FileName FileName::appendBaseName(const std::string &text) const
{
  resolve();
  auto pos = nameParsed.rfind("."+fullExtension().str());
  if(pos == std::string::npos)
    return FileName(nameParsed+text);
  std::string tmp = nameParsed;
  return FileName(tmp.insert(pos, text));
}

/*************************************************/

FileName FileName::operator()(const VariableList &varList) const
{
  try
  {
    VariableList varList2 = this->varList;
    varList2 += varList;
    return FileName(StringParser::parse(nameUnparsed, varList2));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/*************************************************/