1 //===-- MIUtilFileStd.cpp ---------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // File: MIUtilFileStd.cpp
13 // Overview: CMIUtilFileStd implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
22 // Third party headers
25 #include <string.h> // For strerror()
29 #include "MIUtilFileStd.h"
30 #include "MICmnResources.h"
32 //++ ------------------------------------------------------------------------------------
33 // Details: CMIUtilFileStd constructor.
39 CMIUtilFileStd::CMIUtilFileStd(void)
40 : m_fileNamePath(CMIUtilString())
41 , m_pFileHandle(nullptr)
43 , m_constCharNewLine("\r\n")
45 , m_constCharNewLine("\n")
46 #endif // #if defined( _MSC_VER )
51 //++ ------------------------------------------------------------------------------------
52 // Details: CMIUtilFileStd destructor.
58 CMIUtilFileStd::~CMIUtilFileStd(void)
63 //++ ------------------------------------------------------------------------------------
64 // Details: Open file for writing. On the first call to this function after *this object
65 // is created the file is either created or replace, from then on open only opens
68 // Args: vFileNamePath - (R) File name path.
69 // vwrbNewCreated - (W) True - file recreated, false - file appended too.
70 // Return: MIstatus::success - Functional succeeded.
71 // MIstatus::failure - Functional failed.
75 CMIUtilFileStd::CreateWrite(const CMIUtilString &vFileNamePath, bool &vwrbNewCreated)
79 vwrbNewCreated = false;
81 if (vFileNamePath.empty())
84 SetErrorDescription(MIRSRC(IDS_UTIL_FILE_ERR_INVALID_PATHNAME));
85 return MIstatus::failure;
88 // File is already open so exit
89 if (m_pFileHandle != nullptr)
90 return MIstatus::success;
92 #if !defined(_MSC_VER)
93 // Open with 'write' and 'binary' mode
94 m_pFileHandle = ::fopen(vFileNamePath.c_str(), "wb");
96 // Open a file with exclusive write and shared read permissions
97 m_pFileHandle = ::_fsopen(vFileNamePath.c_str(), "wb", _SH_DENYWR);
98 #endif // !defined( _MSC_VER )
100 if (m_pFileHandle == nullptr)
103 SetErrorDescriptionn(MIRSRC(IDS_UTIL_FILE_ERR_OPENING_FILE), strerror(errno), vFileNamePath.c_str());
104 return MIstatus::failure;
107 vwrbNewCreated = true;
108 m_fileNamePath = vFileNamePath;
110 return MIstatus::success;
113 //++ ------------------------------------------------------------------------------------
114 // Details: Write data to existing opened file.
116 // Args: vData - (R) Text data.
117 // Return: MIstatus::success - Functional succeeded.
118 // MIstatus::failure - Functional failed.
122 CMIUtilFileStd::Write(const CMIUtilString &vData)
124 if (vData.size() == 0)
125 return MIstatus::success;
128 return MIstatus::failure;
130 if (m_pFileHandle == nullptr)
133 SetErrorDescriptionn(MIRSRC(IDE_UTIL_FILE_ERR_WRITING_NOTOPEN), m_fileNamePath.c_str());
134 return MIstatus::failure;
137 // Get the string size
138 MIuint size = vData.size();
139 if (::fwrite(vData.c_str(), 1, size, m_pFileHandle) == size)
141 // Flush the data to the file
142 ::fflush(m_pFileHandle);
143 return MIstatus::success;
146 // Not all of the data has been transferred
148 SetErrorDescriptionn(MIRSRC(IDE_UTIL_FILE_ERR_WRITING_FILE), m_fileNamePath.c_str());
149 return MIstatus::failure;
152 //++ ------------------------------------------------------------------------------------
153 // Details: Write data to existing opened file.
155 // Args: vData - (R) Text data.
156 // vCharCnt - (R) Text data length.
157 // Return: MIstatus::success - Functional succeeded.
158 // MIstatus::failure - Functional failed.
162 CMIUtilFileStd::Write(const MIchar *vpData, const MIuint vCharCnt)
165 return MIstatus::success;
168 return MIstatus::failure;
170 if (m_pFileHandle == nullptr)
173 SetErrorDescriptionn(MIRSRC(IDE_UTIL_FILE_ERR_WRITING_NOTOPEN), m_fileNamePath.c_str());
174 return MIstatus::failure;
177 if (::fwrite(vpData, 1, vCharCnt, m_pFileHandle) == vCharCnt)
179 // Flush the data to the file
180 ::fflush(m_pFileHandle);
181 return MIstatus::success;
184 // Not all of the data has been transferred
186 SetErrorDescriptionn(MIRSRC(IDE_UTIL_FILE_ERR_WRITING_FILE), m_fileNamePath.c_str());
187 return MIstatus::failure;
190 //++ ------------------------------------------------------------------------------------
191 // Details: Close existing opened file. Note Close() must must an open!
198 CMIUtilFileStd::Close(void)
200 if (m_pFileHandle == nullptr)
203 ::fclose(m_pFileHandle);
204 m_pFileHandle = nullptr;
205 // m_bFileError = false; Do not reset as want to remain until next attempt at open or create
208 //++ ------------------------------------------------------------------------------------
209 // Details: Retrieve state of whether the file is ok.
212 // Return: True - file ok.
213 // False - file has a problem.
217 CMIUtilFileStd::IsOk(void) const
219 return !m_bFileError;
222 //++ ------------------------------------------------------------------------------------
223 // Details: Status on a file existing already.
225 // Args: vFileNamePath.
226 // Return: True - Exists.
227 // False - Not found.
231 CMIUtilFileStd::IsFileExist(const CMIUtilString &vFileNamePath) const
233 if (vFileNamePath.empty())
236 FILE *pTmp = nullptr;
237 pTmp = ::fopen(vFileNamePath.c_str(), "wb");
247 //++ ------------------------------------------------------------------------------------
248 // Details: Retrieve the file current carriage line return characters used.
251 // Return: CMIUtilString & - Text.
254 const CMIUtilString &
255 CMIUtilFileStd::GetLineReturn(void) const
257 return m_constCharNewLine;
260 //++ ------------------------------------------------------------------------------------
261 // Details: Given a file name directory path, strip off the filename and return the path.
262 // It look for either backslash or forward slash.
264 // Args: vDirectoryPath - (R) Text directory path.
265 // Return: CMIUtilString - Directory path.
269 CMIUtilFileStd::StripOffFileName(const CMIUtilString &vDirectoryPath) const
271 const MIint nPos = vDirectoryPath.rfind('\\');
272 MIint nPos2 = vDirectoryPath.rfind('/');
273 if ((nPos == (MIint)std::string::npos) && (nPos2 == (MIint)std::string::npos))
274 return vDirectoryPath;
279 const CMIUtilString strPath(vDirectoryPath.substr(0, nPos2).c_str());
283 //++ ------------------------------------------------------------------------------------
284 // Details: Return either backslash or forward slash appropriate to the OS this applilcation
286 // Type: Static method.
288 // Return: MIchar - '/' or '\' character.
292 CMIUtilFileStd::GetSlash(void)
294 #if !defined(_MSC_VER)
298 #endif // !defined( _MSC_VER )