ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
textfile.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_files of the \aliblong.
4///
5/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_FILES_TEXTFILE
9#define HPP_ALIB_FILES_TEXTFILE 1
10#pragma once
11
12#include "alib/files/ftree.hpp"
15#include <fstream>
16
17namespace alib { namespace files {
18
19/// A rather simple text file reader and writer.
20/// @tparam TNString The string-type.
21/// This may also be a type derived from \alib{NString}
22/// which contains further fields available with each line of the file.
23/// @tparam TAllocator The allocator type to use, as prototyped with \alib{lang;Allocator}.
24template <typename TNString= NString, typename TAllocator= MonoAllocator>
25class TTextFile : public lang::AllocatorMember<TAllocator>
26 , public StdVectorMono<TNString>
27{
28 protected:
29 /// The given allocator
31
32 public:
33 /// Type definition publishing template parameter \p{TAllocator}.
34 using AllocatorType = TAllocator;
35
36 /// Type definition publishing the type in the <c>std::vector</c> that this type is derived
37 /// of.
38 /// (As is defined with template parameter \p{TNString}.)
39 using StoredType = TNString;
40
41 /// Type definition publishing the base container type.
43
44 /// Constructor.
45 /// @param ma The allocator to use.
46
48 : lang::AllocatorMember<TAllocator>(ma)
49 , Vector(ma)
50 , allocator( ma ) {}
51
52 /// Reads the file into this vector of lines.
53 /// @param filePath The path of the file.
54 /// @return \alib{lang::system;SystemErrors;SystemErrors::OK} if all went well, otherwise an
55 /// error code.
57 {
58 errno= 0;
59 Path tFilePath(filePath);
60 ALIB_STRINGS_TO_NARROW(filePath, nFilePath, 256)
61 std::ifstream iFile( nFilePath );
62 if ( !iFile.is_open() )
63 {
64 auto result= lang::system::SystemErrors(errno);
65 ALIB_WARNING( "FILES/TEXTFILE", "Error <{}: {!Q}> opening input file {!Q}", errno, result, filePath)
66 return result;
67 }
68 ALIB_MESSAGE( "FILES/TEXTFILE", "file {!Q} opened for reading", filePath)
69
70 alib::ISReadLineN readOp( &iFile );
72 while( !readOp.IsEOF )
73 {
74 line.Reset( readOp );
75 Vector::emplace_back( allocator, line );
76 }
77
78 ALIB_MESSAGE( "FILES/TEXTFILE", "File {!Q}, {} lines read", filePath, Vector::size() )
79
80 return lang::system::SystemErrors::OK;
81 }
82
83 /// Reads the file into this vector of lines.
84 /// @param file The file to read.
85 /// @return \alib{lang::system;SystemErrors;SystemErrors::OK} if all went well, otherwise an
86 /// error code.
88 {
89 Path filePath;
90 file.AsCursor().AssemblePath(filePath);
91 return Read(filePath);
92 }
93
94 /// Writes this text file to the given \p{filePath}.
95 /// @param filePath The path of the file.
96 /// @return \alib{lang::system;SystemErrors;SystemErrors::OK} if all went well, otherwise an
97 /// error code.
100 {
101 errno= 0;
102 Path tFilePath(filePath);
103 ALIB_STRINGS_TO_NARROW(tFilePath, nTFilePath, 256)
104 std::ofstream oFile( nTFilePath.Terminate() );
105 if ( !oFile.is_open() )
106 {
107 auto result= lang::system::SystemErrors(errno);
108 ALIB_WARNING( "FILES/TEXTFILE", "Error <{}: {!Q}> opening output file {!Q}", errno, result, filePath)
109 return result;
110 }
111 ALIB_MESSAGE( "FILES/TEXTFILE", "file {!Q} opened for writing", filePath)
112
113 for( auto& line : *this )
114 oFile << line << std::endl;
115
116 ALIB_MESSAGE( "FILES/TEXTFILE", "File {!Q}, {} lines written", filePath, Vector::size() )
117
118 return lang::system::SystemErrors::OK;
119 }
120
121};
122
123} // namespace alib[::files]
124
125/// Type alias in namespace \b alib.
127
128} // namespace [alib]
129
130
131#endif // HPP_ALIB_FILES_TEXTFILE
132
strings::TAString< typename cmTree::CharacterType, lang::HeapAllocator > & AssemblePath(strings::TAString< typename cmTree::CharacterType, lang::HeapAllocator > &targetString, lang::CurrentData targetData=lang::CurrentData::Clear) const
Cursor & AsCursor()
Definition ftree.hpp:749
TTextFile(MonoAllocator &ma)
Definition textfile.hpp:47
StdVectorMono< TNString > Vector
Type definition publishing the base container type.
Definition textfile.hpp:42
TAllocator AllocatorType
Type definition publishing template parameter TAllocator.
Definition textfile.hpp:34
ALIB_API lang::system::SystemErrors Write(const String &filePath)
Definition textfile.hpp:99
lang::system::SystemErrors Read(const CString &filePath)
Definition textfile.hpp:56
lang::system::SystemErrors Read(files::File file)
Definition textfile.hpp:87
MonoAllocator & allocator
The given allocator.
Definition textfile.hpp:30
void DbgDisableBufferReplacementWarning()
Definition tastring.inl:363
#define ALIB_WARNING(...)
Definition alib.hpp:1268
#define ALIB_MESSAGE(...)
Definition alib.hpp:1269
#define ALIB_STRINGS_TO_NARROW( src, dest, bufSize)
#define ALIB_API
Definition alib.hpp:639
#define ALIB_DBG(...)
Definition alib.hpp:390
SystemErrors
Denotes result values returned by system functions (glibc, etc).
Definition alib.cpp:69
std::vector< T, SCAMono< T > > StdVectorMono
Type alias in namespace alib.
Definition stdvector.hpp:21