1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
//===-- MIUtilFileStd.cpp ---------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Third party headers
#include <stdio.h>
#include <assert.h>
#include <string.h> // For strerror()
#include <cerrno>
// In-house headers:
#include "MIUtilFileStd.h"
#include "MICmnResources.h"
//++ ------------------------------------------------------------------------------------
// Details: CMIUtilFileStd constructor.
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
CMIUtilFileStd::CMIUtilFileStd(void)
: m_fileNamePath(CMIUtilString())
, m_pFileHandle(nullptr)
#if defined(_MSC_VER)
, m_constCharNewLine("\r\n")
#else
, m_constCharNewLine("\n")
#endif // #if defined( _MSC_VER )
, m_bFileError(false)
{
}
//++ ------------------------------------------------------------------------------------
// Details: CMIUtilFileStd destructor.
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
CMIUtilFileStd::~CMIUtilFileStd(void)
{
Close();
}
//++ ------------------------------------------------------------------------------------
// 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.
//--
bool
CMIUtilFileStd::CreateWrite(const CMIUtilString &vFileNamePath, bool &vwrbNewCreated)
{
// 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");
#else
// Open a file with exclusive write and shared read permissions
m_pFileHandle = ::_fsopen(vFileNamePath.c_str(), "wb", _SH_DENYWR);
#endif // !defined( _MSC_VER )
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;
}
//++ ------------------------------------------------------------------------------------
// 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.
//--
bool
CMIUtilFileStd::Write(const CMIUtilString &vData)
{
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;
}
//++ ------------------------------------------------------------------------------------
// 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.
//--
bool
CMIUtilFileStd::Write(const char *vpData, const MIuint vCharCnt)
{
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;
}
//++ ------------------------------------------------------------------------------------
// Details: Close existing opened file. Note Close() must must an open!
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
void
CMIUtilFileStd::Close(void)
{
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
}
//++ ------------------------------------------------------------------------------------
// Details: Retrieve state of whether the file is ok.
// Type: Method.
// Args: None.
// Return: True - file ok.
// False - file has a problem.
// Throws: None.
//--
bool
CMIUtilFileStd::IsOk(void) const
{
return !m_bFileError;
}
//++ ------------------------------------------------------------------------------------
// Details: Status on a file existing already.
// Type: Method.
// Args: vFileNamePath.
// Return: True - Exists.
// False - Not found.
// Throws: None.
//--
bool
CMIUtilFileStd::IsFileExist(const CMIUtilString &vFileNamePath) const
{
if (vFileNamePath.empty())
return false;
FILE *pTmp = nullptr;
pTmp = ::fopen(vFileNamePath.c_str(), "wb");
if (pTmp != nullptr)
{
::fclose(pTmp);
return true;
}
return false;
}
//++ ------------------------------------------------------------------------------------
// Details: Retrieve the file current carriage line return characters used.
// Type: Method.
// Args: None.
// Return: CMIUtilString & - Text.
// Throws: None.
//--
const CMIUtilString &
CMIUtilFileStd::GetLineReturn(void) const
{
return m_constCharNewLine;
}
//++ ------------------------------------------------------------------------------------
// 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.
//--
CMIUtilString
CMIUtilFileStd::StripOffFileName(const CMIUtilString &vDirectoryPath)
{
const size_t nPos = vDirectoryPath.rfind('\\');
size_t nPos2 = vDirectoryPath.rfind('/');
if ((nPos == std::string::npos) && (nPos2 == std::string::npos))
return vDirectoryPath;
if (nPos > nPos2)
nPos2 = nPos;
const CMIUtilString strPath(vDirectoryPath.substr(0, nPos2).c_str());
return strPath;
}
//++ ------------------------------------------------------------------------------------
// Details: Return either backslash or forward slash appropriate to the OS this application
// is running on.
// Type: Static method.
// Args: None.
// Return: char - '/' or '\' character.
// Throws: None.
//--
char
CMIUtilFileStd::GetSlash(void)
{
#if !defined(_MSC_VER)
return '/';
#else
return '\\';
#endif // !defined( _MSC_VER )
}
|