root / trunk / Linux / addons / ofxDirList / src / ofxDirList.cpp @ 58

View | Annotate | Download (3.8 KB)

1
#include "ofxDirList.h"
2
#include <algorithm>
3
#include <string>
4
5
6
// Handy string functions
7
8
static std::string::size_type idx;
9
static string getExt(string filename){
10
    idx = filename.rfind('.');
11
12
    if(idx != std::string::npos){
13
        return filename.substr(idx+1);
14
    }
15
    else{
16
        return "";
17
    }
18
}
19
20
static string strToLower(string myStr){
21
    transform( myStr.begin(), myStr.end(), myStr.begin(), ::tolower );
22
    return myStr;
23
}
24
25
//----------------------------------------------------------
26
ofxDirList::ofxDirList(){
27
    reset();
28
}
29
30
//----------------------------------------------------------
31
void ofxDirList::reset(){
32
        allowedFileExt.clear();
33
        nameArray.clear();
34
        pathArray.clear();
35
}
36
37
//----------------------------------------------------------
38
void ofxDirList::setVerbose(bool _verbose){
39
    ofLog(OF_LOG_WARNING, "ofxDirList setVerbose is depreciated use ofSetLogLevel instead");
40
}
41
42
//----------------------------------------------------------
43
bool ofxDirList::allowExt(string ext){
44
        allowedFileExt.push_back( strToLower(ext) );
45
        return true;
46
}
47
48
//----------------------------------------------------------
49
string ofxDirList::getName(int pos){
50
        if(pos >= (int)nameArray.size()) return 0;
51
        return nameArray[pos];
52
}
53
54
//----------------------------------------------------------
55
string ofxDirList::getPath(int pos){
56
        if(pos >= (int)pathArray.size()) return 0;
57
        return pathArray[pos];
58
}
59
60
//----------------------------------------------------------
61
int ofxDirList::listDir(string directory){
62
    directory = ofToDataPath(directory);
63
64
        nameArray.clear();
65
        pathArray.clear();
66
67
    if(directory.length() <= 0)return 0;
68
69
    //if the trailing slash was not added - then add it
70
        if( directory[directory.length()-1] != '/'){
71
        directory = directory + "/";
72
        }
73
74
        DIR *dir = NULL;
75
        struct dirent *entry;
76
77
    //open the directory
78
    ofLog(OF_LOG_VERBOSE, "ofxDirList - attempting to open %s", directory.c_str());
79
    dir = opendir(directory.c_str());
80
81
        if(dir == NULL){
82
                ofLog(OF_LOG_ERROR, "ofxDirList - error opening directory");
83
                return 0;
84
        }else{
85
                ofLog(OF_LOG_VERBOSE, "ofxDirList - success opening directory");
86
        }
87
88
    string entry_name = "";
89
    string ext = "";
90
        bool skip = false;
91
92
        while ((entry = readdir(dir)) != NULL){
93
94
        //turn it into a C++ string
95
        entry_name = entry->d_name;
96
97
        //lets get the length of the string here as we query it again
98
        int fileLen = entry_name.length();
99
100
                if(fileLen <= 0)continue; //if the name is not existant
101
                if(entry_name[0] == '.')continue; //ignore invisible files, ./ and ../
102
103
                //by default we don't skip files unless we are checking extensions
104
                skip = false;
105
106
                if(allowedFileExt.size() > 0){
107
                        //we will skip this files unless it has an allowed extension
108
                        skip = true;
109
                        for(int i = 0; i < (int)allowedFileExt.size(); i++){
110
111
                                //if the wildecard * has been entered for an ext type then don't check any extensions
112
                                if( allowedFileExt[i] == "*"){ skip = false; break; }
113
114
115
                                int extLen = allowedFileExt[i].length();
116
117
                                //the extension has to be shorter than the filename - simple check
118
                                if(extLen >= fileLen) continue;
119
120
                //lets get the ext as lowercase
121
                ext = strToLower( getExt(entry_name) );
122
123
                //if no ext - then skip this ext check
124
                if( ext == "" )continue;
125
126
                //if we find a match then stop checking and approve this file
127
                if(ext == allowedFileExt[i]){
128
                    skip = false;
129
                    break;
130
                }
131
                        }
132
                }
133
134
                if(skip) continue;
135
136
                //finally we store the result
137
        pathArray.push_back(directory + entry_name);
138
        nameArray.push_back(entry_name);
139
140
                ofLog(OF_LOG_VERBOSE, "ofxDirList - listing %s ", nameArray.back().c_str());
141
        }
142
143
        ofLog(OF_LOG_VERBOSE, "ofxDirList - listed %i files in %s", nameArray.size(), directory.c_str());
144
        return nameArray.size();
145
}