Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
ioVLX.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlX//ioVLX.hpp>
33 #include <vlCore/Time.hpp>
34 
35 using namespace vl;
36 
37 //-----------------------------------------------------------------------------
39 {
41  return loadVLT(file.get());
42 }
43 //-----------------------------------------------------------------------------
45 {
46  vlX::VLXSerializer serializer;
47 
48  ref<Object> obj = serializer.loadVLT(file);
49 
50  if (serializer.error())
51  Log::error( Say("vl::loadVLT : vlX::VLXSerializer reported: %s.\n") << serializer.errorString() );
52 
53  if (!obj)
54  return NULL;
55 
56  ref<ResourceDatabase> res_db = obj->as<ResourceDatabase>();
57 
58  return res_db;
59 }
60 //-----------------------------------------------------------------------------
62 {
64  return loadVLB(file.get());
65 }
66 //-----------------------------------------------------------------------------
68 {
69  vlX::VLXSerializer serializer;
70 
71  ref<Object> obj = serializer.loadVLB(file);
72 
73  if (serializer.error())
74  Log::error( Say("vl::loadVLB : vlX::VLXSerializer reported: %s.\n") << serializer.errorString() );
75 
76  if (!obj)
77  return NULL;
78 
79  ref<ResourceDatabase> res_db = obj->as<ResourceDatabase>();
80 
81  return res_db;
82 }
83 //-----------------------------------------------------------------------------
84 bool vlX::saveVLT(const String& path, const ResourceDatabase* res_db)
85 {
86  ref<DiskFile> file = new DiskFile(path);
87  return saveVLT(file.get(), res_db);
88 }
89 //-----------------------------------------------------------------------------
90 bool vlX::saveVLT(vl::VirtualFile* file, const ResourceDatabase* res_db)
91 {
92  VL_CHECK(res_db);
93  if (!res_db)
94  return false;
95 
96  vlX::VLXSerializer serializer;
97  serializer.saveVLT( file, res_db );
98 
99  if (serializer.error())
100  Log::error( Say("vlX::saveVLT : vlX::VLXSerializer reported: %s.\n") << serializer.errorString() );
101 
102  return serializer.error() == vlX::VLXSerializer::NoError;
103 }
104 //-----------------------------------------------------------------------------
105 bool vlX::saveVLB(const String& path, const ResourceDatabase* res_db)
106 {
107  ref<DiskFile> file = new DiskFile(path);
108  return saveVLB(file.get(), res_db);
109 }
110 //-----------------------------------------------------------------------------
112 {
113  VL_CHECK(res_db);
114  if (!res_db)
115  return false;
116 
117  vlX::VLXSerializer serializer;
118  serializer.saveVLB( file, res_db );
119 
120  if (serializer.error())
121  Log::error( Say("vlX::saveVLB : vlX::VLXSerializer reported: %s.\n") << serializer.errorString() );
122 
123  return serializer.error() == vlX::VLXSerializer::NoError;
124 }
125 //-----------------------------------------------------------------------------
126 bool vlX::isVLT(const vl::String& path)
127 {
129  return isVLT(file.get());
130 }
131 //-----------------------------------------------------------------------------
133 {
134  if (!file)
135  return false;
136  char vlx[12] = { 0 };
137  memset(vlx, 0, sizeof(vlx));
138  file->close();
139  file->open(OM_ReadOnly);
140  file->read(vlx, sizeof(vlx));
141  file->close();
142  return memcmp(vlx, "VLX version=", sizeof(vlx)) == 0;
143 }
144 //-----------------------------------------------------------------------------
145 bool vlX::isVLB(const vl::String& path)
146 {
148  return isVLT(file.get());
149 }
150 //-----------------------------------------------------------------------------
152 {
153  if (!file)
154  return false;
155  unsigned char vlx_identifier[] = { 0xAB, 'V', 'L', 'X', 0xBB, 0x0D, 0x0A, 0x1A, 0x0A };
156  unsigned char vlx[sizeof(vlx_identifier)];
157  memset(vlx, 0, sizeof(vlx));
158  file->close();
159  file->open(OM_ReadOnly);
160  file->read(vlx, sizeof(vlx));
161  file->close();
162  return memcmp(vlx, vlx_identifier, sizeof(vlx_identifier)) == 0;
163 }
164 //-----------------------------------------------------------------------------
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
Definition: VirtualFile.cpp:82
VLX_EXPORT bool saveVLT(vl::VirtualFile *file, const vl::ResourceDatabase *)
Definition: ioVLX.cpp:90
const T * get() const
Definition: Object.hpp:128
An abstract class representing a file.
Definition: VirtualFile.hpp:60
vl::ref< vl::Object > loadVLB(const vl::String &path, bool start_fresh=true)
Definition: Seriailzer.cpp:382
A simple String formatting class.
Definition: Say.hpp:124
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:165
const char * errorString() const
Definition: Seriailzer.cpp:426
vl::ref< vl::Object > loadVLT(const vl::String &path, bool start_fresh=true)
Definition: Seriailzer.cpp:338
virtual void close()=0
Closes the file.
Visualization Library main namespace.
VLX_EXPORT bool isVLT(vl::VirtualFile *file)
Definition: ioVLX.cpp:132
EError error() const
The last signaled error.
Definition: Serializer.hpp:141
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
T * as()
Casts an Object to the specified class.
Definition: Object.hpp:282
VLX_EXPORT bool isVLB(vl::VirtualFile *file)
Definition: ioVLX.cpp:151
#define NULL
Definition: OpenGLDefs.hpp:81
VLX_EXPORT vl::ref< vl::ResourceDatabase > loadVLT(vl::VirtualFile *file)
Definition: ioVLX.cpp:44
bool saveVLT(const vl::String &path, const vl::Object *obj, bool start_fresh=true)
Definition: Seriailzer.cpp:203
VLCORE_EXPORT ref< VirtualFile > locateFile(const String &path)
Utility function, equivalent to vl::defFileSystem()->locateFile(path)
Definition: VirtualFile.cpp:41
Translates an arbitrary set of vl::Object (and subclasses) into VLB and VLT format.
Definition: Serializer.hpp:45
bool saveVLB(const vl::String &path, const vl::Object *obj, bool start_fresh=true)
Definition: Seriailzer.cpp:274
A VirtualFile that operates on regular disk files.
Definition: DiskFile.hpp:64
VLX_EXPORT bool saveVLB(vl::VirtualFile *file, const vl::ResourceDatabase *)
Definition: ioVLX.cpp:111
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
VLX_EXPORT vl::ref< vl::ResourceDatabase > loadVLB(vl::VirtualFile *file)
Definition: ioVLX.cpp:67
#define VL_CHECK(expr)
Definition: checks.hpp:73
The ResourceDatabase class contains and manipulates a set of resources.