Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
FStreamSupport.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
6#include "Stroika/Foundation/Execution/Exceptions.h"
7#include "Stroika/Foundation/Execution/Throw.h"
8#if qStroika_Foundation_Common_Platform_Windows
9#include "Stroika/Foundation/Execution/Platform/Windows/Exception.h"
10#endif
11
12#include "FStreamSupport.h"
13
14using namespace Stroika::Foundation;
15using namespace Stroika::Foundation::Streams;
17
18namespace {
19 /**
20 * fstream::open () sets failbit on failed open. But most other operations set 'badbit' when things
21 * go wrong (like failed writes).
22 */
23 constexpr auto kErrBitsToTreatAsExceptions_ = ifstream::badbit | ifstream::failbit;
24}
25
26/*
27 ********************************************************************************
28 ********************** iostream::OpenInputFileStream ***************************
29 ********************************************************************************
30 */
31ifstream& Streams::iostream::OpenInputFileStream (ifstream* ifStream, const filesystem::path& fileName, ios_base::openmode mode)
32{
33 RequireNotNull (ifStream);
34 ifStream->exceptions (kErrBitsToTreatAsExceptions_); // so throws on failed open
35 ifStream->open (fileName, mode | ios_base::in);
36 Assert (ifStream->is_open ());
37 return *ifStream;
38}
39
40ifstream& Streams::iostream::OpenInputFileStream (ifstream& tmpIFStream, const filesystem::path& fileName, ios_base::openmode mode)
41{
42 OpenInputFileStream (&tmpIFStream, fileName, mode);
43 return tmpIFStream;
44}
45
46/*
47 ********************************************************************************
48 ****************** StreamUtils::OpenOutputFileStream ***************************
49 ********************************************************************************
50 */
51ofstream& Streams::iostream::OpenOutputFileStream (ofstream* ofStream, const filesystem::path& fileName, ios_base::openmode mode)
52{
53 RequireNotNull (ofStream);
54 ofStream->exceptions (kErrBitsToTreatAsExceptions_); // so throws on failed open
55 ofStream->open (fileName, mode | ios_base::out);
56 Assert (ofStream->is_open ());
57 return *ofStream;
58}
59
60ofstream& Streams::iostream::OpenOutputFileStream (ofstream& tmpOfStream, const filesystem::path& fileName, ios_base::openmode mode)
61{
62 OpenOutputFileStream (&tmpOfStream, fileName, mode);
63 return tmpOfStream;
64}
#define RequireNotNull(p)
Definition Assertions.h:347