FileSystemPathBuilder.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore
19  * @author Raphael Grimm ( raphael dot grimm at kit dot edu)
20  * @date 2017
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 #include <string>
28 #include <functional>
29 #include <fstream>
30 
31 #include <filesystem>
32 #include <boost/regex.hpp>
33 
34 #include "Registrar.h"
35 
36 namespace armarx
37 {
38  /**
39  * @brief Helps to build a path via format strings:
40  * All format strings are of the form '{[^{}]+}'
41  * If the resulting path should contain the characters '{' or '}', they have to be replaced with '{LEFT CURLY}' and '{RIGHT CURLY}'
42  *
43  * Default patterns are:
44  * * {LEFT CURLY} -> {
45  * * {RIGHT CURLY} -> }
46  * * {year / Year} -> year (2/4 digits)
47  * * {month}
48  * * {day}
49  * * {hour}
50  * * {minute}
51  * * {second}
52  * * {Date} -> YYYY-MM-DD
53  * * {Time} -> HH-MM-SS
54  * * {DateTime} -> {Date}_{Time}
55  * * {time-since-epoch} -> nanoseconds since epoch (can be used as uuid)
56  * * {PackageDir:...} -> Directory of the given package
57  * * {ScenarioDir:...} -> Scenario Directory of the given package
58  * * {DataDir:...} -> Data Directory of the given package
59  * * {BinaryeDir:...} -> Binary Directory of the given package
60  * * {CMakeDir:...} -> CMake Directory of the given package
61  * * {BuildDir:...} -> Build Directory of the given package
62  */
64  {
65  static const boost::regex RawPathRegex; //"([^{}]+|{[^{}]+})+"
66  /**
67  * @brief Replacer for a pattern.
68  * The matched pattern hast to match '[^{}]+' (must not contain '{' or '}').
69  * The replacement string may contain '{' and '}'
70  * The patternReplacer is applied to patterns matched by patternRegex.
71  */
73  {
74  std::string name;
75  std::string description;
76  boost::regex patternRegex;
77  std::function<std::string(const std::string&)> patternReplacer;
78  };
79 
81  {
83  std::string name,
84  boost::regex patternRegex,
85  std::function<std::string(const std::string&)> replacer,
86  std::string description = "");
87  };
88 
89  /**
90  * @brief FileSystemPathBuilder
91  * @param rawPath must match RawPathRegex ("([^{}]+|{[^{}]+})+")
92  */
93  FileSystemPathBuilder(std::string rawPath) : rawPath {std::move(rawPath)}, path {ApplyFormatting(this->rawPath)}, bpath {path} {}
94 
95  const std::filesystem::path& getBoostPath() const
96  {
97  return bpath;
98  }
99  const std::string& getRawPath() const
100  {
101  return rawPath;
102  }
103  const std::string& getPath() const
104  {
105  return path;
106  }
108  {
109  std::filesystem::create_directories(bpath.parent_path());
110  }
111 
112  //broken since old gcc has no move ctor for fstreams
113  // std::ofstream getOfStream() const
114  // {
115  // std::filesystem::create_directories(bpath.parent_path());
116  // return std::ofstream {path};
117  // }
118  // std::ifstream getIfStream() const
119  // {
120  // return std::ifstream {path};
121  // }
122 
123 
124  static std::map<std::string, FormatStringOption> GetFormatStringOptions();
125 
126  static std::string ApplyFormatting(const std::string& rawPath);
127  static std::string ApplyFormattingAndResolveEnvAndCMakeVars(const std::string& rawPath);
128 
129  const std::string rawPath;
130  const std::string path;
131  const std::filesystem::path bpath;
132 
133  };
135 }
armarx::FileSystemPathBuilder::FormatStringOption
Replacer for a pattern.
Definition: FileSystemPathBuilder.h:72
armarx::FileSystemPathBuilder::FileSystemPathBuilder
FileSystemPathBuilder(std::string rawPath)
FileSystemPathBuilder.
Definition: FileSystemPathBuilder.h:93
armarx::FileSystemPathBuilder::createParentDirectories
void createParentDirectories() const
Definition: FileSystemPathBuilder.h:107
armarx::FileSystemPathBuilder::bpath
const std::filesystem::path bpath
Definition: FileSystemPathBuilder.h:131
armarx::FileSystemPathBuilder::rawPath
const std::string rawPath
Definition: FileSystemPathBuilder.h:129
armarx::FileSystemPathBuilder::path
const std::string path
Definition: FileSystemPathBuilder.h:130
armarx::FileSystemPathBuilder::RawPathRegex
static const boost::regex RawPathRegex
Definition: FileSystemPathBuilder.h:65
armarx::FileSystemPathBuilder::ApplyFormatting
static std::string ApplyFormatting(const std::string &rawPath)
Definition: FileSystemPathBuilder.cpp:51
Registrar.h
armarx::FileSystemPathBuilder::getBoostPath
const std::filesystem::path & getBoostPath() const
Definition: FileSystemPathBuilder.h:95
armarx::FileSystemPathBuilder::RegisterFormatStringOption::RegisterFormatStringOption
RegisterFormatStringOption(std::string name, boost::regex patternRegex, std::function< std::string(const std::string &)> replacer, std::string description="")
Definition: FileSystemPathBuilder.cpp:102
armarx::FileSystemPathBuilder::getPath
const std::string & getPath() const
Definition: FileSystemPathBuilder.h:103
armarx::FileSystemPathBuilder::FormatStringOption::description
std::string description
Definition: FileSystemPathBuilder.h:75
armarx::FileSystemPathBuilder::FormatStringOption::patternRegex
boost::regex patternRegex
Definition: FileSystemPathBuilder.h:76
armarx::FileSystemPathBuilder::FormatStringOption::name
std::string name
Definition: FileSystemPathBuilder.h:74
armarx::Registrar
Stores key object pairs.
Definition: Registrar.h:62
armarx::FileSystemPathBuilder::RegisterFormatStringOption
Definition: FileSystemPathBuilder.h:80
armarx::FileSystemPathBuilder::GetFormatStringOptions
static std::map< std::string, FormatStringOption > GetFormatStringOptions()
Definition: FileSystemPathBuilder.cpp:41
armarx::FileSystemPathBuilder::getRawPath
const std::string & getRawPath() const
Definition: FileSystemPathBuilder.h:99
armarx::FileSystemPathBuilder::FormatStringOption::patternReplacer
std::function< std::string(const std::string &)> patternReplacer
Definition: FileSystemPathBuilder.h:77
armarx::FileSystemPathBuilder::ApplyFormattingAndResolveEnvAndCMakeVars
static std::string ApplyFormattingAndResolveEnvAndCMakeVars(const std::string &rawPath)
Definition: FileSystemPathBuilder.cpp:94
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::FileSystemPathBuilder
Helps to build a path via format strings: All format strings are of the form '{[^{}]+}' If the result...
Definition: FileSystemPathBuilder.h:63