@@ -74,16 +74,28 @@ typedef struct _dbfheader {
74
74
// uchar Dbfox :4; /* FoxPro if equal to 3 */
75
75
uchar Version; /* Version information flags */
76
76
char Filedate[3 ]; /* date, YYMMDD, binary. YY=year-1900 */
77
- uint Records; /* records in the file */
78
- ushort Headlen; /* bytes in the header */
79
- ushort Reclen; /* bytes in a record */
80
- ushort Fields; /* Reserved but used to store fields */
77
+ private:
78
+ /* The following four members are stored in little-endian format on disk */
79
+ char m_RecordsBuf[4 ]; /* records in the file */
80
+ char m_HeadlenBuf[2 ]; /* bytes in the header */
81
+ char m_ReclenBuf[2 ]; /* bytes in a record */
82
+ char m_FieldsBuf[2 ]; /* Reserved but used to store fields */
83
+ public:
81
84
char Incompleteflag; /* 01 if incomplete, else 00 */
82
85
char Encryptflag; /* 01 if encrypted, else 00 */
83
86
char Reserved2[12 ]; /* for LAN use */
84
87
char Mdxflag; /* 01 if production .mdx, else 00 */
85
88
char Language; /* Codepage */
86
89
char Reserved3[2 ];
90
+
91
+ uint Records () const { return uint4korr (m_RecordsBuf); }
92
+ ushort Headlen () const { return uint2korr (m_HeadlenBuf); }
93
+ ushort Reclen () const { return uint2korr (m_ReclenBuf); }
94
+ ushort Fields () const { return uint2korr (m_FieldsBuf); }
95
+
96
+ void SetHeadlen (ushort num) { int2store (m_HeadlenBuf, num); }
97
+ void SetReclen (ushort num) { int2store (m_ReclenBuf, num); }
98
+ void SetFields (ushort num) { int2store (m_FieldsBuf, num); }
87
99
} DBFHEADER;
88
100
89
101
/* ***************************************************************************/
@@ -143,7 +155,7 @@ static int dbfhead(PGLOBAL g, FILE *file, PSZ fn, DBFHEADER *buf)
143
155
strcpy (g->Message , MSG (DBASE_FILE));
144
156
145
157
// Check last byte(s) of header
146
- if (fseek (file, buf->Headlen - dbc, SEEK_SET) != 0 ) {
158
+ if (fseek (file, buf->Headlen () - dbc, SEEK_SET) != 0 ) {
147
159
sprintf (g->Message , MSG (BAD_HEADER), fn);
148
160
return RC_FX;
149
161
} // endif fseek
@@ -163,7 +175,7 @@ static int dbfhead(PGLOBAL g, FILE *file, PSZ fn, DBFHEADER *buf)
163
175
} // endif endmark
164
176
165
177
// Calculate here the number of fields while we have the dbc info
166
- buf->Fields = ( buf->Headlen - dbc - 1 ) / 32 ;
178
+ buf->SetFields (( buf->Headlen () - dbc - 1 ) / 32 ) ;
167
179
fseek (file, HEADLEN, SEEK_SET);
168
180
return rc;
169
181
} // end of dbfhead
@@ -219,7 +231,7 @@ PQRYRES DBFColumns(PGLOBAL g, char *dp, const char *fn, bool info)
219
231
/* ***********************************************************************/
220
232
/* Allocate the structures used to refer to the result set. */
221
233
/* ***********************************************************************/
222
- fields = mainhead.Fields ;
234
+ fields = mainhead.Fields () ;
223
235
} else
224
236
fields = 0 ;
225
237
@@ -236,11 +248,11 @@ PQRYRES DBFColumns(PGLOBAL g, char *dp, const char *fn, bool info)
236
248
if (trace) {
237
249
htrc (" Structure of %s\n " , filename);
238
250
htrc (" headlen=%hd reclen=%hd degree=%d\n " ,
239
- mainhead.Headlen , mainhead.Reclen , fields);
251
+ mainhead.Headlen () , mainhead.Reclen () , fields);
240
252
htrc (" flags(iem)=%d,%d,%d cp=%d\n " , mainhead.Incompleteflag ,
241
253
mainhead.Encryptflag , mainhead.Mdxflag , mainhead.Language );
242
254
htrc (" %hd records, last changed %02d/%02d/%d\n " ,
243
- mainhead.Records , mainhead.Filedate [1 ], mainhead.Filedate [2 ],
255
+ mainhead.Records () , mainhead.Filedate [1 ], mainhead.Filedate [2 ],
244
256
mainhead.Filedate [0 ] + (mainhead.Filedate [0 ] <= 30 ) ? 2000 : 1900 );
245
257
htrc (" Field Type Offset Len Dec Set Mdx\n " );
246
258
} // endif trace
@@ -398,13 +410,13 @@ int DBFBASE::ScanHeader(PGLOBAL g, PSZ fname, int lrecl, char *defpath)
398
410
} else if (rc == RC_FX)
399
411
return -1 ;
400
412
401
- if ((int )header.Reclen != lrecl) {
402
- sprintf (g->Message , MSG (BAD_LRECL), lrecl, header.Reclen );
413
+ if ((int ) header.Reclen () != lrecl) {
414
+ sprintf (g->Message , MSG (BAD_LRECL), lrecl, header.Reclen () );
403
415
return -1 ;
404
416
} // endif Lrecl
405
417
406
- Records = (int )header.Records ;
407
- return (int )header.Headlen ;
418
+ Records = (int ) header.Records () ;
419
+ return (int ) header.Headlen () ;
408
420
} // end of ScanHeader
409
421
410
422
/* ---------------------------- Class DBFFAM ------------------------------ */
@@ -565,8 +577,8 @@ bool DBFFAM::AllocateBuffer(PGLOBAL g)
565
577
header->Filedate [0 ] = datm->tm_year - 100 ;
566
578
header->Filedate [1 ] = datm->tm_mon + 1 ;
567
579
header->Filedate [2 ] = datm->tm_mday ;
568
- header->Headlen = ( ushort)hlen;
569
- header->Reclen = ( ushort)reclen;
580
+ header->SetHeadlen (( ushort) hlen) ;
581
+ header->SetReclen (( ushort) reclen) ;
570
582
descp = (DESCRIPTOR*)header;
571
583
572
584
// Currently only standard Xbase types are supported
@@ -627,13 +639,13 @@ bool DBFFAM::AllocateBuffer(PGLOBAL g)
627
639
DBFHEADER header;
628
640
629
641
if ((rc = dbfhead (g, Stream, Tdbp->GetFile (g), &header)) == RC_OK) {
630
- if (Lrecl != (int )header.Reclen ) {
631
- sprintf (g->Message , MSG (BAD_LRECL), Lrecl, header.Reclen );
642
+ if (Lrecl != (int ) header.Reclen () ) {
643
+ sprintf (g->Message , MSG (BAD_LRECL), Lrecl, header.Reclen () );
632
644
return true ;
633
645
} // endif Lrecl
634
646
635
- Records = (int )header.Records ;
636
- Headlen = (int )header.Headlen ;
647
+ Records = (int ) header.Records () ;
648
+ Headlen = (int ) header.Headlen () ;
637
649
} else if (rc == RC_NF) {
638
650
Records = 0 ;
639
651
Headlen = 0 ;
@@ -868,8 +880,10 @@ void DBFFAM::CloseTableFile(PGLOBAL g, bool abort)
868
880
PlugSetPath (filename, To_File, Tdbp->GetPath ());
869
881
if ((Stream= global_fopen (g, MSGID_OPEN_MODE_STRERROR, filename, " r+b" )))
870
882
{
883
+ char nRecords[4 ];
884
+ int4store (&nRecords, n);
871
885
fseek (Stream, 4 , SEEK_SET); // Get header.Records position
872
- fwrite (&n , sizeof (int ), 1 , Stream);
886
+ fwrite (&nRecords , sizeof (nRecords ), 1 , Stream);
873
887
fclose (Stream);
874
888
Stream= NULL ;
875
889
Records= n; // Update Records value
@@ -944,13 +958,13 @@ bool DBMFAM::AllocateBuffer(PGLOBAL g)
944
958
/* ***********************************************************************/
945
959
DBFHEADER *hp = (DBFHEADER*)Memory;
946
960
947
- if (Lrecl != (int )hp->Reclen ) {
948
- sprintf (g->Message , MSG (BAD_LRECL), Lrecl, hp->Reclen );
961
+ if (Lrecl != (int ) hp->Reclen () ) {
962
+ sprintf (g->Message , MSG (BAD_LRECL), Lrecl, hp->Reclen () );
949
963
return true ;
950
964
} // endif Lrecl
951
965
952
- Records = (int )hp->Records ;
953
- Headlen = (int )hp->Headlen ;
966
+ Records = (int ) hp->Records () ;
967
+ Headlen = (int ) hp->Headlen () ;
954
968
} // endif Headlen
955
969
956
970
/* *************************************************************************/
0 commit comments