FileUtilities Manual

Table of Contents

About

The class maxon::FileUtilities provides several functions to easily access the content of a given file.

Utilities

These functions automatically read the content of a file or stream into the given memory. The memory (typically a maxon::BaseArray) is automatically resized.

// This example stores the content of the given stream in a maxon::BaseArray.
// get input stream
const maxon::InputStreamRef inputStream = url.OpenInputStream() iferr_return;
// using ReadFileToMemory()
{
maxon::String content(charArray);
DiagnosticOutput("File Content: @", content);
}
// reset stream
inputStream.Seek(0) iferr_return;
// using ReadToArray()
{
maxon::String content(charArray);
DiagnosticOutput("File Content: @", content);
}

maxon::FileUtilities::ReadFileToMemory() can be used to retrieve the answer from a web server using POST parameters.

// This example retrieve the result from a POST and print it.
maxon::Url theServer { "http://localhost:8080"_s };
// Prepares the data that will be post
maxon::String postData = "foo=bar&bin=go"_s;
theServer.Set(maxon::URLFLAGS::HTTP_POSTMETHOD, maxon::HTTPMETHOD::POST) iferr_return;
theServer.Set(maxon::URLFLAGS::HTTP_POSTDATA, maxon::CString(postData, maxon::StringEncodings::Utf8())) iferr_return;
// Retrieves the answer and read it to memory
DiagnosticOutput("@", err);
// Prints the answer from server
ApplicationOutput("answer @", memReq);
// Prints the answer as a string
maxon::String result(memReq);
ApplicationOutput("result @", result);

These functions handle the file's data as maxon::Utf32Char text:

See also Stream Conversions Manual.

The size and content of two files or streams is easily compared with:

// This example compares the two given input streams. If the content of the streams
// is not identical, the error message describing the difference is printed.
// compare streams
const maxon::Result<void> res = maxon::FileUtilities::CompareFiles(inputStreamA, inputStreamB);
// check if streams are not the identical
if (res == maxon::FAILED)
{
// print error message with description of the difference
const maxon::String errorMessage = res.GetError().GetMessage();
DiagnosticOutput("Files are different: @", errorMessage);
}

Further Reading

maxon::FileUtilities::ReadToArray
static Result< void > ReadToArray(UrlOrInputStream &&url, BaseArray< T > &dest)
Definition: file_utilities.h:116
maxon::Result::GetError
SFINAEHelper< Error, RESULT_TYPE >::type GetError() const
Definition: resultbase.h:1032
maxon::FileUtilities::ReadFileToMemory
static MAXON_METHOD Result< void > ReadFileToMemory(UrlOrInputStream &&name, WritableArrayInterface< Char > &arr)
maxon::String
Definition: string.h:1213
iferr_return
#define iferr_return
Definition: resultbase.h:1465
maxon::BaseArray
Definition: basearray.h:366
maxon::HTTPMETHOD::POST
@ POST
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:167
maxon::Url
Definition: url.h:855
maxon::Result< void >
maxon::FAILED
static const ERROR_FAILED FAILED
Definition: resultbase.h:68
ApplicationOutput
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:208
iferr
#define iferr(...)
Definition: errorbase.h:380
maxon::FileUtilities::CompareFiles
static MAXON_METHOD Result< void > CompareFiles(UrlOrInputStream &&file1, UrlOrInputStream &&file2)
MAXON_SCOPE
#define MAXON_SCOPE
Definition: apibase.h:2659
maxon::CString
Definition: string.h:1452