YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
yfilesys.h
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2010 - 2013.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #ifndef YSL_INC_CORE_YFILESYS_H_
29 #define YSL_INC_CORE_YFILESYS_H_ 1
30 
31 #include "ystring.h"
32 #include <iterator>
33 
35 
37 
38 const auto CP_Path(Text::CS_Default);
39 
49 yconstexpr const ucs2_t* FS_Now_X(u".");
50 yconstexpr const ucs2_t* FS_Parent_X(u"..");
52 
53 
59 
60 
65 class YF_API Path : public String
66 {
67 public:
70 // typedef std::codecvt<wchar_t, char, std::mbstate_t> codecvt_type;
71 
73  static const Path Now;
74  static const Path Parent;
75 
76 public:
77  //迭代器。
78  class iterator : public std::iterator<std::bidirectional_iterator_tag, Path>
79  {
80  private:
81  const value_type* ptr;
82  StringType::size_type n;
83 
89  : ptr(), n(StringType::npos)
90  {}
91 
92  public:
96  iterator(const value_type& p)
97  : ptr(&p), n(StringType::npos)
98  {}
102  iterator(const iterator& i)
103  : ptr(i.ptr), n(i.n)
104  {}
105 
109  iterator&
110  operator++();
115  iterator
116  operator++(int)
117  {
118  return ++iterator(*this);
119  }
120 
124  iterator&
125  operator--();
130  iterator
131  operator--(int)
132  {
133  return --iterator(*this);
134  }
135 
139  bool
140  operator==(const iterator& i) const
141  {
142  return ptr == i.ptr && n == i.n;
143  }
144 
148  bool
149  operator!=(const iterator& i) const
150  {
151  return !(*this == i);
152  }
153 
157  value_type
158  operator*() const;
159 
160  DefGetter(const ynothrow, const value_type*, Ptr, ptr)
161  DefGetter(const ynothrow, StringType::size_type, Position, n)
162  };
163 
165 
166  //编码转换。
167 // static std::locale imbue(const std::locale&);
168 // static const codecvt_type& codecvt();
169 
173  inline DefDeCtor(Path)
177  inline DefDeCopyCtor(Path)
181  inline DefDeMoveCtor(Path)
182  Path(const ucs2_t* pathstr)
183  : String(pathstr)
184  {}
185  Path(const NativePathCharType* pathstr)
186  : String(pathstr, CP_Path)
187  {}
188  Path(const NativeString& pathstr)
189  : String(pathstr, CP_Path)
190  {}
191  template<class _tString>
192  Path(const _tString& pathstr)
193  : String(pathstr)
194  {}
195  inline DefDeDtor(Path)
196 
197 
201  inline DefDeCopyAssignment(Path)
206  inline DefDeMoveAssignment(Path)
207 
208  //追加路径。
209  Path&
210  operator/=(const Path&);
211 
212  //查询。
213  DefPred(const ynothrow, Absolute,
214  YSLib::IsAbsolute(GetNativeString().c_str()))
215  DefPred(const ynothrow, Relative, !IsAbsolute())
221  bool
222  IsDirectory() const;
226  bool
227  HasRootName() const
228  {
229  return !GetRootName().empty();
230  }
234  bool
235  HasRootDirectory() const
236  {
237  return !GetRootDirectory().empty();
238  }
242  bool
243  HasRootPath() const
244  {
245  return !GetRootPath().empty();
246  }
250  bool
251  HasRelativePath() const
252  {
253  return !GetRelativePath().empty();
254  }
258  bool
259  HasParentPath() const
260  {
261  return !GetParentPath().empty();
262  }
266  bool
267  HasFilename() const
268  {
269  return !GetFilename().empty();
270  }
274  bool
275  HasStem() const
276  {
277  return !GetStem().empty();
278  }
282  bool
283  HasExtension() const
284  {
285  return !GetExtension().empty();
286  }
287 
288  //路径分解。
292  Path
293  GetRootName() const;
297  Path
298  GetRootDirectory() const;
302  Path
303  GetRootPath() const;
307  Path
308  GetRelativePath() const;
312  Path
313  GetParentPath() const;
317  Path
318  GetFilename() const;
322  Path
323  GetStem() const;
327  Path
328  GetExtension() const;
329  DefGetter(const ynothrow, NativeString, NativeString,
330  GetMBCS(CP_Path))
331 
332  //取迭代器。
336  iterator
337  begin() const
338  {
339  return ++iterator(*this);
340  }
341 
345  iterator
346  end() const
347  {
348  return iterator(*this);
349  }
350 
351  //修改函数。
352 
356  Path&
357  MakeAbsolute(const Path&);
366  bool
367  NormalizeTrailingSlash();
371  Path&
372  RemoveFilename();
376  Path&
377  ReplaceExtension(const Path& = {});
378 };
379 
380 inline bool
381 operator==(const Path& x, const Path& y)
382 {
383  return x.GetNativeString() == y.GetNativeString();
384 }
385 inline bool
386 operator!=(const Path& x, const Path& y)
387 {
388  return !(x == y);
389 }
390 inline bool
391 operator<(const Path& x, const Path& y)
392 {
393  return x.GetNativeString() < y.GetNativeString();
394 }
395 inline bool
396 operator<=(const Path& x, const Path& y)
397 {
398  return !(y < x);
399 }
400 inline bool operator>(const Path& x, const Path& y)
401 {
402  return y < x;
403 }
404 inline bool
405 operator>=(const Path& x, const Path& y)
406 {
407  return !(x < y);
408 }
409 
410 inline Path
411 operator/(const Path& x, const Path& y)
412 {
413  return Path(x) /= y;
414 }
415 
419 inline void
420 swap(Path& x, Path& y)
421 {
422  x.swap(y);
423 }
424 
425 #if 0
426 bool lexicographical_compare(Path::iterator, Path::iterator,
427  Path::iterator, Path::iterator);
428 #endif
429 
430 
434 YF_API const char*
439 YF_API string
440 GetFileNameOf(const string&);
441 
445 YF_API string
446 GetDirectoryNameOf(const string&);
447 
451 YF_API string::size_type
452 SplitPath(const string&, string&, string&);
453 
454 
459 YF_API string
460 GetStemOf(const string&);
461 
465 YF_API bool
466 IsStemOf(const char*, const char*);
470 YF_API bool
471 IsStemOf(const string&, const string&);
472 
477 YF_API bool
478 HaveSameStems(const char*, const char*);
483 YF_API bool
484 HaveSameStems(const string&, const string&);
485 
490 YF_API const char*
491 GetExtensionOf(const char*);
496 YF_API string
497 GetExtensionOf(const string&);
498 
502 YF_API bool
503 IsExtensionOf(const char*, const char*);
507 YF_API bool
508 IsExtensionOf(const string&, const string&);
509 
514 YF_API bool
515 HaveSameExtensions(const char*, const char*);
520 YF_API bool
521 HaveSameExtensions(const string&, const string&);
522 
523 
527 inline int
529 {
530  return uchdir(path);
531 }
535 YF_API int
536 ChangeDirectory(const string&);
537 
543 YF_API String
545 
550 YF_API bool
551 ValidatePath(const string&);
556 inline bool
557 ValidatePath(const Path& path)
558 {
559  return ValidatePath(path.GetNativeString());
560 }
561 
562 
563 // \brief 文件名过滤器。
564 // TODO: Definition & impl.
565 
566 
569 {
570 public:
571  typedef String ItemType;
572  typedef vector<ItemType> ListType;
573 
574 protected:
576  shared_ptr<ListType> hList;
577 
578 public:
583  FileList(const_path_t = {});
588  FileList(const string&);
593  FileList(const ItemType&);
595 
596 
601  bool
602  operator=(const Path&);
608  bool
609  operator/=(const Path&);
610 
611  DefGetter(const ynothrow, const Path&, Directory, Directory) \
613  DefGetter(const ynothrow, shared_ptr<ListType>, ListPtr, hList) \
615 // DefGetter(const ynothrow, const ListType&, List, List) //!< 取项目列表。
616 
620  ListType::size_type
621  ListItems();
622 };
623 
625 
626 YSL_END
627 
628 #endif
629