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 "FS_Reader.h"
16 #include <UT/UT_Assert.h>
17 #include <UT/UT_IStream.h>
18 #include <UT/UT_SharedPtr.h>
19 #include <SYS/SYS_BoostStreams.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 /// FS_IStreamDevice reader(path);
26 /// if (!reader.isValid())
27 /// return false;
28 /// auto streambuf = UTmakeUnique<FS_IStreamDeviceBuffer>(reader);
29 /// auto stream = UTmakeUnique<std::istream>(streambuf);
30 /// ...
31 /// @endcode
33 {
34 public:
35  using char_type = char;
36  using stream_offset = bios::stream_offset;
37  struct category
38  : public bios::device_tag
39  , public bios::seekable
40  , public bios::closable_tag
41  {
42  };
43 
44  FS_IStreamDevice(const char *path, const UT_Options *opts=nullptr)
45  : myReader(new FS_Reader(path, opts))
46  {
47  myStream = myReader->isGood() ? myReader->getStream() : nullptr;
48  }
50  {
51  myStream = stream;
52  }
53 
54  // This class must be copyable to be used with FS_IStreamDeviceBuffer
55  ~FS_IStreamDevice() = default;
56  FS_IStreamDevice(const FS_IStreamDevice &) = default;
57  FS_IStreamDevice &operator=(const FS_IStreamDevice &) = default;
58 
59  /// Test whether stream is valid
60  bool isValid() const { return myStream != nullptr; }
61 
62  /// Writing always fails
63  std::streamsize write(const char *, std::streamsize) { return -1; }
64  /// Close the input stream
65  void close()
66  {
67  if (myReader)
68  myReader->close();
69  myStream = nullptr;
70  }
71  /// Try to read data from the stream
72  std::streamsize read(char *buffer, std::streamsize n)
73  {
74  return myStream ? myStream->bread(buffer, n) : -1;
75  }
76  /// Putback a character
78  {
79  return myStream ? myStream->ungetc() == 1 : false;
80  }
81  /// Seek in the stream
82  stream_offset seek(stream_offset off, std::ios_base::seekdir way)
83  {
84  bool ok = false;
85  if (myStream)
86  {
87  if (way == std::ios_base::beg)
88  {
89  ok = myStream->seekg(off, UT_IStream::UT_SEEK_BEG);
90  }
91  else if (way == std::ios_base::cur)
92  {
93  ok = myStream->seekg(off, UT_IStream::UT_SEEK_CUR);
94  }
95  else
96  {
98  ok = myStream->seekg(off, UT_IStream::UT_SEEK_END);
99  }
100  }
101  return ok ? myStream->tellg() : -1;
102  }
103 private:
104  UT_SharedPtr<FS_Reader> myReader;
105  UT_IStream *myStream;
106 };
107 
108 using FS_IStreamDeviceBuffer = bios::stream_buffer<FS_IStreamDevice>;
109 
110 #endif
GLuint GLuint stream
Definition: glcorearb.h:1832
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:3341
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)
bios::stream_buffer< FS_IStreamDevice > FS_IStreamDeviceBuffer
GLdouble n
Definition: glcorearb.h:2008
stream_offset seek(stream_offset off, std::ios_base::seekdir way)
Seek in the stream.
Definition: core.h:760
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
GLuint GLuint end
Definition: glcorearb.h:475
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:84
bios::stream_offset stream_offset
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
#define FS_API
Definition: FS_API.h:10