KSquare Utilities
ImageDirTree.cpp
Go to the documentation of this file.
1 /* ImageDirTree.cpp -- Builds list of Image Files in a Sub-Directory Tree.
2  * Copyright (C) 1994-2011 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #include "FirstIncludes.h"
6 
7 #include <stdio.h>
8 
9 #include <vector>
10 #include <map>
11 #include <algorithm>
12 #include <functional>
13 #include <iostream>
14 
15 #include "MemoryDebug.h"
16 
17 using namespace std;
18 
19 #include "ImageDirTree.h"
20 #include "ImageIO.h"
21 #include "KKBaseTypes.h"
22 #include "KKStr.h"
23 #include "OSservices.h"
24 using namespace KKB;
25 
26 
28  subDir (_subDir)
29 {
30  Load (_subDir);
31 }
32 
33 
34 
36 {
37  kkuint32 x;
38  for (x = 0; x < directories.size (); x++)
39  delete directories[x];
40 }
41 
42 
43 
44 void ImageDirTree::Load (const KKStr& _subDir)
45 {
46  if (!osValidDirectory (_subDir))
47  return;
48 
49  EntryTable::const_iterator existingIdx;
50  DuplicateTable::iterator dupIdx;
51 
52  KKStrPtr dirPath = new KKStr (_subDir);
53  osAddLastSlash (*dirPath);
54  directories.push_back (dirPath);
55 
56  KKStr fileSpec (*dirPath);
57  fileSpec << "*.*";
58 
59  KKStrListPtr files = osGetListOfFiles (fileSpec);
60 
61  if (files)
62  {
63  KKStrPtr fileName = NULL;
64 
65  KKStrList::iterator fIDX;
66 
67  for (fIDX = files->begin (); fIDX != files->end (); ++fIDX)
68  {
69  fileName = *fIDX;
70  if (SupportedImageFileFormat (*fileName))
71  {
72  existingIdx = entries.find (*fileName);
73  if (existingIdx != entries.end ())
74  {
75  dupIdx = duplicates.find (*fileName);
76  if (dupIdx == duplicates.end ())
77  duplicates.insert (pair<KKStr, kkint32> (*fileName, 2));
78  else
79  (dupIdx->second)++;
80  }
81 
82  entries.insert (pair<KKStr, KKStrPtr> (*fileName, dirPath));
83  }
84  }
85 
86  delete files;
87  files = NULL;
88  }
89 
90 
91  KKStrListPtr dirs = osGetListOfDirectories (*dirPath);
92 
93  if (dirs)
94  {
95  KKStrPtr subDirName = NULL;
96  KKStrList::iterator dIDX;
97  for (dIDX = dirs->begin (); dIDX != dirs->end (); ++dIDX)
98  {
99  subDirName = *dIDX;
100  if ((*subDirName == ".") || (*subDirName == ".."))
101  continue;
102 
103  Load (*dirPath + *subDirName);
104  }
105 
106  delete dirs;
107  dirs = NULL;
108  }
109 } /* Load */
110 
111 
112 
113 
114 
115 
116 KKStrConstPtr ImageDirTree::LocateImage (const KKStr& fileName)
117 {
118  EntryTable::const_iterator p;
119 
120  p = entries.find (fileName);
121  if (p == entries.end ())
122  return NULL;
123 
124  if (p->first == fileName)
125  return p->second;
126 
127  return NULL;
128 } /* LocateImage */
129 
130 
131 
132 
133 KKStrListPtr ImageDirTree::Directories (const KKStr& fileName) const
134 {
135  EntryTable::const_iterator p;
136  p = entries.find (fileName);
137  if (p == entries.end ())
138  return NULL;
139 
140  KKStrListPtr dirList = new KKStrList (true);
141  while (p != entries.end ())
142  {
143  if (p->first != fileName)
144  break;
145  dirList->PushOnBack (new KKStr ((*p->second)));
146  }
147 
148  return dirList;
149 } /* Directories */
bool osValidDirectory(const KKStr &_name)
Definition: OSservices.cpp:398
bool operator==(const char *rtStr) const
Definition: KKStr.cpp:1588
Creates a index of all images in a specified directory structure.
Definition: ImageDirTree.h:22
KKStrListPtr osGetListOfFiles(const KKStr &fileSpec)
Returns a list of files that match the provided file specification.
bool SupportedImageFileFormat(const KKStr &imageFileName)
Definition: ImageIO.cpp:948
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
KKTHread * KKTHreadPtr
KKStr(const KKStr &str)
Copy Constructor.
Definition: KKStr.cpp:561
KKStrConstPtr LocateImage(const KKStr &fileName)
Locate image specified by &#39;fileName&#39; and return the directory where it is located.
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
KKStr operator+(const KKStr &right) const
Definition: KKStr.cpp:3998
KKStrListPtr osGetListOfDirectories(KKStr fileSpec)
ImageDirTree(KKStr _subDir)
Construct a Index of images for a specified SubDirectory structure. details Given a specified directo...
KKStrList(bool owner)
Definition: KKStr.cpp:4485
KKStrListPtr Directories(const KKStr &fileName) const
Returns list of directories that &#39;fileName&#39; occurs in.
void osAddLastSlash(KKStr &fileSpec)
Will add the appropriate Directory separator character to the end of fileSpec if one is not there alr...