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 <filesystem>
28 #include <fstream>
29 #include <functional>
30 #include <string>
31 
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  /**
68  * @brief Replacer for a pattern.
69  * The matched pattern hast to match '[^{}]+' (must not contain '{' or '}').
70  * The replacement string may contain '{' and '}'
71  * The patternReplacer is applied to patterns matched by patternRegex.
72  */
74  {
75  std::string name;
76  std::string description;
77  boost::regex patternRegex;
78  std::function<std::string(const std::string&)> patternReplacer;
79  };
80 
82  {
83  RegisterFormatStringOption(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  */
94  rawPath{std::move(rawPath)}, path{ApplyFormatting(this->rawPath)}, bpath{path}
95  {
96  }
97 
98  const std::filesystem::path&
99  getBoostPath() const
100  {
101  return bpath;
102  }
103 
104  const std::string&
105  getRawPath() const
106  {
107  return rawPath;
108  }
109 
110  const std::string&
111  getPath() const
112  {
113  return path;
114  }
115 
116  void
118  {
119  std::filesystem::create_directories(bpath.parent_path());
120  }
121 
122  //broken since old gcc has no move ctor for fstreams
123  // std::ofstream getOfStream() const
124  // {
125  // std::filesystem::create_directories(bpath.parent_path());
126  // return std::ofstream {path};
127  // }
128  // std::ifstream getIfStream() const
129  // {
130  // return std::ifstream {path};
131  // }
132 
133 
134  static std::map<std::string, FormatStringOption> GetFormatStringOptions();
135 
136  static std::string ApplyFormatting(const std::string& rawPath);
137  static std::string ApplyFormattingAndResolveEnvAndCMakeVars(const std::string& rawPath);
138 
139  const std::string rawPath;
140  const std::string path;
141  const std::filesystem::path bpath;
142  };
143 
146 } // namespace armarx
armarx::FileSystemPathBuilder::FormatStringOption
Replacer for a pattern.
Definition: FileSystemPathBuilder.h:73
armarx::FileSystemPathBuilder::FileSystemPathBuilder
FileSystemPathBuilder(std::string rawPath)
FileSystemPathBuilder.
Definition: FileSystemPathBuilder.h:93
armarx::FileSystemPathBuilder::createParentDirectories
void createParentDirectories() const
Definition: FileSystemPathBuilder.h:117
armarx::FileSystemPathBuilder::bpath
const std::filesystem::path bpath
Definition: FileSystemPathBuilder.h:141
armarx::FileSystemPathBuilder::rawPath
const std::string rawPath
Definition: FileSystemPathBuilder.h:139
armarx::FileSystemPathBuilder::path
const std::string path
Definition: FileSystemPathBuilder.h:140
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:99
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:100
armarx::FileSystemPathBuilder::getPath
const std::string & getPath() const
Definition: FileSystemPathBuilder.h:111
armarx::FileSystemPathBuilder::FormatStringOption::description
std::string description
Definition: FileSystemPathBuilder.h:76
armarx::FileSystemPathBuilder::FormatStringOption::patternRegex
boost::regex patternRegex
Definition: FileSystemPathBuilder.h:77
armarx::FileSystemPathBuilder::FormatStringOption::name
std::string name
Definition: FileSystemPathBuilder.h:75
armarx::Registrar
Stores key object pairs.
Definition: Registrar.h:62
armarx::FileSystemPathBuilder::RegisterFormatStringOption
Definition: FileSystemPathBuilder.h:81
armarx::FileSystemPathBuilder::GetFormatStringOptions
static std::map< std::string, FormatStringOption > GetFormatStringOptions()
Definition: FileSystemPathBuilder.cpp:40
armarx::FileSystemPathBuilder::getRawPath
const std::string & getRawPath() const
Definition: FileSystemPathBuilder.h:105
armarx::FileSystemPathBuilder::FormatStringOption::patternReplacer
std::function< std::string(const std::string &)> patternReplacer
Definition: FileSystemPathBuilder.h:78
armarx::FileSystemPathBuilder::ApplyFormattingAndResolveEnvAndCMakeVars
static std::string ApplyFormattingAndResolveEnvAndCMakeVars(const std::string &rawPath)
Definition: FileSystemPathBuilder.cpp:92
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::FileSystemPathBuilder
Helps to build a path via format strings: All format strings are of the form '{[^{}]+}' If the result...
Definition: FileSystemPathBuilder.h:63