File: system.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 (183 lines) | stat: -rw-r--r-- 4,684 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
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
175
176
177
178
179
180
181
182
183
/***********************************************/
/**
* @file system.cpp
*
* @brief Operating system related functions.
*
* @author Andreas Kvas
* @date 2017-03-21
*
*/
/***********************************************/

#ifdef _WIN32
  #define popen  _popen
  #define pclose _pclose
#endif

#include <ctime>
#include <filesystem>
#include "base/importStd.h"
#include "base/string.h"
#include "base/time.h"
#include "system.h"

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

Bool System::exec(const std::string &command, std::vector<std::string> &output)
{
  try
  {
    std::FILE *pipe = popen(command.c_str(), "r");
    if(!pipe)
      throw(Exception("Cannot open pipe"));

    char buffer[1024];
    while(std::fgets(buffer, sizeof(buffer), pipe) != nullptr)
    {
      std::string line(buffer);
      output.push_back(line.substr(0, line.find_last_of('\n')));
    }

    return (pclose(pipe) == 0);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

Bool System::exec(const std::string &command)
{
  std::vector<std::string> output;
  return exec(command, output);
}

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

Bool System::createDirectories(const FileName &fileName)
{
  if(isDirectory(fileName))
    return TRUE;
  return std::filesystem::create_directories(fileName.str());
}

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

Bool System::remove(const FileName &fileName)
{
  return (std::filesystem::remove_all(fileName.str()) != 0);
}

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

Bool System::move(const FileName &fileNameOld, const FileName &fileNameNew)
{
  try
  {
    if(!exists(fileNameOld))
      return FALSE;
    if(isDirectory(fileNameNew))
      std::filesystem::rename(fileNameOld.str(), fileNameNew.append(fileNameOld.stripDirectory()).str());
    else
      std::filesystem::rename(fileNameOld.str(), fileNameNew.str());
    return TRUE;
  }
  catch(std::exception &/*e*/)
  {
    return FALSE;
  }
}

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

Bool System::exists(const FileName &fileName)
{
  try
  {
    // contains no wildcards?
    if(fileName.str().find_first_of("*?") == std::string::npos)
      return std::filesystem::exists(fileName.str());

    // split path into parts
    std::vector<std::string> parts = String::split(fileName.str(), "/\\");

    // deepest directory without wildcards
    std::string path;
    UInt level = 0;
    while((level<parts.size()-1) && (parts.at(level).find_first_of("*?") == std::string::npos))
      path += parts.at(level++) + "/";
    if(path.empty())
      path = currentWorkingDirectory().str() + "/";

    // search directory for matching entries
    const std::regex pattern = String::wildcard2regex(parts.at(level));
    for(auto const &entry : std::filesystem::directory_iterator{path})
      if(std::regex_match(entry.path().filename().string(), pattern))
      {
        if(level == parts.size()-1)
          return TRUE;
        if(std::filesystem::is_directory(entry))
        {
          // replace pattern by current entry
          std::string path2 = path + entry.path().filename().string() + "/";
          for(UInt i=level+1; i<parts.size()-1; i++)
            path2 += parts.at(i) + "/";
          if(exists(path2+parts.back()))
            return TRUE;
        }
      }
    return FALSE;
  }
  catch(std::exception &/*e*/)
  {
    return FALSE;
  }
}

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

Bool System::isDirectory(const FileName &fileName)
{
  return std::filesystem::is_directory(fileName.str());
}

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

FileName System::currentWorkingDirectory()
{
  return FileName(std::filesystem::current_path().string());
}

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

std::vector<FileName> System::directoryListing(const FileName &path, const std::regex &pattern)
{
  try
  {
    std::vector<FileName> files;
    for(auto const &entry : std::filesystem::directory_iterator{path.str()})
      if(std::filesystem::is_regular_file(entry) && std::regex_match(entry.path().filename().string(), pattern))
        files.push_back(FileName(entry.path().filename().string()));
    return files;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

Time System::now()
{
  std::time_t tt = std::time(nullptr);
  std::tm     t  = *std::localtime(&tt);
  return date2time(t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
}

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