Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7621 in orxonox.OLD


Ignore:
Timestamp:
May 15, 2006, 6:35:32 PM (18 years ago)
Author:
bensch
Message:

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

Location:
branches/qt_gui/src/lib/util
Files:
2 edited

Legend:

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

    r7620 r7621  
    3333#include <cassert>
    3434
     35File::File()
     36{
     37  this->init();
     38}
    3539
    3640File::File(const std::string& fileName)
    3741{
    38   this->_name = fileName;
    39   File::homeDirCheck(this->_name);
    4042  this->init();
     43  this->setFileName(fileName);
    4144}
    4245
    4346File::File(const File& file)
    4447{
    45   this->_name = file._name;
     48  this->init();
     49  this->setFileName(file.name());
    4650}
    4751
    4852File::~File()
    4953{
     54  this->close();
     55
    5056  if (this->_status)
    5157    delete this->_status;
    52   if (this->_handle)
    53     assert(0);
    54 }
     58}
     59
    5560
    5661/**
     
    6267{
    6368  this->_handle = 0;
    64 
    65   this->_status = new struct stat;
     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;
    6690  // Check the End of the FileName and chop away any \ and //
    6791  /*  std::string name = this->_name;
     
    6993        this->_name[this->_name.size()-1] == '\\')
    7094         name.resize(name.size()-1);*/
    71 
    7295  if (stat(this->_name.c_str(), this->_status))
    7396  {
     
    87110}
    88111
    89 int File::handle()
    90 {
    91   return this->_handle;
    92 }
    93 
    94112bool File::exists() const
    95113{
     
    97115}
    98116
     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 */
    99121bool File::isLink() const
    100122{
     
    105127#endif
    106128}
    107 // only on UNIX
    108 
     129
     130/**
     131 * @brief checks if the File is a regular File
     132 * @returns true if the File is a Regular file.
     133 */
    109134bool File::isFile() const
    110135{
     
    113138
    114139/**
    115  * @brief Checks if it is a Directory
    116  * @param directoryName the Directory to check for
     140 * @brief Checks if the File is a Directory
    117141 * @returns true if it is a directory/symlink false otherwise
    118142 */
     
    138162#endif
    139163}
    140 
    141164bool File::isWriteable() const
    142165{
     
    156179}
    157180
    158 
     181/**
     182 * @brief copies the File to another File.
     183 * @param destination the Destination File.
     184 * @returns true on success, false otherwise.
     185 */
    159186bool File::copy(const File& destination)
    160187{
     
    168195}
    169196
     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 */
    170204bool File::rename(const File& destination)
    171205{
     
    173207  {
    174208    this->close();
    175     delete this->_status;
    176     this->_status = NULL;
    177209    this->_name = destination.name();
    178     this->init();
     210    this->statFile();
    179211
    180212    return true;
     
    183215}
    184216
     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 */
    185223bool File::touch()
    186224{
     
    195233}
    196234
     235/**
     236 * @brief delete the File on the Disk
     237 * @returns true on success, false otherwise.
     238 */
    197239bool File::remove()
    198240{
     
    203245}
    204246
    205 void File::relToAbs(std::string& fileName)
    206 {
    207   if (fileName.empty())
     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())
    208254    return ;
    209   if (fileName[0] !=  '/')
    210   {
    211     if (fileName[0] == '.' && fileName[1] != '.')
    212       fileName.erase(0);
    213     fileName = File::cwd() + fileName;
    214   }
    215 }
    216 
    217 void File::absToRel(std::string& fileName)
    218 {
    219   if (fileName.find(cwd()) == 0)
    220     fileName.replace(0, File::cwd().size(), ".");
    221 }
    222 
    223 
     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}
    224268
    225269
     
    243287}
    244288
    245 
     289/**
     290 * @brief check if fileName has the '~/` prepended.
     291 * @returns the fileName in absolute coordinate.
     292 */
    246293void File::homeDirCheck(std::string& fileName)
    247294{
  • branches/qt_gui/src/lib/util/file.h

    r7620 r7621  
    2323
    2424public:
     25  File();
    2526  File(const std::string& fileName);
    2627  File(const File& file);
    2728  ~File();
     29  void setFileName(const std::string& fileName);
     30  File& operator=(const std::string& fileName);
     31  File& operator=(const File& file);
     32  bool operator==(const std::string& fileName) const;
     33  bool operator==(const File& file) const;
     34
     35
    2836
    2937  virtual bool open(OpenMode mode);
    3038  virtual bool close();
    31   int handle();
     39  int handle() const { return this->_handle; };
    3240
    3341  /** @returns the FileName of this File */
     
    4856  bool remove();
    4957
    50   static void relToAbs(std::string& fileName);
    51   static void absToRel(std::string& fileName);
     58  static void relToAbs(std::string& relFileName);
     59  static void absToRel(std::string& absFileName);
    5260  static const std::string& cwd();
    5361
    5462  private:
    5563    void init();
    56     bool statFile();
     64    void statFile();
    5765    void homeDirCheck(std::string& fileName);
    5866
Note: See TracChangeset for help on using the changeset viewer.