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 bool CSG_Table_DBase::Header_Read(
void)
239 fseek(m_hFile, 0, SEEK_SET);
243 fread(&h.FileType ,
sizeof(
char), 1, m_hFile);
248 fread(&h.LastUpdate ,
sizeof(
char), 3, m_hFile);
249 fread(&
m_nRecords ,
sizeof(
char), 4, m_hFile);
250 fread(&m_nHeaderBytes ,
sizeof(
char), 2, m_hFile);
251 fread(&m_nRecordBytes ,
sizeof(
char), 2, m_hFile);
252 fread( buf ,
sizeof(
char), 2, m_hFile);
253 fread(&h.Transaction ,
sizeof(
char), 1, m_hFile);
256 fread(&h.bEncrypted ,
sizeof(
char), 1, m_hFile);
258 fread( buf ,
sizeof(
char), 12, m_hFile);
259 fread(&h.ProductionIdx ,
sizeof(
char), 1, m_hFile);
260 fread(&h.LanguageDrvID ,
sizeof(
char), 1, m_hFile);
261 fread( buf ,
sizeof(
char), 2, m_hFile);
265 while( ftell(m_hFile) < (
long)m_nHeaderBytes - 1 && !feof(m_hFile) )
269 fread( m_Fields[
m_nFields].Name ,
sizeof(
char), 11, m_hFile);
270 fread(&m_Fields[
m_nFields].Type ,
sizeof(
char), 1, m_hFile);
271 fread(&m_Fields[
m_nFields].Displacement ,
sizeof(
char), 4, m_hFile);
272 fread(&m_Fields[
m_nFields].Width ,
sizeof(
char), 1, m_hFile);
273 fread(&m_Fields[
m_nFields].Decimals ,
sizeof(
char), 1, m_hFile);
274 fread( buf ,
sizeof(
char), 2, m_hFile);
275 fread(&m_Fields[
m_nFields].WorkAreaID ,
sizeof(
char), 1, m_hFile);
276 fread( buf ,
sizeof(
char), 10, m_hFile);
277 fread(&m_Fields[
m_nFields].ProductionIdx ,
sizeof(
char), 1, m_hFile);
286 fread( buf ,
sizeof(
char), 1, m_hFile);
318 if( (m_hFile = fopen(
CSG_String(FileName),
"w+b")) == NULL )
337 for(
int j=0; j<11 && j<(int)Name.
Length(); j++)
339 m_Fields[iField].Name[j] = Name.
b_str()[j];
346 m_Fields[iField].Width = (BYTE)((nBytes = pTable->
Get_Field_Length(iField,
m_Encoding)) > 255 ? 255 : nBytes < 1 ? 1 : nBytes);
351 m_Fields[iField].Width = (BYTE)8;
356 m_Fields[iField].Width = (BYTE)1;
361 m_Fields[iField].Width = (BYTE)1;
366 m_Fields[iField].Width = (BYTE)3;
372 m_Fields[iField].Width = (BYTE)6;
381 m_Fields[iField].Width = (BYTE)16;
386 m_Fields[iField].Width = (BYTE)16;
387 m_Fields[iField].Decimals = (BYTE)8;
392 m_Fields[iField].Width = (BYTE)19;
393 m_Fields[iField].Decimals = (BYTE)10;
400 m_nFileBytes = m_nHeaderBytes;
440 void CSG_Table_DBase::Header_Write(
void)
442 if( !m_hFile || m_bReadOnly )
454 struct tm *pTime = localtime(<ime);
461 h.LastUpdate[0] = (
unsigned char)pTime->tm_year;
462 h.LastUpdate[1] = (
unsigned char)pTime->tm_mon + 1;
463 h.LastUpdate[2] = (
unsigned char)pTime->tm_mday;
465 m_nHeaderBytes = (m_nFields + 1) * 32 + 1;
468 for(iField=0; iField<m_nFields; iField++)
472 if( m_Fields[iField].Width < 1 )
474 m_Fields[iField].Width = 1;
476 else if( m_Fields[iField].Width > 255 )
478 m_Fields[iField].Width = 255;
482 m_nRecordBytes += m_Fields[iField].Width;
487 fseek(m_hFile, 0, SEEK_SET);
489 memset(buf, 0, 16 *
sizeof(
char));
493 fwrite(&h.FileType ,
sizeof(
char), 1, m_hFile);
498 fwrite(&h.LastUpdate ,
sizeof(
char), 3, m_hFile);
499 fwrite(&m_nRecords ,
sizeof(
char), 4, m_hFile);
500 fwrite(&m_nHeaderBytes ,
sizeof(
char), 2, m_hFile);
501 fwrite(&m_nRecordBytes ,
sizeof(
char), 2, m_hFile);
502 fwrite( buf ,
sizeof(
char), 2, m_hFile);
503 fwrite(&h.Transaction ,
sizeof(
char), 1, m_hFile);
506 fwrite(&h.bEncrypted ,
sizeof(
char), 1, m_hFile);
508 fwrite( buf ,
sizeof(
char), 12, m_hFile);
509 fwrite(&h.ProductionIdx ,
sizeof(
char), 1, m_hFile);
510 fwrite(&h.LanguageDrvID ,
sizeof(
char), 1, m_hFile);
511 fwrite( buf ,
sizeof(
char), 2, m_hFile);
515 for(iField=0; iField<m_nFields; iField++)
517 fwrite( m_Fields[iField].Name ,
sizeof(
char), 11, m_hFile);
518 fwrite(&m_Fields[iField].Type ,
sizeof(
char), 1, m_hFile);
519 fwrite(&m_Fields[iField].Displacement ,
sizeof(
char), 4, m_hFile);
520 fwrite(&m_Fields[iField].Width ,
sizeof(
char), 1, m_hFile);
521 fwrite(&m_Fields[iField].Decimals ,
sizeof(
char), 1, m_hFile);
522 fwrite( buf ,
sizeof(
char), 2, m_hFile);
523 fwrite(&m_Fields[iField].WorkAreaID ,
sizeof(
char), 1, m_hFile);
524 fwrite( buf ,
sizeof(
char), 10, m_hFile);
525 fwrite(&m_Fields[iField].ProductionIdx ,
sizeof(
char), 1, m_hFile);
531 fwrite( buf ,
sizeof(
char), 1, m_hFile);
540 void CSG_Table_DBase::Init_Record(
void)
542 m_Record = (
char *)
SG_Realloc(m_Record, m_nRecordBytes *
sizeof(
char));
545 for(
int iField=0, iPos=1; iField<m_nFields; iPos+=m_Fields[iField++].Width)
547 m_Fields[iField].Offset = iPos;
554 return( m_hFile ? ftell(m_hFile) : 0 );
571 fseek(m_hFile, m_nHeaderBytes, SEEK_SET);
573 if( fread(m_Record, m_nRecordBytes,
sizeof(
char), m_hFile) == 1 )
578 fseek(m_hFile, m_nHeaderBytes, SEEK_SET);
593 fseek(m_hFile, m_nRecordBytes, SEEK_CUR);
595 if( fread(m_Record, m_nRecordBytes,
sizeof(
char), m_hFile) == 1 )
600 fseek(m_hFile, -m_nRecordBytes, SEEK_CUR);
618 memset(m_Record,
' ', m_nRecordBytes);
620 fseek(m_hFile, 0, SEEK_END);
621 fwrite(m_Record, m_nRecordBytes,
sizeof(
char), m_hFile);
622 fseek(m_hFile, -m_nRecordBytes, SEEK_END);
625 m_nFileBytes += m_nRecordBytes;
632 if( m_hFile && !m_bReadOnly && m_bModified )
635 fwrite(m_Record, m_nRecordBytes,
sizeof(
char), m_hFile);
636 fseek(m_hFile, -m_nRecordBytes, SEEK_CUR);
648 return( m_hFile && *m_Record ==
'*' );
669 if( !m_hFile || iField < 0 || iField >= m_nFields )
677 char *c = m_Record + m_Fields[iField].Offset;
679 for(
int i=0; i<m_Fields[iField].Width && *c; i++, c++)
701 int d = s.
Mid(6, 2).
asInt();
if( d < 1 ) d = 1;
else if( d > 31 ) d = 31;
702 int m = s.
Mid(4, 2).
asInt();
if( m < 1 ) m = 1;
else if( m > 12 ) m = 12;
705 Value = 10000 * y + 100 * m + d;
717 if( !m_hFile || iField < 0 || iField >= m_nFields )
728 {
char *s = m_Record + m_Fields[iField].Offset;
730 for(
int i=0; i<m_Fields[iField].Width && *s; i++, s++)
747 char *s = m_Record + m_Fields[iField].Offset;
773 if( !m_hFile || iField < 0 || iField >= m_nFields || m_Fields[iField].Width < 1 )
795 sprintf(s,
"%*.*e", m_Fields[iField].Width, m_Fields[iField].Decimals, Value);
797 size_t n = strlen(s);
if( n > m_Fields[iField].Width ) { n = m_Fields[iField].Width; }
799 memset(m_Record + m_Fields[iField].Offset,
' ', m_Fields[iField].Width);
800 memcpy(m_Record + m_Fields[iField].Offset, s ,
M_GET_MIN(strlen(s), m_Fields[iField].Width));
812 if( m_Fields[iField].Decimals > 0 )
814 sprintf(s,
"%*.*f", m_Fields[iField].Width, m_Fields[iField].Decimals, Value);
818 sprintf(s,
"%*d" , m_Fields[iField].Width, (
int)Value);
821 memset(m_Record + m_Fields[iField].Offset,
' ', m_Fields[iField].Width);
822 memcpy(m_Record + m_Fields[iField].Offset, s ,
M_GET_MIN(strlen(s), m_Fields[iField].Width));
836 if( !m_hFile || iField < 0 || iField >= m_nFields || m_Fields[iField].Width < 1 )
846 memset(m_Record + m_Fields[iField].Offset,
' ', m_Fields[iField].Width);
868 memset(m_Record + m_Fields[iField].Offset,
' ', m_Fields[iField].Width);
880 if( Value.
Length() >= 10 )
882 char *s = m_Record + m_Fields[iField].Offset;
884 s[0] = Value.
b_str()[0];
885 s[1] = Value.
b_str()[1];
886 s[2] = Value.
b_str()[2];
887 s[3] = Value.
b_str()[3];
888 s[4] = Value.
b_str()[5];
889 s[5] = Value.
b_str()[6];
890 s[6] = Value.
b_str()[8];
891 s[7] = Value.
b_str()[9];
906 if( !m_hFile || iField < 0 || iField >= m_nFields || m_Fields[iField].Width < 1 )
912 memset(m_Record + m_Fields[iField].Offset,
' ', m_Fields[iField].Width);