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.
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);
137 #include <sys/types.h>
140 #include <sys/stat.h>
142 std::vector<DirListNode> GetDirListing(std::string pathstring)
144 std::vector<DirListNode> listing;
148 if((dp = opendir(pathstring.c_str())) == NULL) {
149 //std::cout<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
153 while ((dirp = readdir(dp)) != NULL) {
154 if(dirp->d_name[0]!='.'){
156 node.name = dirp->d_name;
157 if(dirp->d_type == DT_DIR) node.dir = true;
158 else node.dir = false;
159 listing.push_back(node);
167 bool CreateDir(std::string path)
169 int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
176 // If already exists, return true
183 bool PathExists(std::string path)
186 return (stat(path.c_str(),&st) == 0);
191 #include "boost/filesystem/operations.hpp"
192 namespace bfsys = boost::filesystem;
194 std::vector<DirListNode> GetDirListing(std::string pathstring)
196 std::vector<DirListNode> listing;
198 bfsys::path path(pathstring);
200 if( !exists( path ) ) return listing;
202 bfsys::directory_iterator end_itr; // default construction yields past-the-end
203 for( bfsys::directory_iterator itr( path ); itr != end_itr; ++itr ){
205 node.name = itr->leaf();
206 node.dir = is_directory(*itr);
207 listing.push_back(node);
213 bool CreateDir(std::string path)
215 std::cout<<"CreateDir not implemented in boost"<<std::endl;