libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNPath.h
Go to the documentation of this file.
1 /* Copyright 2010-2016 CoReNum, INSA-Lyon, Université Paris Descartes, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNPath.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNPath_HEADER
23 #define CRNPath_HEADER
24 
25 #include <CRNStringUTF8.h>
26 
27 namespace crn
28 {
39  class Path: public StringUTF8
40  {
41  public:
42  enum class Format
43  {
44  AUTO = 0, URI = 1, UNIX = 2, WINDOWS = 3,
45 #ifdef _MSC_VER
46  LOCAL = 3
47 #else
48  LOCAL = 2
49 #endif
50  };
52  // Constructors
54 
56  Path(Format fmt = Format::LOCAL):format(fmt) {}
58  Path(const std::string &s, Format fmt = Format::LOCAL):StringUTF8(s),format(Format::AUTO) { ConvertTo(fmt); }
60  Path(std::string &s, Format fmt = Format::LOCAL):StringUTF8(s),format(Format::AUTO) { ConvertTo(fmt); }
62  Path(char *s, Format fmt = Format::LOCAL):StringUTF8(s),format(Format::AUTO) { ConvertTo(fmt); }
64  Path(const char *s, Format fmt = Format::LOCAL):StringUTF8(s),format(Format::AUTO) { ConvertTo(fmt); }
66  Path(const Path &s, Format fmt = Format::AUTO):StringUTF8(s),format(s.format) { ConvertTo(fmt); }
67  Path(Path&&) = default;
69  Path(const StringUTF8 &s, Format fmt = Format::LOCAL):StringUTF8(s),format(Format::AUTO) { ConvertTo(fmt); }
71  explicit Path(const String &s, Format fmt = Format::LOCAL):StringUTF8(s),format(Format::AUTO) { ConvertTo(fmt); }
73  Path(char c, size_t n = 1, Format fmt = Format::LOCAL):StringUTF8(c, n),format(Format::AUTO) { ConvertTo(fmt); }
75  Path(int i, Format fmt = Format::LOCAL):StringUTF8(i),format(fmt) { }
77  Path(xml::Element &el):format(Format::AUTO) { Deserialize(el); }
78  virtual ~Path() override = default;
79 
80  Path& operator=(const Path&) = default;
81  Path& operator=(Path&&) = default;
82 
84  std::vector<Path> Split(const StringUTF8 &sep) const;
85 
87  // Format info
89 
90  Format GetFormat() const;
92  bool IsURI() const;
94  bool IsUnix() const;
96  bool IsWindows() const;
98  Path& ConvertTo(Format fmt);
99 
101  // Generic methods
103 
105  const Path& operator+=(const Path &s);
107  const Path& operator/=(const Path &s);
108 
110  bool IsAbsolute() const;
112  bool IsRelative() const { return !IsAbsolute(); }
114  Path GetFilename() const;
116  Path GetBase() const;
118  Path GetExtension() const;
120  Path GetDirectory() const;
121 
123  static char Separator() noexcept;
124 
126  void Swap(Path &str) noexcept { StringUTF8::Swap(str); std::swap(format, str.format); }
127 
129  // URI methods
131 
133  StringUTF8 GetScheme() const;
135  Path& ToURI();
137  Path& Decode();
138 
140  // Unix methods
142 
144  Path& ToUnix();
145 
147  // Windows methods
149 
151  char GetDrive() const;
153  static char NoDrive();
155  Path& ToWindows();
156 
158  Path& ToLocal();
159 
161  virtual void Deserialize(xml::Element &el) override;
163  virtual xml::Element Serialize(xml::Element &parent) const override;
164  private:
165  Format format;
166 
168  };
169  template<> struct IsSerializable<Path> : public std::true_type {};
170  template<> struct IsClonable<Path> : public std::true_type {};
171 
175  inline Path operator+(const Path &s1, const Path &s2) { Path tmp(s1); return tmp += s2; }
177  inline Path operator/(const Path &s1, const Path &s2) { Path tmp(s1); return tmp /= s2; }
179  inline size_t Size(Path &p) noexcept { return p.Size(); }
181  inline void Swap(Path &p1, Path &p2) noexcept { p1.Swap(p2); }
182 
183  namespace literals
184  {
186  inline Path operator"" _p(const char *str, size_t len)
187  {
188  return Path{std::string{str, len}};
189  }
190  }
192 }
193 
194 namespace std
195 {
197  inline void swap(crn::Path &p1, crn::Path &p2) noexcept { p1.Swap(p2); }
198 
199  template<> struct hash<crn::Path>
200  {
201  inline size_t operator()(const crn::Path &p) { return hash<string>{}(p.Std()); }
202  };
203 }
204 
205 #include <CRNIO/CRNPathPtr.h>
206 #endif
207 
208 
const Path & operator+=(const Path &s)
Appends a string.
Definition: CRNPath.cpp:56
void Swap(Path &str) noexcept
Swaps two strings.
Definition: CRNPath.h:126
virtual void Deserialize(xml::Element &el) override
Initializes the object from an XML element.
Definition: CRNPath.cpp:562
Path(std::string &s, Format fmt=Format::LOCAL)
Constructor from a std string.
Definition: CRNPath.h:60
Path(xml::Element &el)
Constructor from an xml element.
Definition: CRNPath.h:77
XML element.
Definition: CRNXml.h:135
Path & operator=(const Path &)=default
Path(char c, size_t n=1, Format fmt=Format::LOCAL)
Constructor from a char.
Definition: CRNPath.h:73
Path & ToUnix()
Converts the path to Unix format.
Definition: CRNPath.cpp:366
StringUTF8 GetScheme() const
Returns the scheme of the URI.
Definition: CRNPath.cpp:303
static char Separator() noexcept
Local directory separator.
Definition: CRNPath.cpp:42
Path(const std::string &s, Format fmt=Format::LOCAL)
Constructor from a std string.
Definition: CRNPath.h:58
Path & Decode()
Replaces % codes with the corresponding character.
Definition: CRNPath.cpp:490
A UTF32 character string class.
Definition: CRNString.h:61
bool IsAbsolute() const
Is the path absolute?
Definition: CRNPath.cpp:173
char GetDrive() const
Gets the drive letter.
Definition: CRNPath.cpp:477
bool IsWindows() const
Is the path in Windows format?
Definition: CRNPath.cpp:135
const Path & operator/=(const Path &s)
Appends a string after adding directory separator if needed.
Definition: CRNPath.cpp:80
size_t Size(const Vector &v) noexcept
Size of a vector.
Definition: CRNVector.h:169
Path(char *s, Format fmt=Format::LOCAL)
Constructor from a cstring.
Definition: CRNPath.h:62
A convenience class for file paths.
Definition: CRNPath.h:39
Path & ToWindows()
Converts the path to Windows format.
Definition: CRNPath.cpp:410
Path GetDirectory() const
Returns the full directory path.
Definition: CRNPath.cpp:267
BoolNotBoolDummy operator+(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
Definition: CRNImage.h:123
static char NoDrive()
Invalid drive or no drive found.
Definition: CRNPath.cpp:34
bool IsURI() const
Is the path a URI?
Definition: CRNPath.cpp:119
Path(Format fmt=Format::LOCAL)
Default constructor (empty string)
Definition: CRNPath.h:56
size_t operator()(const crn::Path &p)
Definition: CRNPath.h:201
Path & ConvertTo(Format fmt)
Converts to a specific format.
Definition: CRNPath.cpp:146
Path & ToURI()
Converts the path to URI.
Definition: CRNPath.cpp:315
void Swap(Map &m1, Map &m2) noexcept
Definition: CRNMap.h:151
std::vector< Path > Split(const StringUTF8 &sep) const
Splits the string in multiple strings delimited by a set of separators.
Definition: CRNPath.cpp:545
Path GetBase() const
Returns the base of the filename.
Definition: CRNPath.cpp:235
void Swap(StringUTF8 &str) noexcept
Swaps two strings.
Path(const StringUTF8 &s, Format fmt=Format::LOCAL)
Constructor from a UTF8 string.
Definition: CRNPath.h:69
BoolNotBoolDummy operator/(const BoolNotBoolDummy &, const BoolNotBoolDummy &)
Definition: CRNImage.h:126
bool IsRelative() const
Is the path relative?
Definition: CRNPath.h:112
std::string & Std()&noexcept
Conversion to std string.
Path & ToLocal()
Converts the path to the local format.
Definition: CRNPath.cpp:534
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Definition: CRNObject.h:173
virtual xml::Element Serialize(xml::Element &parent) const override
Dumps the object to an XML element.
Definition: CRNPath.cpp:583
A character string class.
Definition: CRNStringUTF8.h:49
Path(const Path &s, Format fmt=Format::AUTO)
Copy constructor.
Definition: CRNPath.h:66
virtual ~Path() override=default
bool IsUnix() const
Is the path in Unix format?
Definition: CRNPath.cpp:127
Path GetExtension() const
Returns the extension.
Definition: CRNPath.cpp:252
Path(int i, Format fmt=Format::LOCAL)
Constructor from an int.
Definition: CRNPath.h:75
Format GetFormat() const
Returns the format.
Definition: CRNPath.cpp:105
Path(const String &s, Format fmt=Format::LOCAL)
Constructor from a UTF32 string.
Definition: CRNPath.h:71
Path GetFilename() const
Returns the filename.
Definition: CRNPath.cpp:205
Path(const char *s, Format fmt=Format::LOCAL)
Constructor from a cstring.
Definition: CRNPath.h:64