11 #include "FileStreams.h"
13 static inline HRESULT
ConvertBoolToHRESULT(bool result
)
15 // return result ? S_OK: E_FAIL;
17 return result
? S_OK
: (::GetLastError());
19 return result
? S_OK
: E_FAIL
;
23 bool CInFileStream::Open(LPCTSTR fileName
)
25 return File
.Open(fileName
);
30 bool CInFileStream::Open(LPCWSTR fileName
)
32 return File
.Open(fileName
);
37 STDMETHODIMP
CInFileStream::Read(void *data
, UInt32 size
, UInt32
*processedSize
)
41 UInt32 realProcessedSize
;
42 bool result
= File
.ReadPart(data
, size
, realProcessedSize
);
43 if(processedSize
!= NULL
)
44 *processedSize
= realProcessedSize
;
45 return ConvertBoolToHRESULT(result
);
49 if(processedSize
!= NULL
)
51 ssize_t res
= File
.Read(data
, (size_t)size
);
54 if(processedSize
!= NULL
)
55 *processedSize
= (UInt32
)res
;
62 STDMETHODIMP
CStdInFileStream::Read(void *data
, UInt32 size
, UInt32
*processedSize
)
65 UInt32 realProcessedSize
;
66 BOOL res
= ::ReadFile(GetStdHandle(STD_INPUT_HANDLE
),
67 data
, size
, (DWORD
*)&realProcessedSize
, NULL
);
68 if(processedSize
!= NULL
)
69 *processedSize
= realProcessedSize
;
70 if (res
== FALSE
&& GetLastError() == ERROR_BROKEN_PIPE
)
72 return ConvertBoolToHRESULT(res
!= FALSE
);
76 if(processedSize
!= NULL
)
81 res
= read(0, data
, (size_t)size
);
83 while (res
< 0 && (errno
== EINTR
));
86 if(processedSize
!= NULL
)
87 *processedSize
= (UInt32
)res
;
95 STDMETHODIMP
CInFileStream::Seek(Int64 offset
, UInt32 seekOrigin
,
99 return STG_E_INVALIDFUNCTION
;
103 UInt64 realNewPosition
;
104 bool result
= File
.Seek(offset
, seekOrigin
, realNewPosition
);
105 if(newPosition
!= NULL
)
106 *newPosition
= realNewPosition
;
107 return ConvertBoolToHRESULT(result
);
111 off_t res
= File
.Seek(offset
, seekOrigin
);
114 if(newPosition
!= NULL
)
115 *newPosition
= (UInt64
)res
;
121 STDMETHODIMP
CInFileStream::GetSize(UInt64
*size
)
123 return ConvertBoolToHRESULT(File
.GetLength(*size
));
127 //////////////////////////
130 bool COutFileStream::Create(LPCTSTR fileName
, bool createAlways
)
132 return File
.Create(fileName
, createAlways
);
137 bool COutFileStream::Create(LPCWSTR fileName
, bool createAlways
)
139 return File
.Create(fileName
, createAlways
);
144 STDMETHODIMP
COutFileStream::Write(const void *data
, UInt32 size
, UInt32
*processedSize
)
148 UInt32 realProcessedSize
;
149 bool result
= File
.WritePart(data
, size
, realProcessedSize
);
150 if(processedSize
!= NULL
)
151 *processedSize
= realProcessedSize
;
152 return ConvertBoolToHRESULT(result
);
156 if(processedSize
!= NULL
)
158 ssize_t res
= File
.Write(data
, (size_t)size
);
161 if(processedSize
!= NULL
)
162 *processedSize
= (UInt32
)res
;
168 STDMETHODIMP
COutFileStream::Seek(Int64 offset
, UInt32 seekOrigin
,
172 return STG_E_INVALIDFUNCTION
;
175 UInt64 realNewPosition
;
176 bool result
= File
.Seek(offset
, seekOrigin
, realNewPosition
);
177 if(newPosition
!= NULL
)
178 *newPosition
= realNewPosition
;
179 return ConvertBoolToHRESULT(result
);
183 off_t res
= File
.Seek(offset
, seekOrigin
);
186 if(newPosition
!= NULL
)
187 *newPosition
= (UInt64
)res
;
193 STDMETHODIMP
COutFileStream::SetSize(Int64 newSize
)
197 if(!File
.Seek(0, FILE_CURRENT
, currentPos
))
199 bool result
= File
.SetLength(newSize
);
201 result
= result
&& File
.Seek(currentPos
, currentPos2
);
202 return result
? S_OK
: E_FAIL
;
209 STDMETHODIMP
CStdOutFileStream::Write(const void *data
, UInt32 size
, UInt32
*processedSize
)
211 if(processedSize
!= NULL
)
215 UInt32 realProcessedSize
;
219 // Seems that Windows doesn't like big amounts writing to stdout.
220 // So we limit portions by 32KB.
221 UInt32 sizeTemp
= (1 << 15);
224 res
= ::WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
),
225 data
, sizeTemp
, (DWORD
*)&realProcessedSize
, NULL
);
226 size
-= realProcessedSize
;
227 data
= (const void *)((const Byte
*)data
+ realProcessedSize
);
228 if(processedSize
!= NULL
)
229 *processedSize
+= realProcessedSize
;
231 return ConvertBoolToHRESULT(res
!= FALSE
);
238 res
= write(1, data
, (size_t)size
);
240 while (res
< 0 && (errno
== EINTR
));
243 if(processedSize
!= NULL
)
244 *processedSize
= (UInt32
)res
;