Visualization Library 2.1.0

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
QtFile.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 <vlQt6/QtFile.hpp>
33 
34 using namespace vl;
35 
36 //-----------------------------------------------------------------------------
37 // QtFile
38 //-----------------------------------------------------------------------------
39 QtFile::QtFile(const String &path)
40 {
41  setPath(path);
42 }
43 //-----------------------------------------------------------------------------
45 {
46  close();
47 }
48 //-----------------------------------------------------------------------------
49 bool QtFile::open(const String &path, EOpenMode mode)
50 {
51  setPath(path);
52  return open(mode);
53 }
54 //-----------------------------------------------------------------------------
55 bool QtFile::open(EOpenMode mode)
56 {
57  if (isOpen())
58  {
59  Log::error("QtFile::open(): file already open.\n");
60  return false;
61  }
62 
63  QIODevice::OpenMode qmode;
64 
65  if (mode == OM_ReadOnly)
66  {
67  qmode = QIODevice::ReadOnly;
68  }
69  else
70  {
71  qmode = QIODevice::WriteOnly;
72  }
73 
74  mQFile.setFileName(path().toStdString().c_str());
75  if (!mQFile.open(qmode))
76  {
77  Log::error(Say("QtFile::open(): error opening file '%s'\n") << path());
78  return false;
79  }
80 
81  return true;
82 }
83 //-----------------------------------------------------------------------------
84 bool QtFile::isOpen() const
85 {
86  return mQFile.isOpen();
87 }
88 //-----------------------------------------------------------------------------
89 void QtFile::close()
90 {
91  mQFile.close();
92 }
93 //-----------------------------------------------------------------------------
94 long long QtFile::size() const
95 {
96  return QFile(path().toStdString().c_str()).size();
97 }
98 //-----------------------------------------------------------------------------
99 bool QtFile::exists() const
100 {
101  return !path().empty() && QFile::exists(path().toStdString().c_str());
102 }
103 //-----------------------------------------------------------------------------
104 long long QtFile::read_Implementation(void *buffer, long long byte_count)
105 {
106  if (!mQFile.isOpen())
107  {
108  Log::error("QtFile::read_Implementation() called on closed file!\n");
109  return 0;
110  }
111  return mQFile.read((char *)buffer, byte_count);
112 }
113 //-----------------------------------------------------------------------------
114 long long QtFile::write_Implementation(const void *buffer, long long byte_count)
115 {
116  if (!mQFile.isOpen())
117  {
118  Log::error("QtFile::write_Implementation() called on closed file!\n");
119  return 0;
120  }
121  return mQFile.write((char *)buffer, byte_count);
122 }
123 //-----------------------------------------------------------------------------
124 long long QtFile::position_Implementation() const
125 {
126  if (!mQFile.isOpen())
127  {
128  Log::error("QtFile::position_Implementation() called on closed file!\n");
129  return -1;
130  }
131  return mQFile.pos();
132 }
133 //-----------------------------------------------------------------------------
134 bool QtFile::seekSet_Implementation(long long offset)
135 {
136  if (!mQFile.isOpen())
137  {
138  Log::error("QtFile::seekSet_Implementation() called on closed file!\n");
139  return false;
140  }
141  mQFile.seek(offset);
142  return true;
143 }
144 //-----------------------------------------------------------------------------
146 {
147  ref<QtFile> file = new QtFile;
148  file->operator=(*this);
149  return file;
150 }
151 //-----------------------------------------------------------------------------
virtual long long position_Implementation() const
Definition: QtFile.cpp:121
QFile mQFile
Definition: QtFile.hpp:102
A simple String formatting class.
Definition: Say.hpp:124
bool open(const String &path, EOpenMode mode)
The specified path is relative to the parent directory. See setPhysicalPath().
Definition: QtFile.cpp:49
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
virtual long long read_Implementation(void *buffer, long long byte_count)
Definition: QtFile.cpp:101
virtual bool seekSet_Implementation(long long offset)
Definition: QtFile.cpp:131
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
virtual bool exists() const
Returns true if the file exists.
Definition: QtFile.cpp:96
virtual ref< VirtualFile > clone() const
Creates a clone of this class instance.
Definition: QtFile.cpp:142
const String & path() const
Returns the path of the file.
Definition: VirtualFile.hpp:98
Visualization Library main namespace.
bool empty() const
Returns true if length() == 0.
Definition: String.hpp:136
QtFile(const QtFile &other)
Definition: QtFile.hpp:67
virtual long long write_Implementation(const void *buffer, long long byte_count)
Definition: QtFile.cpp:111
void setPath(const String &name)
Changes the path bound to a VirtualFile. Use carefully this function, you shouldn&#39;t rename a VirtualF...
virtual long long size() const
Returns the file size in bytes or -1 on error.
Definition: QtFile.cpp:91
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
virtual void close()
Closes the file.
Definition: QtFile.cpp:86
virtual bool isOpen() const
Returns true if the file has been opened.
Definition: QtFile.cpp:81