ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
textfile.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8#if !defined(ALIB_DOX) // todo remove
9
12#include "alib/alox.hpp"
13
14#include <fstream>
15
16
17namespace alib::files {
18
19
20//==================================================================================================
21//=== TextFile
22//==================================================================================================
23TextFile::TextFile(monomem::MonoAllocator* pAllocator)
24: allocator(pAllocator)
25, priorUsage(pAllocator->TakeSnapshot())
26{
27 ALIB_DBG( if( alib::FILES.IsBootstrapped())
28 {
29 Log_SetDomain( "ALIB/FILES", Scope::Path)
30 Log_SetDomain( "TXTFILE" , Scope::Filename)
31 } )
32 Reset();
33}
34
35TextFile::~TextFile()
36{
37 allocator->Reset(priorUsage);
38}
39
40void TextFile::Reset()
41{
42 allocator->Reset(priorUsage);
43 Lines= allocator->Emplace<std::vector<MAString, StdContMA<MAString>>>(*allocator);
44 Lines->reserve(200);
45}
46
47lang::system::SystemErrors TextFile::Read(const String& filePath, int extraSpacePercentage)
48{
49 errno= 0;
50 PathString tFilePath(filePath);
51 std::ifstream iFile( tFilePath.Terminate() );
52 if ( !iFile.is_open() )
53 {
54 auto result= lang::system::SystemErrors(errno);
55 Log_Error( "Error <{}: {!Q}> opening input file {!Q}", errno, result, filePath)
56 return result;
57 }
58 Log_Verbose( "file {!Q} opened for reading", filePath)
59
60 alib::ISReadLineN readOp= alib::ISReadLineN( &iFile );
61 String4K line; ALIB_DBG( line.DbgDisableBufferReplacementWarning(); )
62 while( !readOp.IsEOF )
63 {
64 line.Reset( readOp );
65 Lines->emplace_back( *allocator,(line.Length() * (100 + extraSpacePercentage)) / 100 + 1 );
66 Lines->back() << line;
67 }
68
69 Log_Info( "File {!Q}, {} lines read", filePath, Lines->size() )
70
71 return lang::system::SystemErrors::OK;
72}
73
74lang::system::SystemErrors TextFile::Write(const String& filePath)
75{
76 errno= 0;
77 PathString tFilePath(filePath);
78 std::ofstream oFile( tFilePath.Terminate() );
79 if ( !oFile.is_open() )
80 {
81 auto result= lang::system::SystemErrors(errno);
82 Log_Error( "Error <{}: {!Q}> opening output file {!Q}", errno, result, filePath)
83 return result;
84 }
85 Log_Verbose( "file {!Q} opened for writing", filePath)
86
87 for( auto& line : *Lines )
88 {
89 oFile << line << std::endl;
90 }
91 Log_Info( "File {!Q}, {} lines written", filePath, Lines->size() )
92
93 return lang::system::SystemErrors::OK;
94}
95
96
97} // namespace alib::files
98
99
100#endif // !defined(ALIB_DOX) // todo remove
#define Log_Error(...)
Definition macros.inl:70
#define Log_Verbose(...)
Definition macros.inl:67
#define Log_SetDomain(...)
Definition macros.inl:60
#define ALIB_DBG(...)
Definition alib.hpp:457
#define Log_Info(...)
Definition macros.inl:68
alib::strings::TLocalString< character, 512 > PathString
Definition finfo.hpp:36
files::Files FILES
Definition filescamp.cpp:30
strings::compatibility::std::TISReadLine< char > ISReadLineN
Type alias in namespace alib.
LocalString< 4096 > String4K
Type alias name for TLocalString<character,4096> .