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
36namespace 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 */
95 {
96 }
97
98 const std::filesystem::path&
100 {
101 return bpath;
102 }
103
104 const std::string&
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
Stores key object pairs.
Definition Registrar.h:63
This file offers overloads of toIce() and fromIce() functions for STL container types.
Registrar< FileSystemPathBuilder::FormatStringOption, std::string > FileSystemPathBuilderFormatStringOptionRegistrar
std::function< std::string(const std::string &)> patternReplacer
RegisterFormatStringOption(std::string name, boost::regex patternRegex, std::function< std::string(const std::string &)> replacer, std::string description="")
static std::string ApplyFormatting(const std::string &rawPath)
const std::filesystem::path bpath
static std::string ApplyFormattingAndResolveEnvAndCMakeVars(const std::string &rawPath)
const std::string & getRawPath() const
static const boost::regex RawPathRegex
const std::string & getPath() const
FileSystemPathBuilder(std::string rawPath)
FileSystemPathBuilder.
const std::filesystem::path & getBoostPath() const
static std::map< std::string, FormatStringOption > GetFormatStringOptions()