diff options
| author | jhurst <jhurst@cinecert.com> | 2008-10-06 04:50:09 +0000 |
|---|---|---|
| committer | jhurst <> | 2008-10-06 04:50:09 +0000 |
| commit | dfed1be13fa82afc6de9f62822e963dec0f4a25a (patch) | |
| tree | 351cf9c4eafadc0c502349fae6b509db838c7142 /src | |
| parent | 34b0ba8aa56c902a013f7ac799e1f64fb31b0a5c (diff) | |
easy marshalling for IArchive
Diffstat (limited to 'src')
| -rw-r--r-- | src/KM_fileio.cpp | 60 | ||||
| -rwxr-xr-x | src/KM_fileio.h | 8 |
2 files changed, 68 insertions, 0 deletions
diff --git a/src/KM_fileio.cpp b/src/KM_fileio.cpp index 61b7c88..4f028f4 100644 --- a/src/KM_fileio.cpp +++ b/src/KM_fileio.cpp @@ -1061,6 +1061,66 @@ Kumu::WriteStringIntoFile(const char* filename, const std::string& inString) return result; } +//------------------------------------------------------------------------------------------ + + +// +Kumu::Result_t +Kumu::ReadFileIntoObject(const std::string& Filename, Kumu::IArchive& Object, ui32_t max_size) +{ + ByteString Buffer; + ui32_t file_size = FileSize(Filename); + Result_t result = Buffer.Capacity(file_size); + + if ( KM_SUCCESS(result) ) + { + ui32_t read_count = 0; + FileWriter Reader; + + result = Reader.OpenRead(Filename.c_str()); + + if ( KM_SUCCESS(result) ) + result = Reader.Read(Buffer.Data(), file_size, &read_count); + + if ( KM_SUCCESS(result) ) + { + assert(file_size == read_count); + Buffer.Length(read_count); + MemIOReader MemReader(&Buffer); + result = Object.Unarchive(&MemReader) ? RESULT_OK : RESULT_READFAIL; + } + } + + return result; +} + +// +Kumu::Result_t +Kumu::WriteObjectIntoFile(const Kumu::IArchive& Object, const std::string& Filename) +{ + ByteString Buffer; + Result_t result = Buffer.Capacity(Object.ArchiveLength()); + + if ( KM_SUCCESS(result) ) + { + ui32_t write_count = 0; + FileWriter Writer; + MemIOWriter MemWriter(&Buffer); + + result = Object.Archive(&MemWriter) ? RESULT_OK : RESULT_WRITEFAIL; + + if ( KM_SUCCESS(result) ) + { + Buffer.Length(MemWriter.Length()); + result = Writer.OpenWrite(Filename.c_str()); + } + + if ( KM_SUCCESS(result) ) + result = Writer.Write(Buffer.RoData(), Buffer.Length(), &write_count); + } + + return result; +} //------------------------------------------------------------------------------------------ // diff --git a/src/KM_fileio.h b/src/KM_fileio.h index 1376716..ceeb8eb 100755 --- a/src/KM_fileio.h +++ b/src/KM_fileio.h @@ -206,6 +206,14 @@ namespace Kumu // Writes a string to a file, overwrites the existing file if present. Result_t WriteStringIntoFile(const char* filename, const std::string& inString); + // Instant IO for archivable objects + // + // Unarchives a file into an object + Result_t ReadFileIntoObject(const std::string& Filename, IArchive& Object, ui32_t max_size = 8 * Kumu::Megabyte); + + // Archives an object into a file + Result_t WriteObjectIntoFile(const IArchive& Object, const std::string& Filename); + // class FileReader { |
