Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/qt_gui/src/lib/util/file.cc @ 7621

Last change on this file since 7621 was 7621, checked in by bensch, 18 years ago

orxonox/trunk: nicer File-Class, that can reopen files.

File size: 6.0 KB
RevLine 
[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
[7621]35File::File()
36{
37  this->init();
38}
[7611]39
40File::File(const std::string& fileName)
41{
[7616]42  this->init();
[7621]43  this->setFileName(fileName);
[7611]44}
45
46File::File(const File& file)
47{
[7621]48  this->init();
49  this->setFileName(file.name());
[7611]50}
51
52File::~File()
53{
[7621]54  this->close();
55
[7616]56  if (this->_status)
57    delete this->_status;
[7611]58}
59
[7621]60
[7616]61/**
62 * @brief initializes the File
63 *
64 * Stats the File, looks if it exists and sets the hadle to 0
65 */
66void File::init()
67{
68  this->_handle = 0;
[7621]69  this->_status = NULL;
70}
[7616]71
[7621]72
73/**
74 * @brief sets a new File to apply to this File.
75 * @param fileName the Filename of the File to access.
76 */
77void File::setFileName(const std::string& fileName)
78{
79  this->close();
80  this->_name = fileName;
81  File::homeDirCheck(this->_name);
82  this->statFile();
83}
84
85
86void File::statFile()
87{
88  if (this->_status == NULL)
89    this->_status = new struct stat;
[7618]90  // Check the End of the FileName and chop away any \ and //
[7619]91  /*  std::string name = this->_name;
92    if (this->_name[this->_name.size()-1] == '/' ||
93        this->_name[this->_name.size()-1] == '\\')
94         name.resize(name.size()-1);*/
95  if (stat(this->_name.c_str(), this->_status))
[7616]96  {
97    delete this->_status;
98    this->_status = NULL;
99  }
100}
101
[7611]102bool File::open(OpenMode mode)
103{
104#warning implement
105}
[7615]106
[7611]107bool File::close()
108{
109#warning implement
110}
[7615]111
[7620]112bool File::exists() const
[7611]113{
[7616]114  return (this->_status != NULL);
[7611]115}
[7615]116
[7621]117/**
118 * @brief checks if the file is a Link (symlink/hardlink on UNIX)
119 * @returns true if the File is a Link, false otherwise (on windows always false)
120 */
[7620]121bool File::isLink() const
[7611]122{
[7616]123#ifndef __WIN32__
124  return (this->_status != NULL && this->_status->st_mode & (S_IFLNK));
125#else
126  return false;
127#endif
[7615]128}
[7616]129
[7621]130/**
131 * @brief checks if the File is a regular File
132 * @returns true if the File is a Regular file.
133 */
[7620]134bool File::isFile() const
[7611]135{
[7616]136  return (this->_status != NULL && this->_status->st_mode & (S_IFREG));
[7611]137}
[7615]138
[7616]139/**
[7621]140 * @brief Checks if the File is a Directory
[7616]141 * @returns true if it is a directory/symlink false otherwise
142 */
[7620]143bool File::isDirectory() const
[7611]144{
[7612]145  // checking for the termination of the string given. If there is a "/" at the end cut it away
[7616]146  //if (this->_name[this->_name.size()-1] == '/' ||
147  //    this->_name[this->_name.size()-1] == '\\')
148  //{
149  //  tmpDirName.erase(tmpDirName.size()-1);
150  //}
151  return (this->_status != NULL && this->_status->st_mode & (S_IFDIR));
[7611]152}
[7612]153
[7616]154
155/// FIXME NEXT THREE FUNCTIONS
[7620]156bool File::isReadeable() const
[7611]157{
[7616]158#ifndef __WIN32__
159  return (this->_status != NULL && this->_status->st_mode & (S_IRUSR));
160#else
161  return (this->_status != NULL);
162#endif
[7611]163}
[7620]164bool File::isWriteable() const
[7611]165{
[7616]166#ifndef __WIN32__
167  return (this->_status != NULL && this->_status->st_mode & (S_IWUSR));
168#else
169  return (this->_status != NULL);
170#endif
[7611]171}
[7620]172bool File::isExecutable() const
[7611]173{
[7616]174#ifndef __WIN32__
175  return (this->_status != NULL && this->_status->st_mode & (S_IXUSR));
176#else
177  return (this->_status != NULL);
178#endif
[7611]179}
180
[7621]181/**
182 * @brief copies the File to another File.
183 * @param destination the Destination File.
184 * @returns true on success, false otherwise.
185 */
[7611]186bool File::copy(const File& destination)
187{
[7616]188  char ch;
189  std::ifstream iFile(this->_name.c_str());
190  std::ofstream oFile(destination.name().c_str());
191  while (iFile.get(ch))
192  {
193    oFile.put(ch);
194  }
[7611]195}
[7616]196
[7621]197/**
198 * @brief renames the File (move)
199 * @param destination the Destination to move this file to.
200 * @returns true on success, false otherwise.
201 *
202 * if the File was opened, it will be closed throuh this function.
203 */
[7611]204bool File::rename(const File& destination)
205{
[7616]206  if (!std::rename(this->_name.c_str(), destination.name().c_str()))
207  {
208    this->close();
209    this->_name = destination.name();
[7621]210    this->statFile();
[7616]211
212    return true;
213  }
214  return false;
[7611]215}
[7616]216
[7621]217/**
218 * @brief touches the File.
219 * @returns true if the file could have been touched. false otherwise.
220 *
221 * Touching a File means creating it.
222 */
[7611]223bool File::touch()
224{
[7616]225  FILE* stream;
226  if( (stream = fopen (this->_name.c_str(), "w")) == NULL)
227  {
228    std::cout << "could not touch '" << this->_name << "' for writing\n";
229    return false;
230  }
231  fclose(stream);
232  return true;
[7611]233}
[7616]234
[7621]235/**
236 * @brief delete the File on the Disk
237 * @returns true on success, false otherwise.
238 */
[7611]239bool File::remove()
240{
[7616]241  unlink(this->_name.c_str());
242  delete this->_status;
243  this->_status = NULL;
244  /// FIXME HANDLE
[7611]245}
246
[7621]247/**
248 * @brief transforms a Relative path to an absolute one.
249 * @param fileName the Absolute Path.
250 */
251void File::relToAbs(std::string& relFileName)
[7611]252{
[7621]253  if (relFileName.empty())
[7616]254    return ;
[7621]255  if (relFileName[0] !=  '/')
[7616]256  {
[7621]257    if (relFileName[0] == '.' && relFileName[1] != '.')
258      relFileName.erase(0);
259    relFileName = File::cwd() + relFileName;
[7616]260  }
[7611]261}
[7616]262
[7621]263void File::absToRel(std::string& absFileName)
[7611]264{
[7621]265  if (absFileName.find(cwd()) == 0)
266    absFileName.replace(0, File::cwd().size(), ".");
[7611]267}
268
269
270std::string File::_cwd = "";
271
272/**
273 * @returns the Current Woring Directory
274 */
275const std::string& File::cwd()
276{
277  if (File::_cwd.empty())
278  {
279    char cwd[1024];
280    char* errorCode = getcwd(cwd, 1024);
281    if (errorCode == 0)
282      return File::_cwd;
283
284    File::_cwd = cwd;
285  }
286  return File::_cwd;
287}
288
[7621]289/**
290 * @brief check if fileName has the '~/` prepended.
291 * @returns the fileName in absolute coordinate.
292 */
[7616]293void File::homeDirCheck(std::string& fileName)
294{
295  if (fileName.size() < 2 || fileName[0] != '~' || fileName[1] != '/')
296    return;
297  std::string homeDir;
298#ifdef __WIN32__
[7619]299  homeDir = getenv("USERPROFILE");
[7616]300#else
[7619]301  homeDir = getenv("HOME");
[7616]302#endif
[7619]303  fileName = homeDir + fileName.substr(1);
[7616]304}
[7611]305
[7616]306
[7611]307#include "file.h"
308
309
Note: See TracBrowser for help on using the repository browser.