root / CCV-HAND / addons / ofxDirList / src / ofxDirList.h @ 59

View | Annotate | Download (8.3 KB)

1
#ifndef OFX_DIRLIST
2
#define OFX_DIRLIST
3
4
/************************************************************
5
6
OpenFrameworks Library
7
8
File:                         ofxDirList.h
9
Description:         List the contents of a directory
10
Notes:                        Now takes string arguements and starts at the data/ folder level
11
                Now all C++ strings! No more arrays.
12
13
Last Modified: 2009.03.01
14
Creator: Theodore Watson
15
16
************************************************************/
17
18
19
#include "ofMain.h"
20
21
#ifdef TARGET_WIN32
22
        #include <stdio.h>
23
        #include <iostream>
24
        #include <string.h>
25
#else
26
        #include <dirent.h>
27
#endif
28
29
30
class ofxDirList{
31
32
        public:
33
                ofxDirList();
34
                void setVerbose(bool _verbose);
35
                string getName(int pos);
36
                string getPath(int pos);
37
        void reset();                                                                                                // resets extension list
38
                bool allowExt(string ext);                                                                        // returns true if ext is accepted
39
                int listDir(string directory);                                                                // returns number of files found
40
41
        private:
42
        vector <string> allowedFileExt;
43
        vector <string> nameArray;
44
        vector <string> pathArray;
45
46
};
47
48
49
50
// this is WIN32 dirent included in here for ease of usage
51
// since windows doesn't have dirent by default.  this is just a wrapper....
52
// because we haven't written win32 version of the same code
53
54
//---------------------------------------------------------------------------------------
55
//---------------------------------------------------------------------------------------
56
//---------------------------------------------------------------------------------------
57
58
/*
59
 * dirent.h - dirent API for Microsoft Visual Studio
60
 *
61
 * Copyright (C) 2006 Toni Ronkko
62
 *
63
 * Permission is hereby granted, free of charge, to any person obtaining
64
 * a copy of this software and associated documentation files (the
65
 * ``Software''), to deal in the Software without restriction, including
66
 * without limitation the rights to use, copy, modify, merge, publish,
67
 * distribute, sublicense, and/or sell copies of the Software, and to
68
 * permit persons to whom the Software is furnished to do so, subject to
69
 * the following conditions:
70
 *
71
 * The above copyright notice and this permission notice shall be included
72
 * in all copies or substantial portions of the Software.
73
 *
74
 * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
75
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
76
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
77
 * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
78
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
79
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
80
 * OTHER DEALINGS IN THE SOFTWARE.
81
 */
82
83
#ifdef TARGET_WIN32
84
85
        #include <windows.h>
86
        #include <tchar.h>
87
        #include <string.h>
88
        #include <assert.h>
89
90
91
        typedef struct dirent {
92
          /* name of current directory entry (a multi-byte character string) */
93
          char d_name[MAX_PATH + 1];
94
95
          /* file attributes */
96
          WIN32_FIND_DATA data;
97
        } dirent;
98
99
100
        typedef struct DIR {
101
          /* current directory entry */
102
          dirent current;
103
104
          /* is there an un-processed entry in current? */
105
          int cached;
106
107
          /* file search handle */
108
          HANDLE search_handle;
109
110
          /* search pattern (3 = zero terminator + pattern "\\*") */
111
          TCHAR patt[MAX_PATH + 3];
112
        } DIR;
113
114
115
        static DIR *opendir (const char *dirname);
116
        static struct dirent *readdir (DIR *dirp);
117
        static int closedir (DIR *dirp);
118
119
120
        /* use the new safe string functions introduced in Visual Studio 2005 */
121
        #if defined(_MSC_VER) && _MSC_VER >= 1400
122
        # define STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
123
        #else
124
        # define STRNCPY(dest,src,size) strncpy((dest),(src),(size))
125
        #endif
126
127
128
        /*
129
         * Open directory stream DIRNAME for read and return a pointer to the
130
         * internal working area that is used to retrieve individual directory
131
         * entries.
132
         */
133
        static DIR*
134
        opendir(
135
            const char *dirname)
136
        {
137
          DIR *dirp;
138
          assert (dirname != NULL);
139
          assert (strlen (dirname) < MAX_PATH);
140
141
          /* construct new DIR structure */
142
          dirp = (DIR*) malloc (sizeof (struct DIR));
143
          if (dirp != NULL) {
144
            TCHAR *p;
145
146
            /* prepare search pattern */
147
        #ifdef _UNICODE
148
149
            /* convert directory name to wide character string */
150
            MultiByteToWideChar(
151
                CP_ACP,                                /* code page */
152
                0,                                     /* conversion flags */
153
                dirname,                               /* mb-string to convert */
154
                -1,                                    /* length of mb-string */
155
                dirp->patt,                            /* wc-string to produce */
156
                MAX_PATH);                             /* max length of wc-string */
157
            dirp->patt[MAX_PATH] = '\0';
158
159
            /* append search pattern to directory name */
160
            p = wcschr (dirp->patt, '\0');
161
            if (dirp->patt < p  &&  *(p-1) != '\\'  &&  *(p-1) != ':') {
162
              *p++ = '\\';
163
            }
164
            *p++ = '*';
165
            *p = '\0';
166
167
        #else /* !_UNICODE */
168
169
            /* take directory name... */
170
            STRNCPY (dirp->patt, dirname, sizeof(dirp->patt));
171
            dirp->patt[MAX_PATH] = '\0';
172
173
            /* ... and append search pattern to it */
174
            p = strchr (dirp->patt, '\0');
175
            if (dirp->patt < p  &&  *(p-1) != '\\'  &&  *(p-1) != ':') {
176
              *p++ = '\\';
177
            }
178
            *p++ = '*';
179
            *p = '\0';
180
181
        #endif /* !_UNICODE */
182
183
            /* open stream and retrieve first file */
184
            dirp->search_handle = FindFirstFile (dirp->patt, &dirp->current.data);
185
            if (dirp->search_handle == INVALID_HANDLE_VALUE) {
186
              /* invalid search pattern? */
187
              free (dirp);
188
              return NULL;
189
            }
190
191
            /* there is an un-processed directory entry in memory now */
192
            dirp->cached = 1;
193
194
          }
195
          return dirp;
196
        }
197
198
199
        /*
200
         * Read a directory entry, and return a pointer to a dirent structure
201
         * containing the name of the entry in d_name field.  Individual directory
202
         * entries returned by this very function include regular files,
203
         * sub-directories, pseudo-directories "." and "..", but also volume labels,
204
         * hidden files and system files may be returned.
205
         */
206
        static struct dirent *
207
        readdir(
208
            DIR *dirp)
209
        {
210
          assert (dirp != NULL);
211
212
          if (dirp->search_handle == INVALID_HANDLE_VALUE) {
213
            /* directory stream was opened/rewound incorrectly or it ended normally */
214
            return NULL;
215
          }
216
217
          /* get next directory entry */
218
          if (dirp->cached != 0) {
219
            /* a valid directory entry already in memory */
220
            dirp->cached = 0;
221
          } else {
222
            /* read next directory entry from disk */
223
            if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) {
224
              /* the very last file has been processed or an error occured */
225
              FindClose (dirp->search_handle);
226
              dirp->search_handle = INVALID_HANDLE_VALUE;
227
              return NULL;
228
            }
229
          }
230
231
          /* copy directory entry to d_name */
232
        #ifdef _UNICODE
233
234
          /* convert entry name to multibyte */
235
          WideCharToMultiByte(
236
              CP_ACP,                                  /* code page */
237
              0,                                       /* conversion flags */
238
              dirp->current.data.cFileName,            /* wc-string to convert */
239
              -1,                                      /* length of wc-string */
240
              dirp->current.d_name,                    /* mb-string to produce */
241
              MAX_PATH,                                /* max length of mb-string */
242
              NULL,                                    /* use sys default character */
243
              NULL);                                   /* don't care  */
244
          dirp->current.d_name[MAX_PATH] = '\0';
245
246
        #else /* !_UNICODE */
247
248
          /* copy as a multibyte character string */
249
          STRNCPY (dirp->current.d_name, dirp->current.data.cFileName, sizeof(dirp->current.d_name));
250
          dirp->current.d_name[MAX_PATH] = '\0';
251
252
        #endif /* !_UNICODE */
253
254
          return &dirp->current;
255
        }
256
257
258
        /*
259
         * Close directory stream opened by opendir() function.  Close of the
260
         * directory stream invalidates the DIR structure as well as any previously
261
         * read directory entry.
262
         */
263
        static int
264
        closedir(
265
            DIR *dirp)
266
        {
267
          assert (dirp != NULL);
268
269
          /* release search handle */
270
          if (dirp->search_handle != INVALID_HANDLE_VALUE) {
271
            FindClose (dirp->search_handle);
272
            dirp->search_handle = INVALID_HANDLE_VALUE;
273
          }
274
275
          /* release directory handle */
276
          free (dirp);
277
          return 0;
278
        }
279
280
281
#endif
282
283
//---------------------------------------------------------------------------------------
284
//---------------------------------------------------------------------------------------
285
//---------------------------------------------------------------------------------------
286
287
288
289
290
291
292
293
294
#endif