ctFile.h 5.88 KB
/*
 ctFile.h - Encapsulates and provides operations I/0 on file.

 Author: Francisco J. Santana Jorge
         <francisco dot santana102 arroba estudiantes dot ulpgc dot es>, 2011

 Inst. Universitario de Sistemas Inteligentes y Aplicaciones Numéricas
 Universidad de Las Palmas de Gran Canaria

 Copyright (C) 2003,2007,2009,2010
    Universidad de Las Palmas de Gran Canaria (ULPGC) (<http://www.ulpgc.es>)

    This software is a collective project which has been designed, developed and
    written, and is maintained by members, students and collaborators of the División
    de Inteligencia Artificial y Sistemas (DIAS) at the Instituto Universitario de
    Sistemas Inteligentes y Aplicaciones Numéricas en Ingeniería (SIANI)
    (<http://www.siani.es/>), and also of the Departamento de Informática y Sistemas
    (DIS) (<http://www.dis.ulpgc.es>) both at ULPGC. The AUTHOR file lists all
    developers and contributors.The particular authors of each file are explicitly
    specified on each one of them.

 This file is part of the CoolBOT Project.
 The CoolBOT Project is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or any
 later version.

 The CoolBOT Project is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy (COPYING file) of the GNU General Public License
 along with the CoolBOT Project . If not, see <http://www.gnu.org/licenses/>.

 In addition to the GNU General Public License, the CoolBOT Project Compiler License
 Exception is applicable to the C++ skeletons generated by the CoolBOT Compiler
 (coolbot-c). The purpose of this Exception is to allow distribution of  CoolBOT
 Compiler's (coolbot-c) typical output under terms of the recipient's choice
 (including proprietary). You should have received a copy of the CoolBOT Project
 Compiler License Exception (COPYING.EXCEPTION file) along with the CoolBOT Project.
 If not see <http://www.coolbotproject.org>.
*/

#ifndef __ctFile_h
#define __ctFile_h

#include <stdio.h>
#include <string>

/**
 ** The ctFile class encapsulates and provides operations I/0 on file.
 **/
class ctFile
{
private:
  /// The file handler.
  FILE* _F;

  /// The file name
  std::string _FileName;

  /// The mode in which to open the file
  std::string _Mode;

  /**
   ** Initializes the class.
   **/
  void _Init(void);

  /**
   ** Opens the file.
   ** @param FileName  the file name
   ** @param Mode      the file mode
   ** @return Return true if successful.
   **/
  bool _Open(const std::string& FileName, const std::string& Mode = "r");

public:
  /**
   ** Default constructor.
   **/
  ctFile(void);

  /**
   ** Constructor a new file handler.
   ** @param FileName  the file name
   ** @param Mode      the file mode
   **/
  ctFile(const std::string& FileName, const std::string& Mode = "r");

  /**
   ** Constructor a new file handler.
   ** @param F  file handler
   **/
  ctFile(FILE *F);

  /// Destructor
  ~ctFile(void);

  /**
   ** Overload of the operator of assignment.
   ** @param C  object of the ctcFile class
   **/
  ctFile & operator=(const ctFile &C);

  /**
   ** Returns the file name.
   ** @return the file name
   **/
  std::string GetFileName(void) const;

  /**
   ** Returns the mode in which to open the file.
   ** @return the file mode
   **/
  std::string GetFileMode(void) const;

  /**
   ** Sets the file handler.
   ** @param F  file handler
   **/
  void SetFileHandler(FILE *F);

  /**
   ** Returns the file handler.
   ** @return file handler
   **/
  FILE* GetFileHandler(void);

  /**
   ** Returns true if the file has been opened.
   ** @return true if the file has been opened
   **/
  bool IsOpened(void) const;

  /**
   ** Opens the file. Returns true if the file has been opened
   ** @param FileName  the file name
   ** @param Mode      the file mode
   ** @return true if the file has been opened
   **/
  bool Open(const std::string& FileName, const std::string& Mode = "r");

  /**
   ** Closes the file.
   ** @return true if the file has been closed
   **/
  bool Close(void);

  /**
   ** Returns true if the an attempt has been made to read past the end of the file.
   ** @return true if the an attempt has been made to read past the end of the file
   **/
  bool Eof() const;

  /**
   ** Reads the specified number of bytes into a buffer.
   ** @param Count the number of bytes to read
   ** @return Returning the actual number read
   **/
  std::string Read(size_t Count);

  /**
   ** Reads the entire contents of the file into a string.
   ** @return Returning a string with the entire contents of the file.
   **/
  std::string ReadAll(void);

  /**
   ** Writes the specified number of bytes from a buffer.
   ** @param Buffer a buffer containing the data
   ** @param Count the number of bytes to write
   ** @return Returns true if the writing is realized correctly.
   **/
  bool Write(const std::string &Buffer, size_t Count);

  /**
   ** Realizes an exact copy of a file.
   ** @param Source       the source file
   ** @param Destination  the destination file
   ** @return Returns true if the backup is realized correctly.
   **/
  static bool Backup(const std::string &Source, const std::string &Destination);

  /**
   ** Renames the source file to destination file.
   ** @param Source       the source file
   ** @param Destination  the destination file
   ** @return Returns true if the rename is realized correctly.
   **/
  static bool Rename(const std::string &Source, const std::string &Destination);

  /**
   ** Determines if a file exists.
   ** @param Name the filename
   ** @return Returns true if the file exists.
   **/
  static bool Exists(const std::string& Name);

}; // ctFile class

#endif ///__ctFile_h