Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
8.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. /*
  9. *
  10. * Copyright (c) 1998-9
  11. * Dr John Maddock
  12. *
  13. * Permission to use, copy, modify, distribute and sell this software
  14. * and its documentation for any purpose is hereby granted without fee,
  15. * provided that the above copyright notice appear in all copies and
  16. * that both that copyright notice and this permission notice appear
  17. * in supporting documentation. Dr John Maddock makes no representations
  18. * about the suitability of this software for any purpose.
  19. * It is provided "as is" without express or implied warranty.
  20. *
  21. */
  22. /*
  23. *
  24. * FILE fileiter.h
  25. * VERSION 2.12
  26. *
  27. * this file declares various platform independent file and directory
  28. * iterators, plus binary file input in the form of class map_file.
  29. *
  30. */
  31. #ifndef __FILEITER_H
  32. #define __FILEITER_H
  33. #include <jm/jm_cfg.h>
  34. #if (defined(__WIN32__) || defined(_WIN32) || defined(WIN32)) && !defined(JM_NO_WIN32)
  35. #define FI_W32
  36. #include <windows.h>
  37. JM_NAMESPACE(__JM)
  38. typedef WIN32_FIND_DATA _fi_find_data;
  39. typedef HANDLE _fi_find_handle;
  40. JM_END_NAMESPACE
  41. #define _fi_invalid_handle INVALID_HANDLE_VALUE
  42. #define _fi_dir FILE_ATTRIBUTE_DIRECTORY
  43. #else
  44. #include <stdio.h>
  45. #include <ctype.h>
  46. #ifndef JM_NO_STL
  47. #include <iterator>
  48. #include <list>
  49. #if defined(__SUNPRO_CC) && !defined(JM_NO_NAMESPACES)
  50. using __JM_STD::list;
  51. #endif
  52. #endif
  53. #include <assert.h>
  54. #include <dirent.h>
  55. #ifndef MAX_PATH
  56. #define MAX_PATH 256
  57. #endif
  58. JM_NAMESPACE(__JM)
  59. struct _fi_find_data
  60. {
  61. unsigned dwFileAttributes;
  62. char cFileName[MAX_PATH];
  63. };
  64. struct _fi_priv_data;
  65. typedef _fi_priv_data* _fi_find_handle;
  66. #define _fi_invalid_handle NULL
  67. #define _fi_dir 1
  68. _fi_find_handle _fi_FindFirstFile(const char* lpFileName, _fi_find_data* lpFindFileData);
  69. bool _fi_FindNextFile(_fi_find_handle hFindFile, _fi_find_data* lpFindFileData);
  70. bool _fi_FindClose(_fi_find_handle hFindFile);
  71. JM_END_NAMESPACE
  72. #ifdef FindFirstFile
  73. #undef FindFirstFile
  74. #endif
  75. #ifdef FindNextFile
  76. #undef FindNextFile
  77. #endif
  78. #ifdef FindClose
  79. #undef FindClose
  80. #endif
  81. #define FindFirstFile _fi_FindFirstFile
  82. #define FindNextFile _fi_FindNextFile
  83. #define FindClose _fi_FindClose
  84. #endif
  85. JM_NAMESPACE(__JM)
  86. #ifdef FI_W32 // win32 mapfile
  87. class JM_IX_DECL mapfile
  88. {
  89. HANDLE hfile;
  90. HANDLE hmap;
  91. const char* _first;
  92. const char* _last;
  93. public:
  94. typedef const char* iterator;
  95. mapfile(){ hfile = hmap = 0; _first = _last = 0; }
  96. mapfile(const char* file){ hfile = hmap = 0; _first = _last = 0; open(file); }
  97. ~mapfile(){ close(); }
  98. void open(const char* file);
  99. void close();
  100. const char* begin(){ return _first; }
  101. const char* end(){ return _last; }
  102. size_t size(){ return _last - _first; }
  103. bool valid(){ return (hfile != 0) && (hfile != INVALID_HANDLE_VALUE); }
  104. };
  105. #elif !defined(JM_NO_STL) // use POSIX API to emulate the memory map:
  106. class JM_IX_DECL mapfile_iterator;
  107. class JM_IX_DECL mapfile
  108. {
  109. typedef char* pointer;
  110. FILE* hfile;
  111. long int _size;
  112. pointer* _first;
  113. pointer* _last;
  114. mutable __JM_STD::list<pointer*> condemed;
  115. enum sizes
  116. {
  117. buf_size = 4096
  118. };
  119. void lock(pointer* node)const;
  120. void unlock(pointer* node)const;
  121. public:
  122. typedef mapfile_iterator iterator;
  123. mapfile(){ hfile = 0; _size = 0; _first = _last = 0; }
  124. mapfile(const char* file){ hfile = 0; _size = 0; _first = _last = 0; open(file); }
  125. ~mapfile(){ close(); }
  126. void open(const char* file);
  127. void close();
  128. iterator begin()const;
  129. iterator end()const;
  130. unsigned long size()const{ return _size; }
  131. bool valid()const{ return hfile != 0; }
  132. friend class mapfile_iterator;
  133. };
  134. class JM_IX_DECL mapfile_iterator : public JM_RA_ITERATOR(char, long)
  135. {
  136. typedef mapfile::pointer pointer;
  137. pointer* node;
  138. const mapfile* file;
  139. unsigned long offset;
  140. long position()const
  141. {
  142. return file ? ((node - file->_first) * mapfile::buf_size + offset) : 0;
  143. }
  144. void position(long pos)
  145. {
  146. if(file)
  147. {
  148. node = file->_first + (pos / mapfile::buf_size);
  149. offset = pos % mapfile::buf_size;
  150. }
  151. }
  152. public:
  153. mapfile_iterator() { node = 0; file = 0; offset = 0; }
  154. mapfile_iterator(const mapfile* f, long position)
  155. {
  156. file = f;
  157. node = f->_first + position / mapfile::buf_size;
  158. offset = position % mapfile::buf_size;
  159. if(file)
  160. file->lock(node);
  161. }
  162. mapfile_iterator(const mapfile_iterator& i)
  163. {
  164. file = i.file;
  165. node = i.node;
  166. offset = i.offset;
  167. if(file)
  168. file->lock(node);
  169. }
  170. ~mapfile_iterator()
  171. {
  172. if(file && node)
  173. file->unlock(node);
  174. }
  175. mapfile_iterator& operator = (const mapfile_iterator& i);
  176. char operator* ()const
  177. {
  178. assert(node >= file->_first);
  179. assert(node < file->_last);
  180. return file ? *(*node + sizeof(int) + offset) : char(0);
  181. }
  182. mapfile_iterator& operator++ ();
  183. mapfile_iterator operator++ (int);
  184. mapfile_iterator& operator-- ();
  185. mapfile_iterator operator-- (int);
  186. mapfile_iterator& operator += (long off)
  187. {
  188. position(position() + off);
  189. return *this;
  190. }
  191. mapfile_iterator& operator -= (long off)
  192. {
  193. position(position() - off);
  194. return *this;
  195. }
  196. friend inline bool operator==(const mapfile_iterator& i, const mapfile_iterator& j)
  197. {
  198. return (i.file == j.file) && (i.node == j.node) && (i.offset == j.offset);
  199. }
  200. #ifndef JM_NO_NOT_EQUAL
  201. friend inline bool operator!=(const mapfile_iterator& i, const mapfile_iterator& j)
  202. {
  203. return !(i == j);
  204. }
  205. #endif
  206. friend inline bool operator<(const mapfile_iterator& i, const mapfile_iterator& j)
  207. {
  208. return i.position() < j.position();
  209. }
  210. friend mapfile_iterator operator + (const mapfile_iterator& i, long off);
  211. friend mapfile_iterator operator - (const mapfile_iterator& i, long off);
  212. friend inline long operator - (const mapfile_iterator& i, const mapfile_iterator& j)
  213. {
  214. return i.position() - j.position();
  215. }
  216. };
  217. #endif
  218. // _fi_sep determines the directory separator, either '\\' or '/'
  219. JM_IX_DECL extern const char* _fi_sep;
  220. struct file_iterator_ref
  221. {
  222. _fi_find_handle hf;
  223. _fi_find_data _data;
  224. long count;
  225. };
  226. class JM_IX_DECL file_iterator : public JM_INPUT_ITERATOR(const char*, __JM_STDC::ptrdiff_t)
  227. {
  228. char* _root;
  229. char* _path;
  230. char* ptr;
  231. file_iterator_ref* ref;
  232. public:
  233. file_iterator();
  234. file_iterator(const char* wild);
  235. ~file_iterator();
  236. file_iterator(const file_iterator&);
  237. file_iterator& operator=(const file_iterator&);
  238. const char* root() { return _root; }
  239. const char* path() { return _path; }
  240. _fi_find_data* data() { return &(ref->_data); }
  241. void next();
  242. file_iterator& operator++() { next(); return *this; }
  243. file_iterator operator++(int);
  244. const char* operator*() { return path(); }
  245. friend inline bool operator == (const file_iterator& f1, const file_iterator& f2)
  246. {
  247. return ((f1.ref->hf == _fi_invalid_handle) && (f1.ref->hf == _fi_invalid_handle));
  248. }
  249. #ifndef JM_NO_NOT_EQUAL
  250. friend inline bool operator != (const file_iterator& f1, const file_iterator& f2)
  251. {
  252. return !(f1 == f2);
  253. }
  254. #endif
  255. };
  256. inline bool operator < (const file_iterator& f1, const file_iterator& f2)
  257. {
  258. return false;
  259. }
  260. class JM_IX_DECL directory_iterator : public JM_INPUT_ITERATOR(const char*, __JM_STDC::ptrdiff_t)
  261. {
  262. char* _root;
  263. char* _path;
  264. char* ptr;
  265. file_iterator_ref* ref;
  266. public:
  267. directory_iterator();
  268. directory_iterator(const char* wild);
  269. ~directory_iterator();
  270. directory_iterator(const directory_iterator& other);
  271. directory_iterator& operator=(const directory_iterator& other);
  272. const char* root() { return _root; }
  273. const char* path() { return _path; }
  274. _fi_find_data* data() { return &(ref->_data); }
  275. void next();
  276. directory_iterator& operator++() { next(); return *this; }
  277. directory_iterator operator++(int);
  278. const char* operator*() { return path(); }
  279. static const char* separator() { return _fi_sep; }
  280. friend inline bool operator == (const directory_iterator& f1, const directory_iterator& f2)
  281. {
  282. return ((f1.ref->hf == _fi_invalid_handle) && (f1.ref->hf == _fi_invalid_handle));
  283. }
  284. #ifndef JM_NO_NOT_EQUAL
  285. friend inline bool operator != (const directory_iterator& f1, const directory_iterator& f2)
  286. {
  287. return !(f1 == f2);
  288. }
  289. #endif
  290. };
  291. inline bool operator < (const directory_iterator& f1, const directory_iterator& f2)
  292. {
  293. return false;
  294. }
  295. JM_END_NAMESPACE
  296. #if !defined(JM_NO_NAMESPACES) && !defined(JM_NO_USING)
  297. using __JM::directory_iterator;
  298. using __JM::file_iterator;
  299. using __JM::mapfile;
  300. #endif
  301. #endif // __WINITER_H