Ticket #8727: getName (POSIX).patch
File getName (POSIX).patch, 2.6 KB (added by , 17 years ago) |
---|
-
home/david/Projects/scummvm/backends/fs/posix/posix-fs.cpp
46 46 String _path; 47 47 bool _isDirectory; 48 48 bool _isValid; 49 49 50 50 public: 51 51 /** 52 52 * Creates a POSIXFilesystemNode with the root node as path. … … 62 62 POSIXFilesystemNode(const String &path, bool verify); 63 63 64 64 virtual bool exists() const { return access(_path.c_str(), F_OK) == 0; } 65 virtual String getDisplayName() const { return _displayName; }66 virtual String getName() const { return _displayName; }65 virtual String getDisplayName() const { return getName(); } 66 virtual String getName() const; 67 67 virtual String getPath() const { return _path; } 68 68 virtual bool isDirectory() const { return _isDirectory; } 69 69 virtual bool isReadable() const { return access(_path.c_str(), R_OK) == 0; } … … 80 80 virtual void setFlags(); 81 81 }; 82 82 83 /**84 * Returns the last component of a given path.85 *86 * Examples:87 * /foo/bar.txt would return /bar.txt88 * /foo/bar/ would return /bar/89 *90 * @param str String containing the path.91 * @return Pointer to the first char of the last component inside str.92 */93 const char *lastPathComponent(const Common::String &str) {94 const char *start = str.c_str();95 const char *cur = start + str.size() - 2;96 97 while (cur >= start && *cur != '/') {98 --cur;99 }100 101 return cur + 1;102 }103 104 83 void POSIXFilesystemNode::setFlags() { 105 84 struct stat st; 106 85 … … 141 120 assert(p.size() > 0); 142 121 143 122 _path = p; 144 _displayName = lastPathComponent(_path);123 _displayName = getName(); 145 124 146 125 if (verify) { 147 126 setFlags(); … … 235 214 return true; 236 215 } 237 216 217 /** 218 * Returns the last component of _path. 219 * 220 * Examples: 221 * /foo/bar.txt would return /bar.txt 222 * /foo/bar/ would return /bar/ 223 */ 224 Common::String POSIXFilesystemNode::getName() const { 225 if (_path == "") 226 return ""; 227 228 if (_displayName == "") { 229 const char *start = _path.c_str(); 230 const char *cur = start + _path.size() - 2; 231 232 while (cur >= start && *cur != '/') { 233 --cur; 234 } 235 236 return String(cur + 1); 237 } else { 238 return _displayName; 239 } 240 } 241 238 242 AbstractFilesystemNode *POSIXFilesystemNode::getParent() const { 239 243 if (_path == "/") 240 244 return 0; … … 240 244 return 0; 241 245 242 246 const char *start = _path.c_str(); 243 const char *end = lastPathComponent(_path);247 const char *end = getName().c_str(); 244 248 245 249 return new POSIXFilesystemNode(String(start, end - start), true); 246 250 }