[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 | |
---|
| 35 | |
---|
| 36 | File::File(const std::string& fileName) |
---|
| 37 | { |
---|
[7616] | 38 | this->_name = fileName; |
---|
| 39 | File::homeDirCheck(this->_name); |
---|
| 40 | this->init(); |
---|
[7611] | 41 | } |
---|
| 42 | |
---|
| 43 | File::File(const File& file) |
---|
| 44 | { |
---|
[7616] | 45 | this->_name = file._name; |
---|
[7611] | 46 | } |
---|
| 47 | |
---|
| 48 | File::~File() |
---|
| 49 | { |
---|
[7616] | 50 | if (this->_status) |
---|
| 51 | delete this->_status; |
---|
| 52 | if (this->_handle) |
---|
| 53 | assert(0); |
---|
[7611] | 54 | } |
---|
| 55 | |
---|
[7616] | 56 | /** |
---|
| 57 | * @brief initializes the File |
---|
| 58 | * |
---|
| 59 | * Stats the File, looks if it exists and sets the hadle to 0 |
---|
| 60 | */ |
---|
| 61 | void File::init() |
---|
| 62 | { |
---|
| 63 | this->_handle = 0; |
---|
[7618] | 64 | std::string name = this->_name; |
---|
[7616] | 65 | |
---|
| 66 | this->_status = new struct stat; |
---|
[7618] | 67 | // Check the End of the FileName and chop away any \ and // |
---|
| 68 | if (this->_name[this->_name.size()-1] == '/' || |
---|
| 69 | this->_name[this->_name.size()-1] == '\\') |
---|
| 70 | name.resize(name.size()-1); |
---|
| 71 | |
---|
| 72 | if (stat(name.c_str(), this->_status)) |
---|
[7616] | 73 | { |
---|
| 74 | delete this->_status; |
---|
| 75 | this->_status = NULL; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
[7611] | 79 | bool File::open(OpenMode mode) |
---|
| 80 | { |
---|
| 81 | #warning implement |
---|
| 82 | } |
---|
[7615] | 83 | |
---|
[7611] | 84 | bool File::close() |
---|
| 85 | { |
---|
| 86 | #warning implement |
---|
| 87 | } |
---|
[7615] | 88 | |
---|
[7611] | 89 | int File::handle() |
---|
| 90 | { |
---|
[7616] | 91 | return this->_handle; |
---|
[7611] | 92 | } |
---|
| 93 | |
---|
| 94 | bool File::exists() |
---|
| 95 | { |
---|
[7616] | 96 | return (this->_status != NULL); |
---|
[7611] | 97 | } |
---|
[7615] | 98 | |
---|
[7611] | 99 | bool File::isLink() |
---|
| 100 | { |
---|
[7616] | 101 | #ifndef __WIN32__ |
---|
| 102 | return (this->_status != NULL && this->_status->st_mode & (S_IFLNK)); |
---|
| 103 | #else |
---|
| 104 | return false; |
---|
| 105 | #endif |
---|
[7615] | 106 | } |
---|
| 107 | // only on UNIX |
---|
[7616] | 108 | |
---|
[7611] | 109 | bool File::isFile() |
---|
| 110 | { |
---|
[7616] | 111 | return (this->_status != NULL && this->_status->st_mode & (S_IFREG)); |
---|
[7611] | 112 | } |
---|
[7615] | 113 | |
---|
[7616] | 114 | /** |
---|
| 115 | * @brief Checks if it is a Directory |
---|
| 116 | * @param directoryName the Directory to check for |
---|
| 117 | * @returns true if it is a directory/symlink false otherwise |
---|
| 118 | */ |
---|
[7611] | 119 | bool File::isDirectory() |
---|
| 120 | { |
---|
[7612] | 121 | // checking for the termination of the string given. If there is a "/" at the end cut it away |
---|
[7616] | 122 | //if (this->_name[this->_name.size()-1] == '/' || |
---|
| 123 | // this->_name[this->_name.size()-1] == '\\') |
---|
| 124 | //{ |
---|
| 125 | // tmpDirName.erase(tmpDirName.size()-1); |
---|
| 126 | //} |
---|
| 127 | return (this->_status != NULL && this->_status->st_mode & (S_IFDIR)); |
---|
[7611] | 128 | } |
---|
[7612] | 129 | |
---|
[7616] | 130 | |
---|
| 131 | /// FIXME NEXT THREE FUNCTIONS |
---|
[7611] | 132 | bool File::isReadeable() |
---|
| 133 | { |
---|
[7616] | 134 | #ifndef __WIN32__ |
---|
| 135 | return (this->_status != NULL && this->_status->st_mode & (S_IRUSR)); |
---|
| 136 | #else |
---|
| 137 | return (this->_status != NULL); |
---|
| 138 | #endif |
---|
[7611] | 139 | } |
---|
[7612] | 140 | |
---|
[7611] | 141 | bool File::isWriteable() |
---|
| 142 | { |
---|
[7616] | 143 | #ifndef __WIN32__ |
---|
| 144 | return (this->_status != NULL && this->_status->st_mode & (S_IWUSR)); |
---|
| 145 | #else |
---|
| 146 | return (this->_status != NULL); |
---|
| 147 | #endif |
---|
[7611] | 148 | } |
---|
| 149 | bool File::isExecutable() |
---|
| 150 | { |
---|
[7616] | 151 | #ifndef __WIN32__ |
---|
| 152 | return (this->_status != NULL && this->_status->st_mode & (S_IXUSR)); |
---|
| 153 | #else |
---|
| 154 | return (this->_status != NULL); |
---|
| 155 | #endif |
---|
[7611] | 156 | } |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | bool File::copy(const File& destination) |
---|
| 160 | { |
---|
[7616] | 161 | char ch; |
---|
| 162 | std::ifstream iFile(this->_name.c_str()); |
---|
| 163 | std::ofstream oFile(destination.name().c_str()); |
---|
| 164 | while (iFile.get(ch)) |
---|
| 165 | { |
---|
| 166 | oFile.put(ch); |
---|
| 167 | } |
---|
[7611] | 168 | } |
---|
[7616] | 169 | |
---|
[7611] | 170 | bool File::rename(const File& destination) |
---|
| 171 | { |
---|
[7616] | 172 | if (!std::rename(this->_name.c_str(), destination.name().c_str())) |
---|
| 173 | { |
---|
| 174 | this->close(); |
---|
| 175 | delete this->_status; |
---|
| 176 | this->_status = NULL; |
---|
| 177 | this->_name = destination.name(); |
---|
| 178 | this->init(); |
---|
| 179 | |
---|
| 180 | return true; |
---|
| 181 | } |
---|
| 182 | return false; |
---|
[7611] | 183 | } |
---|
[7616] | 184 | |
---|
[7611] | 185 | bool File::touch() |
---|
| 186 | { |
---|
[7616] | 187 | FILE* stream; |
---|
| 188 | if( (stream = fopen (this->_name.c_str(), "w")) == NULL) |
---|
| 189 | { |
---|
| 190 | std::cout << "could not touch '" << this->_name << "' for writing\n"; |
---|
| 191 | return false; |
---|
| 192 | } |
---|
| 193 | fclose(stream); |
---|
| 194 | return true; |
---|
[7611] | 195 | } |
---|
[7616] | 196 | |
---|
[7611] | 197 | bool File::remove() |
---|
| 198 | { |
---|
[7616] | 199 | unlink(this->_name.c_str()); |
---|
| 200 | delete this->_status; |
---|
| 201 | this->_status = NULL; |
---|
| 202 | /// FIXME HANDLE |
---|
[7611] | 203 | } |
---|
| 204 | |
---|
| 205 | void File::relToAbs(std::string& fileName) |
---|
| 206 | { |
---|
[7616] | 207 | if (fileName.empty()) |
---|
| 208 | return ; |
---|
| 209 | if (fileName[0] != '/') |
---|
| 210 | { |
---|
| 211 | if (fileName[0] == '.' && fileName[1] != '.') |
---|
| 212 | fileName.erase(0); |
---|
| 213 | fileName = File::cwd() + fileName; |
---|
| 214 | } |
---|
[7611] | 215 | } |
---|
[7616] | 216 | |
---|
[7611] | 217 | void File::absToRel(std::string& fileName) |
---|
| 218 | { |
---|
[7616] | 219 | if (fileName.find(cwd()) == 0) |
---|
| 220 | fileName.replace(0, File::cwd().size(), "."); |
---|
[7611] | 221 | } |
---|
| 222 | |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | |
---|
| 226 | std::string File::_cwd = ""; |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * @returns the Current Woring Directory |
---|
| 230 | */ |
---|
| 231 | const std::string& File::cwd() |
---|
| 232 | { |
---|
| 233 | if (File::_cwd.empty()) |
---|
| 234 | { |
---|
| 235 | char cwd[1024]; |
---|
| 236 | char* errorCode = getcwd(cwd, 1024); |
---|
| 237 | if (errorCode == 0) |
---|
| 238 | return File::_cwd; |
---|
| 239 | |
---|
| 240 | File::_cwd = cwd; |
---|
| 241 | } |
---|
| 242 | return File::_cwd; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | |
---|
[7616] | 246 | void File::homeDirCheck(std::string& fileName) |
---|
| 247 | { |
---|
| 248 | if (fileName.size() < 2 || fileName[0] != '~' || fileName[1] != '/') |
---|
| 249 | return; |
---|
| 250 | std::string homeDir; |
---|
| 251 | #ifdef __WIN32__ |
---|
| 252 | homeDir = getenv("USERPROFILE"); |
---|
| 253 | #else |
---|
| 254 | homeDir = getenv("HOME"); |
---|
| 255 | #endif |
---|
| 256 | fileName = homeDir + fileName.substr(1); |
---|
| 257 | } |
---|
[7611] | 258 | |
---|
[7616] | 259 | |
---|
[7611] | 260 | #include "file.h" |
---|
| 261 | |
---|
| 262 | |
---|