Sane C++ Libraries
C++ Platform Abstraction Libraries
SC::File Struct Reference

Wraps a SC::FileDescriptor to open it and use strings / containers. 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 More...
 
Result open (StringView path, OpenMode mode, OpenOptions options)
 Opens file at path with a given mode More...
 
Result readUntilEOF (Vector< char > &destination)
 Reads into a given dynamic buffer until End of File (EOF) is signaled. More...
 
Result readUntilEOF (Vector< uint8_t > &destination)
 Reads into a given dynamic buffer until End of File (EOF) is signaled. More...
 
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). More...
 

Public Attributes

FileDescriptorfd
 

Detailed Description

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

Example usage:

StringNative<255> filePath = StringEncoding::Native;
StringNative<255> dirPath = StringEncoding::Native;
// Setup the test
FileSystem fs;
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}));
SC_TEST_EXPECT(fs.init(report.applicationRootDirectory));
SC_TEST_EXPECT(fs.makeDirectory(name));
SC_TEST_EXPECT(fs.changeDirectory(dirPath.view()));
// Open a file, write and close it
FileDescriptor fd;
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));
SC_TEST_EXPECT(fs.changeDirectory(report.applicationRootDirectory));
SC_TEST_EXPECT(fs.removeEmptyDirectory(name));
@ Ascii
Encoding is ASCII.
@ Native
Encoding is UTF8.
#define SC_TEST_EXPECT(e)
Records a test expectation (eventually aborting or breaking o n failed test)
Definition: Testing.h:113
@ WriteCreateTruncate
Opens in write mode, creating or truncating it if another file exists at same location.
Definition: File.h:29
@ ReadOnly
Opens in read-only mode.
Definition: File.h:28
Result open(StringView path, OpenMode mode)
Opens file at path with a given mode
static bool join(String &output, Span< const StringView > inputs, StringView separator=SeparatorStringView(), bool skipEmpty=false)
Joins multiple StringView with a Separator into an output String.

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 
)

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 
)

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/3]

Result SC::File::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).

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

◆ readUntilEOF() [2/3]

Result SC::File::readUntilEOF ( Vector< char > &  destination)

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() [3/3]

Result SC::File::readUntilEOF ( Vector< uint8_t > &  destination)

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

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