CFx SDK Documentation  2023 SP0
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_
FIRSTDLL_EXPORT OdRxDynamicLinker * odrxDynamicLinker()
@ CP_UTF_8
Definition: OdCodePage.h:77
#define ODRX_ABSTRACT
#define GETBIT(flags, bit)
Definition: OdaDefs.h:500
#define FIRSTDLL_EXPORT
Definition: RootExport.h:39
FIRSTDLL_EXPORT OdRxObjectPtr odrxCreateObject(const OdString &sClassName)
FIRSTDLL_EXPORT OdRxSystemServices * odrxSystemServices()
virtual Id endWrite(Hash *hash=0)=0
ODRX_DECLARE_MEMBERS(FilePx)
virtual OdRxObjectPtr open(OdRxObject *obj, const char *path, File::Access accessMode=File::Read, File::Share shareMode=File::DenyNo, File::Creation disposition=File::OpenExisting)=0
static OdRxObjectPtr mount(OdRxObject *fs, const OdString &mountPath=OdString::kEmpty)
static OdRxObjectPtr unmount(const OdString &mountPath)
Definition: RxFS.h:104
virtual void mount(File::Id fid, const OdString &path)=0
static RxFSP byMountPath(const OdString &storageMountPath)
Definition: RxFS.h:144
File::ReadBufP openR(const File::Hash &hsh)
Definition: RxFS.h:133
virtual void attach(const OdString &pathToData)=0
virtual File::ReadBufP openR(File::Id file)=0
void unmount(File::Id fid)
Definition: RxFS.h:122
bool exists(const OdString &path, File::Hash &outHash)
Definition: RxFS.h:274
virtual File::Id fileId(const File::Hash &hsh)=0
File::ReadBufP openR(const OdString &path)
Definition: RxFS.h:126
StreamP open(const OdString &path, int access=File::Read, File::Share sh=File::DenyReadWrite, File::Creation dispos=File::OpenExisting)
Definition: RxFS.h:264
virtual File::Id fileId(const OdString &path)=0
virtual bool fileHash(File::Id fid, File::Hash &outHash)=0
static RxFSP create(const OdString &pathToData=OdString::kEmpty)
Definition: RxFS.h:255
ODRX_DECLARE_MEMBERS(RxFS)
virtual File::WriteBufP openW(const OdString &path=OdString::kEmpty, OdInt64 fixedLength=-1)=0
virtual File::Id put(Stream *data, File::Hash *out, const OdString &mountPath=OdString::kEmpty)=0
OdAnsiString toStr()
Definition: RxFS.h:223
void putStr(const OdString &str)
Definition: RxFS.h:231
virtual OdUInt64 seek(OdInt64 offset, OdDb::FilerSeekType seekType)
OdUInt64 size()
Definition: RxFS.h:221
bool isNull() const
Definition: BaseObjectPtr.h:70
Definition: Int64.h:43
virtual OdRxModulePtr loadApp(const OdString &applicationName, bool silent=true)=0
virtual OdStreamBufPtr createFile(const OdString &filename, Oda::FileAccessMode accessMode=Oda::kFileRead, Oda::FileShareMode shareMode=Oda::kShareDenyNo, Oda::FileCreationDisposition creationDisposition=Oda::kOpenExisting)
virtual bool accessFile(const OdString &filename, int accessMode)
void attach(const T *pObject)
Definition: SmartPtr.h:215
virtual void rewind()
virtual OdUInt64 length()
virtual void getBytes(void *buffer, OdUInt32 numBytes)
virtual OdUInt64 seek(OdInt64 offset, OdDb::FilerSeekType seekType)
virtual void putBytes(const void *buffer, OdUInt32 numBytes)
FIRSTDLL_EXPORT_STATIC static const OdString kEmpty
Definition: OdString.h:98
GLint GLenum GLsizei GLsizei GLint GLsizei const void * data
Definition: gles2_ext.h:110
Creation
Definition: RxFS.h:74
@ OpenExisting
Definition: RxFS.h:77
@ TruncateExisting
Definition: RxFS.h:79
@ OpenAlways
Definition: RxFS.h:78
@ CreateNew
Definition: RxFS.h:75
@ CreateAlways
Definition: RxFS.h:76
OdTfDigest Hash
Definition: RxFS.h:83
OdSmartPtr< Stream > ReadBufP
Definition: RxFS.h:85
Share
Definition: RxFS.h:68
@ DenyWrite
Definition: RxFS.h:70
@ DenyRead
Definition: RxFS.h:71
@ DenyReadWrite
Definition: RxFS.h:69
@ DenyNo
Definition: RxFS.h:72
bool access(const OdString &path, int acc=Read)
Definition: RxFS.h:235
OdAnsiString toStr(const OdString &path)
Definition: RxFS.h:244
void putStr(const OdString &path, const OdString &str)
Definition: RxFS.h:249
void putStr(const OdString &path, const OdAnsiString &str)
Definition: RxFS.h:251
OdUInt64 Id
Definition: RxFS.h:82
Access
Definition: RxFS.h:60
@ Delete
Definition: RxFS.h:64
@ IfExists
Definition: RxFS.h:66
@ Read
Definition: RxFS.h:61
@ Write
Definition: RxFS.h:62
@ Tmp
Definition: RxFS.h:63
@ Test
Definition: RxFS.h:65
OdSmartPtr< WriteBuf > WriteBufP
Definition: RxFS.h:91
StreamP open(const OdString &path, int acc=Read, Creation dispos=OpenExisting, Share sh=DenyNo)
Definition: RxFS.h:239
Definition: OdPathUtil.h:33
OdRxDictionary Dictionary
Definition: RxFS.h:44
OdSmartPtr< Stream > StreamP
Definition: RxFS.h:57
OdSmartPtr< FilePx > FilePxP
Definition: RxFS.h:40
OdRxClass Class
Definition: RxFS.h:42
OdSmartPtr< Dictionary > DictionaryP
Definition: RxFS.h:45
OdAnsiString toUTF8(const OdString &str)
Definition: RxFS.h:36
OdSmartPtr< RxFS > RxFSP
Definition: RxFS.h:102
OdRxClassPtr ClassP
Definition: RxFS.h:43
@ kSeekFromStart
Definition: OdStreamBuf.h:50
@ eKeyNotFound
A try to proceed an operation with a null smart pointer to the Teigha Publish document.
@ kShareDenyRead
@ kShareDenyWrite
@ kShareDenyNo
@ kShareDenyReadWrite
FileCreationDisposition
@ kCreateNew
@ kOpenAlways
@ kCreateAlways
@ kTruncateExisting
@ kOpenExisting
@ kFileDelete
@ kFileWrite
Definition: TfIO.h:9