2014-05-16 18:51:01 +08:00
|
|
|
//===-- MIUtilFileStd.cpp ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//++
|
2014-11-18 02:06:21 +08:00
|
|
|
// File: MIUtilFileStd.cpp
|
2014-05-16 18:51:01 +08:00
|
|
|
//
|
2014-11-18 02:06:21 +08:00
|
|
|
// Overview: CMIUtilFileStd implementation.
|
2014-05-16 18:51:01 +08:00
|
|
|
//
|
2014-11-18 02:06:21 +08:00
|
|
|
// Environment: Compilers: Visual C++ 12.
|
|
|
|
// gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
|
|
|
|
// Libraries: See MIReadmetxt.
|
2014-05-16 18:51:01 +08:00
|
|
|
//
|
2014-11-18 02:06:21 +08:00
|
|
|
// Copyright: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
|
|
|
|
|
|
|
// Third party headers
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2014-11-18 02:06:21 +08:00
|
|
|
#include <string.h> // For strerror()
|
2014-06-25 00:35:50 +08:00
|
|
|
#include <cerrno>
|
2014-05-16 18:51:01 +08:00
|
|
|
|
|
|
|
// In-house headers:
|
|
|
|
#include "MIUtilFileStd.h"
|
|
|
|
#include "MICmnResources.h"
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CMIUtilFileStd constructor.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
CMIUtilFileStd::CMIUtilFileStd(void)
|
|
|
|
: m_fileNamePath(CMIUtilString())
|
|
|
|
, m_pFileHandle(nullptr)
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
, m_constCharNewLine("\r\n")
|
2014-05-16 18:51:01 +08:00
|
|
|
#else
|
2014-11-18 02:06:21 +08:00
|
|
|
, m_constCharNewLine("\n")
|
2014-05-16 18:51:01 +08:00
|
|
|
#endif // #if defined( _MSC_VER )
|
2014-11-18 02:06:21 +08:00
|
|
|
, m_bFileError(false)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CMIUtilFileStd destructor.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
CMIUtilFileStd::~CMIUtilFileStd(void)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
Close();
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Open file for writing. On the first call to this function after *this object
|
|
|
|
// is created the file is either created or replace, from then on open only opens
|
|
|
|
// an existing file.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vFileNamePath - (R) File name path.
|
|
|
|
// vwrbNewCreated - (W) True - file recreated, false - file appended too.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
|
|
|
CMIUtilFileStd::CreateWrite(const CMIUtilString &vFileNamePath, bool &vwrbNewCreated)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
// Reset
|
|
|
|
m_bFileError = false;
|
|
|
|
vwrbNewCreated = false;
|
|
|
|
|
|
|
|
if (vFileNamePath.empty())
|
|
|
|
{
|
|
|
|
m_bFileError = true;
|
|
|
|
SetErrorDescription(MIRSRC(IDS_UTIL_FILE_ERR_INVALID_PATHNAME));
|
|
|
|
return MIstatus::failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
// File is already open so exit
|
|
|
|
if (m_pFileHandle != nullptr)
|
|
|
|
return MIstatus::success;
|
|
|
|
|
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
// Open with 'write' and 'binary' mode
|
|
|
|
m_pFileHandle = ::fopen(vFileNamePath.c_str(), "wb");
|
2014-05-16 18:51:01 +08:00
|
|
|
#else
|
2014-11-18 02:06:21 +08:00
|
|
|
// Open a file with exclusive write and shared read permissions
|
|
|
|
m_pFileHandle = ::_fsopen(vFileNamePath.c_str(), "wb", _SH_DENYWR);
|
2014-05-16 18:51:01 +08:00
|
|
|
#endif // !defined( _MSC_VER )
|
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
if (m_pFileHandle == nullptr)
|
|
|
|
{
|
|
|
|
m_bFileError = true;
|
|
|
|
SetErrorDescriptionn(MIRSRC(IDS_UTIL_FILE_ERR_OPENING_FILE), strerror(errno), vFileNamePath.c_str());
|
|
|
|
return MIstatus::failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
vwrbNewCreated = true;
|
|
|
|
m_fileNamePath = vFileNamePath;
|
|
|
|
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Write data to existing opened file.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vData - (R) Text data.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
|
|
|
CMIUtilFileStd::Write(const CMIUtilString &vData)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
if (vData.size() == 0)
|
|
|
|
return MIstatus::success;
|
|
|
|
|
|
|
|
if (m_bFileError)
|
|
|
|
return MIstatus::failure;
|
|
|
|
|
|
|
|
if (m_pFileHandle == nullptr)
|
|
|
|
{
|
|
|
|
m_bFileError = true;
|
|
|
|
SetErrorDescriptionn(MIRSRC(IDE_UTIL_FILE_ERR_WRITING_NOTOPEN), m_fileNamePath.c_str());
|
|
|
|
return MIstatus::failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the string size
|
|
|
|
MIuint size = vData.size();
|
|
|
|
if (::fwrite(vData.c_str(), 1, size, m_pFileHandle) == size)
|
|
|
|
{
|
|
|
|
// Flush the data to the file
|
|
|
|
::fflush(m_pFileHandle);
|
|
|
|
return MIstatus::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not all of the data has been transferred
|
|
|
|
m_bFileError = true;
|
|
|
|
SetErrorDescriptionn(MIRSRC(IDE_UTIL_FILE_ERR_WRITING_FILE), m_fileNamePath.c_str());
|
|
|
|
return MIstatus::failure;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Write data to existing opened file.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vData - (R) Text data.
|
|
|
|
// vCharCnt - (R) Text data length.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
|
|
|
CMIUtilFileStd::Write(const MIchar *vpData, const MIuint vCharCnt)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
if (vCharCnt == 0)
|
|
|
|
return MIstatus::success;
|
|
|
|
|
|
|
|
if (m_bFileError)
|
|
|
|
return MIstatus::failure;
|
|
|
|
|
|
|
|
if (m_pFileHandle == nullptr)
|
|
|
|
{
|
|
|
|
m_bFileError = true;
|
|
|
|
SetErrorDescriptionn(MIRSRC(IDE_UTIL_FILE_ERR_WRITING_NOTOPEN), m_fileNamePath.c_str());
|
|
|
|
return MIstatus::failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (::fwrite(vpData, 1, vCharCnt, m_pFileHandle) == vCharCnt)
|
|
|
|
{
|
|
|
|
// Flush the data to the file
|
|
|
|
::fflush(m_pFileHandle);
|
|
|
|
return MIstatus::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not all of the data has been transferred
|
|
|
|
m_bFileError = true;
|
|
|
|
SetErrorDescriptionn(MIRSRC(IDE_UTIL_FILE_ERR_WRITING_FILE), m_fileNamePath.c_str());
|
|
|
|
return MIstatus::failure;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2014-05-16 18:51:01 +08:00
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Close existing opened file. Note Close() must must an open!
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
void
|
|
|
|
CMIUtilFileStd::Close(void)
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
if (m_pFileHandle == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
::fclose(m_pFileHandle);
|
|
|
|
m_pFileHandle = nullptr;
|
|
|
|
// m_bFileError = false; Do not reset as want to remain until next attempt at open or create
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Retrieve state of whether the file is ok.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: True - file ok.
|
|
|
|
// False - file has a problem.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
|
|
|
CMIUtilFileStd::IsOk(void) const
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
return !m_bFileError;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Status on a file existing already.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vFileNamePath.
|
|
|
|
// Return: True - Exists.
|
|
|
|
// False - Not found.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
bool
|
|
|
|
CMIUtilFileStd::IsFileExist(const CMIUtilString &vFileNamePath) const
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
if (vFileNamePath.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FILE *pTmp = nullptr;
|
|
|
|
pTmp = ::fopen(vFileNamePath.c_str(), "wb");
|
|
|
|
if (pTmp != nullptr)
|
|
|
|
{
|
|
|
|
::fclose(pTmp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Retrieve the file current carriage line return characters used.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: CMIUtilString & - Text.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
const CMIUtilString &
|
|
|
|
CMIUtilFileStd::GetLineReturn(void) const
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
return m_constCharNewLine;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Given a file name directory path, strip off the filename and return the path.
|
|
|
|
// It look for either backslash or forward slash.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vDirectoryPath - (R) Text directory path.
|
|
|
|
// Return: CMIUtilString - Directory path.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
CMIUtilString
|
|
|
|
CMIUtilFileStd::StripOffFileName(const CMIUtilString &vDirectoryPath) const
|
2014-05-16 18:51:01 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
const MIint nPos = vDirectoryPath.rfind('\\');
|
|
|
|
MIint nPos2 = vDirectoryPath.rfind('/');
|
|
|
|
if ((nPos == (MIint)std::string::npos) && (nPos2 == (MIint)std::string::npos))
|
|
|
|
return vDirectoryPath;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
if (nPos > nPos2)
|
|
|
|
nPos2 = nPos;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2014-11-18 02:06:21 +08:00
|
|
|
const CMIUtilString strPath(vDirectoryPath.substr(0, nPos2).c_str());
|
|
|
|
return strPath;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 00:35:50 +08:00
|
|
|
//++ ------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Return either backslash or forward slash appropriate to the OS this applilcation
|
|
|
|
// is running on.
|
|
|
|
// Type: Static method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIchar - '/' or '\' character.
|
|
|
|
// Throws: None.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2014-11-18 02:06:21 +08:00
|
|
|
MIchar
|
|
|
|
CMIUtilFileStd::GetSlash(void)
|
2014-06-25 00:35:50 +08:00
|
|
|
{
|
2014-11-18 02:06:21 +08:00
|
|
|
#if !defined(_MSC_VER)
|
|
|
|
return '/';
|
2014-06-25 00:35:50 +08:00
|
|
|
#else
|
2014-11-18 02:06:21 +08:00
|
|
|
return '\\';
|
2014-06-25 00:35:50 +08:00
|
|
|
#endif // !defined( _MSC_VER )
|
|
|
|
}
|