Antares Simulator
Power System Simulator
list.h
1 /*
2 ** Copyright 2007-2025, RTE (https://www.rte-france.com)
3 ** See AUTHORS.txt
4 ** SPDX-License-Identifier: MPL-2.0
5 ** This file is part of Antares-Simulator,
6 ** Adequacy and Performance assessment for interconnected energy networks.
7 **
8 ** Antares_Simulator is free software: you can redistribute it and/or modify
9 ** it under the terms of the Mozilla Public Licence 2.0 as published by
10 ** the Mozilla Foundation, either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** Antares_Simulator is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** Mozilla Public Licence 2.0 for more details.
17 **
18 ** You should have received a copy of the Mozilla Public Licence 2.0
19 ** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20 */
21 #ifndef __ANTARES_LIBS_PATHS_LIST_H__
22 #define __ANTARES_LIBS_PATHS_LIST_H__
23 
24 #include <cstdio>
25 #include <map>
26 
27 #include <yuni/yuni.h>
28 #include <yuni/core/bind.h>
29 #include <yuni/core/string.h>
30 
31 enum PathListOption
32 {
34  pathListOptNotFound = -1,
35 
37  pathListOptNone = 0,
39  pathListOptMustExist = 1,
41  pathListOptFolder = 2,
42 };
43 
47 class PathList final
48 {
49 public:
50  struct FileInfo
51  {
52  size_t size;
53  enum PathListOption options;
54  };
55 
56  using ItemList = std::map<Yuni::Clob, FileInfo>;
57 
58  using iterator = ItemList::iterator;
59  using const_iterator = ItemList::const_iterator;
60 
61 public:
62  PathList()
63  {
64  }
65 
66  ~PathList()
67  {
68  }
69 
70  iterator begin()
71  {
72  return item.begin();
73  }
74 
75  const_iterator begin() const
76  {
77  return item.begin();
78  }
79 
80  iterator end()
81  {
82  return item.end();
83  }
84 
85  const_iterator end() const
86  {
87  return item.end();
88  }
89 
90  void clear();
91 
92  template<class StringT>
93  void add(const StringT& s)
94  {
95  add(s, pathListOptNone);
96  }
97 
98  template<class StringT>
99  void add(const StringT& s, size_t size)
100  {
101  add(s, pathListOptNone, size);
102  }
103 
104  template<class StringT>
105  void add(const StringT& s, const PathListOption opt)
106  {
107  internalPrepare(s);
108  if (item.find(pTmp) == item.end())
109  {
110  FileInfo& info = item[pTmp];
111  info.options = opt;
112  info.size = 0;
113  }
114  }
115 
116  template<class StringT>
117  void add(const StringT& s, const PathListOption opt, size_t size)
118  {
119  internalPrepare(s);
120  if (item.find(pTmp) == item.end())
121  {
122  FileInfo& info = item[pTmp];
123  info.options = opt;
124  info.size = size;
125  }
126  }
127 
128  template<class StringT>
129  void addFromFolder(const StringT& folder, const PathList& exclude)
130  {
131  Yuni::Clob s;
132  s = folder;
133  internalAddFromFolder(s, exclude);
134  }
135 
136  template<class StringT>
137  PathListOption find(const StringT& s) const
138  {
139  internalPrepare(s);
140  ItemList::const_iterator i = item.find(pTmp);
141  return ((i != item.end()) ? i->second.options : pathListOptNotFound);
142  }
143 
144  template<class StringT>
145  void remove(const StringT& s)
146  {
147  if (!item.empty())
148  {
149  internalPrepare(s);
150  ItemList::iterator i = item.find(pTmp);
151  if (i != item.end())
152  {
153  item.erase(i);
154  }
155  }
156  }
157 
158  void remove(const PathList& toDelete)
159  {
160  if (item.empty() || toDelete.empty())
161  {
162  return;
163  }
164  const ItemList::const_iterator end = toDelete.item.end();
165  for (ItemList::const_iterator i = toDelete.item.begin(); i != end; ++i)
166  {
167  this->remove(i->first);
168  }
169  }
170 
171  uint size() const
172  {
173  return (uint)item.size();
174  }
175 
176  bool empty() const
177  {
178  return item.empty();
179  }
180 
181  size_t totalSizeInBytes() const;
182 
183  template<class StringT>
184  uint deleteAllEmptyFolders(const StringT& sourceFolder)
185  {
186  if (item.empty())
187  {
188  return 0;
189  }
190  pTmp = sourceFolder;
191  return internalDeleteAllEmptyFolders();
192  }
193 
194  template<class StringT>
195  uint deleteAllFiles(const StringT& sourceFolder)
196  {
197  if (item.empty())
198  {
199  return 0;
200  }
201  pTmp = sourceFolder;
202  return internalDeleteAllFiles();
203  }
204 
205 public:
209  std::function<bool(uint)> onProgress;
210 
211 private:
212  template<class StringT>
213  void internalPrepare(const StringT& s) const
214  {
215  pTmp = s;
216  for (uint i = 0; i != pTmp.size(); ++i)
217  {
218 #ifdef YUNI_OS_WINDOWS
219  if (pTmp[i] == '/')
220  {
221  pTmp[i] = '\\';
222  }
223 #else
224  if (pTmp[i] == '\\')
225  {
226  pTmp[i] = '/';
227  }
228 #endif
229  }
230  }
231 
232  size_t internalSizeOnDisk() const;
233  uint internalDeleteAllEmptyFolders();
234  uint internalDeleteAllFiles();
235  void internalAddFromFolder(const Yuni::Clob& folder, const PathList& exclude);
236 
237 private:
238  ItemList item;
239  mutable Yuni::Clob pTmp;
240 
241 }; /* struct PathList */
242 
243 #endif /* __ANTARES_LIBS_PATHS_LIST_H__ */
Path list structure.
Definition: list.h:48
std::function< bool(uint)> onProgress
Event triggered from time to time.
Definition: list.h:209
Definition: list.h:51