2 * For information about this file see fsal.hh
4 * Copyright (C) 2009 David Kolossa
6 * This file is part of OpenStranded.
8 * OpenStranded is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * OpenStranded is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
37 #include "filesystem.hh"
38 #include "s2string.hh"
39 #include "eal/engine.hh"
47 std::string
FileSystem::modName
= "";
50 FileSystem::setModName(std::string name
)
55 std::vector
<std::string
> *
56 FileSystem::getDirectoryVector(std::string dirPath
)
58 std::vector
<std::string
> *fileNames
= new std::vector
<std::string
>();
60 DIR *dir
= opendir(dirPath
.c_str());
63 std::cerr
<< "Directory " << dirPath
<< "could not be opened";
67 while((file
= readdir(dir
)) != NULL
)
69 if(std::strcmp(file
->d_name
, ".") != 0 &&
70 std::strcmp(file
->d_name
, "..") != 0)
72 fileNames
->push_back(std::string(file
->d_name
));
79 // TODO: Have fun, Windows porters!
85 FileSystem::getFileNameFromDirectoryVector(std::vector
<std::string
> *dirVector
, const std::vector
<std::string
> &extensions
, std::string name
)
87 for(std::vector
<std::string
>::iterator f
= dirVector
->begin(); f
!= dirVector
->end(); f
++)
89 // Split filename into extension and actual filename
90 std::string extension
= f
->substr(f
->find_last_of('.')+1);
91 std::string cutFileName
= f
->substr(0, f
->find_last_of('.'));
93 // Check whether a file with a matching extension fits
94 for(std::vector
<std::string
>::const_iterator e
= extensions
.begin(); e
!= extensions
.end(); e
++)
98 if((cutFileName
+ "." + s2str::toLowercase(extension
)) == (name
+ "." + *e
))
101 if((s2str::toLowercase(cutFileName
) + "." + s2str::toLowercase(extension
)) == (s2str::toLowercase(name
) + "." + s2str::lowercase(*e
)))
118 FileSystem::getUserDirectoryInfo()
120 std::string path
= "";
123 path
+= std::getenv("HOME");
124 path
+= "/.openstranded/";
125 if(opendir(path
.c_str()) == NULL
)
127 int dirCreated
= mkdir(path
.c_str(), S_IRWXU
);
130 // This might be called before the error stream
131 // is created, therefore no Out::error but
133 std::cerr
<< "FATAL: OpenStranded user directory couldn't be created" << std::endl
;
137 return new DirectoryInfo(path
);
139 // TODO: Windows stuff
145 FileSystem::getGameDirectoryInfo()
147 // FIXME: I don't want this hardcoded but I can't get CMake to
148 // deliver me the install path...
149 return new DirectoryInfo("/usr/share/games/openstranded/");
153 FileSystem::getModDirectoryInfo()
155 // FIXME: Same as in getGameDirectoryInfo()
156 return new DirectoryInfo("/usr/share/games/openstranded/mods/" + modName
+ "/");
165 FileSystem::getSaveFileInfo(std::string name
)
169 path
= getUserDirectoryInfo()->path
+ "saves/" + name
;
172 if((f
= std::fopen((path
+".sav").c_str(), "r+")) != NULL
)
175 return new FileInfo(path
, 0);
179 return new FileInfo(path
, errno
);
189 FileSystem::getTextureFileInfo(std::string name
)
191 std::string path
= "";
192 const std::vector
<std::string
> &allowedExtensions
= eal::Engine::get()->getTextureExtensions();
195 // TODO: Map specific lookup
199 DirectoryInfo
* modDir
= getModDirectoryInfo();
200 std::string dirPath
= modDir
->path
+ "gfx/textures/";
206 std::vector
<std::string
> *dirVector
= getDirectoryVector(dirPath
);
207 std::string fileName
= getFileNameFromDirectoryVector(dirVector
, allowedExtensions
, name
);
210 path
= dirPath
+ fileName
;
212 // Does our file actually exist?
215 std::cerr
<< "WARNING: Texture " << fileName
<< " not found" << std::endl
;
216 return new FileInfo(path
, ENOENT
);
219 // Do we have read access to the file?
221 if((f
= std::fopen(path
.c_str(), "r+")) != NULL
)
224 return new FileInfo(path
, 0);
228 return new FileInfo(path
, errno
);
235 // Class DirectoryInfo
237 DirectoryInfo::DirectoryInfo(std::string path
)
246 FileInfo::FileInfo(std::string path
, int error
)
247 : path(path
), error(error
)
253 return this->error
!= ENOENT
;