File: loopDirectoryListing.h

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 (96 lines) | stat: -rw-r--r-- 3,070 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
/***********************************************/
/**
* @file loopDirectoryListing.h
*
* @brief Loop over files of a directory.
*
* @author Torsten Mayer.Guerr
* @date 2017-02-10
*
*/
/***********************************************/

#ifndef __GROOPS_LOOPDIRECTORYLISTING__
#define __GROOPS_LOOPDIRECTORYLISTING__

// Latex documentation
#ifdef DOCSTRING_Loop
static const char *docstringLoopDirectoryListing = R"(
\subsection{DirectoryListing}\label{loopType:directoryListing}
Loop over files of a directory.
)";
#endif

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

#include "base/import.h"
#include "base/string.h"
#include "parallel/parallel.h"
#include "inputOutput/logging.h"
#include "inputOutput/system.h"
#include "classes/loop/loop.h"

/***** CLASS ***********************************/

/** @brief Loop over files of a directory.
* @ingroup loopGroup
* @see Loop */
class LoopDirectoryListing : public Loop
{
  std::string           nameFile, nameIndex, nameCount;
  std::vector<FileName> files;

public:
  LoopDirectoryListing(Config &config);

  UInt count() const override {return files.size();}
  Bool iteration(VariableList &varList) override;
};

/***********************************************/
/***** Inlines *********************************/
/***********************************************/

inline LoopDirectoryListing::LoopDirectoryListing(Config &config)
{
  try
  {
    FileName    directory;
    std::string pattern;
    Bool        isRegularExpression;

    readConfig(config, "directory",           directory,           Config::MUSTSET,  "",         "directory");
    readConfig(config, "pattern",             pattern,             Config::DEFAULT,  "*",        "wildcard pattern");
    readConfig(config, "isRegularExpression", isRegularExpression, Config::DEFAULT,  "0",        "pattern is a regular expression");
    readConfig(config, "variableLoopFile",    nameFile,            Config::OPTIONAL, "loopFile", "name of the variable to be replaced");
    readConfig(config, "variableLoopIndex",   nameIndex,           Config::OPTIONAL, "",         "variable with index of current iteration (starts with zero)");
    readConfig(config, "variableLoopCount",   nameCount,           Config::OPTIONAL, "",         "variable with total number of iterations");
    readConfigCondition(config);
    if(isCreateSchema(config)) return;

    files = System::directoryListing(directory, isRegularExpression ? std::regex(pattern) : String::wildcard2regex(pattern));
    std::sort(files.begin(), files.end());
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

inline Bool LoopDirectoryListing::iteration(VariableList &varList)
{
  if(index() >= count())
    return FALSE;

  if(!nameFile.empty())  varList.setVariable(nameFile,  files.at(index()).str());
  if(!nameIndex.empty()) varList.setVariable(nameIndex, index());
  if(!nameCount.empty()) varList.setVariable(nameCount, count());

  return checkCondition(varList);
}

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

#endif