Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/file.cc @ 7671

Last change on this file since 7671 was 7661, checked in by bensch, 19 years ago

orxonox/trunk: merged the QT_GUI back to the trunk
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/qt_gui . -r7607:HEAD

absolutely no conflicts :)

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