About
HyperFile is a class to read and write data to a file on disk. This class is for example used to store Cinema 4D scenes, but can also be used to create individual files storing Cinema 4D data types. A HyperFile can not be used to create arbitrary files (like e.g. text files) as all data stored in a HyperFile is prefixed with a value header describing the stored data, see BaseFile Manual on Byte Access instead.
HyperFile class makes many things easier compared to BaseFile class (e.g. one does not need to care about byte order), while sacrificing some of the power of the low-level functions provided there (no byte access possible, no repositioning of the read/write pointer). Most advantageous compared to BaseFile is the file versioning and chunk handling.
A HyperFile is always read (and also written) from the beginning of the file and the handling of the read/write pointer is handled completely internally. In theory replacing of parts of the data in a HyperFile is possible, but definitely not recommended, as good knowledge of the internal structure is needed to not corrupt the file.
- Note
- To delete a file see GeFKill() and the accompanying File Functions Manual.
-
Besides the code snippets shown on this page, there's also the hyperfile_objectdata example in cinema4dsdk.
Throughout this page the code snippets use a helper function PrintFileError()
.
This looks like so:
{
if (verbose)
{
return FileErrorToString(
file);
}
else
{
{
else
}
}
}
Manages file and path names.
Definition: c4d_file.h:94
Definition: c4d_file.h:1085
Definition: string.h:1235
maxon::Bool Bool
Definition: ge_sys_math.h:55
maxon::Int Int
Definition: ge_sys_math.h:64
return OK
Definition: apibase.h:2690
FILEERROR
Definition: ge_prepass.h:3984
@ UNKNOWN_VALUE
Unknown value detected.
@ OUTOFMEMORY
Not enough memory.
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67
#define ApplicationOutput(formatString,...)
Definition: debugdiagnostics.h:210
const char const char const char * file
Definition: object.h:439
Access
A HyperFile argument is typically provided in NodeData::Read() and NodeData::Write() functions. See NodeData::Read() / NodeData::Write() Manual.
Allocation/Deallocation
HyperFile objects are created with the usual tools, see Entity Creation and Destruction Manual (Classic).
Open and Close
After allocating a HyperFile object HyperFile::Open() needs to be called to bind it to a file in the filesystem (either an existing or a new one).
- HyperFile::Open(): Opens a file, two parameters influence the mode and error reporting behavior.
- Parameter mode:
- FILEOPEN::READ : The file is opened for reading, only. This is the default mode, if none gets specified.
- FILEOPEN::WRITE : The file is opened for writing, only. Any existing file will be overwritten.
- FILEOPEN::READWRITE : The file is opened for read and write access.
- Parameter (error-) dialog:
- FILEDIALOG::NONE : No dialog will be shown on error. To be used if working with files in a context, where no dialogs are allowed, or to implement a custom error notification for users.
- FILEDIALOG::ANY : A dialog will be shown on every file error.
- FILEDIALOG::IGNOREOPEN : A dialog will be shown on every file error, except if the file does not exist. This is the default option.
- HyperFile::Close(): Closes a file. In most cases this is not needed, as the file will be automatically closed, when the HyperFile object gets destroyed.
- Note
- A HyperFile is always read and written from the beginning of a file. So FILEOPEN::APPEND can NOT be used with a HyperFile, appending is not supported.
Read and Write
The following functions are used to store data in a "BaseContainer"-like fashion, where one does not need to worry about the size of datatypes or positioning the read/write pointer.
A Typical Write Access
- Allocate a HyperFile object with HyperFile::Alloc() (or of course AutoAlloc).
- Open a file using HyperFile::Open(), either creating a new one or opening an existing one.
- Simply write data into the file, see below. Cinema 4D will take care of all needed housekeeping internally.
- Close the file using HyperFile::Close(). This step is actually optional, as the file will also be closed when the HyperFile object gets destroyed.
- Note
- The order of write accesses will determine the order of read accesses (the order will need to be the same).
if (!hf)
return PrintFileError(fn, hf, "Failed to create HyperFile"_s);
if (!hf->WriteInt32(12345678))
return PrintFileError(fn, hf, "Failed to write an Int32 into HyperFile"_s);
if (!hf->WriteString("Hello World!"_s))
return PrintFileError(fn, hf, "Failed to write a String into HyperFile"_s);
hf->Close();
Definition: ge_autoptr.h:37
Bool FileSelect(FILESELECTTYPE type, FILESELECT flags, const maxon::String &title, const maxon::String &force_suffix=maxon::String())
maxon::Int32 Int32
Definition: ge_sys_math.h:60
@ ANY
Show an error dialog for any error.
@ DIRECTORY
Folder selection dialog.
A Typical Read Access
- Allocate a HyperFile object with HyperFile::Alloc() (or of course AutoAlloc).
- Open an existing file using HyperFile::Open().
- Simply read data from the file in the same order it got written before, see below. Cinema 4D will take care of all needed housekeeping internally.
- Close the file using HyperFile::Close(). This step is actually optional, as the file will also be closed when the HyperFile object gets destroyed.
All of the following read/write functions automatically take care of the read/write pointer (i.e. advancing it by the correct amount of bytes depending on the access type) and are able to detect access with wrong data type. Internally this is achieved by not only writing the actual data to the file, but also an additional value header preceding each data, specifying the type and also (where needed) the amount of data.
- Note
- The order of read accesses has to match the order of write accesses.
-
These functions can not be used to read data from an arbitrary file, but only from files created by Cinema 4D, when data got written by the respective write functions.
if (!hf)
return PrintFileError(fn, hf, "Failed to open the HyperFile for reading"_s);
if (!hf->ReadInt32(&
value))
return PrintFileError(fn, hf, "Failed to read an Int32 from HyperFile"_s);
return PrintFileError(fn, hf, "This is not the expected HyperFile"_s);
if (!hf->ReadString(&myText))
return PrintFileError(fn, hf, "Failed to read a String from HyperFile"_s);
PyObject * value
Definition: abstract.h:715
@ READ
Open the file for reading.
Value
As mentioned in the beginning, data/values get stored inside the HyperFile as a pair consisting of a describing header followed by the actual data. With the following functions the type of the next value can be detected (see HYPERFILEVALUE_... defines below) and the value may also be skipped (i.e. continue reading with the next value). Usually this is not needed, but can be helpful if for example writing a loop reading arbitrary values.
Char
See also Primitive Data Types Manual (Classic) on Char.
String
See also String Manual (Classic).
Filename
See also Filename Manual.
Bool
See also Primitive Data Types Manual (Classic) on Bool.
Integer
See also Primitive Data Types Manual (Classic) on Integer.
Float
See also Primitive Data Types Manual (Classic) on Float.
Vector
See also Vector Manual (Classic).
Matrix
See also Matrix Manual (Classic).
Arrays
These functions can be used with standard C arrays:
const Int arrSize =
sizeof(arrFloat) /
sizeof(
Float64);
return PrintFileError(fn, hf, "Failed to read array (Float64) from HyperFile"_s);
for (
Int32 idx = 0; idx < arrSize; ++idx)
{
}
maxon::Float64 Float64
Definition: ge_sys_math.h:67
const Int arrSize =
sizeof(arrFloat) /
sizeof(
Float64);
return PrintFileError(fn, hf, "Failed to write an array (Float64) into HyperFile"_s);
Definition: apibasemath.h:34
PI
Definition: unicodeutils.h:16
And also with maxon::BaseArray:
return PrintFileError(fn, hf, "Failed to read BaseArray<Int64> from HyperFile"_s);
{
}
MAXON_ATTRIBUTE_FORCE_INLINE const T * GetFirst() const
Definition: basearray.h:1326
ResultMem Resize(Int newCnt, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::DEFAULT)
Definition: basearray.h:1369
MAXON_ATTRIBUTE_FORCE_INLINE Int GetCount() const
Definition: basearray.h:573
maxon::Int64 Int64
Definition: ge_sys_math.h:62
#define DebugAssert(condition,...)
Definition: debugdiagnostics.h:248
PyObject const char PyObject PyObject ** val
Definition: pycore_pyerrors.h:76
#define iferr_return
Definition: resultbase.h:1519
return PrintFileError(fn, hf, "Failed to write a BaseArray (Int64) into HyperFile"_s);
MAXON_ATTRIBUTE_FORCE_INLINE ResultRef< T > Append(ARG &&x)
Definition: basearray.h:677
GeData
if (!hf->ReadGeData(&d))
return PrintFileError(fn, hf, "Failed to read a GeData from HyperFile"_s);
Definition: c4d_gedata.h:83
Int32 GetType() const
Definition: c4d_gedata.h:407
const Vector & GetVector() const
Definition: c4d_gedata.h:451
@ DTYPE_VECTOR
Vector
Definition: lib_description.h:70
if (!hf->WriteGeData(d))
return PrintFileError(fn, hf, "Failed to write a GeData(Vector) into HyperFile"_s);
void SetVector(const Vector &v)
Definition: c4d_gedata.h:598
See also GeData Manual.
BaseContainer
See also BaseContainer Manual.
BaseTime
See also BaseTime Manual.
BaseBitmap
See also BaseBitmap Manual.
Raw Memory
- Note
- Only use when really needed. Be aware that the byte sequences will not be platform independent.
void* data;
if (!hf->ReadMemory(&data, &
size))
return PrintFileError(fn, hf, "Failed to read memory (myStructure) from HyperFile"_s);
myStructure* const myData = static_cast<myStructure*>(data);
Py_ssize_t size
Definition: bytesobject.h:86
myStructure someData;
someData.myFloat = 0.5;
someData.myInt = 42;
someData.myBool = true;
if (!hf->WriteMemory(&someData, sizeof(someData)))
return PrintFileError(fn, hf, "Failed to write an memory (myStructure) into HyperFile"_s);
Uuid
Chunk
Chunks provide means to group or organize several values. This can be useful if for example different versions of a plugin store different sets of parameters, so a parameter set no longer understood can be skipped.
Utility
Error
File Version
Other HyperFile Related Functions
Actually not part of the HyperFile class, these convenience functions can be used to read or write a complete GeListNode from/into a single HyperFile referenced by Filename.
return PrintFileError(fn,
nullptr,
"Failed to write object "_s +
maxon::String(
op->GetName()) +
" to file ("_s + errString +
"): "_s);
doc->InsertObject(
op.Release(),
nullptr,
nullptr);
#define Onull
Null.
Definition: ge_prepass.h:1068
FILEERROR ReadHyperFile(BaseDocument *doc, GeListNode *node, const Filename &filename, Int32 ident, maxon::String *warning_string)
const char * doc
Definition: pyerrors.h:226
PyObject * op
Definition: object.h:520
return PrintFileError(fn,
nullptr,
"Failed to write object "_s +
maxon::String {
op->GetName() } +
" to file: "_s);
FILEERROR WriteHyperFile(BaseDocument *doc, GeListNode *node, const Filename &filename, Int32 ident)
Further Reading