131 if( (m_hFile = fopen(
CSG_String(FileName),
"rb")) == NULL )
149 fseek(m_hFile, 0, SEEK_END);
150 m_nFileBytes = ftell(m_hFile);
151 fseek(m_hFile, 0, SEEK_SET);
228 inline bool CSG_Table_DBase::Read(
void *buffer,
size_t size,
size_t count)
230 return( m_hFile && fread(buffer, size, count, m_hFile) == size * count );
234 inline bool CSG_Table_DBase::Write(
void *buffer,
size_t size,
size_t count)
236 return( m_hFile && fwrite(buffer, size, count, m_hFile) == size * count );
240 bool CSG_Table_DBase::Header_Read(
void)
248 char buffer[16]; TDBF_Header h;
250 fseek(m_hFile, 0, SEEK_SET);
254 Read(&h.FileType ,
sizeof(
char), 1);
259 Read(&h.LastUpdate ,
sizeof(
char), 3);
261 Read(&m_nHeaderBytes ,
sizeof(
char), 2);
262 Read(&m_nRecordBytes ,
sizeof(
char), 2);
263 Read(buffer ,
sizeof(
char), 2);
264 Read(&h.Transaction ,
sizeof(
char), 1);
267 Read(&h.bEncrypted ,
sizeof(
char), 1);
269 Read(buffer ,
sizeof(
char), 12);
270 Read(&h.ProductionIdx,
sizeof(
char), 1);
271 Read(&h.LanguageDrvID,
sizeof(
char), 1);
272 Read(buffer ,
sizeof(
char), 2);
276 while( ftell(m_hFile) < (
long)m_nHeaderBytes - 1 && !feof(m_hFile) )
280 Read( m_Fields[
m_nFields].Name ,
sizeof(
char), 11);
281 Read(&m_Fields[
m_nFields].Type ,
sizeof(
char), 1);
282 Read(&m_Fields[
m_nFields].Displacement ,
sizeof(
char), 4);
283 Read(&m_Fields[
m_nFields].Width ,
sizeof(
char), 1);
284 Read(&m_Fields[
m_nFields].Decimals ,
sizeof(
char), 1);
285 Read(buffer ,
sizeof(
char), 2);
286 Read(&m_Fields[
m_nFields].WorkAreaID ,
sizeof(
char), 1);
287 Read(buffer ,
sizeof(
char), 10);
288 Read(&m_Fields[
m_nFields].ProductionIdx,
sizeof(
char), 1);
297 Read(buffer,
sizeof(
char), 1);
299 if( buffer[0] == 0x0d )
329 if( (m_hFile = fopen(
CSG_String(FileName),
"w+b")) == NULL )
346 for(
int j=0; j<11 && j<(int)Name.
Length(); j++)
348 m_Fields[iField].Name[j] = Name.
b_str()[j];
355 m_Fields[iField].Width = (BYTE)((nBytes = pTable->
Get_Field_Length(iField,
m_Encoding)) > 255 ? 255 : nBytes < 1 ? 1 : nBytes);
360 m_Fields[iField].Width = (BYTE)8;
365 m_Fields[iField].Width = (BYTE)1;
370 m_Fields[iField].Width = (BYTE)1;
375 m_Fields[iField].Width = (BYTE)3;
381 m_Fields[iField].Width = (BYTE)6;
390 m_Fields[iField].Width = (BYTE)16;
395 m_Fields[iField].Width = (BYTE)16;
396 m_Fields[iField].Decimals = (BYTE)8;
401 m_Fields[iField].Width = (BYTE)19;
402 m_Fields[iField].Decimals = (BYTE)10;
409 m_nFileBytes = m_nHeaderBytes;
449 void CSG_Table_DBase::Header_Write(
void)
451 if( !m_hFile || m_bReadOnly )
457 char buffer[16]; TDBF_Header h;
459 time_t ltime; time(<ime);
struct tm *pTime = localtime(<ime);
466 h.LastUpdate[0] = (
unsigned char)pTime->tm_year;
467 h.LastUpdate[1] = (
unsigned char)pTime->tm_mon + 1;
468 h.LastUpdate[2] = (
unsigned char)pTime->tm_mday;
470 m_nHeaderBytes = (m_nFields + 1) * 32 + 1;
473 for(
int iField=0; iField<m_nFields; iField++)
477 if( m_Fields[iField].Width < 1 )
479 m_Fields[iField].Width = 1;
481 else if( m_Fields[iField].Width > 255 )
483 m_Fields[iField].Width = 255;
487 m_nRecordBytes += m_Fields[iField].Width;
492 fseek(m_hFile, 0, SEEK_SET);
494 memset(buffer, 0, 16 *
sizeof(
char));
498 Write(&h.FileType ,
sizeof(
char), 1);
503 Write(&h.LastUpdate ,
sizeof(
char), 3);
504 Write(&m_nRecords ,
sizeof(
char), 4);
505 Write(&m_nHeaderBytes ,
sizeof(
char), 2);
506 Write(&m_nRecordBytes ,
sizeof(
char), 2);
507 Write(buffer ,
sizeof(
char), 2);
508 Write(&h.Transaction ,
sizeof(
char), 1);
511 Write(&h.bEncrypted ,
sizeof(
char), 1);
513 Write(buffer ,
sizeof(
char), 12);
514 Write(&h.ProductionIdx,
sizeof(
char), 1);
515 Write(&h.LanguageDrvID,
sizeof(
char), 1);
516 Write(buffer ,
sizeof(
char), 2);
520 for(
int iField=0; iField<m_nFields; iField++)
522 Write( m_Fields[iField].Name ,
sizeof(
char), 11);
523 Write(&m_Fields[iField].Type ,
sizeof(
char), 1);
524 Write(&m_Fields[iField].Displacement ,
sizeof(
char), 4);
525 Write(&m_Fields[iField].Width ,
sizeof(
char), 1);
526 Write(&m_Fields[iField].Decimals ,
sizeof(
char), 1);
527 Write(buffer ,
sizeof(
char), 2);
528 Write(&m_Fields[iField].WorkAreaID ,
sizeof(
char), 1);
529 Write(buffer ,
sizeof(
char), 10);
530 Write(&m_Fields[iField].ProductionIdx,
sizeof(
char), 1);
536 Write(buffer,
sizeof(
char), 1);
545 void CSG_Table_DBase::Init_Record(
void)
547 m_Record = (
char *)
SG_Realloc(m_Record, m_nRecordBytes *
sizeof(
char));
550 for(
int iField=0, iPos=1; iField<m_nFields; iPos+=m_Fields[iField++].Width)
552 m_Fields[iField].Offset = iPos;
559 return( m_hFile ? ftell(m_hFile) : 0 );
576 fseek(m_hFile, m_nHeaderBytes, SEEK_SET);
578 if( Read(m_Record,
sizeof(
char), (
size_t)m_nRecordBytes) )
583 fseek(m_hFile, m_nHeaderBytes, SEEK_SET);
598 fseek(m_hFile, m_nRecordBytes, SEEK_CUR);
600 if( Read(m_Record,
sizeof(
char), (
size_t)m_nRecordBytes) )
605 fseek(m_hFile, -m_nRecordBytes, SEEK_CUR);
623 memset(m_Record,
' ', m_nRecordBytes);
625 fseek(m_hFile, 0, SEEK_END);
626 Write(m_Record, m_nRecordBytes,
sizeof(
char));
627 fseek(m_hFile, -m_nRecordBytes, SEEK_END);
630 m_nFileBytes += m_nRecordBytes;
637 if( m_hFile && !m_bReadOnly && m_bModified )
640 Write(m_Record, m_nRecordBytes,
sizeof(
char));
641 fseek(m_hFile, -m_nRecordBytes, SEEK_CUR);
653 return( m_hFile && *m_Record ==
'*' );
674 if( !m_hFile || iField < 0 || iField >= m_nFields )
682 char *c = m_Record + m_Fields[iField].Offset;
684 for(
int i=0; i<m_Fields[iField].Width && *c; i++, c++)
706 int d = s.
Mid(6, 2).
asInt();
if( d < 1 ) d = 1;
else if( d > 31 ) d = 31;
707 int m = s.
Mid(4, 2).
asInt();
if( m < 1 ) m = 1;
else if( m > 12 ) m = 12;
710 Value = 10000 * y + 100 * m + d;
722 if( !m_hFile || iField < 0 || iField >= m_nFields )
733 {
char *s = m_Record + m_Fields[iField].Offset;
735 for(
int i=0; i<m_Fields[iField].Width && *s; i++, s++)
737 Value += *s > 0 ? *s :
'?';
752 char *s = m_Record + m_Fields[iField].Offset;
778 if( !m_hFile || iField < 0 || iField >= m_nFields || m_Fields[iField].Width < 1 )
800 sprintf(s,
"%*.*e", m_Fields[iField].Width, m_Fields[iField].Decimals, Value);
802 size_t n = strlen(s);
if( n > m_Fields[iField].Width ) { n = m_Fields[iField].Width; }
804 memset(m_Record + m_Fields[iField].Offset,
' ', m_Fields[iField].Width);
805 memcpy(m_Record + m_Fields[iField].Offset, s ,
M_GET_MIN(strlen(s), m_Fields[iField].Width));
817 if( m_Fields[iField].Decimals > 0 )
819 sprintf(s,
"%*.*f", m_Fields[iField].Width, m_Fields[iField].Decimals, Value);
823 sprintf(s,
"%*d" , m_Fields[iField].Width, (
int)Value);
826 memset(m_Record + m_Fields[iField].Offset,
' ', m_Fields[iField].Width);
827 memcpy(m_Record + m_Fields[iField].Offset, s ,
M_GET_MIN(strlen(s), m_Fields[iField].Width));
841 if( !m_hFile || iField < 0 || iField >= m_nFields || m_Fields[iField].Width < 1 )
851 memset(m_Record + m_Fields[iField].Offset,
' ', m_Fields[iField].Width);
873 memset(m_Record + m_Fields[iField].Offset,
' ', m_Fields[iField].Width);
885 if( Value.
Length() >= 10 )
887 char *s = m_Record + m_Fields[iField].Offset;
889 s[0] = Value.
b_str()[0];
890 s[1] = Value.
b_str()[1];
891 s[2] = Value.
b_str()[2];
892 s[3] = Value.
b_str()[3];
893 s[4] = Value.
b_str()[5];
894 s[5] = Value.
b_str()[6];
895 s[6] = Value.
b_str()[8];
896 s[7] = Value.
b_str()[9];
911 if( !m_hFile || iField < 0 || iField >= m_nFields || m_Fields[iField].Width < 1 )
917 memset(m_Record + m_Fields[iField].Offset,
' ', m_Fields[iField].Width);