ctFile.cpp 5.23 KB
/*
 ctFile.cpp - 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>.
*/

#define __ctFile_cpp

#include "ctFile.h"
#include <iostream>
#include <fstream>
#include <cstdlib>

///*****************************************************************************

void ctFile::_Init(void)
{
  _F=NULL;
  _FileName="";
  _Mode="";
} // _Init

bool ctFile::_Open(const std::string& FileName, const std::string& Mode)
{
  _F=fopen(FileName.c_str(), Mode.c_str());
  return (_F!=NULL);
} // _Open

ctFile::ctFile(void)
{
  _Init();
} // ctcFile constructor

ctFile::ctFile(const std::string& FileName, const std::string& Mode)
{
  _FileName=FileName;
  _Mode=Mode;
  _Open(FileName, Mode);
} // ctcFile constructor

ctFile::ctFile(FILE *F)
{
  _Init();
  SetFileHandler(F);
} // ctcFile constructor

ctFile::~ctFile(void)
{
  //TODO
} // ctcFile destructor

ctFile & ctFile::operator=(const ctFile &C)
{
  if (this != &C) {
    _F=C._F;
    _FileName=C._FileName;
    _Mode=C._Mode;
  }
  return *this;
} // Overload of the operator of assignment

std::string ctFile::GetFileName(void) const
{
  return _FileName;
} // GetFileName

std::string ctFile::GetFileMode(void) const
{
  return _Mode;
} // GetFileMode

void ctFile::SetFileHandler(FILE *F)
{
  _F=F;
} // SetFileHandler

FILE* ctFile::GetFileHandler(void)
{
  return _F;
} // GetFileHandler

bool ctFile::IsOpened(void) const
{
  return (_F!=NULL);
} // IsOpened

bool ctFile::Open(const std::string& FileName, const std::string& Mode)
{
  _FileName=FileName;
  _Mode=Mode;
  return _Open(FileName, Mode);
} // Open

bool ctFile::Close(void)
{
  if(IsOpened()) {
    fclose(_F);
  }
  return true;
} // Close

bool ctFile::Eof() const
{
  if(IsOpened()) {
    return (feof(_F)!=0);
  } else {
    return true;
  }
} // Eof

std::string ctFile::Read(size_t Count)
{
  std::string Result="";
  char *Buffer= (char*) malloc (sizeof(char)*Count);

  if (Buffer!=NULL) {
    size_t Counter=fread(Buffer,1,Count,_F);
    Result=Buffer;
    free(Buffer);
  }

  return Result;
} // Read

std::string ctFile::ReadAll(void)
{
  std::string Result="";
  char Line[255];

  while (fgets(Line, 255, _F))
  {
    Result+=Line;
  };

  return Result;
} // ReadAll

bool ctFile::Write(const std::string &Buffer, size_t Count)
{
  bool Result=true;
  size_t Written=fwrite(Buffer.c_str(),1,Count,_F);

  if(Written < Count) {
    Result=false;
  }

  return Result;
} // Write

bool ctFile::Backup(const std::string &Source, const std::string &Destination)
{
  bool Result=true;
  FILE* FSource=fopen(Source.c_str(), "r");
  FILE* FDestination=fopen(Destination.c_str(), "w");

  if((FSource!=NULL) && (FDestination!=NULL)) {
    char Line[255];

    while (fgets(Line, 255, FSource))
    {
      if(fputs(Line,FDestination)==EOF) {
        Result=false;
        break;
      }
    };
    fclose(FSource);
    fclose(FDestination);
  } else {
    Result=false;
  }

  return Result;
} // Backup

bool ctFile::Rename(const std::string &Source, const std::string &Destination)
{
  return ( rename(Source.c_str(),Destination.c_str()) == 0 );
} // Rename

bool ctFile::Exists(const std::string& Name)
{
  return (fopen(Name.c_str(), "r")!=NULL);
} // Exists