OpenClonk
C4Reloc.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2011-2016, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #ifndef C4RELOC_H
17 #define C4RELOC_H
18 
19 #include <stack>
20 #include <limits>
21 
22 class C4Reloc
23 {
24 public:
25  enum PathType
26  {
30  };
31  struct PathInfo
32  {
36  bool operator==(const PathInfo&other) {return pathType==other.pathType && strBuf==other.strBuf;}
37  operator const char*() {return strBuf.getData();}
38  };
39  typedef std::vector<PathInfo> PathList;
40 
41  // Provide an own iterator that is able to resolve subdirectories on-the-fly.
43  {
44  public:
45  const_iterator() = default;
46  const_iterator(const const_iterator&) = default;
47  const_iterator(const const_iterator&& other) :
49  pathListIter(std::move(other.pathListIter)),
50  subdirIters(std::move(other.subdirIters))
51  {}
52  ~const_iterator() = default;
53  const_iterator& operator++(); //prefix increment
54  const PathInfo & operator*() const;
55  const PathInfo * operator->() const { return &(**this); }
56 
58  {
59  this->pathListIter = other.pathListIter;
60  this->subdirIters = other.subdirIters;
61  return *this;
62  }
63  friend bool operator==(const const_iterator&, const const_iterator&);
64  friend bool operator!=(const const_iterator&, const const_iterator&);
65 
66  protected:
67  mutable std::unique_ptr<PathInfo> temporaryPathInfo;
68  PathList::const_iterator pathListIter;
69  std::stack<DirectoryIterator> subdirIters;
70 
71  friend class C4Reloc;
72  };
74 
75  // Can also be used for re-init, drops custom paths added with AddPath.
76  // Make sure to call after Config.Load.
77  void Init();
78 
79  bool AddPath(const char* path, PathType pathType = PATH_Regular);
80 
81  iterator begin() const;
82  iterator end() const;
83 
84  bool Open(C4Group& group, const char* filename) const;
85  bool LocateItem(const char* filename, StdStrBuf& str) const;
86 private:
87  PathList Paths;
88 };
89 
90 extern C4Reloc Reloc;
91 
92 #endif // C4RELOC_H
C4Reloc Reloc
Definition: C4Reloc.cpp:21
PathList::const_iterator pathListIter
Definition: C4Reloc.h:68
const_iterator(const const_iterator &&other)
Definition: C4Reloc.h:47
const_iterator & operator++()
Definition: C4Reloc.cpp:79
const PathInfo & operator*() const
Definition: C4Reloc.cpp:119
friend bool operator!=(const const_iterator &, const const_iterator &)
Definition: C4Reloc.cpp:137
const_iterator(const const_iterator &)=default
const PathInfo * operator->() const
Definition: C4Reloc.h:55
std::unique_ptr< PathInfo > temporaryPathInfo
Definition: C4Reloc.h:67
std::stack< DirectoryIterator > subdirIters
Definition: C4Reloc.h:69
const_iterator & operator=(const const_iterator &other)
Definition: C4Reloc.h:57
friend bool operator==(const const_iterator &, const const_iterator &)
Definition: C4Reloc.cpp:130
std::vector< PathInfo > PathList
Definition: C4Reloc.h:39
PathType
Definition: C4Reloc.h:26
@ PATH_Regular
Definition: C4Reloc.h:27
@ PATH_PreferredInstallationLocation
Definition: C4Reloc.h:28
@ PATH_IncludingSubdirectories
Definition: C4Reloc.h:29
bool LocateItem(const char *filename, StdStrBuf &str) const
Definition: C4Reloc.cpp:174
iterator end() const
Definition: C4Reloc.cpp:149
void Init()
Definition: C4Reloc.cpp:23
bool AddPath(const char *path, PathType pathType=PATH_Regular)
Definition: C4Reloc.cpp:62
const_iterator iterator
Definition: C4Reloc.h:73
bool Open(C4Group &group, const char *filename) const
Definition: C4Reloc.cpp:156
iterator begin() const
Definition: C4Reloc.cpp:142
const char * getData() const
Definition: StdBuf.h:442
StdCopyStrBuf strBuf
Definition: C4Reloc.h:33
PathInfo(const StdCopyStrBuf buf, PathType pathType)
Definition: C4Reloc.h:35
PathType pathType
Definition: C4Reloc.h:34
bool operator==(const PathInfo &other)
Definition: C4Reloc.h:36