ctFileName.cpp 5.44 KB
/*
 ctFileName.cpp - Encapsulates and provides the functions for a file name.

 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 __ctFileName_cpp

#include "ctFileName.h"
#include "ctFile.h"
#include "ctConfig.h"

#include <iostream>

#ifdef __UNIX__
  #define SLASH_TYPE "/"
#else
  #define SLASH_TYPE "\\"
#endif

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

void ctFileName::_Init(void)
{
  _Full="";
  _Path="";
  _Name="";
  _Ext="";
} // _Init

std::string ctFileName::_ExtractPath(const std::string& FileName)
{
  std::string Result="";
  size_t Pos=FileName.find_last_of(SLASH_TYPE);
  if(Pos!=std::string::npos) {
    Result=FileName.substr(0,Pos);
  }
  return Result;
} // _ExtractPath

std::string ctFileName::_ExtractName(const std::string& FileName)
{
  std::string Result="";
  size_t PosBegin=FileName.find_last_of(SLASH_TYPE);

  if(PosBegin!=std::string::npos) {
    Result=FileName.substr(PosBegin+1,FileName.length());
  } else {
    Result=FileName.substr(0,FileName.length());
  }

  size_t PosEnd=Result.find_last_of(".");
  if(PosEnd!=std::string::npos) {
    Result=Result.substr(0,PosEnd);
  } else {
    Result="";
  }

  return Result;
} // _ExtractName

std::string ctFileName::_ExtractExtension(const std::string& FileName)
{
  std::string Result="";
  size_t Pos=FileName.find_last_of(".");
  if(Pos!=std::string::npos) {
    Result=FileName.substr(Pos+1,FileName.length());
  }
  return Result;
} // _ExtractExtension

ctFileName::ctFileName(void)
{
  _Init();
} // ctcFileName constructor

ctFileName::ctFileName(const std::string& FileName)
{
  _Full=FileName;
  _Path=_ExtractPath(FileName);
  _Name=_ExtractName(FileName);
  _Ext=_ExtractExtension(FileName);
} // ctcFileName constructor

ctFileName::~ctFileName(void)
{
  //TODO
} // ctcFileName destructor

ctFileName & ctFileName::operator=(const ctFileName &C)
{
  if (this != &C) {
    _Full=C._Full;
    _Path=C._Path;
    _Name=C._Name;
    _Ext=C._Ext;
  }
  return *this;
} // Overload of the operator of assignment

void ctFileName::SetPath(const std::string& Path)
{
  _Path=Path;
} // SetPath

std::string ctFileName::GetPath(void)
{
  std::string Result;

  if(_Path=="") {
    Result="."; // local directory
  } else {
    Result=_Path;
  }

  return Result;
} // GetPath

void ctFileName::SetName(const std::string& Name)
{
  _Name=Name;
} // SetName

std::string ctFileName::GetName(void)
{
  return _Name;
} // GetName

void ctFileName::SetExt(const std::string& Ext)
{;
  _Ext=Ext;
} // SetExt

std::string ctFileName::GetExt(void)
{
  return _Ext;
} // GetExt

void ctFileName::SetFullName(const std::string& FileName)
{
  _Full=FileName;
  _Path=_ExtractPath(FileName);
  _Name=_ExtractName(FileName);
  _Ext=_ExtractExtension(FileName);
} // SetFullName

std::string ctFileName::GetFullName(void)
{
  std::string Result;

  if(_Full=="") {
    Result=_Path+SLASH_TYPE+_Name;
    if(HasExt()) {
      Result+="."+_Ext;
    }
  } else {
    Result=_Full;
  }

  return Result;
} // GetFullName

std::string ctFileName::GetShortName(void)
{
  return GetName()+"."+ GetExt();
} // GetShortName

bool ctFileName::HasName(void) const
{
  return (_Name!="");
} // HasName
bool ctFileName::HasExt(void) const
{
  return (_Ext!="");
} // HasExt

void ctFileName::Reset(void)
{
  _Init();
} // Reset

bool ctFileName::Exists(const std::string& FileName)
{
  return ctFile::Exists(FileName);
} // Exists