Software and Services, Portable Libraries  2019.Mar.01
A library for managing digital certificates
File.hpp
Go to the documentation of this file.
1 //
2 // BEGIN_SOFTWARE_AND_SERVICES_COPYRIGHT
3 //
4 // Copyright (C) 2010-2017 Software and Services LLC, All Rights Reserved.
5 //
6 // NOTICE:
7 //
8 // THIS SOURCE CODE BELONGS TO Software and Services LLC.
9 // THIS CODE IS PROPERTY OF THE COPYRIGHT HOLDER AND IS NOT TO
10 // BE DIVULGED OR USED BY PARTIES WHO HAVE NOT RECEIVED WRITTEN
11 // AUTHORIZATION FROM THE OWNER.
12 //
13 // THIS SOFTWARE AND MANUAL ARE BOTH PROTECTED BY UNITED STATES
14 // COPYRIGHT LAW (TITLE 17 UNITED STATES CODE). UNAUTHORIZED REPRODUCTION
15 // AND/OR SALE MAY RESULT IN IMPRISONMENT OF UP TO ONE YEAR AND FINES
16 // OF UP TO $100,000 (17 USC 506). COPYRIGHT INFRINGERS MAY ALSO
17 // BE SUBJECT TO CIVIL LIABILITY.
18 //
19 // THIS NOTICE AND THE ABOVE COPYRIGHT NOTICE MUST REMAIN IN ALL
20 // COPIES AND ANY DERIVED WORK.
21 //
22 // ALL OF THIS CODE IS ORIGINAL WORK BY THE COPYRIGHT HOLDER.
23 //
24 // THIS DOCUMENT AND THE INFORMATION CONTAINED HEREIN IS
25 // PROVIDED ON AN "AS IS" BASIS AND Software and Services LLC.
26 // DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
27 // NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
28 // HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED
29 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
30 // PURPOSE.
31 //
32 // END_SOFTWARE_AND_SERVICES_COPYRIGHT
33 //
70 #ifndef SOFTWARE_AND_SERVICES_LIBRARY_COMMON_FILE_HPP_
71 #define SOFTWARE_AND_SERVICES_LIBRARY_COMMON_FILE_HPP_
72 
73 #ifdef WIN32
74 #pragma warning (disable : 4251)
75 #endif
76 
77 #ifdef BUILDING_SAS_COMMON_LIBRARY
78 #include "osver.h"
79 #include "instance.hpp"
80 #include "Tokens.h"
81 #else
82 #include <SaS/Common/osver.h>
83 #include <SaS/Common/instance.hpp>
84 #include <SaS/Common/Tokens.h>
85 #endif
86 
87 #ifndef EXPORT_COMMON
88 #ifdef BUILDING_SAS_COMMON_LIBRARY
89 
90 #define EXPORT_COMMON EXPORT_OUT
91 #else
92 
93 #define EXPORT_COMMON EXPORT_IN
94 #endif
95 #endif
96 
97 #undef FILE_USE_STD
98 #ifdef FILE_USE_STD
99 #include <fstream>
100 #endif
101 
102 #include <stdint.h>
103 #include <sys/types.h>
104 #include <sys/stat.h>
105 #include <fcntl.h>
106 
107 #include <pthread.h>
108 
109 #ifndef WIN32
110 #include <sys/ioctl.h>
111 #else
112 #ifdef BUILDING_SAS_COMMON_LIBRARY
113 #include "WinWrappers.hpp"
114 #else
115 #include <SaS/Common/WinWrappers.hpp>
116 #endif
117 #include <io.h>
118 #endif
119 
120 #include <errno.h>
121 
122 #ifndef EOVERFLOW
123 
124 #define EOVERFLOW EINVAL
125 #endif
126 
127 #include <wchar.h>
128 
129 #ifdef WIN32
130 #ifdef mkdir
131 #undef mkdir
132 #endif
133 #endif
134 
135 #include <stdint.h>
136 
137 namespace SoftwareAndServices
138 {
139  namespace Library
140  {
141  namespace Common
142  {
143 
150  class EXPORT_COMMON File
151  : virtual public instance<File>
152  {
153 
154  public:
155 
166  File();
167 
168 #ifdef FILE_USE_STD
169 
183  File(std::fstream * fs, const char * aFileName);
184 #endif
185 
207  File(const int fd, const char * aFileName);
208 
224  explicit File(const void * buffer, uint32_t BufferSize = 0);
225 
241  explicit File(const wchar_t * buffer, uint32_t BufferSize = 0);
242 
261  explicit File(char ** buffer, uint32_t * BufferSize = nullptr);
262 
281  explicit File(wchar_t ** buffer, uint32_t * BufferSize = nullptr);
282 
288  virtual ~File();
289 
298  void ThrowExceptions(bool Enable);
299 
308  static bool Lock(const char * fileName);
309 
318  static bool LockFileOnly(const char * fileName);
319 
328  static bool Trylock(const char * fileName);
329 
338  static bool Unlock(const char * fileName);// UnLock DIR
339 
348  static bool UnlockFileOnly(const char * fileName);
349 
360  bool Open(const char * file, int RWmode);
361 
372  bool Mkdir(const char * path, mode_t mode);
373 
380  bool Close();
381 
388  bool Flush();
389 
394  uint32_t CanRead();
395 
400  bool AtEOF();
401 
422  uint32_t Read(void * data,
423  const uint32_t Length,
424  bool force = true);
425 
503  bool ReadLine(uint8_t ** Results,
504  uint32_t & Length,
505  bool Force = true,
506  bool DiscardEol = true);
507 
525  uint32_t Write(const void * data,
526  const uint32_t Length);
527 
542  uint32_t Write(const char * data);
543 
553  int Printf(const char * Format, ...);
554 
560  enum Whence_t {
561 
565  SEEK_SET_t = SEEK_SET,
566 
570  SEEK_CUR_t = SEEK_CUR,
571 
575  SEEK_END_t = SEEK_END
576  };
577 
589  off_t Lseek(off_t offset, Whence_t whence);
590 
595  off_t Tell();
596 
601  bool NextIsLwsp();
602 
609  char LookNext();
610 
616  int GetFd();
617 
622  const char * GetFileName() const;
623 
628  const char * GetBuffer() const;
629 
634  bool IsMMapped() const;
635 
640  uint32_t GetBufferSize() const;
641 
646  static const char * GetTopDir();
647 
655  static bool SetTopDir(const char * theTop);
656 
661  static const char * DefaultTopDir;
662 
667  static const char * NewTmpFileExt;
668 
682  bool Keys(const char * Private,
683  const char * Public);
684 
694  bool Encrypt(bool DoEncryption);
695 
696  private:
697 
701  static int _MaxBufferSize;
702 
706  static int _CreateMode;
707 
708 #ifdef FILE_USE_STD
709 
712  std::fstream * _Fs;
713 #else
714 
717  int _Fd; // OS file descriptor.
718 #endif
719 
722  const char * _FileName;
723 #ifndef WIN32
724 
727  struct stat _StatBuf;
728 #else// WIN32
729 
732  struct __stat64 _StatBuf;
733 #endif//WIN32
734 
738  bool _WeOpened;
739 
743  bool _IsSocket;
744 
748  bool _IsBuffer;
749 
753  bool _IsFile;
754 
758  bool _AtEOF;
759 
763  char * _Buf;
764 
768  char * _StartPtr;
769 
774  char ** _OriginalBuf;
775 
780  uint32_t * _OriginalSize;
781 
785  uint32_t _BufSize;
786 
787 
791  uint32_t _BCount;
792 
796  bool _OurBuffer;
797 
801  bool _Mmaped;
802 
806  int _Mode;
807 
811  static const char * _TopDir;
812 
813 #ifdef WIN32
814 
817  HANDLE _FdHandle;
818 
822  HANDLE _CreateMapHandle;
823 #endif
824 
828  bool _IsClosed;
829 
833  bool _ThrowExceptions;
834 
838  void _BlockSigPipe();
839 
843  const char * _Private;
844 
848  const char * _Public;
849 
853  bool _Encrypt;
854 
858  void * _Tls;
859 
863  void _CloseTls();
864 
865  };
866  }
867  }
868 }
869 
870 #endif // SOFTWARE_AND_SERVICES_LIBRARY_COMMON_FILE_HPP_
Like &#39;FILE&#39;, except C++ implementation and allows sockets and disk files to use same object on WINDOW...
Definition: File.hpp:150
Whence_t
How to apply the offset variable in the lseek() member function.
Definition: File.hpp:560
Instance counting and tracking.
Definition: instance.hpp:90
static const char * DefaultTopDir
The current topDir.
Definition: File.hpp:661
Copyright Douglas Mark Royer DouglasRoyer@gmail.com.
Definition: Base.hpp:98
Base class for instance counting objects.
static const char * NewTmpFileExt
defult extension for temporary files created.
Definition: File.hpp:667
"<SaS/Common/psver.h>" Definitions specific to various OS&#39;s and OS versions.