Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7616 in orxonox.OLD for branches/qt_gui/src/lib/util/file.cc


Ignore:
Timestamp:
May 15, 2006, 2:41:51 PM (18 years ago)
Author:
bensch
Message:

more file stuff in File-class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/qt_gui/src/lib/util/file.cc

    r7615 r7616  
    1818#include <sys/types.h>
    1919#include <sys/stat.h>
     20#include <stdio.h>
     21
     22#include <iostream>
     23#include <fstream>
    2024
    2125#ifdef __unix__
     
    2731#endif
    2832
    29 
    30 
    31 File::File()
     33#include <cassert>
     34
     35
     36File::File(const std::string& fileName)
     37{
     38  this->_name = fileName;
     39  File::homeDirCheck(this->_name);
     40  this->init();
     41}
     42
     43File::File(const File& file)
     44{
     45  this->_name = file._name;
     46}
     47
     48File::~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 */
     61void File::init()
     62{
     63  this->_handle = 0;
     64
     65  this->_status = new struct stat;
     66  if (stat(this->_name.c_str(), this->_status))
     67  {
     68    delete this->_status;
     69    this->_status = NULL;
     70  }
     71}
     72
     73bool File::open(OpenMode mode)
    3274{
    3375#warning implement
    3476}
    3577
    36 File::File(const std::string& fileName)
     78bool File::close()
    3779{
    3880#warning implement
    3981}
    4082
    41 File::File(const File& file)
    42 {
    43 #warning implement
    44 }
    45 
    46 File::~File()
    47 {
    48 #warning implement
    49 }
    50 
    51 bool File::open(OpenMode mode)
    52 {
    53 #warning implement
    54 }
    55 
    56 bool File::close()
    57 {
    58 #warning implement
    59 }
    60 
    6183int File::handle()
    6284{
    63 #warning implement
     85  return this->_handle;
    6486}
    6587
    6688bool File::exists()
    6789{
    68 #warning implement
     90  return (this->_status != NULL);
    6991}
    7092
    7193bool File::isLink()
    7294{
    73 #warning implement
     95#ifndef __WIN32__
     96  return (this->_status != NULL && this->_status->st_mode & (S_IFLNK));
     97#else
     98  return false;
     99#endif
    74100}
    75101// only on UNIX
     102
    76103bool File::isFile()
    77104{
    78 #warning implement
    79 }
    80 
     105  return (this->_status != NULL && this->_status->st_mode & (S_IFREG));
     106}
     107
     108/**
     109 * @brief Checks if it is a Directory
     110 * @param directoryName the Directory to check for
     111 * @returns true if it is a directory/symlink false otherwise
     112 */
    81113bool File::isDirectory()
    82114{
    83   std::string tmpDirName = this->_name;
    84   struct stat status;
    85 
    86115  // checking for the termination of the string given. If there is a "/" at the end cut it away
    87   if (this->_name[this->_name.size()-1] == '/' ||
    88       this->_name[this->_name.size()-1] == '\\')
    89   {
    90     tmpDirName.erase(tmpDirName.size()-1);
    91   }
    92 
    93   if(!stat(tmpDirName.c_str(), &status))
    94   {
    95     if (status.st_mode & (S_IFDIR
    96 #ifndef __WIN32__
    97         | S_IFLNK
    98 #endif
    99                          ))
    100     {
    101       return true;
    102     }
    103     else
    104       return false;
    105   }
    106   else
     116  //if (this->_name[this->_name.size()-1] == '/' ||
     117  //    this->_name[this->_name.size()-1] == '\\')
     118  //{
     119  //  tmpDirName.erase(tmpDirName.size()-1);
     120  //}
     121  return (this->_status != NULL && this->_status->st_mode & (S_IFDIR));
     122}
     123
     124
     125/// FIXME NEXT THREE FUNCTIONS
     126bool File::isReadeable()
     127{
     128#ifndef __WIN32__
     129  return (this->_status != NULL && this->_status->st_mode & (S_IRUSR));
     130#else
     131  return (this->_status != NULL);
     132#endif
     133}
     134
     135bool File::isWriteable()
     136{
     137#ifndef __WIN32__
     138  return (this->_status != NULL && this->_status->st_mode & (S_IWUSR));
     139#else
     140  return (this->_status != NULL);
     141#endif
     142}
     143bool File::isExecutable()
     144{
     145#ifndef __WIN32__
     146  return (this->_status != NULL && this->_status->st_mode & (S_IXUSR));
     147#else
     148  return (this->_status != NULL);
     149#endif
     150}
     151
     152
     153bool File::copy(const File& destination)
     154{
     155  char ch;
     156  std::ifstream iFile(this->_name.c_str());
     157  std::ofstream oFile(destination.name().c_str());
     158  while (iFile.get(ch))
     159  {
     160    oFile.put(ch);
     161  }
     162}
     163
     164bool File::rename(const File& destination)
     165{
     166  if (!std::rename(this->_name.c_str(), destination.name().c_str()))
     167  {
     168    this->close();
     169    delete this->_status;
     170    this->_status = NULL;
     171    this->_name = destination.name();
     172    this->init();
     173
     174    return true;
     175  }
     176  return false;
     177}
     178
     179bool File::touch()
     180{
     181  FILE* stream;
     182  if( (stream = fopen (this->_name.c_str(), "w")) == NULL)
     183  {
     184    std::cout << "could not touch '" << this->_name << "' for writing\n";
    107185    return false;
    108 }
    109 
    110 bool File::isReadeable()
    111 {
    112 
    113 #warning implement
    114 }
    115 
    116 bool File::isWriteable()
    117 {
    118 #warning implement
    119 }
    120 bool File::isExecutable()
    121 {
    122 #warning implement
    123 }
    124 
    125 
    126 bool File::copy(const File& destination)
    127 {
    128 #warning implement
    129 }
    130 bool File::rename(const File& destination)
    131 {
    132 #warning implement
    133 }
    134 bool File::touch()
    135 {
    136 #warning implement
    137 }
     186  }
     187  fclose(stream);
     188  return true;
     189}
     190
    138191bool File::remove()
    139192{
    140 #warning implement
     193  unlink(this->_name.c_str());
     194  delete this->_status;
     195  this->_status = NULL;
     196  /// FIXME HANDLE
    141197}
    142198
    143199void File::relToAbs(std::string& fileName)
    144200{
    145 #warning implement
    146 }
     201  if (fileName.empty())
     202    return ;
     203  if (fileName[0] !=  '/')
     204  {
     205    if (fileName[0] == '.' && fileName[1] != '.')
     206      fileName.erase(0);
     207    fileName = File::cwd() + fileName;
     208  }
     209}
     210
    147211void File::absToRel(std::string& fileName)
    148212{
    149 #warning implement
     213  if (fileName.find(cwd()) == 0)
     214    fileName.replace(0, File::cwd().size(), ".");
    150215}
    151216
     
    173238
    174239
     240void File::homeDirCheck(std::string& fileName)
     241{
     242  if (fileName.size() < 2 || fileName[0] != '~' || fileName[1] != '/')
     243    return;
     244  std::string homeDir;
     245#ifdef __WIN32__
     246    homeDir = getenv("USERPROFILE");
     247#else
     248    homeDir = getenv("HOME");
     249#endif
     250    fileName = homeDir + fileName.substr(1);
     251}
     252
    175253
    176254#include "file.h"
Note: See TracChangeset for help on using the changeset viewer.