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);
}
Definition: basearray.h:415
static MAXON_METHOD Result< void > ReadFileToMemory(UrlOrInputStream &&name, WritableArrayInterface< Char > &arr)
static Result< void > ReadToArray(UrlOrInputStream &&url, BaseArray< T > &dest)
Definition: file_utilities.h:138
Definition: string.h:1237
#define MAXON_SCOPE
Definition: apibase.h:2886
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:170
#define iferr_return
Definition: resultbase.h:1524

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
ApplicationOutput("result @", result);
Definition: string.h:1492
Definition: url.h:942
PyObject PyObject * result
Definition: abstract.h:43
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:204
#define iferr(...)
Definition: errorbase.h:388

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
{
// print error message with description of the difference
const maxon::String errorMessage = res.GetError().GetMessage();
DiagnosticOutput("Files are different: @", errorMessage);
}
static MAXON_METHOD Result< void > CompareFiles(UrlOrInputStream &&file1, UrlOrInputStream &&file2)
Py_UCS4 * res
Definition: unicodeobject.h:1113
static const ERROR_FAILED FAILED
Definition: resultbase.h:68

Further Reading