ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
ALib.Files.TextFile.H
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef H_ALIB_FILES_TEXTFILE
9#define H_ALIB_FILES_TEXTFILE
10#pragma once
11#ifndef INL_ALIB
12# include "alib/alib.inl"
13#endif
14
15#if ALIB_FILES
16#include <fstream>
17#include "ALib.Files.H"
18
19namespace alib { namespace files {
20
21/// A rather simple text file reader and writer.
22/// @tparam TNString The string-type.
23/// This may also be a type derived from \alib{NString}
24/// which contains further fields available with each line of the file.
25/// @tparam TAllocator The allocator type to use, as prototyped with \alib{lang;Allocator}.
26template <typename TNString= NString, typename TAllocator= MonoAllocator>
27class TTextFile : public lang::AllocatorMember<TAllocator>
28 , public StdVectorMono<TNString>
29{
30 protected:
31 /// The given allocator
33
34 public:
35 /// Type definition publishing template parameter \p{TAllocator}.
36 using AllocatorType = TAllocator;
37
38 /// Type definition publishing the type in the <c>std::vector</c> that this type is derived
39 /// of.
40 /// (As is defined with template parameter \p{TNString}.)
41 using StoredType = TNString;
42
43 /// Type definition publishing the base container type.
45
46 /// Constructor.
47 /// @param ma The allocator to use.
48
50 : lang::AllocatorMember<TAllocator>(ma)
51 , Vector(ma)
52 , allocator( ma ) {}
53
54 /// Reads the file into this vector of lines.
55 /// @param filePath The path of the file.
56 /// @return \alib{system;SystemErrors;SystemErrors::OK} if all went well, otherwise an
57 /// error code.
59 {
60 errno= 0;
61 Path tFilePath(filePath);
62 ALIB_STRINGS_TO_NARROW(filePath, nFilePath, 256)
63 std::ifstream iFile( nFilePath );
64 if ( !iFile.is_open() )
65 {
66 auto result= system::SystemErrors(errno);
67 ALIB_WARNING( "FILES/TEXTFILE", "Error <{}: \"{}\"> opening input file \"{}\"",
68 errno, result, filePath)
69 return result;
70 }
71 ALIB_MESSAGE( "FILES/TEXTFILE", "file \"{}\" opened for reading", filePath)
72
73 alib::ISReadLineN readOp( &iFile );
75 while( !readOp.IsEOF )
76 {
77 line.Reset( readOp );
78 Vector::emplace_back( allocator, line );
79 }
80
81 ALIB_MESSAGE( "FILES/TEXTFILE", "File \"{}\", {} lines read", filePath, Vector::size() )
82
83 return system::SystemErrors::OK;
84 }
85
86 /// Reads the file into this vector of lines.
87 /// @param file The file to read.
88 /// @return \alib{system;SystemErrors;SystemErrors::OK} if all went well, otherwise an
89 /// error code.
91 {
92 Path filePath;
93 file.AsCursor().AssemblePath(filePath);
94 return Read(filePath);
95 }
96
97 /// Writes this text file to the given \p{filePath}.
98 /// @param filePath The path of the file.
99 /// @return \alib{system;SystemErrors;SystemErrors::OK} if all went well, otherwise an
100 /// error code.
103 {
104 errno= 0;
105 Path tFilePath(filePath);
106 ALIB_STRINGS_TO_NARROW(tFilePath, nTFilePath, 256)
107 std::ofstream oFile( nTFilePath.Terminate() );
108 if ( !oFile.is_open() )
109 {
110 auto result= system::SystemErrors(errno);
111 ALIB_WARNING( "FILES/TEXTFILE", "Error <{}: \"{}\"> opening output file \"{}\"",
112 errno, result, filePath)
113 return result;
114 }
115 ALIB_MESSAGE( "FILES/TEXTFILE", "file \"{}\" opened for writing", filePath)
116
117 for( auto& line : *this )
118 oFile << line << std::endl;
119
120 ALIB_MESSAGE( "FILES/TEXTFILE", "File \"{}\", {} lines written", filePath, Vector::size() )
121
122 return system::SystemErrors::OK;
123 }
124
125};
126
127} // namespace alib[::files]
128
129/// Type alias in namespace \b alib.
131
132} // namespace [alib]
133
134#endif // ALIB_FILES
135
136#endif // H_ALIB_FILES_TEXTFILE
137
Cursor & AsCursor()
Definition ftree.inl:737
TTextFile(MonoAllocator &ma)
TAllocator AllocatorType
Type definition publishing template parameter TAllocator.
StdVectorMono< TNString > Vector
Type definition publishing the base container type.
system::SystemErrors Read(const CString &filePath)
ALIB_DLL system::SystemErrors Write(const String &filePath)
system::SystemErrors Read(files::File file)
void DbgDisableBufferReplacementWarning()
Definition tastring.inl:245
#define ALIB_MESSAGE(domain,...)
Definition alib.inl:1047
#define ALIB_DLL
Definition alib.inl:496
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
#define ALIB_WARNING(domain,...)
Definition alib.inl:1046
#define ALIB_DBG(...)
Definition alib.inl:836
std::vector< T, SCAMono< T > > StdVectorMono
Type alias in namespace alib.
strings::compatibility::std::TISReadLine< char > ISReadLineN
Type alias in namespace alib.
strings::TCString< character > CString
Type alias in namespace alib.
Definition cstring.inl:503
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
system::Path Path
Type alias in namespace alib.
Definition path.inl:392
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
NLocalString< 4096 > NString4K
Type alias name for TLocalString<nchar,8192>.
files::TTextFile< NString > TextFile
Type alias in namespace alib.