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
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
35File::File()
36{
37  this->init();
38}
39
40File::File(const std::string& fileName)
41{
42  this->init();
43  this->setFileName(fileName);
44}
45
46File::File(const File& file)
47{
48  this->init();
49  this->setFileName(file.name());
50}
51
52File::~File()
53{
54  this->close();
55
56  if (this->_status)
57    delete this->_status;
58}
59
60
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;
69  this->_status = NULL;
70}
71
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;
90  // Check the End of the FileName and chop away any \ and //
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))
96  {
97    delete this->_status;
98    this->_status = NULL;
99  }
100}
101
102bool File::open(OpenMode mode)
103{
104#warning implement
105}
106
107bool File::close()
108{
109#warning implement
110}
111
112bool File::exists() const
113{
114  return (this->_status != NULL);
115}
116
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 */
121bool File::isLink() const
122{
123#ifndef __WIN32__
124  return (this->_status != NULL && this->_status->st_mode & (S_IFLNK));
125#else
126  return false;
127#endif
128}
129
130/**
131 * @brief checks if the File is a regular File
132 * @returns true if the File is a Regular file.
133 */
134bool File::isFile() const
135{
136  return (this->_status != NULL && this->_status->st_mode & (S_IFREG));
137}
138
139/**
140 * @brief Checks if the File is a Directory
141 * @returns true if it is a directory/symlink false otherwise
142 */
143bool File::isDirectory() const
144{
145  // checking for the termination of the string given. If there is a "/" at the end cut it away
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));
152}
153
154
155/// FIXME NEXT THREE FUNCTIONS
156bool File::isReadeable() const
157{
158#ifndef __WIN32__
159  return (this->_status != NULL && this->_status->st_mode & (S_IRUSR));
160#else
161  return (this->_status != NULL);
162#endif
163}
164bool File::isWriteable() const
165{
166#ifndef __WIN32__
167  return (this->_status != NULL && this->_status->st_mode & (S_IWUSR));
168#else
169  return (this->_status != NULL);
170#endif
171}
172bool File::isExecutable() const
173{
174#ifndef __WIN32__
175  return (this->_status != NULL && this->_status->st_mode & (S_IXUSR));
176#else
177  return (this->_status != NULL);
178#endif
179}
180
181/**
182 * @brief copies the File to another File.
183 * @param destination the Destination File.
184 * @returns true on success, false otherwise.
185 */
186bool File::copy(const File& destination)
187{
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  }
195}
196
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 */
204bool File::rename(const File& destination)
205{
206  if (!std::rename(this->_name.c_str(), destination.name().c_str()))
207  {
208    this->close();
209    this->_name = destination.name();
210    this->statFile();
211
212    return true;
213  }
214  return false;
215}
216
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 */
223bool File::touch()
224{
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;
233}
234
235/**
236 * @brief delete the File on the Disk
237 * @returns true on success, false otherwise.
238 */
239bool File::remove()
240{
241  unlink(this->_name.c_str());
242  delete this->_status;
243  this->_status = NULL;
244  /// FIXME HANDLE
245}
246
247/**
248 * @brief transforms a Relative path to an absolute one.
249 * @param fileName the Absolute Path.
250 */
251void File::relToAbs(std::string& relFileName)
252{
253  if (relFileName.empty())
254    return ;
255  if (relFileName[0] !=  '/')
256  {
257    if (relFileName[0] == '.' && relFileName[1] != '.')
258      relFileName.erase(0);
259    relFileName = File::cwd() + relFileName;
260  }
261}
262
263void File::absToRel(std::string& absFileName)
264{
265  if (absFileName.find(cwd()) == 0)
266    absFileName.replace(0, File::cwd().size(), ".");
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
289/**
290 * @brief check if fileName has the '~/` prepended.
291 * @returns the fileName in absolute coordinate.
292 */
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__
299  homeDir = getenv("USERPROFILE");
300#else
301  homeDir = getenv("HOME");
302#endif
303  fileName = homeDir + fileName.substr(1);
304}
305
306
307#include "file.h"
308
309
Note: See TracBrowser for help on using the repository browser.