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