Visualization Library 2.0.0-b3

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-2017, 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 
33 #include <vlCore/Time.hpp>
34 
35 using namespace vl;
36 
37 //-----------------------------------------------------------------------------
39 {
40  ref<VirtualFile> file = vl::locateFile(path);
41  return loadVLT(file.get());
42 }
43 //-----------------------------------------------------------------------------
45 {
46  VLXSerializer serializer;
47 
48  ref<Object> obj = serializer.loadVLT(file);
49 
50  if (serializer.error())
51  Log::error( Say("vl::loadVLT : 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 {
63  ref<VirtualFile> file = vl::locateFile(path);
64  return loadVLB(file.get());
65 }
66 //-----------------------------------------------------------------------------
68 {
69  VLXSerializer serializer;
70 
71  ref<Object> obj = serializer.loadVLB(file);
72 
73  if (serializer.error())
74  Log::error( Say("vl::loadVLB : 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 vl::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 vl::saveVLT(VirtualFile* file, const ResourceDatabase* res_db)
91 {
92  VL_CHECK(res_db);
93  if (!res_db)
94  return false;
95 
96  VLXSerializer serializer;
97  serializer.saveVLT( file, res_db );
98 
99  if (serializer.error())
100  Log::error( Say("vl::saveVLT : VLXSerializer reported: %s.\n") << serializer.errorString() );
101 
102  return serializer.error() == VLXSerializer::NoError;
103 }
104 //-----------------------------------------------------------------------------
105 bool vl::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 //-----------------------------------------------------------------------------
111 bool vl::saveVLB(VirtualFile* file, const ResourceDatabase* res_db)
112 {
113  VL_CHECK(res_db);
114  if (!res_db)
115  return false;
116 
117  VLXSerializer serializer;
118  serializer.saveVLB( file, res_db );
119 
120  if (serializer.error())
121  Log::error( Say("vl::saveVLB : VLXSerializer reported: %s.\n") << serializer.errorString() );
122 
123  return serializer.error() == VLXSerializer::NoError;
124 }
125 //-----------------------------------------------------------------------------
126 bool vl::isVLT(const String& path)
127 {
128  ref<VirtualFile> file = vl::locateFile(path);
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 vl::isVLB(const String& path)
146 {
147  ref<VirtualFile> file = vl::locateFile(path);
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 //-----------------------------------------------------------------------------
ref< Object > loadVLB(const String &path, bool start_fresh=true)
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
const T * get() const
Definition: Object.hpp:128
An abstract class representing a file.
Definition: VirtualFile.hpp:60
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
VLGRAPHICS_EXPORT bool isVLB(VirtualFile *file)
Definition: ioVLX.cpp:151
VLGRAPHICS_EXPORT bool isVLT(VirtualFile *file)
Definition: ioVLX.cpp:132
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:164
VLGRAPHICS_EXPORT bool saveVLT(VirtualFile *file, const ResourceDatabase *)
Definition: ioVLX.cpp:90
VLGRAPHICS_EXPORT ref< ResourceDatabase > loadVLB(VirtualFile *file)
Definition: ioVLX.cpp:67
VLGRAPHICS_EXPORT ref< ResourceDatabase > loadVLT(VirtualFile *file)
Definition: ioVLX.cpp:44
ref< Object > loadVLT(const String &path, bool start_fresh=true)
virtual void close()=0
Closes the file.
EError error() const
The last signaled error.
Visualization Library main namespace.
const char * errorString() const
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
#define NULL
Definition: OpenGLDefs.hpp:81
bool saveVLT(const String &path, const Object *obj, bool start_fresh=true)
VLCORE_EXPORT ref< VirtualFile > locateFile(const String &path)
Utility function, equivalent to vl::defFileSystem()->locateFile(path)
Definition: VirtualFile.cpp:41
A VirtualFile that operates on regular disk files.
Definition: DiskFile.hpp:64
bool saveVLB(const String &path, const Object *obj, bool start_fresh=true)
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
VLGRAPHICS_EXPORT bool saveVLB(VirtualFile *file, const ResourceDatabase *)
Definition: ioVLX.cpp:111
Translates an arbitrary set of vl::Object (and subclasses) into VLB and VLT format.
#define VL_CHECK(expr)
Definition: checks.hpp:73
The ResourceDatabase class contains and manipulates a set of resources.