ewmscp  ..
requestProvider.cpp
Go to the documentation of this file.
1 #include "requestProvider.h"
2 #include "ewmscp.h"
3 #include "errMsgQueue.h"
4 #include <libgen.h>
5 #include <string.h>
6 
8  "map of source to destination directories");
10 
11 decltype(requestProvider::getFactoryMap()) requestProvider::getFactoryMap() {
12  static std::remove_reference<decltype(getFactoryMap())>::type map;
13  return map;
14 }
15 
16 requestProvider* requestProvider::newProvider(const std::string& choice,
17  decltype(requests) aRequests,
18  decltype(delayedRequests) aDelayedRequests,
19  decltype(parents) aParents,
20  decltype(InputHandler) aInputHandler,
21  decltype(OutputHandler) aOutputHandler) {
22  return getFactoryMap()[choice]->create(aRequests, aDelayedRequests, aParents, aInputHandler, aOutputHandler);
23 }
25  for (const auto& item : getFactoryMap()) {
26  option.fAddToRange(item.first);
27  }
28 }
29 
30 const singleMap& requestProvider::getDstPath(const std::string& source,
31  std::string& destination,
32  bool baseNameOnly) {
33  for (const auto& item : pathMap) {
34  if (item.first.empty() // we have the catchall item
35  || source.find(item.first) == 0) { // srcPath starts with this mapped dir
36  destination = item.second;
37  if (destination.back() != '/') {
38  destination += "/";
39  }
40  auto shortPath = source.substr(item.first.size());
41  while (!shortPath.empty() && shortPath.front() == '/') {
42  shortPath.erase(0, 1);
43  }
44  if (parents && !baseNameOnly) {
45  if (shortPath.find("./") == 0) {
46  destination += shortPath.substr(2);
47  } else {
48  destination += shortPath;
49  }
50  } else {
51  auto disposable_buffer = strdup(shortPath.c_str());
52  destination += basename(disposable_buffer);
53  free(disposable_buffer);
54  }
55  return item;
56  }
57  }
59  source, "map paths", "no dir mapping found");
60  for (const auto& item : pathMap) {
62  source, "map paths",
63  "candidate: ", item.first, " -> ", item.second);
64  }
65  throw std::runtime_error("no dir mapping found for " + source);
66  return badMapEntry;
67 }
68 
69 
70 void requestProvider::prepareMappings(std::vector<std::string>& sources,
71  std::string& destination) {
72  if (!destination.empty()) { // last arg (destination) is also a source
73  sources.push_back(destination);
74  destination.clear();
75  }
76  if (pathMap.empty()) {
77  throw std::runtime_error("no mapping given");
78  }
79 }
80 
81 void requestProvider::printMappings(std::ostream& stream) {
82  stream << "Path mapping:\n";
83  stream << "There are " << pathMap.size() << " directory mappings:\n";
84  for (const auto& item : pathMap) {
85  stream << "'" << item.first << "' -> '" << item.second << "'\n";
86  }
87 }
options::single< std::string >
errMsgQueue.h
errMsg::location
class for defining the location of a error message in the source code.
Definition: errMsgQueue.h:14
options::internal::typed_base::fAddToRange
virtual void fAddToRange(rangeValueType aValue)
add a value to the range of allowed values
Definition: Options.h:458
requestProvider::newProvider
static requestProvider * newProvider(const std::string &choice, decltype(requests) aRequests, decltype(delayedRequests) aDelayedRequests, decltype(parents) aParents, decltype(InputHandler) aInputHandler, decltype(OutputHandler) aOutputHandler)
Definition: requestProvider.cpp:16
errMsg::level::crit
@ crit
requestProvider
generic provider of copy requests
Definition: requestProvider.h:13
requestProvider::parents
bool parents
Definition: requestProvider.h:33
requestProvider::badMapEntry
static singleMap badMapEntry
Definition: requestProvider.h:30
requestProvider::addAllowedNamesToOption
static void addAllowedNamesToOption(options::single< std::string > &option)
Definition: requestProvider.cpp:24
requestProvider.h
requestProvider::pathMap
static options::map< std::string, pathMapType > pathMap
Definition: requestProvider.h:29
requestProvider::getFactoryMap
static std::map< std::string, factoryClass * > & getFactoryMap()
requestProvider::prepareMappings
virtual void prepareMappings(std::vector< std::string > &sources, std::string &destination)
Definition: requestProvider.cpp:70
errMsg::emit
void emit(level aLogLevel, const location &loc, const std::string &aObject, const std::string &aAction, const Args &... args)
function to create and enqueue a message, this is the only way that messages should be created!
Definition: errMsgQueue.h:148
singleMap
std::pair< std::string, std::string > singleMap
Definition: copyRequest.h:52
requestProvider::printMappings
virtual void printMappings(std::ostream &stream)
Definition: requestProvider.cpp:81
requestProvider::getDstPath
virtual const singleMap & getDstPath(const std::string &source, std::string &destination, bool baseNameOnly=false)
get detstination papth for a given source path
Definition: requestProvider.cpp:30
ewmscp.h
defineStatic
#define defineStatic(var,...)
defines a static variable and instatitates the constructor with the variable number of arguments.
Definition: ewmscp.h:42