KDirStat – a graphical disk usage utility
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.

kdirtreeiterators.h 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * File name: kdirtreeiterators.h
  3. * Summary: Support classes for KDirStat - KDirTree iterators
  4. * License: LGPL - See file COPYING.LIB for details.
  5. * Author: Stefan Hundhammer <sh@suse.de>
  6. *
  7. * Updated: 2003-01-07
  8. */
  9. #ifndef KDirTreeIterators_h
  10. #define KDirTreeIterators_h
  11. #ifdef HAVE_CONFIG_H
  12. # include <config.h>
  13. #endif
  14. #include "kdirtree.h"
  15. namespace KDirStat
  16. {
  17. /**
  18. * Policies how to treat a "dot entry" for iterator objects.
  19. * See @ref KFileInfoIterator for details.
  20. **/
  21. typedef enum
  22. {
  23. KDotEntryTransparent, // Flatten hierarchy - move dot entry children up
  24. KDotEntryAsSubDir, // Treat dot entry as ordinary subdirectory
  25. KDotEntryIgnore // Ignore dot entry and its children completely
  26. } KDotEntryPolicy;
  27. typedef enum
  28. {
  29. KUnsorted,
  30. KSortByName,
  31. KSortByTotalSize,
  32. KSortByLatestMtime
  33. } KFileInfoSortOrder;
  34. // Forward declarations
  35. class KFileInfoList;
  36. /**
  37. * Iterator class for children of a @ref KFileInfo object. For optimum
  38. * performance, this iterator class does NOT return children in any
  39. * specific sort order. If you need that, use @ref KFileInfoSortedIterator
  40. * instead.
  41. *
  42. * Sample usage:
  43. *
  44. * KFileInfoIterator it( node, KDotEntryTransparent );
  45. *
  46. * while ( *it )
  47. * {
  48. * kdDebug() << *it << ":\t" << (*it)->totalSize() ) << endl;
  49. * ++it;
  50. * }
  51. *
  52. * This will output the URL (path+name) and the total size of each (direct)
  53. * subdirectory child and each (direct) file child of 'node'.
  54. * Notice: This does not recurse into subdirectories!
  55. *
  56. * @short (unsorted) iterator for @ref KFileInfo children.
  57. **/
  58. class KFileInfoIterator
  59. {
  60. public:
  61. /**
  62. * Constructor: Initialize an iterator object to iterate over the
  63. * children of 'parent' (unsorted!), depending on 'dotEntryPolicy':
  64. *
  65. * KDotEntryTransparent (default):
  66. *
  67. * Treat the dot entry as if it wasn't there - pretend to move all its
  68. * children up to the real parent. This makes a directory look very
  69. * much like the directory on disk, without the dot entry. 'current()'
  70. * or 'operator*()' will never return the dot entry, but all of its
  71. * children. Subdirectories will be processed before any file children.
  72. *
  73. * KDotEntryIsSubDir:
  74. *
  75. * Treat the dot entry just like any other subdirectory. Don't iterate
  76. * over its children, too (unlike KDotEntryTransparent above).
  77. * 'current()' or 'operator*()' will return the dot entry, but none of
  78. * its children (unless, of course, you create an iterator with the dot
  79. * entry as the parent).
  80. *
  81. * KDotEntryIgnore:
  82. *
  83. * Ignore the dot entry and its children completely. Useful if children
  84. * other than subdirectories are not interesting anyway. 'current()'
  85. * or 'operator*()' will never return the dot entry nor any of its
  86. * children.
  87. *
  88. **/
  89. KFileInfoIterator( KFileInfo * parent,
  90. KDotEntryPolicy dotEntryPolicy = KDotEntryTransparent );
  91. protected:
  92. /**
  93. * Alternate constructor to be called from derived classes: Those can
  94. * choose not to call next() in the constructor.
  95. **/
  96. KFileInfoIterator ( KFileInfo * parent,
  97. KDotEntryPolicy dotEntryPolicy,
  98. bool callNext );
  99. private:
  100. /**
  101. * Internal initialization called from any constructor.
  102. **/
  103. void init ( KFileInfo * parent,
  104. KDotEntryPolicy dotEntryPolicy,
  105. bool callNext );
  106. public:
  107. /**
  108. * Destructor.
  109. **/
  110. virtual ~KFileInfoIterator();
  111. /**
  112. * Return the current child object or 0 if there is no more.
  113. * Same as @ref operator*() .
  114. **/
  115. virtual KFileInfo * current() { return _current; }
  116. /**
  117. * Return the current child object or 0 if there is no more.
  118. * Same as @ref current().
  119. **/
  120. KFileInfo * operator*() { return current(); }
  121. /**
  122. * Advance to the next child. Same as @ref operator++().
  123. **/
  124. virtual void next();
  125. /**
  126. * Advance to the next child. Same as @ref next().
  127. **/
  128. void operator++() { next(); }
  129. /**
  130. * Returns 'true' if this iterator is finished and 'false' if not.
  131. **/
  132. virtual bool finished() { return _current == 0; }
  133. /**
  134. * Check whether or not the current child is a directory, i.e. can be
  135. * cast to @ref KDirInfo * .
  136. **/
  137. bool currentIsDir() { return _current && _current->isDirInfo(); }
  138. /**
  139. * Return the current child object cast to @ref KDirInfo * or 0 if
  140. * there either is no more or it isn't a directory. Check with @ref
  141. * currentIsDir() before using this!
  142. **/
  143. KDirInfo * currentDir() { return currentIsDir() ? (KDirInfo *) _current : 0; }
  144. /**
  145. * Return the number of items that will be processed.
  146. * This is an expensive operation.
  147. **/
  148. int count();
  149. protected:
  150. KFileInfo * _parent;
  151. KDotEntryPolicy _policy;
  152. KFileInfo * _current;
  153. bool _directChildrenProcessed;
  154. bool _dotEntryProcessed;
  155. bool _dotEntryChildrenProcessed;
  156. }; // class KFileInfoIterator
  157. /**
  158. * Iterator class for children of a @ref KFileInfo object. This iterator
  159. * returns children sorted by name: Subdirectories first, then the dot
  160. * entry (if desired - depending on policy), then file children (if
  161. * desired). Note: If you don't need the sorting feature, you might want to
  162. * use @ref KFileItemIterator instead which has better performance.
  163. *
  164. * @short sorted iterator for @ref KFileInfo children.
  165. **/
  166. class KFileInfoSortedIterator: public KFileInfoIterator
  167. {
  168. public:
  169. /**
  170. * Constructor. Specify the sorting order with 'sortOrder' and 'ascending'.
  171. * See @ref KFileInfoIterator for more details.
  172. **/
  173. KFileInfoSortedIterator( KFileInfo * parent,
  174. KDotEntryPolicy dotEntryPolicy = KDotEntryTransparent,
  175. KFileInfoSortOrder sortOrder = KSortByName,
  176. bool ascending = true );
  177. /**
  178. * Destructor.
  179. **/
  180. virtual ~KFileInfoSortedIterator();
  181. /**
  182. * Return the current child object or 0 if there is no more.
  183. *
  184. * Inherited from @ref KFileInfoIterator.
  185. * Overwritten to overcome some shortcomings of C++:
  186. * Virtual methods cannot be used in the constructor.
  187. **/
  188. virtual KFileInfo * current();
  189. /**
  190. * Advance to the next child. Same as @ref operator++().
  191. * Sort by name, sub directories first, then the dot entry (if
  192. * desired), then files (if desired).
  193. *
  194. * Inherited from @ref KFileInfoIterator.
  195. **/
  196. virtual void next();
  197. /**
  198. * Returns 'true' if this iterator is finished and 'false' if not.
  199. *
  200. * Inherited from @ref KFileInfoIterator.
  201. **/
  202. virtual bool finished();
  203. protected:
  204. /**
  205. * Delayed initialization for class parts that rely on availability of
  206. * virtual methods. This is a kludge to overcome a major shortcoming of
  207. * C++: Virtual methods are not available in the constructor yet.
  208. * This is a neverending cause of trouble.
  209. **/
  210. void delayedInit();
  211. /**
  212. * Make a 'default order' children list:
  213. * First all subdirectories sorted by name,
  214. * then the dot entry (depending on policy),
  215. * then the dot entry's children (depending on policy).
  216. **/
  217. virtual void makeDefaultOrderChildrenList();
  218. /**
  219. * Make a sorted children list according to the current sort
  220. * criteria - unless KSortByName is requested, in which case
  221. * makeDefaultOrderChildrenList() above is used.
  222. **/
  223. virtual void makeChildrenList();
  224. // Data members
  225. KFileInfoList * _childrenList;
  226. KFileInfoSortOrder _sortOrder;
  227. bool _ascending;
  228. bool _initComplete;
  229. }; // class KFileInfoSortedIterator
  230. /**
  231. * Specialized KFileInfo iterator that sorts by (total) size, yet
  232. * disregards children below a minimum size. This can considerably improve
  233. * performance if the number of children that need to be sorted decreases
  234. * dramatically.
  235. *
  236. * For example, treemaps can only display a limited portion of large
  237. * directory trees since the number of available pixels is very
  238. * limited. Thus, files (or directories) below a certain size usually don't
  239. * get a individual visual representation anyway, so they may as well be
  240. * omitted right away - no need for expensive list sorting operations.
  241. **/
  242. class KFileInfoSortedBySizeIterator: public KFileInfoSortedIterator
  243. {
  244. public:
  245. /**
  246. * Constructor. Children below 'minSize' will be ignored by this iterator.
  247. **/
  248. KFileInfoSortedBySizeIterator( KFileInfo * parent,
  249. KFileSize minSize = 0,
  250. KDotEntryPolicy dotEntryPolicy = KDotEntryTransparent,
  251. bool ascending = false );
  252. /**
  253. * Destructor.
  254. **/
  255. virtual ~KFileInfoSortedBySizeIterator() {};
  256. protected:
  257. /**
  258. * Create the (sorted) children list. Disregard children below minSize.
  259. * Reimplemented from KFileInfoSortedIterator.
  260. **/
  261. virtual void makeChildrenList();
  262. // Data members
  263. KFileSize _minSize;
  264. }; // class KFileInfoSortedBySizeIterator
  265. /**
  266. * Internal helper class for sorting iterators.
  267. **/
  268. class KFileInfoList: public TQPtrList<KFileInfo>
  269. {
  270. public:
  271. /**
  272. * Constructor.
  273. **/
  274. KFileInfoList( KFileInfoSortOrder sortOrder = KSortByName,
  275. bool ascending = true );
  276. /**
  277. * Destructor.
  278. **/
  279. virtual ~KFileInfoList();
  280. /**
  281. * Returns the sum of all the total sizes in the list.
  282. **/
  283. KFileSize sumTotalSizes();
  284. protected:
  285. /**
  286. * Comparison function. This is why this class is needed at all.
  287. **/
  288. virtual int compareItems( TQPtrCollection::Item it1, TQPtrCollection::Item it2 );
  289. KFileInfoSortOrder _sortOrder;
  290. bool _ascending;
  291. };
  292. typedef TQPtrListIterator<KFileInfo> KFileInfoListIterator;
  293. //----------------------------------------------------------------------
  294. // Static Functions
  295. //----------------------------------------------------------------------
  296. /**
  297. * Generic comparison function as expected by all kinds of sorting etc.
  298. * algorithms. Requires operator<() and operator==() to be defined for this
  299. * class.
  300. **/
  301. template<class T>
  302. inline int compare( T val1, T val2 )
  303. {
  304. if ( val1 < val2 ) return -1;
  305. else if ( val1 == val2 ) return 0;
  306. else return 1;
  307. }
  308. } // namespace KDirStat
  309. #endif // ifndef KDirTreeIterators_h
  310. // EOF