1cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky// Common/C_FileIO.cpp
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "C_FileIO.h"
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <fcntl.h>
6cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#ifdef _WIN32
7cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#include <io.h>
8cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#else
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <unistd.h>
10cd66d540cead3f8200b0c73bad9c276d67896c3dDavid Srbecky#endif
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
12baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace NC {
13baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace NFile {
14baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace NIO {
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
16baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CFileBase::OpenBinary(const char *name, int flags)
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifdef O_BINARY
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  flags |= O_BINARY;
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Close();
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _handle = ::open(name, flags, 0666);
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return _handle != -1;
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
26baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CFileBase::Close()
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (_handle == -1)
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return true;
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (close(_handle) != 0)
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return false;
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _handle = -1;
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return true;
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
36baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CFileBase::GetLength(UInt64 &length) const
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  off_t curPos = Seek(0, SEEK_CUR);
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  off_t lengthTemp = Seek(0, SEEK_END);
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Seek(curPos, SEEK_SET);
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  length = (UInt64)lengthTemp;
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return true;
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
45baa3858d3f5d128a5c8466b700098109edcad5f2repo syncoff_t CFileBase::Seek(off_t distanceToMove, int moveMethod) const
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return ::lseek(_handle, distanceToMove, moveMethod);
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/////////////////////////
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// CInFile
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
53baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CInFile::Open(const char *name)
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return CFileBase::OpenBinary(name, O_RDONLY);
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
58baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool CInFile::OpenShared(const char *name, bool)
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return Open(name);
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
63baa3858d3f5d128a5c8466b700098109edcad5f2repo syncssize_t CInFile::Read(void *data, size_t size)
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return read(_handle, data, size);
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync/////////////////////////
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// COutFile
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
71baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool COutFile::Create(const char *name, bool createAlways)
72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (createAlways)
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Close();
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _handle = ::creat(name, 0666);
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return _handle != -1;
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return OpenBinary(name, O_CREAT | O_EXCL | O_WRONLY);
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
82baa3858d3f5d128a5c8466b700098109edcad5f2repo syncbool COutFile::Open(const char *name, DWORD creationDisposition)
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return Create(name, false);
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
87baa3858d3f5d128a5c8466b700098109edcad5f2repo syncssize_t COutFile::Write(const void *data, size_t size)
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return write(_handle, data, size);
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}}}
93