[Common-dev] CR: imlement additional fileio operations on windows
Ryan Gammon rgammon at real.comThe attached patch copies & pastes the standard c library
implementations for reading & writing files to buffers/strings, and for
moving/renaming files.
For 150Cay, maybe for HEAD.
Ideally on HEAD we could figure this out in a better way.
-------------- next part --------------
? Makefile
? Umakefil.upp
? common_fileio.sln
? common_fileio.vcproj
? dbg32
? vc70.idb
? winfileio.patch
Index: win.pcf
===================================================================
RCS file: /cvsroot/common/fileio/win.pcf,v
retrieving revision 1.7
diff -u -r1.7 win.pcf
--- win.pcf 9 Jul 2004 18:20:35 -0000 1.7
+++ win.pcf 21 Apr 2005 01:21:16 -0000
@@ -64,6 +64,7 @@
'platform/win/winff.cpp',
'platform/win/winfile.cpp',
'platform/win/filespec.cpp',
- 'platform/win/filespecutils.cpp')
+ 'platform/win/filespecutils.cpp',
+ 'platform/win/filespecutils_win.cpp')
cc.args['default'] = string.replace(cc.args['default'], '/GX-', '/GX')
Index: platform/win/filespecutils.cpp
===================================================================
RCS file: /cvsroot/common/fileio/platform/win/filespecutils.cpp,v
retrieving revision 1.7
diff -u -r1.7 filespecutils.cpp
--- platform/win/filespecutils.cpp 9 Jul 2004 18:20:11 -0000 1.7
+++ platform/win/filespecutils.cpp 21 Apr 2005 01:13:55 -0000
@@ -58,6 +58,8 @@
#include <shlobj.h>
#include "hlxclib/sys/stat.h"
+#include <stdio.h>
+#include <errno.h>
//////////////////////////////////////////////////////////
//
@@ -695,29 +697,178 @@
}
//******************************************************************************
+//******************************************************************************
HX_RESULT CHXFileSpecUtils::ReadBinaryFile(const CHXFileSpecifier& fileSpec, IHXBuffer*& pOutBuffer)
{
- HX_ASSERT(!"Not yet implemented on this platform");
- return HXR_NOTIMPL;
+ if(!fileSpec.IsSet())
+ {
+ return HXR_INVALID_PARAMETER;
+ }
+
+ CHXString strFilename = fileSpec.GetPathName();
+ FILE* pFile = NULL;
+ struct stat aStatBuf;
+ int nRet;
+ HX_RESULT retVal = HXR_OK;
+
+ pFile = fopen(strFilename, "rb");
+ if(!pFile)
+ {
+ if(errno == EACCES || errno == ENOENT)
+ {
+ retVal = HXR_DOC_MISSING;
+ }
+ else
+ {
+ retVal = HXR_FAIL;
+ }
+ }
+
+ if(SUCCEEDED(retVal))
+ {
+ nRet = fstat(fileno(pFile), &aStatBuf);
+ if(nRet != 0)
+ {
+ retVal = HXR_FAIL;
+ }
+ }
+
+ if(SUCCEEDED(retVal))
+ {
+ retVal = pOutBuffer->SetSize(aStatBuf.st_size);
+ }
+
+ if(SUCCEEDED(retVal))
+ {
+ void* pBufData = pOutBuffer->GetBuffer();
+ int nRead;
+
+ nRead = fread(pBufData, aStatBuf.st_size, 1, pFile);
+
+ HX_ASSERT(nRead == aStatBuf.st_size);
+ }
+
+ // Clean up
+ if(pFile)
+ {
+ fclose(pFile);
+ }
+
+ return retVal;
}
HX_RESULT CHXFileSpecUtils::ReadTextFile(const CHXFileSpecifier& fileSpec, CHXString& outStr)
{
- HX_ASSERT(!"Not yet implemented on this platform");
- return HXR_NOTIMPL;
+ if(!fileSpec.IsSet())
+ {
+ return HXR_INVALID_PARAMETER;
+ }
+
+ CHXString strFilename = fileSpec.GetPathName();
+ FILE* pFile = NULL;
+ struct stat aStatBuf;
+ int nRet;
+ HX_RESULT retVal = HXR_OK;
+
+ pFile = fopen(strFilename, "r");
+ if(!pFile)
+ {
+ if(errno == EACCES || errno == ENOENT)
+ {
+ retVal = HXR_DOC_MISSING;
+ }
+ else
+ {
+ retVal = HXR_FAIL;
+ }
+ }
+
+ if(SUCCEEDED(retVal))
+ {
+ nRet = fstat(fileno(pFile), &aStatBuf);
+ if(nRet != 0)
+ {
+ retVal = HXR_FAIL;
+ }
+ }
+
+ if(SUCCEEDED(retVal))
+ {
+ char* szBufData = outStr.GetBufferSetLength(aStatBuf.st_size);
+ int nRead;
+
+ nRead = fread(szBufData, aStatBuf.st_size, 1, pFile);
+
+ HX_ASSERT(nRead == 1);
+ }
+
+ // Clean up
+ if(pFile)
+ {
+ fclose(pFile);
+ }
+
+ return retVal;
}
HX_RESULT CHXFileSpecUtils::WriteBinaryFile(CHXFileSpecifier& fileSpec, IHXBuffer* inBuffer, BOOL bReplaceExistingFile)
{
- // the first parameter is not const because on the Mac it can change during the call
- HX_ASSERT(!"Not yet implemented on this platform");
- return HXR_NOTIMPL;
+ HX_RESULT res = HXR_FAIL;
+
+ if(!fileSpec.IsSet())
+ {
+ return HXR_INVALID_PARAMETER;
+ }
+
+ BOOL bExists = FileExists(fileSpec);
+ if (!bReplaceExistingFile && bExists)
+ {
+ return HXR_FILE_EXISTS;
+ }
+
+ FILE* pFile = fopen(fileSpec.GetPathName(), "wb");
+ if(pFile)
+ {
+ int nWritten;
+ int nBufferSize = inBuffer->GetSize();
+ void* pBufferData = inBuffer->GetBuffer();
+
+ nWritten = fwrite(pBufferData, nBufferSize, 1, pFile);
+ if(nWritten == 1)
+ {
+ res = HXR_OK;
+ }
+ fclose(pFile);
+ }
+
+ return res;
}
HX_RESULT CHXFileSpecUtils::WriteTextFile(CHXFileSpecifier& fileSpec, const CHXString& inStr, BOOL bReplaceExistingFile)
{
- // the first parameter is not const because on the Mac it can change during the call
- HX_ASSERT(!"Not yet implemented on this platform");
- return HXR_NOTIMPL;
+ HX_RESULT res = HXR_FAIL;
+
+ if(!fileSpec.IsSet())
+ {
+ return HXR_INVALID_PARAMETER;
+ }
+
+ BOOL bExists = FileExists(fileSpec);
+ if (!bReplaceExistingFile && bExists)
+ {
+ return HXR_FILE_EXISTS;
+ }
+
+ FILE* pFile = fopen(fileSpec.GetPathName(), "w");
+ if(pFile)
+ {
+ if(fputs(inStr, pFile) >= 0)
+ {
+ res = HXR_OK;
+ }
+ fclose(pFile);
+ }
+
+ return res;
}
Index: pub/filespecutils.h
===================================================================
RCS file: /cvsroot/common/fileio/pub/filespecutils.h,v
retrieving revision 1.8
diff -u -r1.8 filespecutils.h
--- pub/filespecutils.h 19 Jul 2004 16:57:32 -0000 1.8
+++ pub/filespecutils.h 13 Apr 2005 01:20:12 -0000
@@ -87,10 +87,8 @@
static HX_RESULT MakeFileNotReadOnly(const CHXFileSpecifier& fileSpec);
static BOOL IsDiskAudioCD(const CHXDirSpecifier& volSpec);
#endif
-
-#if defined(_MACINTOSH) || defined(_MAC_UNIX) || defined(_UNIX)
+
static HX_RESULT RenameMoveFile(CHXFileSpecifier& fileSpec, const char* pNewNameIfAny, const CHXDirSpecifier* pNewDirectoryIfAny);
-#endif
// file read & write
//