ctString.cpp 5.63 KB
/*
 ctString.cpp - Encapsulates and provides the functions for handle strings.

 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 __ctString_cpp

#include "ctString.h"
#include "ctConfig.h"
#include <sstream>
#include <cctype>
#include <time.h>

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

std::string ctString::Upper(const std::string& Value)
{
  std::string Result;
  int j = Value.length();
  Result = Value;
  for(int i=0; i < j; i++)
      Result[i] = toupper(Value[i]);
  return Result;
} // Upper

std::string ctString::Lower(const std::string& Value)
{
  std::string Result;
  int j = Value.length();
  Result = Value;
  for(int i=0; i < j; i++)
      Result[i] = tolower(Value[i]);
  return Result;
} // Lower

std::string ctString::Capitalize(const std::string& Value, bool Reverse)
{
  std::string Result;
  if(!Reverse) {
    Result=toupper(Value[0]);
  } else {
    Result=tolower(Value[0]);
  }
  Result+=Value.substr(1,Value.length());

  return Result;
} // Capitalize

std::string ctString::FromInteger(int Value)
{
  std::stringstream ss;
  ss << Value;
  return ss.str();
} // FromInteger

std::string ctString::Replace(const std::string& Text,
                              const std::string& Pattern,
                              const std::string& Value)
{
  std::string Result;
  int Pos=0;
  int PatternLen=Pattern.length();
  int ValueLen=Value.length();

  Result=Text;
  if (PatternLen != 0) {
    for( ; (Pos = Result.find( Pattern, Pos )) != std::string::npos; ) {
      Result.replace( Pos, PatternLen, Value );
      Pos += ValueLen;
    }
  }

  return Result;
} // Replace

bool ctString::IsEqual(const std::string& First,
                       const std::string& Second,
                       bool CaseSensitive)
{
  if(!CaseSensitive) {
    if (Upper(First)==Upper(Second))
      return true;
    else
      return false;
  } else {
    if (First==Second)
      return true;
    else
      return false;
  }
} // IsEqual

std::string ctString::Trim(const std::string& Value)
{
  std::string Result;
  int Len=Value.length();

  for(int i=0; i < Len; i++) {
    if(Value[i] != ' ')
      Result[i]=Value[i];
  }

  return Result;
} // Trim

std::string ctString::GetDateTime(const std::string& Format)
{
  std::string Result;
  char cAux[100];
  time_t t;

  time(&t);
  struct tm* petm = localtime(&t);
  strftime(cAux, 100, Format.c_str(), petm);
  Result = cAux;

  return Result;
} // GetDateTime

std::string ctString::GetDate(void)
{
  return GetDateTime("%d %B %Y");
} // GetDate

std::string ctString::GetTime(void)
{
  return GetDateTime("%d%m%y_%H%M%S");
} // GetTime

std::string ctString::EmptyString(void)
{
  return "";
} // EmptyString

std::string ctString::LineFeed(void)
{
#ifdef __UNIX__
  return "\n";
#else
  return "\r\n";
#endif
} // LineFeed

std::string ctString::Normalize(const std::string &Name)
{
  std::string Result;

  Result=Replace(Name,".","_");
  Result=Replace(Result,"-","_");

  return Result;
} // Normalize

bool ctString::MustNormalize(const std::string& Name)
{
  bool Result=false;
  size_t PosA=Name.find_first_of('-');
  size_t PosB=Name.find_first_of('.');

  if( (PosA!=std::string::npos) || (PosB!=std::string::npos) ) {
    Result=true;
  }

  return Result;
} // MustNormalize

std::string ctString::GetSlash(void)
{
#ifdef __UNIX__
  return "/";
#else
  return "\\";
#endif
} // GetSlash

std::string ctString::ConvertSlash(const std::string& FileName)
{
  std::string Result;

  Result=Replace(FileName,"\\",GetSlash());
  Result=Replace(Result,"/",GetSlash());

  return Result;
} // ConvertSlash