CFx SDK Documentation  2020SP3
RxFS.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 #ifndef _OD_RxFS_INCLUDED_
25 #define _OD_RxFS_INCLUDED_
26 
27 #include "RxSystemServices.h"
28 #include "RxSysRegistry.h"
29 #include "Tf/TfIO.h"
30 #include "DynamicLinker.h"
31 
32 
33 class OdRxDictionary;
34 
35 namespace OD {
36  inline OdAnsiString toUTF8(const OdString& str) { return OdAnsiString(str, CP_UTF_8); }
37  inline OdAnsiString toUTF8(const char* sz) { return OdAnsiString(sz, CP_UTF_8); }
38  inline OdAnsiString toUTF8(const char* sz, int len) { return OdAnsiString(sz, len, CP_UTF_8); }
39 
40  class FilePx;
42  typedef OdRxClass Class;
46 
47  class Stream : public OdStreamBuf {
48  typedef OdStreamBuf Super;
49  public:
50  using Super::seek;
51  Stream& seek(OdUInt64 pos);
52  OdUInt64 size();
53  inline OdAnsiString toStr();
54  inline void putStr(const OdString& str);
55  inline void putStr(const OdAnsiString& str);
56  };
58 
59  namespace File {
60  enum Access {
61  Read = Oda::kFileRead, // Read access. Use kFileRead | kFileWrite for read/write access.
62  Write = Oda::kFileWrite, // Write access. Use kFileRead | kFileWrite for read/write access.
63  Tmp = Oda::kFileTmp, // hint to use FILE_ATTRIBUTE_TEMPORARY for 'FlagsAndAttributes'
64  Delete = Oda::kFileDelete,// hint to use FILE_FLAG_DELETE_ON_CLOSE for 'FlagsAndAttributes'
65  Test = 1, // Do not open just check existence
66  IfExists = 2 // Supresses OdError_FileNotFound() exception; open() will return NULL instead
67  };
68  enum Share {
73  };
74  enum Creation {
75  CreateNew = Oda::kCreateNew , // Creates a new file; fails if the specified file already exists.
76  CreateAlways = Oda::kCreateAlways , // Creates a new file; overwrites any existing file.
77  OpenExisting = Oda::kOpenExisting , // Opens the file; fails if the file does not exist.
78  OpenAlways = Oda::kOpenAlways , // Opens the file; creates the file if it does not exist.
79  TruncateExisting = Oda::kTruncateExisting // Truncates the file; fails if the file does not exist. The file must be open at least kFileWrite.
80  };
81 
82  typedef OdUInt64 Id;
83  typedef OdTfDigest Hash;
84 
86 
87  class WriteBuf : public Stream {
88  public:
89  virtual Id endWrite(Hash* hash = 0) = 0;
90  };
92 
93  inline bool access(const OdString& path, int acc = Read);
94  inline StreamP open(const OdString& path, int acc = Read, Creation dispos = OpenExisting, Share sh = DenyNo);
95 
96  inline OdAnsiString toStr(const OdString& path);
97 
98  inline void putStr(const OdString& path, const OdString& str);
99  inline void putStr(const OdString& path, const OdAnsiString& str);
100  };
101 
102  class RxFS;
105  public:
107 
108  static RxFSP create(const OdString& pathToData = OdString::kEmpty);
109  virtual void attach(const OdString& pathToData) = 0;
110 
111  StreamP open(const OdString& path,
112  int access = File::Read,
115  );
116 
117  virtual File::Id fileId(const File::Hash& hsh) = 0;
118  virtual File::Id fileId(const OdString& path) = 0;
119  bool exists(const OdString& path, File::Hash& outHash);
120  virtual bool fileHash(File::Id fid, File::Hash& outHash) = 0;
121  virtual void mount(File::Id fid, const OdString& path) = 0;
122  void unmount(File::Id fid) { mount(fid, OdString::kEmpty); }
123 
124  virtual File::ReadBufP openR(File::Id file) = 0;
125 
127  File::Id fid = fileId(path);
128  if(!fid)
129  throw OdError_FileNotFound(path);
130  return openR(fid);
131  }
132 
134  File::Id fid = fileId(hsh);
135  if(!fid)
136  throw OdError(eKeyNotFound);
137  return openR(fid);
138  }
139  virtual File::WriteBufP openW(const OdString& path = OdString::kEmpty, OdInt64 fixedLength = -1) = 0;
140  virtual File::Id put(Stream* data, File::Hash* out, const OdString& mountPath = OdString::kEmpty) = 0;
141 
142  void put(const OdString& path, const char* msg, OdUInt64 len = -1, File::Creation dispos = File::CreateAlways);
143 
144  static RxFSP byMountPath(const OdString& storageMountPath) {
145  return File::open(storageMountPath, File::Read|File::Write, File::OpenExisting);
146  }
147 
148  };
149 
159  public:
161 
163 
164  static OdRxObjectPtr mount(OdRxObject* fs, const OdString& mountPath = OdString::kEmpty);
165  static OdRxObjectPtr unmount(const OdString& mountPath);
166 
210  virtual OdRxObjectPtr open(OdRxObject* obj, const char* path,
211  File::Access accessMode = File::Read,
212  File::Share shareMode = File::DenyNo,
213  File::Creation disposition = File::OpenExisting) = 0;
214  };
215 
216 
217 
218 
219  inline Stream& Stream::seek(OdUInt64 pos) { ((OdStreamBuf*)this)->seek(pos, OdDb::kSeekFromStart); return *this; }
220 
221  inline OdUInt64 Stream::size() { return length(); }
222 
223  inline OdAnsiString Stream::toStr() {
224  OdAnsiString res;
225  OdUInt64 len = length();
226  rewind();
227  getBytes(res.getBuffer(len), len);
228  res.releaseBuffer(len);
229  return res;
230  }
231  inline void Stream::putStr(const OdString& str) { putStr(toUTF8(str)); }
232  inline void Stream::putStr(const OdAnsiString& str) { putBytes(str.c_str(), str.getLength()); }
233 
234 
235  inline bool File::access(const OdString& path, int acc) {
237 }
238 
239  inline StreamP File::open(const OdString& path, int access, Creation dispos, Share sh) {
242  }
243 
244  inline OdAnsiString File::toStr(const OdString& path) {
245  if(!access(path, File::Read))
246  return "";
247  return open(path, File::Read, File::OpenExisting, File::DenyReadWrite)->toStr();
248  }
249  inline void File::putStr(const OdString& path, const OdString& str) { putStr(path, toUTF8(str)); }
250 
251  inline void File::putStr(const OdString& path, const OdAnsiString& str) {
252  open(path, File::Write, File::OpenAlways)->putStr(str);
253  }
254 
255  inline OD::RxFSP RxFS::create(const OdString& pathToData) {
256  ::odrxDynamicLinker()->loadApp("RxFS", false);
257  OD::RxFSP fs = odrxCreateObject("RxFS");
258  if(fs.isNull())
259  throw OdError("Class RxFS is not found");
260  fs->attach(pathToData);
261  return fs;
262  }
263 
264  inline StreamP RxFS::open(const OdString& path, int access, File::Share sh, File::Creation dispos) {
265 # pragma MARKMESSAGE(dna: todo: take file creation disposition into account)
267  return openW(path);
268  return openR(path);
269  }
270 
271  inline void RxFS::put(const OdString& path, const char* msg, OdUInt64 len, File::Creation dispos)
272  { (StreamP(open(path, File::Write, File::DenyReadWrite, dispos)))->putBytes(msg, len); }
273 
274  inline bool RxFS::exists(const OdString& path, File::Hash& outHash) {
275  File::Id fid = fileId(path);
276  if(fid)
277  return fileHash(fid, outHash);
278  return false;
279  }
280 }
281 
282 
283 #endif //#define _OD_RxFS_INCLUDED_
OdError_FileNotFound
Definition: RxSystemServices.h:110
GETBIT
#define GETBIT(flags, bit)
Definition: OdaDefs.h:498
OD::Class
OdRxClass Class
Definition: RxFS.h:42
FIRSTDLL_EXPORT
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
OdStreamBuf::seek
virtual OdUInt64 seek(OdInt64 offset, OdDb::FilerSeekType seekType)
OdString
Definition: OdString.h:95
Oda::kOpenExisting
@ kOpenExisting
Definition: RxSystemServices.h:76
OdRxObjectPtr
Definition: RxObject.h:345
OdStreamBuf::getBytes
virtual void getBytes(void *buffer, OdUInt32 numBytes)
OD::FilePx
Definition: RxFS.h:158
odrxDynamicLinker
FIRSTDLL_EXPORT OdRxDynamicLinker * odrxDynamicLinker()
OD::RxFS::unmount
void unmount(File::Id fid)
Definition: RxFS.h:122
OD::FilePxP
OdSmartPtr< FilePx > FilePxP
Definition: RxFS.h:40
OdTfDigest
Definition: TfIO.h:9
OD::Stream::putStr
void putStr(const OdString &str)
Definition: RxFS.h:231
OdRxSystemServices::createFile
virtual OdStreamBufPtr createFile(const OdString &filename, Oda::FileAccessMode accessMode=Oda::kFileRead, Oda::FileShareMode shareMode=Oda::kShareDenyNo, Oda::FileCreationDisposition creationDisposition=Oda::kOpenExisting)
OD::Stream
Definition: RxFS.h:47
Oda::kOpenAlways
@ kOpenAlways
Definition: RxSystemServices.h:77
OdInt64
Definition: Int64.h:43
OD::ClassP
OdRxClassPtr ClassP
Definition: RxFS.h:43
OD::File::toStr
OdAnsiString toStr(const OdString &path)
Definition: RxFS.h:244
OD::File::DenyWrite
@ DenyWrite
Definition: RxFS.h:70
OdRxObject
Definition: RxObject.h:564
OD::StreamP
OdSmartPtr< Stream > StreamP
Definition: RxFS.h:57
OD::File::Share
Share
Definition: RxFS.h:68
OD::File::IfExists
@ IfExists
Definition: RxFS.h:66
OD::File::Tmp
@ Tmp
Definition: RxFS.h:63
Oda::kCreateNew
@ kCreateNew
Definition: RxSystemServices.h:74
OD::RxFS::openW
virtual File::WriteBufP openW(const OdString &path=OdString::kEmpty, OdInt64 fixedLength=-1)=0
Oda::kFileRead
@ kFileRead
Definition: RxSystemServices.h:56
Oda::kFileDelete
@ kFileDelete
Definition: RxSystemServices.h:60
OD::File::DenyNo
@ DenyNo
Definition: RxFS.h:72
OD::FilePx::open
virtual OdRxObjectPtr open(OdRxObject *obj, const char *path, File::Access accessMode=File::Read, File::Share shareMode=File::DenyNo, File::Creation disposition=File::OpenExisting)=0
OdRxSystemServices::accessFile
virtual bool accessFile(const OdString &filename, int accessMode)
OD::File::Delete
@ Delete
Definition: RxFS.h:64
OD::RxFSP
OdSmartPtr< RxFS > RxFSP
Definition: RxFS.h:102
Oda::kShareDenyNo
@ kShareDenyNo
Definition: RxSystemServices.h:68
data
GLint GLenum GLsizei GLsizei GLint GLsizei const void * data
Definition: gles2_ext.h:110
OD::RxFS::fileId
virtual File::Id fileId(const OdString &path)=0
OD::File::Creation
Creation
Definition: RxFS.h:74
OD::File::DenyReadWrite
@ DenyReadWrite
Definition: RxFS.h:69
Oda::kFileWrite
@ kFileWrite
Definition: RxSystemServices.h:58
OD::RxFS::byMountPath
static RxFSP byMountPath(const OdString &storageMountPath)
Definition: RxFS.h:144
OdRxClass
Definition: RxObject.h:865
OD::RxFS::create
static RxFSP create(const OdString &pathToData=OdString::kEmpty)
Definition: RxFS.h:255
OD::RxFS::exists
bool exists(const OdString &path, File::Hash &outHash)
Definition: RxFS.h:274
OdStreamBuf::length
virtual OdUInt64 length()
OD::File::Write
@ Write
Definition: RxFS.h:62
OdSmartPtr
Definition: SmartPtr.h:58
OD::File::access
bool access(const OdString &path, int acc=Read)
Definition: RxFS.h:235
OdDb::kSeekFromStart
@ kSeekFromStart
Definition: OdStreamBuf.h:50
Oda::kShareDenyReadWrite
@ kShareDenyReadWrite
Definition: RxSystemServices.h:65
OD::File::Id
OdUInt64 Id
Definition: RxFS.h:82
OD::FilePx::unmount
static OdRxObjectPtr unmount(const OdString &mountPath)
CP_UTF_8
@ CP_UTF_8
Definition: OdCodePage.h:77
Oda::kShareDenyRead
@ kShareDenyRead
Definition: RxSystemServices.h:67
OdStreamBuf::putBytes
virtual void putBytes(const void *buffer, OdUInt32 numBytes)
OD::File::putStr
void putStr(const OdString &path, const OdAnsiString &str)
Definition: RxFS.h:251
odrxSystemServices
FIRSTDLL_EXPORT OdRxSystemServices * odrxSystemServices()
DynamicLinker.h
OD::File::Read
@ Read
Definition: RxFS.h:61
OD::RxFS::openR
virtual File::ReadBufP openR(File::Id file)=0
OD::RxFS::open
StreamP open(const OdString &path, int access=File::Read, File::Share sh=File::DenyReadWrite, File::Creation dispos=File::OpenExisting)
Definition: RxFS.h:264
OdStreamBuf::rewind
virtual void rewind()
OdRxDictionary
Definition: RxDictionary.h:114
OD::File::Test
@ Test
Definition: RxFS.h:65
OD::Dictionary
OdRxDictionary Dictionary
Definition: RxFS.h:44
OdRxDynamicLinker::loadApp
virtual OdRxModulePtr loadApp(const OdString &applicationName, bool silent=true)=0
OD::File::WriteBuf
Definition: RxFS.h:87
OD::File::ReadBufP
OdSmartPtr< Stream > ReadBufP
Definition: RxFS.h:85
ODRX_ABSTRACT
#define ODRX_ABSTRACT
Definition: OdPlatformSettings.h:117
OD::Stream::size
OdUInt64 size()
Definition: RxFS.h:221
odrxCreateObject
FIRSTDLL_EXPORT OdRxObjectPtr odrxCreateObject(const OdString &sClassName)
OD::File::CreateAlways
@ CreateAlways
Definition: RxFS.h:76
OD::Stream::toStr
OdAnsiString toStr()
Definition: RxFS.h:223
OD::File::TruncateExisting
@ TruncateExisting
Definition: RxFS.h:79
OD::RxFS::fileHash
virtual bool fileHash(File::Id fid, File::Hash &outHash)=0
OD::File::WriteBuf::endWrite
virtual Id endWrite(Hash *hash=0)=0
OD::File::WriteBufP
OdSmartPtr< WriteBuf > WriteBufP
Definition: RxFS.h:91
OD::File::Hash
OdTfDigest Hash
Definition: RxFS.h:83
OD::FilePx::ODRX_DECLARE_MEMBERS
ODRX_DECLARE_MEMBERS(FilePx)
OD::RxFS::put
virtual File::Id put(Stream *data, File::Hash *out, const OdString &mountPath=OdString::kEmpty)=0
OdStreamBuf
Definition: OdStreamBuf.h:67
Oda::FileCreationDisposition
FileCreationDisposition
Definition: RxSystemServices.h:73
RxSystemServices.h
OD::File::DenyRead
@ DenyRead
Definition: RxFS.h:71
OdError
Definition: OdError.h:43
Oda::kShareDenyWrite
@ kShareDenyWrite
Definition: RxSystemServices.h:66
OD::File::Access
Access
Definition: RxFS.h:60
OdPdfPublish::eKeyNotFound
@ eKeyNotFound
A try to proceed an operation with a null smart pointer to the Teigha Publish document.
Definition: PdfPublishCommon.h:48
OD::RxFS::fileId
virtual File::Id fileId(const File::Hash &hsh)=0
OdString::kEmpty
FIRSTDLL_EXPORT_STATIC static const OdString kEmpty
Definition: OdString.h:98
Oda::kFileTmp
@ kFileTmp
Definition: RxSystemServices.h:59
OD::RxFS::openR
File::ReadBufP openR(const File::Hash &hsh)
Definition: RxFS.h:133
OD::toUTF8
OdAnsiString toUTF8(const OdString &str)
Definition: RxFS.h:36
OD::DictionaryP
OdSmartPtr< Dictionary > DictionaryP
Definition: RxFS.h:45
OD::RxFS::openR
File::ReadBufP openR(const OdString &path)
Definition: RxFS.h:126
OD::RxFS::mount
virtual void mount(File::Id fid, const OdString &path)=0
OD::Stream::seek
virtual OdUInt64 seek(OdInt64 offset, OdDb::FilerSeekType seekType)
OD::File::OpenExisting
@ OpenExisting
Definition: RxFS.h:77
RxSysRegistry.h
OD::File::OpenAlways
@ OpenAlways
Definition: RxFS.h:78
TfIO.h
Oda::kTruncateExisting
@ kTruncateExisting
Definition: RxSystemServices.h:78
Oda::FileShareMode
FileShareMode
Definition: RxSystemServices.h:64
OD
Definition: OdPathUtil.h:33
OD::File::open
StreamP open(const OdString &path, int acc=Read, Creation dispos=OpenExisting, Share sh=DenyNo)
Definition: RxFS.h:239
OD::FilePx::mount
static OdRxObjectPtr mount(OdRxObject *fs, const OdString &mountPath=OdString::kEmpty)
OdSmartPtr::attach
void attach(const T *pObject)
Definition: SmartPtr.h:215
OD::RxFS
Definition: RxFS.h:104
Oda::FileAccessMode
FileAccessMode
Definition: RxSystemServices.h:55
Oda::kCreateAlways
@ kCreateAlways
Definition: RxSystemServices.h:75
OD::RxFS::ODRX_DECLARE_MEMBERS
ODRX_DECLARE_MEMBERS(RxFS)
OD::RxFS::attach
virtual void attach(const OdString &pathToData)=0
OdUInt64
Definition: Int64.h:137
OD::File::CreateNew
@ CreateNew
Definition: RxFS.h:75
OD::File::putStr
void putStr(const OdString &path, const OdString &str)
Definition: RxFS.h:249