Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/lib/util/filesys/file.cc @ 10246

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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