13 #include "eckit/thread/AutoLock.h"
14 #include "eckit/thread/Mutex.h"
15 #include "eckit/types/Types.h"
16 #include "eckit/utils/StringTools.h"
17 #include "eckit/filesystem/PathName.h"
18 #include "eckit/config/Resource.h"
23 using namespace eckit;
26 typedef StringTools
S;
32 parsePathNameSchema(pathNameSchema);
42 ASSERT(
"FileMapper: roots_ not set" && roots_.size());
44 bool atLeastOneRootExists (
false);
46 for (
size_t i (0);
i < roots_.size(); ++
i)
48 bool exists (PathName(roots_[
i]).exists());
50 Log::info() <<
"checkRoots: " <<
i <<
": " << roots_[
i] <<
" " << exists << std::endl;
53 atLeastOneRootExists =
true;
55 if (! atLeastOneRootExists)
58 msg <<
"No directory specified in odbServerRoots exists, checked: " << roots_[0];
59 for (
size_t i(1);
i < roots_.size(); ++
i)
60 msg <<
":" << roots_[
i];
62 throw UserError(msg.str());
70 Log::debug() <<
"pathNameSchema: " << pathNameSchema << std::endl;
72 placeholders_.clear();
74 const string& s (pathNameSchema);
75 for (
size_t i (0);
i < s.size(); )
77 size_t begin (s.find(
'{',
i));
78 if (begin == std::string::npos)
80 separators_.push_back(s.substr(
i));
83 size_t end (s.find(
'}', begin));
84 ASSERT(end != std::string::npos);
86 separators_.push_back(s.substr(
i, begin -
i));
87 placeholders_.push_back(s.substr(begin + 1, end - begin - 1));
95 Log::debug() <<
"pathNameSchema: separators_=" << separators_ << std::endl;
96 Log::debug() <<
"pathNameSchema: placeholders_ =" << placeholders_ << std::endl;
108 for (
size_t i (0);
i < roots.size(); ++
i)
114 ASSERT(
"Format of time" && s.size() != 3 && !(s.size() > 6));
120 bool odbServerTimeFormat4Digits = eckit::Resource<bool>(
"odbServerTimeFormat,$ODB_SERVER_TIME_FORMAT_FOUR_DIGITS",
false);
122 if(odbServerTimeFormat4Digits) {
123 if (s.size() == 1) r = string(
"0") + s +
"00";
125 if (s.size() == 5) r = string(
"0") + s.substr(0,3);
127 if (s.size() == 6) r = s.substr(0,4);
129 ASSERT(r.size() == 4);
132 if (s.size() == 1) r = string(
"0") + s;
134 if (s.size() == 4) r = s.substr(0,2);
136 if (s.size() == 5) r = string(
"0") + s.substr(0,1);
138 if (s.size() == 6) r = s.substr(0,2);
140 ASSERT(r.size() == 2);
148 ostream& L(Log::info());
149 L <<
"FileMapper::encode: values:" << endl;
150 for (map<string,string>::const_iterator it(values.begin()); it != values.end(); ++it)
151 L <<
" " << it->first <<
":" << it->second << endl;
155 for (
size_t i (0);
i < separators_.size(); ++
i)
158 if (
pi < placeholders_.size())
160 string placeholder (S::upper(placeholders_[
pi++]));
162 const map<string,string>::const_iterator end(values.end());
164 if (values.find(placeholder) == end && values.find(S::lower(placeholder)) == end)
165 throw UserError(
string(
"Could not find value of '") + placeholder +
"' in values suplied.");
167 const map<string,string>::const_iterator it( values.find(placeholder) != end
168 ? values.find(placeholder)
169 : values.find(S::lower(placeholder)) );
171 string value (it->second);
177 bool patchTimeEncodeRelative = eckit::Resource<bool>(
"odbPatchTimeEncodeRelative;$ODB_PATCH_TIME_ENCODE_RELATIVE",
false);
179 string patchedValue = value;
180 if (S::upper(placeholder) ==
"ANTIME" || (patchTimeEncodeRelative && S::upper(placeholder) ==
"TIME")) {
181 patchedValue = patchTime(value);
184 if (value != patchedValue)
185 L <<
"FileMapper::encodeRelative: value of '" << placeholder <<
"' was '" << value <<
"' changed to '" << patchedValue <<
"'" << endl;
195 string path (encodeRelative(values));
197 for (
size_t i (0);
i < roots_.size(); ++
i)
198 r.push_back(roots_[
i] +
'/' + path);
static std::string expandTilde(const std::string &s)
std::vector< std::string > encode(const std::map< std::string, std::string > &values) const
std::string encodeRelative(const std::map< std::string, std::string > &values) const
void addRoot(const std::string &)
FileMapper(const std::string &pathNameSchema)
std::vector< std::string > keywords() const
std::string patchTime(const std::string &) const
void parsePathNameSchema(const std::string &pathNameSchema)
void addRoots(const std::vector< std::string > &)
real(kind_real), parameter, public pi
Pi.