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