Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7616 in orxonox.OLD for branches/qt_gui


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

more file stuff in File-class

Location:
branches/qt_gui/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/qt_gui/src/lib/gui/gtk_gui/gui_exec.cc

    r7221 r7616  
    2626#include "gui_exec.h"
    2727
     28#include "file.h"
    2829#include "util/loading/resource_manager.h"
    2930#include "parser/ini_parser/ini_parser.h"
     
    3334#include <sys/stat.h>
    3435#include <sys/types.h>
     36
    3537
    3638
     
    147149void GuiExec::setConfDir(const char* confDir)
    148150{
    149   this->confDir = ResourceManager::homeDirCheck(confDir);
     151  this->confDir = File(confDir).name();
    150152
    151153  PRINTF(5)("Config Directory is: %s.\n", this->confDir);
     
    199201  IniParser iniParser;
    200202  this->writeFileText(widget, &iniParser, 0);
    201   std::string fileName = ResourceManager::homeDirCheck(confFile);
    202   iniParser.writeFile(fileName);
     203  iniParser.writeFile(File(confFile).name());
    203204}
    204205
     
    253254void GuiExec::readFromFile(Widget* widget)
    254255{
    255   std::string fileName = ResourceManager::homeDirCheck(confFile);
     256  std::string fileName = File(confFile).name();
    256257  IniParser iniParser(fileName);
    257258  if (!iniParser.isOpen())
  • branches/qt_gui/src/lib/shell/shell_completion_plugin.cc

    r7610 r7616  
    136136        printf("%s\n", (ResourceManager::getInstance()->getDataDir() + fileName).c_str());
    137137        if (!nocaseCmp(completionBegin, fileName, completionBegin.size()) &&
    138              ResourceManager::isDir(ResourceManager::getInstance()->getDataDir() + fileName))
     138             ResourceManager::isInDataDir(fileName))
    139139        {
    140140          printf("Dir %s\n", fileName.c_str());
  • 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"
  • branches/qt_gui/src/lib/util/file.h

    r7612 r7616  
    99
    1010#include <string>
     11typedef struct stat;
    1112
    1213class File
     
    2223
    2324public:
    24   File();
    2525  File(const std::string& fileName);
    2626  File(const File& file);
     
    3030  virtual bool close();
    3131  int handle();
     32
     33  /** @returns the FileName of this File */
     34  const std::string& name() const { return this->_name; };
    3235
    3336  bool exists();
     
    5053
    5154  private:
     55    void init();
     56    bool statFile;
    5257    void homeDirCheck(std::string& fileName);
    5358
    5459  private:
    5560    int                 _handle;          //!< The FileHandle (if set).
    56     std::string         _name;             //!< The Name of the File.
     61    std::string         _name;            //!< The Name of the File.
     62    stat*               _status;          //!< The Stat of the File.
    5763
    5864    static std::string  _cwd;             //!< The currend Working directory.
     65
    5966};
    6067
  • branches/qt_gui/src/lib/util/loading/resource_manager.cc

    r7611 r7616  
    8686bool ResourceManager::setDataDir(const std::string& dataDir)
    8787{
    88   std::string realDir = ResourceManager::homeDirCheck(dataDir);
    89   if (isDir(realDir))
    90   {
    91     if (dataDir[dataDir.size()-1] == '/' || dataDir[dataDir.size()-1] == '\\')
     88  File dataDirectory(dataDir);
     89  if (dataDirectory.isDirectory())
     90  {
     91    this->dataDir = dataDirectory.name();
     92
     93    if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\')
    9294    {
    93       this->dataDir = realDir;
    94     }
    95     else
    96     {
    97       this->dataDir = realDir;
    9895      this->dataDir += '/';
    9996    }
     
    10299  else
    103100  {
    104     PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", realDir.c_str(), this->dataDir.c_str());
     101    PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", dataDir.c_str(), this->dataDir.c_str());
    105102    return false;
    106103  }
     
    115112bool ResourceManager::tryDataDir(const std::string& dataDir)
    116113{
    117   std::string realDir = ResourceManager::homeDirCheck(dataDir);
    118   if (isDir(realDir))
    119   {
    120     if (dataDir[dataDir.size()-1] == '/' || dataDir[dataDir.size()-1] == '\\')
     114  File dataDirectory(dataDir);
     115  if (dataDirectory.isDirectory())
     116  {
     117    this->dataDir = dataDirectory.name();
     118
     119    if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\')
    121120    {
    122       this->dataDir = realDir;
    123     }
    124     else
    125     {
    126       this->dataDir = realDir;
    127121      this->dataDir += '/';
    128122    }
     
    139133bool ResourceManager::verifyDataDir(const std::string& fileInside)
    140134{
    141   bool retVal;
    142   if (!isDir(this->dataDir))
    143   {
    144     PRINTF(1)("%s is not a directory\n", this->dataDir.c_str());
     135  File dataDirectory(this->dataDir);
     136  if (!dataDirectory.isDirectory())
     137  {
     138    PRINTF(1)("'%s' is not a directory\n", this->dataDir.c_str());
    145139    return false;
    146140  }
    147141
    148   std::string testFile = this->dataDir + fileInside;
    149   retVal = isFile(testFile);
    150   return retVal;
     142  File testFile(this->dataDir + fileInside);
     143  return testFile.isFile();
    151144}
    152145
     
    160153bool ResourceManager::addImageDir(const std::string& imageDir)
    161154{
    162   std::string newDir;
    163   if (imageDir[imageDir.size()-1] == '/' || imageDir[imageDir.size()-1] == '\\')
    164   {
    165     newDir = imageDir;
    166   }
    167   else
    168   {
    169     newDir = imageDir;
     155  std::string newDir = imageDir;
     156  if (imageDir[imageDir.size()-1] != '/' && imageDir[imageDir.size()-1] != '\\')
     157  {
    170158    newDir += '/';
    171159  }
    172160  // check if the param is a Directory
    173   if (isDir(newDir))
     161  if (File(newDir).isDirectory())
    174162  {
    175163    // check if the Directory has been added before
     
    380368        tmpResource->param[0] = 1.0f;
    381369
    382       if(ResourceManager::isFile(fullName))
     370      if(File(fullName).isFile())
    383371        tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat());
    384372      else
     
    406394      break;
    407395    case MD2:
    408       if(ResourceManager::isFile(fullName))
     396      if(File(fullName).isFile())
    409397      {
    410398        tmpResource->param[0] = param0;
     
    424412        tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE;
    425413
    426       if(isFile(fullName))
     414      if(File(fullName).isFile())
    427415        tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt());
    428416      else
     
    432420#ifndef NO_AUDIO
    433421    case WAV:
    434       if(isFile(fullName))
     422      if(File(fullName).isFile())
    435423        tmpResource->pointer = new OrxSound::SoundBuffer(fullName);
    436424      break;
    437425    case OGG:
    438       if (isFile(fullName))
     426      if (File(fullName).isFile())
    439427        tmpResource->pointer = new OrxSound::OggPlayer(fullName);
    440428      break;
     
    446434      else
    447435        tmpResource->param[0] = GL_TEXTURE_2D;
    448       if(isFile(fullName))
     436      if(File(fullName).isFile())
    449437      {
    450438        PRINTF(4)("Image %s resides to %s\n", fileName, fullName);
     
    457445        {
    458446          std::string imgName = *imageDir + fileName;
    459           if(isFile(imgName))
     447          if(File(imgName).isFile())
    460448          {
    461449            PRINTF(4)("Image %s resides to %s\n", fileName, imgName);
     
    471459#ifndef NO_SHADERS
    472460    case SHADER:
    473       if(ResourceManager::isFile(fullName))
     461      if(File(fullName).isFile())
    474462      {
    475463        if (param0 != MT_NULL)
     
    477465          MultiType param = param0; /// HACK
    478466          std::string secFullName = ResourceManager::getFullName(param.getCString());
    479           if (ResourceManager::isFile(secFullName))
     467          if (File(secFullName).isFile())
    480468          {
    481469            tmpResource->param[0] = secFullName;
     
    749737
    750738/**
    751  * @brief Checks if it is a Directory
    752  * @param directoryName the Directory to check for
    753  * @returns true if it is a directory/symlink false otherwise
    754 */
    755 bool ResourceManager::isDir(const std::string& directoryName)
    756 {
    757   std::string tmpDirName = directoryName;
    758   struct stat status;
    759 
    760   // checking for the termination of the string given. If there is a "/" at the end cut it away
    761   if (directoryName[directoryName.size()-1] == '/' ||
    762       directoryName[directoryName.size()-1] == '\\')
    763   {
    764     tmpDirName.erase(tmpDirName.size()-1);
    765   }
    766 
    767   if(!stat(tmpDirName.c_str(), &status))
    768   {
    769     if (status.st_mode & (S_IFDIR
    770 #ifndef __WIN32__
    771                           | S_IFLNK
    772 #endif
    773                          ))
    774     {
    775       return true;
    776     }
    777     else
    778       return false;
    779   }
    780   else
    781     return false;
    782 }
    783 
    784 /**
    785  * @brief Checks if the file is either a Regular file or a Symlink
    786  * @param fileName the File to check for
    787  * @returns true if it is a regular file/symlink, false otherwise
    788 */
    789 bool ResourceManager::isFile(const std::string& fileName)
    790 {
    791   if (fileName.empty())
    792     return false;
    793   std::string tmpFileName = ResourceManager::homeDirCheck(fileName);
    794   // actually checks the File
    795   struct stat status;
    796   if (!stat(tmpFileName.c_str(), &status))
    797   {
    798     if (status.st_mode & (S_IFREG
    799 #ifndef __WIN32__
    800                           | S_IFLNK
    801 #endif
    802                          ))
    803     {
    804       return true;
    805     }
    806     else
    807       return false;
    808   }
    809   else
    810     return false;
    811 }
    812 
    813 /**
    814  * @brief touches a File on the disk (thereby creating it)
    815  * @param fileName The file to touch
    816 */
    817 bool ResourceManager::touchFile(const std::string& fileName)
    818 {
    819   std::string tmpName = ResourceManager::homeDirCheck(fileName);
    820   if (tmpName.empty())
    821     return false;
    822   FILE* stream;
    823   if( (stream = fopen (tmpName.c_str(), "w")) == NULL)
    824   {
    825     PRINTF(1)("could not open %s fro writing\n", fileName.c_str());
    826     return false;
    827   }
    828   fclose(stream);
    829 }
    830 
    831 /**
    832  * @brief deletes a File from disk
    833  * @param fileName the File to delete
    834 */
    835 bool ResourceManager::deleteFile(const std::string& fileName)
    836 {
    837   std::string tmpName = ResourceManager::homeDirCheck(fileName);
    838   unlink(tmpName.c_str());
    839 }
    840 
    841 /**
    842  * @param name the Name of the file to check
    843  * @returns The name of the file, including the HomeDir
    844  */
    845 std::string ResourceManager::homeDirCheck(const std::string& name)
    846 {
    847   if (name.size() >= 2 && name[0] == '~' && name[1] == '/')
    848   {
    849     std::string homeDir;
    850     std::string newName = name.substr(1);
    851 #ifdef __WIN32__
    852     homeDir = getenv("USERPROFILE");
    853 #else
    854     homeDir = getenv("HOME");
    855 #endif
    856     return homeDir + newName;
    857   }
    858   else
    859     return name;
    860 }
    861 
    862 /**
    863  * @param name the relative name of the File/Directory.
    864  * @returns a new std::string with the name in abs-dir-format
    865  */
    866 std::string ResourceManager::getAbsDir(const std::string& name)
    867 {
    868   if (name.empty())
    869     return "";
    870   std::string retName = name;
    871   if (strncmp(name.c_str(), "/", 1))
    872   {
    873     if (name[0] == '.' && name[1] != '.')
    874       retName.erase(0);
    875     const std::string& absDir = File::cwd();
    876     retName = absDir + retName;
    877   }
    878   return retName;
    879 }
    880 
    881 
    882 /**
    883739 * @param fileName the Name of the File to check
    884740 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist
     
    891747
    892748  std::string retName = ResourceManager::getInstance()->getDataDir() +fileName;
    893   if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName))
     749  if (File(retName).isFile() || File(retName).isDirectory())
    894750    return retName;
    895751  else
     
    911767  std::string checkFile = ResourceManager::getInstance()->getDataDir() + fileName;
    912768
    913   if (ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile))
     769  if (File(checkFile).exists())
    914770    retVal = true;
    915771  else
  • branches/qt_gui/src/lib/util/loading/resource_manager.h

    r7611 r7616  
    2121
    2222#include "base_object.h"
     23#include "file.h"
     24
    2325#include "multi_type.h"
    24 
    2526#include <vector>
    2627
     
    130131
    131132  // utility functions for handling files in and around the data-directory
    132   static bool isDir(const std::string& directory);
    133   static bool isFile(const std::string& fileName);
    134   static bool touchFile(const std::string& fileName);
    135   static bool deleteFile(const std::string& fileName);
    136   static std::string homeDirCheck(const std::string& fileName);
    137133  static std::string getFullName(const std::string& fileName);
    138134  static bool isInDataDir(const std::string& fileName);
    139   static std::string getAbsDir(const std::string& fileName);
    140135
    141136  static const char* ResourceTypeToChar(ResourceType type);
  • branches/qt_gui/src/orxonox.cc

    r7608 r7616  
    3232#include "gui/qt_gui/qt_gui.h"
    3333
     34#include "file.h"
    3435#include "parser/ini_parser/ini_parser.h"
    3536#include "util/loading/game_loader.h"
     
    189190const std::string& Orxonox::getConfigFile ()
    190191{
    191   if (ResourceManager::isFile("orxonox.conf"))
     192  File orxConfFile("orxonox.conf");
     193  if (orxConfFile.isFile())
    192194  {
    193195    this->configFileName =  "orxonox.conf";
    194196  }
    195197  else
    196     this->configFileName = ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE);
     198    this->configFileName = File(DEFAULT_CONFIG_FILE).name();
    197199
    198200  PRINTF(3)("Parsed Config File: '%s'\n", this->configFileName);
     
    426428  CmdLinePrefsReader prefs;
    427429
    428   IniFilePrefsReader ini(ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE));
    429   Preferences::getInstance()->setUserIni(ResourceManager::homeDirCheck(DEFAULT_CONFIG_FILE));
     430  IniFilePrefsReader ini(File(DEFAULT_CONFIG_FILE).name());
     431  Preferences::getInstance()->setUserIni(File(DEFAULT_CONFIG_FILE).name());
    430432
    431433  prefs.parse(argc, argv);
     
    503505{
    504506  // checking for existence of the configuration-files, or if the lock file is still used
    505   if (showGui || (!ResourceManager::isFile("./orxonox.conf") &&
    506                   !ResourceManager::isFile(DEFAULT_CONFIG_FILE))
     507  if (showGui || (!File("./orxonox.conf").isFile() &&
     508      !File(DEFAULT_CONFIG_FILE).isFile())
    507509#if DEBUG < 3 // developers do not need to see the GUI, when orxonox fails
    508510      || ResourceManager::isFile(DEFAULT_LOCK_FILE)
     
    510512     )
    511513  {
    512     if (ResourceManager::isFile(DEFAULT_LOCK_FILE))
    513       ResourceManager::deleteFile(DEFAULT_LOCK_FILE);
     514    File lockFile(DEFAULT_LOCK_FILE);
     515    if (lockFile.isFile())
     516      lockFile.remove();
    514517
    515518    // starting the GUI
     
    524527  PRINT(0)(">>> Starting Orxonox <<<\n");
    525528
    526   ResourceManager::touchFile(DEFAULT_LOCK_FILE);
     529  File(DEFAULT_LOCK_FILE).touch();
    527530
    528531  Orxonox *orx = Orxonox::getInstance();
     
    538541
    539542  delete orx;
    540   ResourceManager::deleteFile("~/.orxonox/orxonox.lock");
    541 }
     543  File("~/.orxonox/orxonox.lock").remove();
     544}
  • branches/qt_gui/src/story_entities/story_entity.cc

    r7283 r7616  
    2222#include "story_entity.h"
    2323
     24#include "util/file.h"
     25#include "util/loading/load_param.h"
    2426#include "util/loading/resource_manager.h"
    25 #include "util/loading/load_param.h"
    2627
    2728
     
    102103void StoryEntity::setLoadFile(const std::string& fileName)
    103104{
    104   if (ResourceManager::isFile(fileName))
     105  if (File(fileName).isFile())
    105106  {
    106107    this->loadFile =  fileName;
Note: See TracChangeset for help on using the changeset viewer.