2002-11-13 17:16:18 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2005-01-01 16:09:25 +00:00
|
|
|
* Copyright (C) 2002-2005 The ScummVM project
|
2002-11-13 17:16:18 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*/
|
|
|
|
|
2005-03-09 22:21:57 +00:00
|
|
|
#if defined(UNIX) || defined(__DC__)
|
2003-11-03 22:20:51 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
2002-11-13 17:16:18 +00:00
|
|
|
|
|
|
|
#include "../fs.h"
|
|
|
|
|
2003-03-26 21:17:14 +00:00
|
|
|
#ifdef MACOSX
|
2002-11-13 17:16:18 +00:00
|
|
|
#include <sys/types.h>
|
2003-03-26 21:17:14 +00:00
|
|
|
#endif
|
2003-03-25 12:14:14 +00:00
|
|
|
#include <sys/param.h>
|
2002-11-15 17:05:50 +00:00
|
|
|
#include <sys/stat.h>
|
2003-11-03 22:20:51 +00:00
|
|
|
#ifndef __DC__
|
2002-11-13 17:16:18 +00:00
|
|
|
#include <dirent.h>
|
2003-11-03 22:20:51 +00:00
|
|
|
#endif
|
2002-11-15 17:54:38 +00:00
|
|
|
#include <stdio.h>
|
2003-03-25 12:14:14 +00:00
|
|
|
#include <unistd.h>
|
2002-11-13 17:16:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of the ScummVM file system API based on POSIX.
|
|
|
|
*/
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
class POSIXFilesystemNode : public AbstractFilesystemNode {
|
2002-11-13 17:16:18 +00:00
|
|
|
protected:
|
|
|
|
String _displayName;
|
|
|
|
bool _isDirectory;
|
|
|
|
bool _isValid;
|
|
|
|
String _path;
|
|
|
|
|
|
|
|
public:
|
|
|
|
POSIXFilesystemNode();
|
|
|
|
POSIXFilesystemNode(const String &path);
|
|
|
|
POSIXFilesystemNode(const POSIXFilesystemNode *node);
|
|
|
|
|
|
|
|
virtual String displayName() const { return _displayName; }
|
|
|
|
virtual bool isValid() const { return _isValid; }
|
|
|
|
virtual bool isDirectory() const { return _isDirectory; }
|
|
|
|
virtual String path() const { return _path; }
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
virtual FSList listDir(ListMode mode = kListDirectoriesOnly) const;
|
|
|
|
virtual AbstractFilesystemNode *parent() const;
|
2002-11-13 17:16:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-10-02 17:43:02 +00:00
|
|
|
static const char *lastPathComponent(const Common::String &str) {
|
2003-03-25 12:14:14 +00:00
|
|
|
const char *start = str.c_str();
|
|
|
|
const char *cur = start + str.size() - 2;
|
|
|
|
|
|
|
|
while (cur > start && *cur != '/') {
|
|
|
|
--cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cur+1;
|
|
|
|
}
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
AbstractFilesystemNode *FilesystemNode::getRoot() {
|
2002-11-13 17:16:18 +00:00
|
|
|
return new POSIXFilesystemNode();
|
|
|
|
}
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
AbstractFilesystemNode *FilesystemNode::getNodeForPath(const String &path) {
|
2004-02-01 01:31:50 +00:00
|
|
|
return new POSIXFilesystemNode(path);
|
|
|
|
}
|
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
POSIXFilesystemNode::POSIXFilesystemNode() {
|
2003-11-03 22:20:51 +00:00
|
|
|
#ifndef __DC__
|
2003-03-25 12:14:14 +00:00
|
|
|
char buf[MAXPATHLEN];
|
2003-03-26 21:17:14 +00:00
|
|
|
getcwd(buf, MAXPATHLEN);
|
2003-03-25 12:14:14 +00:00
|
|
|
|
|
|
|
_path = buf;
|
|
|
|
_displayName = lastPathComponent(_path);
|
|
|
|
_path += '/';
|
|
|
|
#else
|
|
|
|
_path = "/";
|
|
|
|
_displayName = _path;
|
|
|
|
#endif
|
2002-11-13 17:16:18 +00:00
|
|
|
_isValid = true;
|
|
|
|
_isDirectory = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
POSIXFilesystemNode::POSIXFilesystemNode(const String &p) {
|
2004-02-01 01:31:50 +00:00
|
|
|
int len = 0, offset = p.size();
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
assert(offset > 0);
|
|
|
|
|
|
|
|
_path = p;
|
|
|
|
|
|
|
|
// Extract last component from path
|
|
|
|
const char *str = p.c_str();
|
|
|
|
while (offset > 0 && str[offset-1] == '/')
|
|
|
|
offset--;
|
|
|
|
while (offset > 0 && str[offset-1] != '/') {
|
|
|
|
len++;
|
|
|
|
offset--;
|
|
|
|
}
|
|
|
|
_displayName = String(str + offset, len);
|
|
|
|
|
|
|
|
// Check whether it is a directory, and whether the file actually exists
|
|
|
|
#ifdef __DC__
|
2002-11-13 17:16:18 +00:00
|
|
|
_isValid = true;
|
|
|
|
_isDirectory = true;
|
2004-02-01 01:31:50 +00:00
|
|
|
#else
|
|
|
|
_isValid = (0 == stat(_path.c_str(), &st));
|
|
|
|
_isDirectory = S_ISDIR(st.st_mode);
|
|
|
|
#endif
|
2002-11-13 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
POSIXFilesystemNode::POSIXFilesystemNode(const POSIXFilesystemNode *node) {
|
|
|
|
_displayName = node->_displayName;
|
|
|
|
_isValid = node->_isValid;
|
|
|
|
_isDirectory = node->_isDirectory;
|
|
|
|
_path = node->_path;
|
|
|
|
}
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
FSList POSIXFilesystemNode::listDir(ListMode mode) const {
|
2002-11-13 17:16:18 +00:00
|
|
|
assert(_isDirectory);
|
|
|
|
DIR *dirp = opendir(_path.c_str());
|
2002-11-15 17:05:50 +00:00
|
|
|
struct stat st;
|
2002-11-13 17:16:18 +00:00
|
|
|
|
|
|
|
struct dirent *dp;
|
2004-11-20 21:35:49 +00:00
|
|
|
FSList myList;
|
2002-12-27 00:13:51 +00:00
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
if (dirp == NULL)
|
|
|
|
return myList;
|
2004-02-01 01:31:50 +00:00
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
// ... loop over dir entries using readdir
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
2002-11-14 14:42:04 +00:00
|
|
|
// Skip 'invisible' files
|
|
|
|
if (dp->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
POSIXFilesystemNode entry;
|
|
|
|
entry._displayName = dp->d_name;
|
2002-11-15 17:05:50 +00:00
|
|
|
entry._path = _path;
|
2004-02-01 01:31:50 +00:00
|
|
|
if (entry._path.lastChar() != '/')
|
|
|
|
entry._path += '/';
|
2002-11-15 17:05:50 +00:00
|
|
|
entry._path += dp->d_name;
|
|
|
|
|
2003-11-03 22:20:51 +00:00
|
|
|
#ifdef __DC__
|
|
|
|
entry._isDirectory = dp->d_size < 0;
|
|
|
|
#else
|
2002-11-15 17:05:50 +00:00
|
|
|
if (stat(entry._path.c_str(), &st))
|
|
|
|
continue;
|
|
|
|
entry._isDirectory = S_ISDIR(st.st_mode);
|
2003-11-03 22:20:51 +00:00
|
|
|
#endif
|
2002-11-13 17:16:18 +00:00
|
|
|
|
2002-11-19 01:36:47 +00:00
|
|
|
// Honor the chosen mode
|
|
|
|
if ((mode == kListFilesOnly && entry._isDirectory) ||
|
|
|
|
(mode == kListDirectoriesOnly && !entry._isDirectory))
|
|
|
|
continue;
|
2002-11-14 13:46:35 +00:00
|
|
|
|
2002-11-13 17:16:18 +00:00
|
|
|
if (entry._isDirectory)
|
|
|
|
entry._path += "/";
|
2004-11-20 21:35:49 +00:00
|
|
|
myList.push_back(wrap(new POSIXFilesystemNode(&entry)));
|
2002-11-13 17:16:18 +00:00
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
return myList;
|
|
|
|
}
|
|
|
|
|
2004-11-20 21:35:49 +00:00
|
|
|
AbstractFilesystemNode *POSIXFilesystemNode::parent() const {
|
2004-11-21 13:18:07 +00:00
|
|
|
if (_path == "/")
|
|
|
|
return 0;
|
|
|
|
|
2002-11-15 17:54:38 +00:00
|
|
|
POSIXFilesystemNode *p = new POSIXFilesystemNode();
|
2004-11-21 13:18:07 +00:00
|
|
|
const char *start = _path.c_str();
|
|
|
|
const char *end = lastPathComponent(_path);
|
|
|
|
|
|
|
|
p->_path = String(start, end - start);
|
|
|
|
p->_displayName = lastPathComponent(p->_path);
|
2002-11-15 17:54:38 +00:00
|
|
|
|
2003-10-08 22:47:41 +00:00
|
|
|
p->_isValid = true;
|
|
|
|
p->_isDirectory = true;
|
2004-11-21 13:18:07 +00:00
|
|
|
|
2002-11-15 17:54:38 +00:00
|
|
|
return p;
|
2002-11-13 17:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // defined(UNIX)
|