chunked_file.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 ********************************************************************/
34 
35 #include "rosbag/chunked_file.h"
36 
37 #include <iostream>
38 
39 //#include <ros/ros.h>
40 #ifdef _WIN32
41 # ifdef __MINGW32__
42 # define fseeko fseeko64
43 # define ftello ftello64
44 // not sure if we need a ftruncate here yet or not
45 # else
46 # include <io.h>
47 # define fseeko _fseeki64
48 # define ftello _ftelli64
49 # define fileno _fileno
50 # define ftruncate _chsize_s //Intel Realsense Change, Was: #define ftruncate _chsize
51 # endif
52 #else
53 #include <unistd.h>
54 #endif
55 
56 using std::string;
57 using std::shared_ptr;
59 
60 namespace rosbag {
61 
63  file_(NULL),
64  offset_(0),
65  compressed_in_(0),
66  unused_(NULL),
67  nUnused_(0)
68 {
69  stream_factory_ = std::make_shared<StreamFactory>(this);
70 }
71 
73  close();
74 }
75 
76 void ChunkedFile::openReadWrite(string const& filename) { open(filename, "r+b"); }
77 void ChunkedFile::openWrite (string const& filename) { open(filename, "w+b"); }
78 void ChunkedFile::openRead (string const& filename) { open(filename, "rb"); }
79 
80 void ChunkedFile::open(string const& filename, string const& mode) {
81  // Check if file is already open
82  if (file_)
83  throw BagIOException( "File already open: " + filename );
84 
85  // Open the file
86  if (mode == "r+b") {
87  // check if file already exists
88  #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
89  fopen_s( &file_, filename.c_str(), "r" );
90  #else
91  file_ = fopen(filename.c_str(), "r");
92  #endif
93  if (file_ == NULL)
94  // create an empty file and open it for update
95  #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
96  fopen_s( &file_, filename.c_str(), "w+b" );
97  #else
98  file_ = fopen(filename.c_str(), "w+b");
99  #endif
100  else {
101  fclose(file_);
102  // open existing file for update
103  #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
104  fopen_s( &file_, filename.c_str(), "r+b" );
105  #else
106  file_ = fopen(filename.c_str(), "r+b");
107  #endif
108  }
109  }
110  else
111  #if defined(_MSC_VER) && (_MSC_VER >= 1400 )
112  fopen_s( &file_, filename.c_str(), mode.c_str() );
113  #else
114  file_ = fopen(filename.c_str(), mode.c_str());
115  #endif
116 
117  if (!file_)
118  throw BagIOException( "Error opening file: " + filename );
119 
120  read_stream_ = std::make_shared<UncompressedStream>(this);
121  write_stream_ = std::make_shared<UncompressedStream>(this);
123  offset_ = ftello(file_);
124 }
125 
126 bool ChunkedFile::good() const {
127  return feof(file_) == 0 && ferror(file_) == 0;
128 }
129 
130 bool ChunkedFile::isOpen() const { return file_ != NULL; }
131 string ChunkedFile::getFileName() const { return filename_; }
132 
134  if (!file_)
135  return;
136 
137  // Close any compressed stream by changing to uncompressed mode
139 
140  // Close the file
141  int success = fclose(file_);
142  if (success != 0)
143  throw BagIOException( "Error closing file: " + filename_ );
144 
145  file_ = NULL;
146  filename_.clear();
147 
148  clearUnused();
149 }
150 
151 // Read/write modes
152 
154  if (!file_)
155  throw BagIOException("Can't set compression mode before opening a file");
156 
157  if (type != write_stream_->getCompressionType()) {
158  write_stream_->stopWrite();
159  shared_ptr<Stream> stream = stream_factory_->getStream(type);
160  stream->startWrite();
162  }
163 }
164 
166  if (!file_)
167  throw BagIOException("Can't set compression mode before opening a file");
168 
169  if (type != read_stream_->getCompressionType()) {
170  read_stream_->stopRead();
171  shared_ptr<Stream> stream = stream_factory_->getStream(type);
172  stream->startRead();
174  }
175 }
176 
177 void ChunkedFile::seek(uint64_t offset, int origin) {
178  if (!file_)
179  throw BagIOException("Can't seek - file not open");
180 
182 
183  int success = fseeko(file_, offset, origin);
184  if (success != 0)
185  throw BagIOException("Error seeking");
186 
187  offset_ = ftello(file_);
188 }
189 
192 
193 void ChunkedFile::write(string const& s) { write((void*) s.c_str(), s.size()); }
194 void ChunkedFile::write(void* ptr, size_t size) { write_stream_->write(ptr, size); }
195 void ChunkedFile::read(void* ptr, size_t size) { read_stream_->read(ptr, size); }
196 
198  int fd = fileno(file_);
199  return ftruncate(fd, length) == 0;
200 }
201 
204  char buffer[1024];
205  if(fgets(buffer, 1024, file_))
206  {
207  string s(buffer);
208  offset_ += s.size();
209  return s;
210  }
211  else
212  return string("");
213 }
214 
215 void ChunkedFile::decompress(CompressionType compression, uint8_t* dest, unsigned int dest_len, uint8_t* source, unsigned int source_len) {
216  stream_factory_->getStream(compression)->decompress(dest, dest_len, source, source_len);
217 }
218 
220  unused_ = NULL;
221  nUnused_ = 0;
222 }
223 
224 } // namespace rosbag
length
GLenum GLuint GLenum GLsizei length
Definition: glad/glad/glad.h:135
rosbag::ChunkedFile::getFileName
std::string getFileName() const
return path of currently open file
Definition: chunked_file.cpp:131
uint8_t
unsigned char uint8_t
Definition: stdint.h:78
rosbag::ChunkedFile::setWriteMode
void setWriteMode(CompressionType type)
Definition: chunked_file.cpp:153
rosbag::ChunkedFile::unused_
char * unused_
extra data read by compressed stream
Definition: chunked_file.h:153
librealsense::ds::success
@ success
Definition: d400-private.h:266
rosbag::ChunkedFile::seek
void seek(uint64_t offset, int origin=std::ios_base::beg)
seek to given offset from origin
Definition: chunked_file.cpp:177
rosbag::ChunkedFile::write_stream_
std::shared_ptr< Stream > write_stream_
Definition: chunked_file.h:159
rosbag::ChunkedFile::nUnused_
int nUnused_
number of bytes of extra data read by compressed stream
Definition: chunked_file.h:154
rosbag::ChunkedFile::open
void open(std::string const &filename, std::string const &mode)
Definition: chunked_file.cpp:80
offset
GLintptr offset
Definition: glad/glad/glad.h:2737
rosbag::ChunkedFile::openReadWrite
void openReadWrite(std::string const &filename)
open file for reading & writing
Definition: chunked_file.cpp:76
string
GLsizei const GLchar *const * string
Definition: glad/glad/glad.h:2861
mode
GLenum mode
Definition: glad/glad/glad.h:1385
rosbag::ChunkedFile::compressed_in_
uint64_t compressed_in_
number of bytes written to current compressed stream
Definition: chunked_file.h:152
type
GLenum type
Definition: glad/glad/glad.h:135
size
GLsizeiptr size
Definition: glad/glad/glad.h:2734
rosbag::ChunkedFile::getOffset
uint64_t getOffset() const
return current offset from the beginning of the file
Definition: chunked_file.cpp:190
rosbag::ChunkedFile::ChunkedFile
ChunkedFile()
Definition: chunked_file.cpp:62
rosbag::ChunkedFile::isOpen
bool isOpen() const
return true if file is open for reading or writing
Definition: chunked_file.cpp:130
uint32_t
unsigned int uint32_t
Definition: stdint.h:80
rosbag::ChunkedFile::getline
std::string getline()
Definition: chunked_file.cpp:203
rosbag::BagIOException
Exception thrown when on IO problems.
Definition: exceptions.h:82
NULL
#define NULL
Definition: tinycthread.c:47
rosbag::ChunkedFile::good
bool good() const
return true if hasn't reached end-of-file and no error
Definition: chunked_file.cpp:126
buffer
GLenum GLfloat * buffer
Definition: glad/glad/glad.h:2066
uint64_t
unsigned __int64 uint64_t
Definition: stdint.h:90
rs2rosinternal::Exception
Base class for all exceptions thrown by ROS.
Definition: exception.h:39
rosbag::ChunkedFile::getCompressedBytesIn
uint32_t getCompressedBytesIn() const
return the number of bytes written to current compressed stream
Definition: chunked_file.cpp:191
rosbag::ChunkedFile::~ChunkedFile
~ChunkedFile()
Definition: chunked_file.cpp:72
rosbag::ChunkedFile::close
void close()
close the file
Definition: chunked_file.cpp:133
rosbag::ChunkedFile::openWrite
void openWrite(std::string const &filename)
open file for writing
Definition: chunked_file.cpp:77
test-device-discovery.stream
stream
Definition: test-device-discovery.py:295
rosbag::ChunkedFile::file_
FILE * file_
file pointer
Definition: chunked_file.h:150
source
GLsizei GLsizei GLchar * source
Definition: glad/glad/glad.h:2828
rosbag::ChunkedFile::decompress
void decompress(CompressionType compression, uint8_t *dest, unsigned int dest_len, uint8_t *source, unsigned int source_len)
Definition: chunked_file.cpp:215
rosbag::ChunkedFile::stream_factory_
std::shared_ptr< StreamFactory > stream_factory_
Definition: chunked_file.h:156
rosbag::compression::CompressionType
CompressionType
Definition: third-party/realsense-file/rosbag/rosbag_storage/include/rosbag/stream.h:116
test-projection-from-recording.filename
filename
Definition: test-projection-from-recording.py:15
dest
char * dest
Definition: lz4.h:697
rosbag::ChunkedFile::setReadMode
void setReadMode(CompressionType type)
Definition: chunked_file.cpp:165
rosbag::ChunkedFile::offset_
uint64_t offset_
current position in the file
Definition: chunked_file.h:151
rosbag::compression::Uncompressed
@ Uncompressed
Definition: third-party/realsense-file/rosbag/rosbag_storage/include/rosbag/stream.h:150
rosbag::ChunkedFile::clearUnused
void clearUnused()
Definition: chunked_file.cpp:219
rosbag::ChunkedFile::openRead
void openRead(std::string const &filename)
open file for reading
Definition: chunked_file.cpp:78
rosbag
Definition: bag.h:61
rosbag::ChunkedFile::read_stream_
std::shared_ptr< Stream > read_stream_
Definition: chunked_file.h:158
rosbag::ChunkedFile::read
void read(void *ptr, size_t size)
read size bytes from the file into ptr
Definition: chunked_file.cpp:195
rosbag::ChunkedFile::write
void write(std::string const &s)
Definition: chunked_file.cpp:193
rosbag::ChunkedFile::filename_
std::string filename_
path to file
Definition: chunked_file.h:149
s
GLdouble s
Definition: glad/glad/glad.h:2441
rosbag::ChunkedFile::truncate
bool truncate(uint64_t length)
Definition: chunked_file.cpp:197
chunked_file.h


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Mon Apr 22 2024 02:12:55