Initial files
[oweals/minetest.git] / src / filesys.cpp
1 #include "filesys.h"
2 #include <iostream>
3
4 namespace fs
5 {
6
7 #ifdef _WIN32
8
9 #define _WIN32_WINNT 0x0501
10 #include <Windows.h>
11 #include <stdio.h>
12 #include <malloc.h>
13 #include <tchar.h> 
14 #include <wchar.h> 
15 #include <stdio.h>
16
17 #define BUFSIZE MAX_PATH
18
19 std::vector<DirListNode> GetDirListing(std::string pathstring)
20 {
21         std::vector<DirListNode> listing;
22
23         WIN32_FIND_DATA FindFileData;
24         HANDLE hFind = INVALID_HANDLE_VALUE;
25         DWORD dwError;
26         LPTSTR DirSpec;
27         INT retval;
28
29         DirSpec = (LPTSTR) malloc (BUFSIZE);
30
31         if( DirSpec == NULL )
32         {
33           printf( "Insufficient memory available\n" );
34           retval = 1;
35           goto Cleanup;
36         }
37
38         // Check that the input is not larger than allowed.
39         if (pathstring.size() > (BUFSIZE - 2))
40         {
41           _tprintf(TEXT("Input directory is too large.\n"));
42           retval = 3;
43           goto Cleanup;
44         }
45
46         //_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
47
48         sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
49
50         // Find the first file in the directory.
51         hFind = FindFirstFile(DirSpec, &FindFileData);
52
53         if (hFind == INVALID_HANDLE_VALUE) 
54         {
55           _tprintf (TEXT("Invalid file handle. Error is %u.\n"), 
56                                 GetLastError());
57           retval = (-1);
58         } 
59         else 
60         {
61                 DirListNode node;
62                 node.name = FindFileData.cFileName;
63                 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
64                 listing.push_back(node);
65
66                 // List all the other files in the directory.
67                 while (FindNextFile(hFind, &FindFileData) != 0) 
68                 {
69                         DirListNode node;
70                         node.name = FindFileData.cFileName;
71                         node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
72                         listing.push_back(node);
73                 }
74
75                 dwError = GetLastError();
76                 FindClose(hFind);
77                 if (dwError != ERROR_NO_MORE_FILES) 
78                 {
79                  _tprintf (TEXT("FindNextFile error. Error is %u.\n"), 
80                                    dwError);
81                 retval = (-1);
82                 goto Cleanup;
83                 }
84         }
85         retval  = 0;
86
87 Cleanup:
88         free(DirSpec);
89
90         if(retval != 0) listing.clear();
91
92         //for(unsigned int i=0; i<listing.size(); i++){
93         //      std::cout<<listing[i].name<<(listing[i].dir?" (dir)":" (file)")<<std::endl;
94         //}
95         
96         return listing;
97 }
98
99 bool CreateDir(std::string path)
100 {
101         bool r = CreateDirectory(path.c_str(), NULL);
102         if(r == true)
103                 return true;
104         if(GetLastError() == ERROR_ALREADY_EXISTS)
105                 return true;
106         return false;
107 }
108
109 bool PathExists(std::string path)
110 {
111         return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
112 }
113
114 #else
115
116 #ifdef linux
117
118 #include <sys/types.h>
119 #include <dirent.h>
120 #include <errno.h>
121 #include <sys/stat.h>
122
123 std::vector<DirListNode> GetDirListing(std::string pathstring)
124 {
125         std::vector<DirListNode> listing;
126
127     DIR *dp;
128     struct dirent *dirp;
129     if((dp  = opendir(pathstring.c_str())) == NULL) {
130                 //std::cout<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
131         return listing;
132     }
133
134     while ((dirp = readdir(dp)) != NULL) {
135                 if(dirp->d_name[0]!='.'){
136                         DirListNode node;
137                         node.name = dirp->d_name;
138                         if(dirp->d_type == DT_DIR) node.dir = true;
139                         else node.dir = false;
140                         listing.push_back(node);
141                 }
142     }
143     closedir(dp);
144
145         return listing;
146 }
147
148 bool CreateDir(std::string path)
149 {
150         int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
151         if(r == 0)
152         {
153                 return true;
154         }
155         else
156         {
157                 // If already exists, return true
158                 if(errno == EEXIST)
159                         return true;
160                 return false;
161         }
162 }
163
164 bool PathExists(std::string path)
165 {
166         struct stat st;
167         return (stat(path.c_str(),&st) == 0);
168 }
169
170 #else
171
172 #include "boost/filesystem/operations.hpp"
173 namespace bfsys = boost::filesystem;
174
175 std::vector<DirListNode> GetDirListing(std::string pathstring)
176 {
177         std::vector<DirListNode> listing;
178
179         bfsys::path path(pathstring);
180
181         if( !exists( path ) ) return listing;
182
183         bfsys::directory_iterator end_itr; // default construction yields past-the-end
184         for( bfsys::directory_iterator itr( path ); itr != end_itr; ++itr ){
185                 DirListNode node;
186                 node.name = itr->leaf();
187                 node.dir = is_directory(*itr);
188                 listing.push_back(node);
189         }
190
191         return listing;
192 }
193
194 bool CreateDir(std::string path)
195 {
196         std::cout<<"CreateDir not implemented in boost"<<std::endl;
197         return false;
198 }
199
200 #endif
201
202 #endif
203
204 }
205