3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #ifdef _WIN32 // WINDOWS
28 #define _WIN32_WINNT 0x0501
36 #define BUFSIZE MAX_PATH
38 std::vector<DirListNode> GetDirListing(std::string pathstring)
40 std::vector<DirListNode> listing;
42 WIN32_FIND_DATA FindFileData;
43 HANDLE hFind = INVALID_HANDLE_VALUE;
48 DirSpec = (LPTSTR) malloc (BUFSIZE);
52 printf( "Insufficient memory available\n" );
57 // Check that the input is not larger than allowed.
58 if (pathstring.size() > (BUFSIZE - 2))
60 _tprintf(TEXT("Input directory is too large.\n"));
65 //_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
67 sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
69 // Find the first file in the directory.
70 hFind = FindFirstFile(DirSpec, &FindFileData);
72 if (hFind == INVALID_HANDLE_VALUE)
74 _tprintf (TEXT("Invalid file handle. Error is %u.\n"),
81 node.name = FindFileData.cFileName;
82 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
83 listing.push_back(node);
85 // List all the other files in the directory.
86 while (FindNextFile(hFind, &FindFileData) != 0)
89 node.name = FindFileData.cFileName;
90 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
91 listing.push_back(node);
94 dwError = GetLastError();
96 if (dwError != ERROR_NO_MORE_FILES)
98 _tprintf (TEXT("FindNextFile error. Error is %u.\n"),
109 if(retval != 0) listing.clear();
111 //for(unsigned int i=0; i<listing.size(); i++){
112 // std::cout<<listing[i].name<<(listing[i].dir?" (dir)":" (file)")<<std::endl;
118 bool CreateDir(std::string path)
120 bool r = CreateDirectory(path.c_str(), NULL);
123 if(GetLastError() == ERROR_ALREADY_EXISTS)
128 bool PathExists(std::string path)
130 return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
135 #include <sys/types.h>
138 #include <sys/stat.h>
140 std::vector<DirListNode> GetDirListing(std::string pathstring)
142 std::vector<DirListNode> listing;
146 if((dp = opendir(pathstring.c_str())) == NULL) {
147 //std::cout<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
151 while ((dirp = readdir(dp)) != NULL) {
152 if(dirp->d_name[0]!='.'){
154 node.name = dirp->d_name;
155 if(dirp->d_type == DT_DIR) node.dir = true;
156 else node.dir = false;
157 listing.push_back(node);
165 bool CreateDir(std::string path)
167 int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
174 // If already exists, return true
181 bool PathExists(std::string path)
184 return (stat(path.c_str(),&st) == 0);