[7609] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Benjamin Grauer |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[7611] | 16 | #include "file.h" |
---|
[7609] | 17 | |
---|
[7615] | 18 | #include <sys/types.h> |
---|
| 19 | #include <sys/stat.h> |
---|
[7616] | 20 | #include <stdio.h> |
---|
[7615] | 21 | |
---|
[7616] | 22 | #include <iostream> |
---|
| 23 | #include <fstream> |
---|
| 24 | |
---|
[8333] | 25 | #ifdef __unix__ |
---|
[7611] | 26 | #include <unistd.h> |
---|
| 27 | #elif __WIN32__ || _MS_DOS_ |
---|
| 28 | #include <dir.h> |
---|
| 29 | #else |
---|
[8293] | 30 | //#include <direct.h> |
---|
[7611] | 31 | #endif |
---|
| 32 | |
---|
[7623] | 33 | /** |
---|
| 34 | * @brief default constructor. |
---|
| 35 | */ |
---|
[7621] | 36 | File::File() |
---|
| 37 | { |
---|
| 38 | this->init(); |
---|
| 39 | } |
---|
[7611] | 40 | |
---|
[7623] | 41 | /** |
---|
| 42 | * @brief A File will be constructed and stated from a given FileName. |
---|
| 43 | * @param fileName the FileName to load and stat. |
---|
| 44 | */ |
---|
[7611] | 45 | File::File(const std::string& fileName) |
---|
| 46 | { |
---|
[7616] | 47 | this->init(); |
---|
[7621] | 48 | this->setFileName(fileName); |
---|
[7611] | 49 | } |
---|
| 50 | |
---|
[7623] | 51 | /** |
---|
| 52 | * @brief A File will be constructed and stated from a given other File. |
---|
| 53 | * @param file the File to get the Name from. |
---|
| 54 | */ |
---|
[7611] | 55 | File::File(const File& file) |
---|
| 56 | { |
---|
[7621] | 57 | this->init(); |
---|
| 58 | this->setFileName(file.name()); |
---|
[7611] | 59 | } |
---|
| 60 | |
---|
| 61 | File::~File() |
---|
| 62 | { |
---|
[7621] | 63 | this->close(); |
---|
| 64 | |
---|
[7616] | 65 | if (this->_status) |
---|
| 66 | delete this->_status; |
---|
[7611] | 67 | } |
---|
| 68 | |
---|
[7621] | 69 | |
---|
[7616] | 70 | /** |
---|
| 71 | * @brief initializes the File |
---|
| 72 | * |
---|
| 73 | * Stats the File, looks if it exists and sets the hadle to 0 |
---|
| 74 | */ |
---|
| 75 | void File::init() |
---|
| 76 | { |
---|
| 77 | this->_handle = 0; |
---|
[7621] | 78 | this->_status = NULL; |
---|
| 79 | } |
---|
[7616] | 80 | |
---|
[7621] | 81 | |
---|
| 82 | /** |
---|
| 83 | * @brief sets a new File to apply to this File. |
---|
| 84 | * @param fileName the Filename of the File to access. |
---|
| 85 | */ |
---|
| 86 | void File::setFileName(const std::string& fileName) |
---|
| 87 | { |
---|
| 88 | this->close(); |
---|
| 89 | this->_name = fileName; |
---|
| 90 | File::homeDirCheck(this->_name); |
---|
| 91 | this->statFile(); |
---|
| 92 | } |
---|
| 93 | |
---|
[7622] | 94 | /** |
---|
| 95 | * @brief sets the file to the new File. |
---|
| 96 | * @param fileName the FileName to set the File to. |
---|
| 97 | * @returns this File. |
---|
| 98 | */ |
---|
| 99 | File& File::operator=(const std::string& fileName) |
---|
| 100 | { |
---|
| 101 | this->setFileName(fileName); |
---|
| 102 | return *this; |
---|
| 103 | } |
---|
[7621] | 104 | |
---|
[7622] | 105 | /** |
---|
| 106 | * @brief sets the file to the new File. |
---|
| 107 | * @param file the File to set the File to. |
---|
| 108 | * @returns this File. |
---|
| 109 | */ |
---|
| 110 | File& File::operator=(const File& file) |
---|
| 111 | { |
---|
| 112 | this->setFileName(file.name()); |
---|
| 113 | return *this; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * @brief compares two files. |
---|
| 118 | * @param fileName the File to compare against the stored one. |
---|
| 119 | * @returns true if the filenames match. |
---|
| 120 | */ |
---|
| 121 | bool File::operator==(const std::string& fileName) const |
---|
| 122 | { |
---|
| 123 | return (this->_name == fileName); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * @brief compares two files. |
---|
| 128 | * @param file the File to compare against the stored one. |
---|
| 129 | * @returns true if the filenames match. |
---|
| 130 | */ |
---|
| 131 | bool File::operator==(const File& file) const |
---|
| 132 | { |
---|
| 133 | return (this->_name == file.name()); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * @brief stats a File. |
---|
| 139 | * Gathers information about the File, like permissions, and if it exists. |
---|
| 140 | */ |
---|
[7621] | 141 | void File::statFile() |
---|
| 142 | { |
---|
| 143 | if (this->_status == NULL) |
---|
| 144 | this->_status = new struct stat; |
---|
[7618] | 145 | // Check the End of the FileName and chop away any \ and // |
---|
[7673] | 146 | std::string name = this->_name; |
---|
| 147 | while (name[name.size()-1] == '/' || |
---|
| 148 | name[name.size()-1] == '\\') |
---|
| 149 | name.resize(name.size()-1); |
---|
| 150 | if (stat(name.c_str(), this->_status)) |
---|
[7616] | 151 | { |
---|
| 152 | delete this->_status; |
---|
| 153 | this->_status = NULL; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
[9324] | 157 | bool File::open( OpenMode mode ) |
---|
[7611] | 158 | { |
---|
[9324] | 159 | static const char* openModeStrings[] = { |
---|
| 160 | "r", "w", "r+", "a+" }; |
---|
| 161 | _mode = mode; |
---|
| 162 | _handle = fopen( name().c_str(), openModeStrings[mode] ); |
---|
| 163 | return ( _handle != NULL ); |
---|
[7611] | 164 | } |
---|
[7615] | 165 | |
---|
[7611] | 166 | bool File::close() |
---|
| 167 | { |
---|
[9324] | 168 | if ( _handle != NULL ) { |
---|
| 169 | int success = fclose( _handle ); |
---|
| 170 | _handle = NULL; |
---|
| 171 | return ( success == 0 ); |
---|
| 172 | } |
---|
| 173 | return false; |
---|
[7611] | 174 | } |
---|
[7615] | 175 | |
---|
[7620] | 176 | bool File::exists() const |
---|
[7611] | 177 | { |
---|
[7616] | 178 | return (this->_status != NULL); |
---|
[7611] | 179 | } |
---|
[7615] | 180 | |
---|
[7621] | 181 | /** |
---|
| 182 | * @brief checks if the file is a Link (symlink/hardlink on UNIX) |
---|
| 183 | * @returns true if the File is a Link, false otherwise (on windows always false) |
---|
| 184 | */ |
---|
[7620] | 185 | bool File::isLink() const |
---|
[7611] | 186 | { |
---|
[7616] | 187 | #ifndef __WIN32__ |
---|
| 188 | return (this->_status != NULL && this->_status->st_mode & (S_IFLNK)); |
---|
| 189 | #else |
---|
| 190 | return false; |
---|
| 191 | #endif |
---|
[7615] | 192 | } |
---|
[7616] | 193 | |
---|
[7621] | 194 | /** |
---|
| 195 | * @brief checks if the File is a regular File |
---|
| 196 | * @returns true if the File is a Regular file. |
---|
| 197 | */ |
---|
[7620] | 198 | bool File::isFile() const |
---|
[7611] | 199 | { |
---|
[7616] | 200 | return (this->_status != NULL && this->_status->st_mode & (S_IFREG)); |
---|
[7611] | 201 | } |
---|
[7615] | 202 | |
---|
[7616] | 203 | /** |
---|
[7621] | 204 | * @brief Checks if the File is a Directory |
---|
[7616] | 205 | * @returns true if it is a directory/symlink false otherwise |
---|
| 206 | */ |
---|
[7620] | 207 | bool File::isDirectory() const |
---|
[7611] | 208 | { |
---|
[7616] | 209 | return (this->_status != NULL && this->_status->st_mode & (S_IFDIR)); |
---|
[7611] | 210 | } |
---|
[7612] | 211 | |
---|
[7616] | 212 | |
---|
| 213 | /// FIXME NEXT THREE FUNCTIONS |
---|
[8619] | 214 | bool File::isReadable() const |
---|
[7611] | 215 | { |
---|
[7616] | 216 | #ifndef __WIN32__ |
---|
| 217 | return (this->_status != NULL && this->_status->st_mode & (S_IRUSR)); |
---|
| 218 | #else |
---|
| 219 | return (this->_status != NULL); |
---|
| 220 | #endif |
---|
[7611] | 221 | } |
---|
[7620] | 222 | bool File::isWriteable() const |
---|
[7611] | 223 | { |
---|
[7616] | 224 | #ifndef __WIN32__ |
---|
| 225 | return (this->_status != NULL && this->_status->st_mode & (S_IWUSR)); |
---|
| 226 | #else |
---|
| 227 | return (this->_status != NULL); |
---|
| 228 | #endif |
---|
[7611] | 229 | } |
---|
[7620] | 230 | bool File::isExecutable() const |
---|
[7611] | 231 | { |
---|
[7616] | 232 | #ifndef __WIN32__ |
---|
| 233 | return (this->_status != NULL && this->_status->st_mode & (S_IXUSR)); |
---|
| 234 | #else |
---|
| 235 | return (this->_status != NULL); |
---|
| 236 | #endif |
---|
[7611] | 237 | } |
---|
| 238 | |
---|
[7621] | 239 | /** |
---|
| 240 | * @brief copies the File to another File. |
---|
| 241 | * @param destination the Destination File. |
---|
| 242 | * @returns true on success, false otherwise. |
---|
| 243 | */ |
---|
[7611] | 244 | bool File::copy(const File& destination) |
---|
| 245 | { |
---|
[7623] | 246 | if (*this == destination) |
---|
| 247 | { |
---|
| 248 | std::cout << "files are the Same '" << this->_name << "'\n"; |
---|
| 249 | return false; |
---|
| 250 | } |
---|
[7616] | 251 | char ch; |
---|
| 252 | std::ifstream iFile(this->_name.c_str()); |
---|
| 253 | std::ofstream oFile(destination.name().c_str()); |
---|
| 254 | while (iFile.get(ch)) |
---|
| 255 | { |
---|
| 256 | oFile.put(ch); |
---|
| 257 | } |
---|
[8276] | 258 | return true; |
---|
[7611] | 259 | } |
---|
[7616] | 260 | |
---|
[7621] | 261 | /** |
---|
| 262 | * @brief renames the File (move) |
---|
| 263 | * @param destination the Destination to move this file to. |
---|
| 264 | * @returns true on success, false otherwise. |
---|
| 265 | * |
---|
| 266 | * if the File was opened, it will be closed throuh this function. |
---|
[7624] | 267 | * The File will also be closed, if the File was not renamed. |
---|
[7621] | 268 | */ |
---|
[7611] | 269 | bool File::rename(const File& destination) |
---|
| 270 | { |
---|
[7624] | 271 | this->close(); |
---|
| 272 | |
---|
[7616] | 273 | if (!std::rename(this->_name.c_str(), destination.name().c_str())) |
---|
| 274 | { |
---|
| 275 | this->_name = destination.name(); |
---|
[7621] | 276 | this->statFile(); |
---|
[7616] | 277 | return true; |
---|
| 278 | } |
---|
| 279 | return false; |
---|
[7611] | 280 | } |
---|
[7616] | 281 | |
---|
[7621] | 282 | /** |
---|
| 283 | * @brief touches the File. |
---|
| 284 | * @returns true if the file could have been touched. false otherwise. |
---|
| 285 | * |
---|
| 286 | * Touching a File means creating it. |
---|
| 287 | */ |
---|
[7611] | 288 | bool File::touch() |
---|
| 289 | { |
---|
[7616] | 290 | FILE* stream; |
---|
| 291 | if( (stream = fopen (this->_name.c_str(), "w")) == NULL) |
---|
| 292 | { |
---|
| 293 | std::cout << "could not touch '" << this->_name << "' for writing\n"; |
---|
| 294 | return false; |
---|
| 295 | } |
---|
| 296 | fclose(stream); |
---|
[7624] | 297 | |
---|
| 298 | this->statFile(); |
---|
[7616] | 299 | return true; |
---|
[7611] | 300 | } |
---|
[7616] | 301 | |
---|
[7621] | 302 | /** |
---|
| 303 | * @brief delete the File on the Disk |
---|
| 304 | * @returns true on success, false otherwise. |
---|
| 305 | */ |
---|
[7611] | 306 | bool File::remove() |
---|
| 307 | { |
---|
[8276] | 308 | if (!this->exists()) |
---|
| 309 | return false; |
---|
| 310 | |
---|
[7623] | 311 | this->close(); |
---|
[7616] | 312 | unlink(this->_name.c_str()); |
---|
| 313 | delete this->_status; |
---|
| 314 | this->_status = NULL; |
---|
[8276] | 315 | |
---|
| 316 | return true; |
---|
[7611] | 317 | } |
---|
| 318 | |
---|
[7621] | 319 | /** |
---|
| 320 | * @brief transforms a Relative path to an absolute one. |
---|
| 321 | * @param fileName the Absolute Path. |
---|
| 322 | */ |
---|
| 323 | void File::relToAbs(std::string& relFileName) |
---|
[7611] | 324 | { |
---|
[7621] | 325 | if (relFileName.empty()) |
---|
[7616] | 326 | return ; |
---|
[7621] | 327 | if (relFileName[0] != '/') |
---|
[7616] | 328 | { |
---|
[7621] | 329 | if (relFileName[0] == '.' && relFileName[1] != '.') |
---|
| 330 | relFileName.erase(0); |
---|
| 331 | relFileName = File::cwd() + relFileName; |
---|
[7616] | 332 | } |
---|
[7611] | 333 | } |
---|
[7616] | 334 | |
---|
[7621] | 335 | void File::absToRel(std::string& absFileName) |
---|
[7611] | 336 | { |
---|
[7621] | 337 | if (absFileName.find(cwd()) == 0) |
---|
| 338 | absFileName.replace(0, File::cwd().size(), "."); |
---|
[7611] | 339 | } |
---|
| 340 | |
---|
| 341 | |
---|
| 342 | std::string File::_cwd = ""; |
---|
| 343 | |
---|
| 344 | /** |
---|
| 345 | * @returns the Current Woring Directory |
---|
| 346 | */ |
---|
| 347 | const std::string& File::cwd() |
---|
| 348 | { |
---|
| 349 | if (File::_cwd.empty()) |
---|
| 350 | { |
---|
| 351 | char cwd[1024]; |
---|
| 352 | char* errorCode = getcwd(cwd, 1024); |
---|
| 353 | if (errorCode == 0) |
---|
| 354 | return File::_cwd; |
---|
| 355 | |
---|
| 356 | File::_cwd = cwd; |
---|
| 357 | } |
---|
| 358 | return File::_cwd; |
---|
| 359 | } |
---|
| 360 | |
---|
[7621] | 361 | /** |
---|
| 362 | * @brief check if fileName has the '~/` prepended. |
---|
| 363 | * @returns the fileName in absolute coordinate. |
---|
| 364 | */ |
---|
[7616] | 365 | void File::homeDirCheck(std::string& fileName) |
---|
| 366 | { |
---|
| 367 | if (fileName.size() < 2 || fileName[0] != '~' || fileName[1] != '/') |
---|
| 368 | return; |
---|
| 369 | std::string homeDir; |
---|
| 370 | #ifdef __WIN32__ |
---|
[7619] | 371 | homeDir = getenv("USERPROFILE"); |
---|
[7616] | 372 | #else |
---|
[7619] | 373 | homeDir = getenv("HOME"); |
---|
[7616] | 374 | #endif |
---|
[7619] | 375 | fileName = homeDir + fileName.substr(1); |
---|
[7616] | 376 | } |
---|