Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::File Struct Reference

Wraps a SC::FileDescriptor to open it and use strings / buffers. More...

#include <File.h>

Public Member Functions

 File (FileDescriptor &descriptor)
 
Result open (StringView path, FileOpen mode)
 Opens file at path with a given mode
 
Result readUntilEOF (Buffer &destination)
 Reads into a given dynamic buffer until End of File (EOF) is signaled.
 
Result readUntilEOF (String &destination)
 Reads into a given string until End of File (EOF) is signaled It works also for non-seekable file descriptors (stdout / in / err).
 

Public Attributes

FileDescriptorfd
 

Detailed Description

Wraps a SC::FileDescriptor to open it and use strings / buffers.

Example usage:

StringNative<255> filePath = StringEncoding::Native;
StringNative<255> dirPath = StringEncoding::Native;
// Setup the test
const StringView name = "FileTest";
const StringView fileName = "test.txt";
SC_TEST_EXPECT(Path::join(dirPath, {report.applicationRootDirectory, name}));
SC_TEST_EXPECT(Path::join(filePath, {dirPath.view(), fileName}));
// Open a file, write and close it
SC_TEST_EXPECT(File(fd).open(filePath.view(), FileOpen::Write));
SC_TEST_EXPECT(fd.write(StringView("test").toCharSpan()));
SC_TEST_EXPECT(fd.close());
// Re-open the file for read
SC_TEST_EXPECT(File(fd).open(filePath.view(), FileOpen::Read));
// Read some data from the file
char buffer[4] = {0};
Span<char> spanOut;
SC_TEST_EXPECT(fd.read({buffer, sizeof(buffer)}, spanOut));
SC_TEST_EXPECT(fd.close());
// Check if read content matches
StringView sv(spanOut, false, StringEncoding::Ascii);
SC_TEST_EXPECT(sv.compare("test") == StringView::Comparison::Equals);
// Shutdown test
SC_TEST_EXPECT(fs.removeFile(fileName));

Member Function Documentation

◆ open()

Result SC::File::open ( StringView path,
FileOpen mode )
nodiscard

Opens file at path with a given mode

Parameters
pathThe path to file
modeThe mode used to open file (read-only, write-append etc.)
Returns
Valid Result if file is opened successfully

◆ readUntilEOF() [1/2]

Result SC::File::readUntilEOF ( Buffer & destination)
nodiscard

Reads into a given dynamic buffer until End of File (EOF) is signaled.

It works also for non-seekable file descriptors (stdout / in / err).

Parameters
destinationA destination buffer to write to (it will be resized as needed)
Returns
Valid result if read succeeded until EOF

◆ readUntilEOF() [2/2]

Result SC::File::readUntilEOF ( String & destination)
nodiscard

Reads into a given string until End of File (EOF) is signaled It works also for non-seekable file descriptors (stdout / in / err).

Parameters
destinationA destination string to write to (it will be sized as needed)
Returns
Valid result if read succeeded until EOF

The documentation for this struct was generated from the following file: