HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FS_IStreamDevice.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: FS_IStreamDevice.h (FS Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __FS_IStreamHelper__
12 #define __FS_IStreamHelper__
13 
14 #include "FS_API.h"
15 #include <SYS/SYS_BoostStreams.h>
16 #include <UT/UT_Assert.h>
17 #include <UT/UT_SharedPtr.h>
18 #include <UT/UT_IStream.h>
19 #include "FS_Reader.h"
20 
21 /// This class is a wrapper to create a std::istream using FS to handle stream
22 /// I/O.
23 /// There are three objects involved when using this code. The device, the
24 /// stream buffer and the stream itself. You need code like: @code
25 /// auto reader = new FS_IStreamDevice(path);
26 /// if (!reader->isValid())
27 /// {
28 /// delete reader;
29 /// return false;
30 /// }
31 /// auto streambuf = new FS_IStreamDeviceBuffer(*reader);
32 /// auto stream = new std::istream(streambuf);
33 /// ...
34 /// delete reader;
35 /// delete stream;
36 /// delete streambuf;
37 /// @endcode
39 {
40 public:
41  using char_type = char;
42  using stream_offset = bios::stream_offset;
43  struct category
44  : public bios::device_tag
45  , public bios::seekable
46  , public bios::closable_tag
47  {
48  };
49 
50  FS_IStreamDevice(const char *path, const UT_Options *opts=nullptr)
51  : myReader(new FS_Reader(path, opts))
52  {
53  myStream = myReader->isGood() ? myReader->getStream() : nullptr;
54  }
56  {
57  myStream = stream;
58  }
60 
61  /// Test whether stream is valid
62  bool isValid() const { return myStream != nullptr; }
63 
64  /// Writing always fails
65  std::streamsize write(const char *, std::streamsize) { return -1; }
66  /// Close the input stream
67  void close()
68  {
69  if (myReader)
70  myReader->close();
71  myStream = nullptr;
72  }
73  /// Try to read data from the stream
74  std::streamsize read(char *buffer, std::streamsize n)
75  {
76  return myStream ? myStream->bread(buffer, n) : -1;
77  }
78  /// Putback a character
80  {
81  return myStream ? myStream->ungetc() == 1 : false;
82  }
83  /// Seek in the stream
84  stream_offset seek(stream_offset off, std::ios_base::seekdir way)
85  {
86  bool ok = false;
87  if (myStream)
88  {
89  if (way == std::ios_base::beg)
90  {
91  ok = myStream->seekg(off, UT_IStream::UT_SEEK_BEG);
92  }
93  else if (way == std::ios_base::cur)
94  {
95  ok = myStream->seekg(off, UT_IStream::UT_SEEK_CUR);
96  }
97  else
98  {
100  ok = myStream->seekg(off, UT_IStream::UT_SEEK_END);
101  }
102  }
103  return ok ? myStream->tellg() : -1;
104  }
105 private:
106  UT_SharedPtr<FS_Reader> myReader;
107  UT_IStream *myStream;
108 };
109 
110 using FS_IStreamDeviceBuffer = bios::stream_buffer<FS_IStreamDevice>;
111 
112 #endif
GLuint GLuint stream
Definition: glcorearb.h:1831
Class for reading files.
Definition: FS_Reader.h:33
std::streamsize write(const char *, std::streamsize)
Writing always fails.
bool isValid() const
Test whether stream is valid.
bool putback(char_type c)
Putback a character.
GLsizei const GLchar *const * path
Definition: glcorearb.h:3340
std::streamsize read(char *buffer, std::streamsize n)
Try to read data from the stream.
FS_IStreamDevice(const char *path, const UT_Options *opts=nullptr)
GLuint buffer
Definition: glcorearb.h:659
bios::stream_buffer< FS_IStreamDevice > FS_IStreamDeviceBuffer
GLdouble n
Definition: glcorearb.h:2007
stream_offset seek(stream_offset off, std::ios_base::seekdir way)
Seek in the stream.
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:28
GLuint GLuint end
Definition: glcorearb.h:474
FS_IStreamDevice(UT_IStream *stream)
void close()
Close the input stream.
A map of string to various well defined value types.
Definition: UT_Options.h:42
bios::stream_offset stream_offset
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:126
#define FS_API
Definition: FS_API.h:10