On 10/11/2013 at 04:12, xxxxxxxx wrote:
Hi ingvarai,
check this out:
>
> /* Copyright (c) 2013 Niklas Rosenstein
> *
> * Permission is hereby granted, free of charge, to any person obtaining a copy
> * of this software and associated documentation files (the "Software"), to deal
> * in the Software without restriction, including without limitation the rights
> * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> * copies of the Software, and to permit persons to whom the Software is
> * furnished to do so, subject to the following conditions:
> *
> * The above copyright notice and this permission notice shall be included in
> * all copies or substantial portions of the Software.
> *
> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> * THE SOFTWARE. */
>
> #include <c4d.h>
>
>
> /* This class describes the interface for the writer and reader class that
>
>
> is given as a template parameter to the ReadArray() and WriteArray()
>
>
> functions. */
> class HyperFileRW {
>
> public:
>
> Bool Read(HyperFile* hf, Real* dest, LONG disklevel) {
> return hf->ReadReal(dest);
> }
>
> Bool Read(HyperFile* hf, LONG* dest, LONG disklevel) {
> return hf->ReadLong(dest);
> }
>
> Bool Read(HyperFile* hf, Vector* dest, LONG disklevel) {
> return hf->ReadVector(dest);
> }
>
> Bool Read(HyperFile* hf, Matrix* dest, LONG disklevel) {
> return hf->ReadMatrix(dest);
> }
>
> /* More possible overloaded methods ... */
>
> Bool Write(HyperFile* hf, const Real value) {
> return hf->WriteReal(value);
> }
>
> Bool Write(HyperFile* hf, const LONG value) {
> return hf->WriteLong(value);
> }
>
> Bool Write(HyperFile* hf, const Vector& value) {
> return hf->WriteVector(value);
> }
>
> Bool Write(HyperFile* hf, const Matrix& value) {
> return hf->WriteMatrix(value);
> }
>
> /* More possible overloaded methods ... */
>
> };
>
> /* Write an array to a HyperFile. */
> template <typename ArrayClass, typename WriterClass>
> Bool WriteArray(HyperFile* hf, ArrayClass& array, WriterClass writer) {
> /* Write the size of the array. */
> LONG count = array.GetCount();
> if (!hf->WriteLong(count)) return FALSE;
>
> /* And now each and every element. */
> ArrayClass::ConstIterator it = array.Begin();
> for (; it != array.End(); it++) {
> if (!writer.Write(hf, *it)) return FALSE;
> }
>
> return TRUE;
> }
>
> /* Read an array from a HyperFile. */
> template <typename Datatype, typename ArrayClass, typename ReaderClass>
> Bool ReadArray(HyperFile* hf, ArrayClass& array, ReaderClass reader, LONG disklevel) {
> /* Read the size of the array. */
> LONG size;
> if (!hf->ReadLong(&size)) return FALSE;
>
> /* Resize the array and fill in the values. */
> array.Resize(size);
> ArrayClass::Iterator it = array.Begin();
> Datatype value;
> for (; it != array.End(); it++) {
> if (!reader.Read(hf, &value, disklevel)) return FALSE;
> *it = value;
> }
>
> return TRUE;
> }
>
>
> Bool PluginStart() {
> GePrint("-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-");
> GePrint("PluginStart(), running write/read tests...");
>
> /* In-memory file. */
> AutoAlloc<MemoryFileStruct> mfs;
> if (!mfs) {
> GePrint("Could not allocate MemoryFileStruct.");
> return FALSE;
> }
>
> /* Write Test */
> /* ---------- */
>
> /* Allocate the HyperFile. */
> AutoAlloc<HyperFile> hf;
> if (!hf) {
> GePrint("Could not allocate HyperFile.");
> return FALSE;
> }
>
> /* Open a new HyperFile with the in-memory file. */
> Filename mem;
> mem.SetMemoryWriteMode(mfs);
> if (!hf->Open(0, mem, FILEOPEN_WRITE, FILEDIALOG_NONE)) {
> GePrint("Could not open HyperFile in WRITE mode.");
> return FALSE;
> }
>
> /* Create an array of vectors. */
> c4d_misc::BaseArray<Vector> source_array;
> source_array.Resize(100);
> for (LONG i=0; i < 100; i++) source_array = Vector(SNoise(i), SNoise(i * i), SNoise(1 * 3 + 2)); /* And write the array to the file. */
> if (!WriteArray(hf, source_array, HyperFileRW())) {
> GePrint("Failed to write array.");
> return FALSE;
> }
>
> /* Close the HyperFile and report about our success. */
> hf->Close();
> GePrint("Array successfully written!");
>
> /* Read Test */
> /* --------- */
>
> /* Retrieve a pointer to the data of the MemoryFileStruct. */
> void* data = NULL;
> VLONG data_size = -1;
> mfs->GetData(data, data_size, FALSE);
> if (!data) {
> GePrint("Could not obtain data-pointer from MemoryFileStruct.");
> return FALSE;
> }
>
> /* And open the HyperFile in READ mode. */
> hf.Free();
> hf.Assign(HyperFile::Alloc());
> if (!hf) {
> GePrint("Could not allocated HyperFile.");
> return FALSE;
> }
> mem.SetMemoryReadMode(data, data_size);
> if (!hf->Open(0, mem, FILEOPEN_READ, FILEDIALOG_NONE)) {
> GePrint("Could not open HyperFile in READ mode.");
> return FALSE;
> }
>
> /* Read the array. */
> c4d_misc::BaseArray<Vector> read_array;
> Bool result = ReadArray<Vector, c4d_misc::BaseArray<Vector>, HyperFileRW>(
> hf, read_array, HyperFileRW(), 0);
> if (!result) {
> GePrint("Could not read array.");
> return FALSE;
> }
>
> GePrint("Array successfully read from HyperFile, checking results..");
>
> /* Do they have the same size? */
> if (source_array.GetCount() != read_array.GetCount()) {
> GePrint("> Sizes do not equal.");
> return FALSE;
> }
>
> /* Are the values equal? */
> c4d_misc::BaseArray<Vector>::Iterator it1 = source_array.Begin();
> c4d_misc::BaseArray<Vector>::Iterator it2 = read_array.Begin();
> LONG index = 0;
> for (; it1 != source_array.End() && it2 != read_array.End(); it1++, it2++, index++) {
> if (*it1 != *it2) {
> GePrint("> Elements at index " + LongToString(index) + " do not equal.");
> return FALSE;
> }
> }
>
> GePrint("A pure success!");
> return TRUE;
> }
>
> Bool PluginMessage(LONG type, void* pData) {
> switch (type) {
> case C4DPL_INIT_SYS:
> return resource.Init();
> };
> return TRUE;
> }
>
> void PluginEnd() {
> }
You can easily use this to write any data you like.
Best,
-Niklas
__