summaryrefslogtreecommitdiff
path: root/src/include/xbfile.h
blob: a6366324ca6f675df522ff2efce0964ff41b314d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*  xbfile.h

XBase64 Software Library

Copyright (c) 1997,2003,2014,2022 Gary A Kunkel

The xb64 software library is covered under the terms of the GPL Version 3, 2007 license.

Email Contact:

    XDB-devel@lists.sourceforge.net
    XDB-users@lists.sourceforge.net

*/

#ifndef __XB_FILE_H__
#define __XB_FILE_H__

#ifdef  CMAKE_COMPILER_IS_GNUCC
#pragma interface
#endif

/*****************************/
/* File Open Modes           */
#define XB_READ       0
#define XB_READ_WRITE 1
#define XB_WRITE      2

/*****************************/
/* File Access Modes         */
#define XB_SINGLE_USER  0      // file buffering on
#define XB_MULTI_USER   1      // file buffering off

namespace xb{

/*! @brief Class for handling low level file I/O.
*/

/*!
The xbFile class is used as an abstraction layer to isolate the library from the nuances 
of different operating systems and machine architectures.

This module handles low level file I/O and is a base class
for the (dbf) table, (dbt) memo and (ndx,mdx) index classes.
Files are opened and manipulated via methods in the xbFile class.<br>
This class handles:<br>
1) Big and little endian reads/writes<br>
2) 32 or 64 bit system calls, depending on OS<br>
3) Various different c/c++ calls to open/close/read/write based on OS and compiler version<br>
<br>
If you are wanting to port this library to a new platform, start with this class.
This class could be used if you want to write a platform independent program that needs R/W file access.
*/


class XBDLLEXPORT xbFile : public xbSsv {

  public:
    //         xbFile();
             xbFile( xbXBase * x );

             ~xbFile();
    xbInt16  CreateUniqueFileName( const xbString &sDirIn, const xbString &sExtIn, xbString &sFqnOut );
    xbInt16  CreateUniqueFileName( const xbString &sDirIn, const xbString &sExtIn, xbString &sFqnOut, xbInt16 iOption );

    const    xbString& GetDirectory() const;
    const    xbString& GetFileName() const;
    const    xbString& GetFqFileName() const;

    void     SetDirectory ( const xbString &sDirectory);
    void     SetFileName  ( const xbString &sFileName );
    void     SetFqFileName( const xbString &sFqName );

    xbUInt32 GetBlockSize () const;
    xbInt16  SetBlockSize ( xbUInt32 ulBlockSize );

    xbInt16  GetOpenMode  () const;
    xbInt16  GetShareMode () const;

    xbInt16  GetFileDirPart ( xbString &sFileDirPartOut ) const;
    xbInt16  GetFileDirPart ( const xbString &sCompleteFileNameIn, xbString &sFileDirPartOut ) const;
    xbInt16  GetFileExtPart ( xbString &sFileExtPartOut ) const;
    xbInt16  GetFileExtPart ( const xbString &sCompleteFileNameIn, xbString &sFileExtPartOut ) const;
    xbInt16  GetFileNamePart( xbString &sFileNamePartOut ) const;
    xbInt16  GetFileNamePart( const xbString &sCompleteFileNameIn, xbString &sFileNamePartOut ) const;
    xbInt16  GetFileType    ( xbString &sFileType ) const;

    xbInt16  GetXbaseFileTypeByte( const xbString &sFileName, xbInt16 &iVersion );
    xbInt16  GetXbaseFileTypeByte( const xbString &sFileName, unsigned char &cFileTypeByte );
    xbInt16  GetXbaseFileTypeByte( const xbString &sFileName, unsigned char &cFileTypeByte, xbInt16 &iVersion );
    xbInt16  DetermineXbaseTableVersion( unsigned char cFileTypeByte ) const;
    xbInt16  DetermineXbaseMemoVersion( unsigned char cFileTypeByte ) const;

    xbBool   FileExists () const;
    xbBool   FileExists ( xbInt16 iOption ) const;
    xbBool   FileExists ( const xbString &sFileName ) const;
    xbBool   FileExists ( const xbString &sFileName, xbInt16 iOption ) const;

    xbBool   FileIsOpen () const;

    xbInt16  ReadBlock ( xbUInt32 ulBlockNo, size_t readSize,  void *buf );
    xbInt16  ReadBlock ( xbUInt32 ulBlockNo, xbUInt32 ulBlockSize, size_t readSize,  void *buf );
    xbInt16  WriteBlock( xbUInt32 ulBlockNo, size_t writeSize, void *buf );

    xbInt16  GetFileSize( xbUInt64 &ullFileSize );
    xbInt16  GetFileMtime( time_t &mtime );

    xbDouble eGetDouble( const char *p ) const;
    xbInt32  eGetInt32 ( const char *p ) const;
    xbUInt32 eGetUInt32( const char *p ) const;
    xbInt16  eGetInt16 ( const char *p ) const;
    xbUInt16 eGetUInt16( const char *p ) const;
    void     ePutDouble( char *p, xbDouble d );
    void     ePutInt32 ( char *p, xbInt32 l );
    void     ePutUInt32( char *p, xbUInt32 ul );
    void     ePutInt16 ( char *p, xbInt16 s );
    void     ePutUInt16( char *p, xbUInt16 s );

    xbInt16  xbFclose    ();
    xbInt16  xbFeof      ();
    xbInt16  xbFflush    ();
    xbInt16  xbFgetc     ( xbInt32 &c );
    xbInt16  xbFgetc     ( char &c );

    #ifdef XB_LOCKING_SUPPORT
    xbInt16  xbLock           ( xbInt16 iFunction, xbInt64 llOffset, size_t stLen );
    xbInt16  GetLockRetryCount() const;
    void     SetLockRetryCount( xbInt16 iLockRetries );
    #endif

    xbInt16  xbFopen      ( xbInt16 iOpenMode );
    xbInt16  xbFopen      ( const xbString &sOpenMode, xbInt16 iShareMode );
    xbInt16  xbFopen      ( xbInt16 iOpenMode, xbInt16 iShareMode );
    xbInt16  xbFopen      ( const xbString &sMode, const xbString &sFileName, xbInt16 iShareMode );

    xbInt16  xbFputc      ( xbInt32 c );
    xbInt16  xbFputc      ( xbInt32 c, xbInt32 iNoTimes );
    xbInt16  xbFputs      ( const xbString &s );
    xbInt16  xbFread      ( void *ptr, size_t size, size_t nmemb );
    xbInt16  xbFgets      ( size_t lSize, xbString &sLine );

    size_t   xbFtell      ();
    xbInt16  xbFseek      ( xbInt64 llOffset, xbInt32 whence );

    void     xbFTurnOffFileBuffering();
    xbInt16  xbFwrite     ( const void *ptr, size_t lSize, size_t lNmemb );

    xbInt16  xbReadUntil  ( const char cDelim, xbString &sOut );
    xbInt16  xbRemove     ( const xbString &sFileName, xbInt16 iOption );
    xbInt16  xbRemove     ( const xbString &sFileName );
    xbInt16  xbRemove     ();

    xbInt16  xbRename     ( const xbString &sOldName, const xbString &sNewName );
    xbInt16  xbRename     ( const xbString &sOldName, const xbString &sNewName, xbInt16 iOption );
    void     xbRewind     ();

    xbInt16  xbTruncate   ( xbInt64 llSize );
    xbInt16  NameSuffixMissing( const xbString &sFileName, xbInt16 iOption ) const;

    #ifdef XB_DEBUG_SUPPORT
    xbInt16  DumpBlockToDisk( xbUInt32 ulBlockNo, size_t lBlockSize );
    #endif


  protected:

    xbXBase *xbase;              /* pointer to the main structures                       */

  private:
    FILE     *fp;
    xbString sFqFileName;        /* fully qualified file name                            */
    xbString sFileName;          /* file name                                            */
    xbString sDirectory;         /* directory, ends with / or \                          */
    xbBool   bFileOpen;          /* true if file is open                                 */
    xbUInt32 ulBlockSize;        /* used for memo and index files                        */

    xbInt16  iOpenMode;          /* XB_READ || XB_READ_WRITE || XB_WRITE                 */
    xbInt16  iShareMode;         /* XB_SINGLE_USER || XB_MULTI_USER - set file buffering */
    xbInt32  iFileNo;            /* Library File No                                      */

    #ifdef XB_LOCKING_SUPPORT
    xbInt16  iLockRetries;       /* file override number of lock attempts                */
    #endif

    #ifdef HAVE_SETENDOFFILE_F
    HANDLE   fHandle;
    #endif


};

}        /* namespace     */
#endif   /*  __XBFILE_H__ */