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 | |
---|
16 | #include "file.h" |
---|
17 | |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <stdio.h> |
---|
21 | |
---|
22 | #include <iostream> |
---|
23 | #include <fstream> |
---|
24 | |
---|
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 | |
---|
33 | #include <cassert> |
---|
34 | |
---|
35 | |
---|
36 | File::File(const std::string& fileName) |
---|
37 | { |
---|
38 | this->_name = fileName; |
---|
39 | File::homeDirCheck(this->_name); |
---|
40 | this->init(); |
---|
41 | } |
---|
42 | |
---|
43 | File::File(const File& file) |
---|
44 | { |
---|
45 | this->_name = file._name; |
---|
46 | } |
---|
47 | |
---|
48 | File::~File() |
---|
49 | { |
---|
50 | if (this->_status) |
---|
51 | delete this->_status; |
---|
52 | if (this->_handle) |
---|
53 | assert(0); |
---|
54 | } |
---|
55 | |
---|
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; |
---|
64 | |
---|
65 | this->_status = new struct stat; |
---|
66 | // Check the End of the FileName and chop away any \ and // |
---|
67 | /* std::string name = this->_name; |
---|
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(this->_name.c_str(), this->_status)) |
---|
73 | { |
---|
74 | delete this->_status; |
---|
75 | this->_status = NULL; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | bool File::open(OpenMode mode) |
---|
80 | { |
---|
81 | #warning implement |
---|
82 | } |
---|
83 | |
---|
84 | bool File::close() |
---|
85 | { |
---|
86 | #warning implement |
---|
87 | } |
---|
88 | |
---|
89 | int File::handle() |
---|
90 | { |
---|
91 | return this->_handle; |
---|
92 | } |
---|
93 | |
---|
94 | bool File::exists() const |
---|
95 | { |
---|
96 | return (this->_status != NULL); |
---|
97 | } |
---|
98 | |
---|
99 | bool File::isLink() const |
---|
100 | { |
---|
101 | #ifndef __WIN32__ |
---|
102 | return (this->_status != NULL && this->_status->st_mode & (S_IFLNK)); |
---|
103 | #else |
---|
104 | return false; |
---|
105 | #endif |
---|
106 | } |
---|
107 | // only on UNIX |
---|
108 | |
---|
109 | bool File::isFile() const |
---|
110 | { |
---|
111 | return (this->_status != NULL && this->_status->st_mode & (S_IFREG)); |
---|
112 | } |
---|
113 | |
---|
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 | */ |
---|
119 | bool File::isDirectory() const |
---|
120 | { |
---|
121 | // checking for the termination of the string given. If there is a "/" at the end cut it away |
---|
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)); |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | /// FIXME NEXT THREE FUNCTIONS |
---|
132 | bool File::isReadeable() const |
---|
133 | { |
---|
134 | #ifndef __WIN32__ |
---|
135 | return (this->_status != NULL && this->_status->st_mode & (S_IRUSR)); |
---|
136 | #else |
---|
137 | return (this->_status != NULL); |
---|
138 | #endif |
---|
139 | } |
---|
140 | |
---|
141 | bool File::isWriteable() const |
---|
142 | { |
---|
143 | #ifndef __WIN32__ |
---|
144 | return (this->_status != NULL && this->_status->st_mode & (S_IWUSR)); |
---|
145 | #else |
---|
146 | return (this->_status != NULL); |
---|
147 | #endif |
---|
148 | } |
---|
149 | bool File::isExecutable() const |
---|
150 | { |
---|
151 | #ifndef __WIN32__ |
---|
152 | return (this->_status != NULL && this->_status->st_mode & (S_IXUSR)); |
---|
153 | #else |
---|
154 | return (this->_status != NULL); |
---|
155 | #endif |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | bool File::copy(const File& destination) |
---|
160 | { |
---|
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 | } |
---|
168 | } |
---|
169 | |
---|
170 | bool File::rename(const File& destination) |
---|
171 | { |
---|
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; |
---|
183 | } |
---|
184 | |
---|
185 | bool File::touch() |
---|
186 | { |
---|
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; |
---|
195 | } |
---|
196 | |
---|
197 | bool File::remove() |
---|
198 | { |
---|
199 | unlink(this->_name.c_str()); |
---|
200 | delete this->_status; |
---|
201 | this->_status = NULL; |
---|
202 | /// FIXME HANDLE |
---|
203 | } |
---|
204 | |
---|
205 | void File::relToAbs(std::string& fileName) |
---|
206 | { |
---|
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 | } |
---|
215 | } |
---|
216 | |
---|
217 | void File::absToRel(std::string& fileName) |
---|
218 | { |
---|
219 | if (fileName.find(cwd()) == 0) |
---|
220 | fileName.replace(0, File::cwd().size(), "."); |
---|
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 | |
---|
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 | } |
---|
258 | |
---|
259 | |
---|
260 | #include "file.h" |
---|
261 | |
---|
262 | |
---|