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>

Classes

struct  OpenOptions
 Additional flags to be set when opening files. More...
 

Public Types

enum  OpenMode {
  ReadOnly ,
  WriteCreateTruncate ,
  WriteAppend ,
  ReadAndWrite
}
 Define mode for opening the file (read, write etc.) More...
 

Public Member Functions

 File (FileDescriptor &descriptor)
 
Result open (StringView path, OpenMode mode)
 Opens file at path with a given mode
 
Result open (StringView path, OpenMode mode, OpenOptions options)
 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(), File::WriteCreateTruncate));
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(), File::ReadOnly));
// 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 Enumeration Documentation

◆ OpenMode

Define mode for opening the file (read, write etc.)

Enumerator
ReadOnly 

Opens in read-only mode.

WriteCreateTruncate 

Opens in write mode, creating or truncating it if another file exists at same location.

WriteAppend 

Opens write mode, appending to existing file that must exist at the same location.

ReadAndWrite 

Opens file for read / write mode.

Member Function Documentation

◆ open() [1/2]

Result SC::File::open ( StringView path,
OpenMode 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

◆ open() [2/2]

Result SC::File::open ( StringView path,
OpenMode mode,
OpenOptions options )
nodiscard

Opens file at path with a given mode

Parameters
pathThe path to file
modeThe mode used to open file
optionsOptions that can be applied when opening the file (inheritable, blocking 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: