#include "stdafx.h"
#include "utillib.h"
/*#include <stdlib.h>
#include <objbase.h>
*/
#include <io.h>
#include <tchar.h>
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::mid(const std::string s, int iPos)
// Description..: Returns std::string a copy of input from iPos to end
// Params.......: const std::string& sPath
// Returns......: bool
// On failure...: false
/*---------------------------------------------------------------*/
std::string CUtilLib::mid(const std::string s, int iPos)
{
std::string _s(s);
_s.erase(0, iPos);
return _s;
}
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::IntToStr(const int& i)
// Description..: Converts int to std::string
// Params.......: int
// Returns......: std::string ... on error ""
/*---------------------------------------------------------------*/
std::string CUtilLib::IntToStr( int i)
{
char s[20] ={0};
try
{
itoa(i, s, 10);
}
catch(...)
{
return std::string( "" );
}
return std::string( s );
}
/*---------------------------------------------------------------*/
// Procedure....: int UtilLib::CStdToInt( const std::string& s)
// Description..: Converts std::string to int
// Params.......: std::string
// Returns......: int
/*---------------------------------------------------------------*/
int CUtilLib::StdToInt( const std::string& s )
{
return atoi(s.c_str());
}
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::PathAddExt( std::string sPath, std::string sExt )
// Description..: Adds extension inserting dot as required
// Params.......: std::string sPath, std::string sExt
// Returns......: std::string sPath concatenating "." and sExt
// On failure...: ""
/*---------------------------------------------------------------*/
std::string CUtilLib::PathAddExt( const std::string& sPath, const std::string& sExt )
{
std::string sRet =sPath;
try
{
if( *( sRet.end()-1 ) != '.' )
sRet.append(".");
sRet.append(sExt);
}
catch(...)
{
return std::string("");
}
return sRet;
}
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::PathRemoveExt( std::string& sPath )
// Description..: Returns std::string with removed extension and "."
// Params.......: std::string sPath (path to remove extension from)
// Returns......: modified std::string
// On failure...: ""
/*---------------------------------------------------------------*/
std::string CUtilLib::PathRemoveExt( const std::string& sPath )
{
std::string sRet =sPath;
try
{
int iFind = sRet.rfind('.');
if( iFind != -1 )
{
sRet.resize( iFind );
return sRet;
}
else
return std::string("");
}
catch(...)
{
return std::string("");
}
}
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::PathAppend(std::string& sPath, std::string& sToAppend)
// Description..: Returns std::string with appended std::string
// Params.......: std::string sPath std::string sToAppend
// Returns......: modified std::string
// On failure...: ""
/*---------------------------------------------------------------*/
std::string CUtilLib::PathAppend(std::string& sPath, const std::string& sToAppend)
{
std::string sRet(sPath);
try
{
sRet.append(sToAppend);
}
catch(...)
{
return std::string("");
}
return sRet;
}
/*---------------------------------------------------------------*/
// Procedure....: PathReplaceExt( std::string& sPath, const std::string& sNewExt )
// Description..: Returns std::string sNewExt
// Params.......: std::string sPath std::string sNewExt
// Returns......: modified std::string
// On failure...: ""
/*---------------------------------------------------------------*/
std::string CUtilLib::PathReplaceExt( const std::string& sPath, const std::string& sNewExt )
{
try
{
std::string sRes =PathRemoveExt(sPath);
sRes =( PathAddExt( sRes, sNewExt ) );
return sRes;
}
catch(...)
{
return std::string("");
}
}
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::PathGetExt(const std::string& sPath )
// Description..: Returns std::string with extension extracted
// Params.......: const std::string& sPath
// Returns......: std::string consisting of the filename
// On failure...: ""
/*---------------------------------------------------------------*/
std::string CUtilLib::PathGetExt(const std::string& sPath )
{
try
{
int iFind =sPath.rfind('.');
if( iFind != (-1) )
{
return mid( sPath, iFind + 1 );
}
else
{
return "";
}
}
catch(...)
{
return "";
}
}
/*---------------------------------------------------------------*/
// Procedure....: bool UtilLib::FileExists(conststd::string& sPath)
// Description..: Tests if file exists
// Params.......: std::string& sPath
// Returns......: bool
// On failure...: false
/*---------------------------------------------------------------*/
bool CUtilLib::FileExists(const std::string& sPath)
{
return (_taccess( sPath.c_str(), 0 ) == 0) ? true: false;
}
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::PathMakeDblBackslash(const std::string& sPath)
// Description..: Returns std::string with double backslashes
// Params.......: const std::string& sPath
// Returns......: std::string
// On failure...: ""
/*---------------------------------------------------------------*/
std::string CUtilLib::PathMakeDblBackslash(const std::string& sPath)
{
std::string sRes;
try
{
if(-1 !=sPath.find("\\\\"))
return std::string("");
std::string::const_iterator in =sPath.begin();
while(in < sPath.end() )
{
sRes.insert(sRes.end(),*in);
if(*in =='\\')
sRes.insert(sRes.end(),'\\');
in++;
}
}
catch(...)
{
return std::string("");
}
return sRes;
}
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::PathMakeSnglBackslash(const std::string& sPath)
// Description..: Returns std::string with single backslashes
// Params.......: const std::string& sPath
// Returns......: std::string
// On failure...: ""
/*---------------------------------------------------------------*/
std::string CUtilLib::PathMakeSnglBackslash(const std::string& sPath)
{
std::string sRes;
try
{
if( -1 == sPath.find("\\\\") )
return std::string("");
std::string::const_iterator in =sPath.begin();
while(in <= sPath.end() )
{
sRes.insert(sRes.end(),*in);
if( *in =='\\' && *(in+1) =='\\' )
in++;
in++;
}
}
catch(...)
{
return std::string("");
}
return sRes;
}
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::PathGetFileName(const std::string sPath)
// Description..: Returns std::string consists of filename extracted from path
// Params.......: const std::string& sPath
// Returns......: std::string consisting of the filename
// On failure...: ""
/*---------------------------------------------------------------*/
std::string CUtilLib::PathGetFileName( const std::string& sPath )
{
try
{
TCHAR buf[260] ={0};
_tsplitpath(sPath.c_str(), 0, 0, buf, 0);
return buf;
}
catch(...)
{
return "";
}
}
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::PathReplaceFileName( const std::string& sPath, const std::string& sNewFileName )
// Description..: Returns std::string sPath replacing filename
// Params.......: const std::string& sPath, const std::string& sNewFileName
// Returns......: std::string consisting of the path with new filename
// On failure...: ""
/*---------------------------------------------------------------*/
std::string CUtilLib::PathReplaceFileName( const std::string& sPath, const std::string& sNewFileName )
{
try
{
std::string sRet = sPath;
if( sRet.find('\\') == (-1) )
return std::string("");
std::string sExt= PathGetExt(sRet);
if( 0 == sExt.compare("") )
return std::string("");
sRet.resize( sRet.rfind('\\')+1 );
sRet +=sNewFileName;
sRet =PathAddExt(sRet, sExt) ;
return sRet;
}
catch(...)
{
return std::string("");
}
}
/*---------------------------------------------------------------*/
// Procedure....: std::string::value_type UtilLib::PathGetDriveLetter(const std::string& sPath )
// Description..: Returns std::string::value_type with drive letter
// Params.......: const std::string& sPath
// Returns......: std::string::value_type consisting of the drive letter
// On failure...: '\0'
/*---------------------------------------------------------------*/
std::string::value_type CUtilLib::PathGetDriveLetter( const std::string& sPath )
{
try
{
std::string::size_type iFind =sPath.find(":\\");
if( iFind != (-1) )
{
return sPath.at( iFind -1 );
}
else
{
return '\0';
}
}
catch(...)
{
return '\0';
}
}
/*---------------------------------------------------------------*/
// Procedure....: bool UtilLib::IsPathInQuotes( const std::string& sPath )
// Description..: Tests for quotes at beginning and end of path
// Params.......: const std::string& sPath
// Returns......: true on success
/*---------------------------------------------------------------*/
bool CUtilLib::IsPathInQuotes( const std::string& sPath )
{
return ('\"' == *sPath.begin() ) && ( *( sPath.end()-1 ) == '\"' );
}
/*---------------------------------------------------------------*/
// Procedure....: std::string UtilLib::SearchPath( const std::string& sPath, const std::string& sFile )
/* Description..:
function searches for the specified file
in the following directories in the following sequence:
1. The path specified in sPath
2. The directory from which the application loaded
3. The current directory for the current process
4. Windows 95/98/Me: The Windows system directory
5. Windows NT/2000 or later: The 32-bit Windows system directory (SYSTEM32)
6. Windows NT/2000 or later: The 16-bit Windows system directory (SYSTEM)
7. The Windows directory
8. The directories that are listed in the PATH environment variable
*/
// Params.......: const std::string& sPath, const std::string& sFile
// Returns......: std::string consisting of the file's path
// On failure...: ""
/*---------------------------------------------------------------*/
/*
std::string SearchPath( const std::string& sPath, const std::string& sFile )
{
TCHAR szPath[260] ={0};
TCHAR szFileName[260] ={0};
TCHAR szExtension[260] ={0};
unsigned long nBufferLength=260;
TCHAR szBuffer[260] ={0};
TCHAR**lpFilePart =0;
std::string sFileAndExt(sFile);
std::string sExt;
// get extension including dot
std::string::size_type iFind =sFileAndExt.rfind('.');
if( iFind != (-1) )
{
sExt = sFileAndExt.erase(0, iFind);//mid( sFileAndExt, iFind );
sFileAndExt.resize(iFind);
}
_tcscpy(szPath, sPath.c_str());
_tcscpy(szFileName, sFileAndExt.c_str());
_tcscpy(szExtension,sExt.c_str());
if( ::SearchPath(szPath, szFileName, szExtension, nBufferLength, szBuffer, lpFilePart) )
{
return std::string(szBuffer);
}
if( ::SearchPath(NULL, szFileName, szExtension, nBufferLength, szBuffer, lpFilePart) )
{
return std::string(szBuffer);
}
return std::string("");
}
*/
/*---------------------------------------------------------------*/
// Procedure....: UtilLib::SearchWindowsPath()
/* Description..:
function searches for the specified file
in the following directories in the following sequence:
1. The directory from which the application loaded
2. The current directory for the current process
3. Windows 95/98/Me: The Windows system directory
4. Windows NT/2000 or later: The 32-bit Windows system directory (SYSTEM32)
5. Windows NT/2000 or later: The 16-bit Windows system directory (SYSTEM)
6. The Windows directory
7. The directories that are listed in the PATH environment variable
*/
// Params.......: void
// Returns......: std::string consisting of the file's path
// On failure...: ""
/*---------------------------------------------------------------*/
/*
std::string SearchWindowsPath(const std::string& sFile)
{
TCHAR szFileName[MAX_PATH] ={0};
TCHAR szExtension[MAX_PATH] ={0};
DWORD nBufferLength=MAX_PATH;
TCHAR szBuffer[MAX_PATH] ={0};
LPTSTR* lpFilePart =0;
std::string sFileAndExt(sFile);
std::string sExt;
std::string::size_type iFind =sFileAndExt.rfind('.');
if( iFind != (npos) )
{
sExt = mid( sFileAndExt, iFind );
sFileAndExt.resize(iFind);
}
_tcscpy(szFileName, sFileAndExt.c_str());
_tcscpy(szExtension,sExt.c_str());
if( ::SearchPath(NULL, szFileName, szExtension, nBufferLength, szBuffer, lpFilePart) )
{
return std::string(szBuffer);
}
return std::string("");
}
*/
// Exception Handling
void dump_error()
//////////////////////////////////////////////////////////////////////////
{
}
/*---------------------------------------------------------------*/
// Procedure....: UtilLib::IsPathTooBig( const std::string& sPath )
// Description..: Tests sPath agains MAX_PATH
// Params.......: const std::string& sPath
// Returns......: bool
// On failure...: false
/*---------------------------------------------------------------*/
bool CUtilLib::IsPathTooBig( const std::string& sPath )
{
return( sPath.size() > 260 );
}
/*---------------------------------------------------------------*/
// Procedure....: UtilLib::IsValidDriveFormat( const std::string& sPath )
// Description..: Tests drive component in path is of "C:\" format
// Params.......: const std::string& sPath
// Returns......: bool
// On failure...: false
/*---------------------------------------------------------------*/
bool CUtilLib::IsValidDriveFormat( const std::string& sPath )
{
std::string::value_type c =PathGetDriveLetter(sPath);
if(c == '\0')
return false;
else
{
std::string::const_iterator it = sPath.begin();
it++;
if( *(it++) !=':')
return false;
if(*(it) != '\\' )
return false;
}
return true;
}
/*---------------------------------------------------------------*/
// Procedure....: UtilLib::IsValidFilename( const std::string& sFilename )
// Description..: Tests whether filename is valid
// Params.......: const std::string& sPath
// Returns......: bool
// On failure...: false
/*---------------------------------------------------------------*/
bool CUtilLib::IsValidFilename( const std::string& sFilename )
{
if ( sFilename.empty() ) { return false; }
// test for invalid characters : * ? " < > |
if ( sFilename.find(':') != std::string::npos) { return false; }
if ( sFilename.find('*') != std::string::npos) { return false; }
if ( sFilename.find('?') != std::string::npos) { return false; }
if ( sFilename.find('"') != std::string::npos) { return false; }
if ( sFilename.find('<') != std::string::npos) { return false; }
if ( sFilename.find('>') != std::string::npos) { return false; }
if ( sFilename.find('|') != std::string::npos) { return false; }
return true;
}
/*---------------------------------------------------------------*/
// Procedure....: UtilLib::PathExists( const std::string& sPath )
// Description..: Tests if directory path exists
// Params.......: const std::string& sPath
// Returns......: bool
// On failure...: false
/*---------------------------------------------------------------*/
bool CUtilLib::PathExists( const std::string& sPath )
{
return (_taccess( sPath.c_str(), 0 ) == 0) ? true: false;
// return PathIsDirectory( sPath.c_str() ) ? true : false;
}