159 lines
3.9 KiB
C++

/**
*******************************************************************************
* @file File.cpp
* @brief Editeur De N'ours : File folder and name abstraction (Sources)
* @author Edouard DUPIN
* @date 16/07/2011
* @par Project
* Edn
*
* @par Copyright
* Copyright 2010 Edouard DUPIN, all right reserved
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY.
*
* Licence summary :
* You can modify and redistribute the sources code and binaries.
* You can send me the bug-fix
* You can not earn money with this Software (if the source extract from Edn
* represent less than 50% of original Sources)
* Term of the licence in in the file licence.txt.
*
*******************************************************************************
*/
#include "tools_debug.h"
#include "tools_globals.h"
#include "Edn.h"
#define MAX_FILE_NAME (10240)
Edn::File::File(Edn::String &filename, int32_t LineNumber)
{
m_lineNumberOpen = 0;
SetCompleateName(filename);
}
Edn::File::File(Edn::String &filename, Edn::String &folder, int32_t lineNumber)
{
Edn::String tmpString = folder;
tmpString += '/';
tmpString += filename;
SetCompleateName(tmpString);
m_lineNumberOpen = lineNumber;
}
Edn::File::~File(void)
{
// nothing to do ...
}
Edn::String Edn::File::GetFolder(void)
{
return m_folder;
}
Edn::String Edn::File::GetShortFilename(void)
{
return m_shortFilename;
}
Edn::String Edn::File::GetCompleateName(void)
{
Edn::String out;
out = m_folder;
out += '/';
out += m_shortFilename;
}
const Edn::File& Edn::File::operator= (const Edn::File &ednF )
{
if( this != &ednF ) // avoid copy to itself
{
m_folder = ednF.m_folder;
m_shortFilename = ednF.m_shortFilename;
m_lineNumberOpen = ednF.m_lineNumberOpen;
}
return *this;
}
void Edn::File::SetCompleateName(Edn::String &newFilename)
{
char buf[MAX_FILE_NAME];
memset(buf, 0, MAX_FILE_NAME);
char * ok;
// Reset ALL DATA :
m_folder = "";
m_shortFilename = "";
m_lineNumberOpen = 0;
Edn::String destFilename;
if (newFilename.Size() == 0) {
destFilename = "no-name";
} else {
destFilename = newFilename;
}
if ('/' != *destFilename.c_str()) {
// Get the command came from the running of the program :
char cCurrentPath[FILENAME_MAX];
if (!getcwd(cCurrentPath, FILENAME_MAX)) {
return;
}
cCurrentPath[FILENAME_MAX - 1] = '\0';
Edn::String tmpFilename = destFilename;
destFilename = cCurrentPath;
destFilename += '/';
destFilename += tmpFilename;
}
// Get the real Path of the current File
ok = realpath(destFilename.c_str(), buf);
if (!ok) {
int32_t lastPos = destFilename.FindBack('/');
if (-1 != lastPos) {
// Get the FileName
Edn::String tmpFilename = destFilename.Extract(lastPos+1);
destFilename.Remove(lastPos, destFilename.Size() - lastPos);
//EDN_DEBUG("try to find :\"" << destFilename.c_str() << "\" / \"" << tmpFilename.c_str() << "\" ");
ok = realpath(destFilename.c_str(), buf);
if (!ok) {
EDN_ERROR("Can not find real Path name of \"" << destFilename.c_str() << "\"");
m_shortFilename = tmpFilename;
m_folder = destFilename;
} else {
// ALL is OK ...
m_shortFilename = tmpFilename;
m_folder = destFilename;
}
} else {
EDN_WARNING("file : \"" << destFilename.c_str() << "\" ==> No data???");
// Basic ERROR ...
m_shortFilename = destFilename;
}
} else {
destFilename = buf;
int32_t lastPos = destFilename.FindBack('/');
if (-1 != lastPos) {
m_shortFilename = destFilename.Extract(lastPos+1);
m_folder = destFilename.Extract(0, lastPos);
} else {
// Basic ERROR ...
EDN_WARNING("file : \"" << destFilename.c_str() << "\" ==> No data???");
m_shortFilename = destFilename;
}
}
EDN_DEBUG("Set FileName :\"" << m_folder.c_str() << "\" / \"" << m_shortFilename.c_str() << "\" ");
}
int32_t Edn::File::GetLineNumber(void)
{
return m_lineNumberOpen;
}