China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 19:49
中国DOS联盟论坛 » DOS开发编程 & 发展交流 (开发室) » Copyright-free database that Office Excel can recognize and open View 2,334 Replies 5
Original Poster Posted 2018-07-14 12:40 ·  中国 广东 深圳 电信
高级用户
★★
Credits 668
Posts 295
Joined 2005-07-26 00:00
21-year member
UID 41110
Gender Male
From 广东深圳
Status Offline
How to customize a database that can be recognized by Office Excel?

In single-chip microcomputer systems, DOS systems, or Linux systems, custom databases are often used. How to make this database file recognizable by Office Excel under Windows? Now there is a method. You can use the database of Foxpro 2.6 version. The file structure is open and can be recognized by Office Excel.

In WinSail, it is composed of the Symbol2.h header file, the Foxpro.h header file, and the Foxpro.Cpp source file. All source codes will be made public later, and those who need it can port it!

[ Last edited by firstsail on 2018-7-14 at 19:32 ]
Floor 2 Posted 2018-07-14 12:42 ·  中国 广东 深圳 电信
高级用户
★★
Credits 668
Posts 295
Joined 2005-07-26 00:00
21-year member
UID 41110
Gender Male
From 广东深圳
Status Offline
//Below is the Symbol2.h file

#ifndef _SYMBOL2_H_
#define _SYMBOL2_H_

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef NULL
#define NULL 0
#endif



#define _fmemcpy memcpy
#define _fstrcpy strcpy
#define _fstricmp stricmp
#define _fstrcmp strcmp
#define _fstrlen strlen
#define _fmemset memset

#define farmalloc malloc
#define farfree free

#define pi 3.1415926
#define CONST_SINGLE_T 3.14159f
#define CONST_DOUBLE_T 3.14159265359

typedef char* LPCTSTR;
typedef void* LPVOID;
typedef int COLOR;
typedef int HANDLE;
typedef unsigned int WORD;
typedef unsigned char BYTE;
typedef int BOOL;
typedef unsigned long DWORD;
typedef DWORD SORT;
typedef int BOOL;
typedef unsigned int BOOLEN;
typedef int HWND;
typedef DWORD WPARAM;
typedef DWORD LPARAM;
typedef long LONG;
typedef long* LPLONG;
typedef void VOID;
typedef DWORD ULONG;
typedef float FLOAT;
typedef int INT;
typedef signed short SHORT;
typedef unsigned short USHORT;
typedef unsigned short WCHAR;



#define DELETE(a) {if ((a) != NULL) delete (a); (a) = NULL;}



#define MAX(a,b) ((a>b)?a:b)
#define MIN(a,b) ((a<b)?a:b)

#define SHIFT8(a) ((a)<<8)
#define SHIFT16(a) ((a)<<16)
#define LOBYTE(a) ((BYTE)((a)&0xFF))
#define HIBYTE(a) ((((DWORD)(a))>>8)&0xFF)
#define LOWORD(a) ((WORD)((WORD)(((DWORD)(a))&0xFFFFL)))
#define HIWORD(a) ((WORD)((((DWORD)(a))>>16)&0xFFFFL))


#endif
Floor 3 Posted 2018-07-14 12:44 ·  中国 广东 深圳 电信
高级用户
★★
Credits 668
Posts 295
Joined 2005-07-26 00:00
21-year member
UID 41110
Gender Male
From 广东深圳
Status Offline
// The following file is the Foxpro.h file

#ifndef _FOXPRO_20110811_H_
#define _FOXPRO_20110811_H_

// Database header
struct FOXPRO_HEAD
{
BYTE byFlags;
BYTE byYear;
BYTE byMonth;
BYTE byDay;

DWORD dwRecordCount;
WORD wRecordAddress;
WORD wRecordLength;
DWORD dwReserve1;
DWORD dwReserve2;
DWORD dwReserve3;
DWORD dwReserve4;
DWORD dwReserve5;
};

// Database "field" attributes
struct FOXPRO_FIELDS
{
BYTE strName[10];
BYTE byFill;
BYTE byStyle;
DWORD dwDataAddress;
BYTE byLength;
BYTE byDotCount;
WORD wReserve1;
BYTE byFlags;
BYTE wsReserve[11];

};

// Database "class" definition
class CFoxpro
{
private:
BOOL m_bNew;
long m_lRecordPoint;
char m_strFile[256];
FOXPRO_HEAD m_FoxproHead;
FOXPRO_FIELDS m_FoxproFields;
FILE* m_pFile;
BYTE* m_pRecordBuf;
FOXPRO_FIELDS* m_pFoxproFields;
public:
// Create a new database
BOOL CreateFoxpro(const char*pStr);
// Open an existing database
BOOL OpenFoxpro(const char*pStr);
// Close the database
BOOL CloseFoxpro();
// Add a field
BOOL AddFields(const char* strName,BYTE byStyle,int nLength,int nDot);
// Get the "field" attributes of the specified column
FOXPRO_FIELDS* GetFoxproFields(int nIndex);
public:
// Get the number of columns
int GetFieldsCount() {return(m_FoxproHead.wRecordAddress-sizeof(FOXPRO_HEAD))/sizeof(FOXPRO_FIELDS);}
// Get the total number of records
long GetRecordCount() {return(m_FoxproHead.dwRecordCount);}
// Get the record address
long GetRecordAddress() {return((DWORD)m_FoxproHead.wRecordAddress);}
// Get the last modification date of the database - "month"
int GetMonth() {return((int)m_FoxproHead.byMonth);}
// Get the last modification date of the database - "day"
int GetDay() {return((int)m_FoxproHead.byDay);}
// Get the last modification date of the database - "year"
int GetYear() {return((int)1900+(DWORD)m_FoxproHead.byYear);}
// Get the length of the record
int GetRecordLength(){return((int)(DWORD)m_FoxproHead.wRecordLength);}
// Display database debugging information
void Debug();
public:
// Trim leading spaces of the specified string
static char* TrimLeft(char *pStr);
// Trim trailing spaces of the specified string
static char* TrimRight(char *pStr);

public:
// Set the string of the string type "field"
BOOL SetFieldsString(int nIndex, const char* pStr);
// Set the numeric value of the numeric type "field"
BOOL SetFieldsNumber(int nIndex,float fValue);
// Set the date value of the date type "field"
BOOL SetFieldsDate(int nIndex,int nYear,int nMonth,int nDay);
// Set the logical value of the logical type "field"
BOOL SetFieldsLogic(int nIndex,BOOL bLogic);
// Set the memo value of the memo type "field"
BOOL SetFieldsMemo(int nIndex, const char* pStr);

// Get the string of the string type "field"
BOOL GetFieldsString(int nIndex,char* pStr);
// Get the numeric value of the numeric type "field"
float GetFieldsNumber(int nIndex);
// Get the date value of the date type "field"
BOOL GetFieldsDate(int nIndex,int* pnYear,int* pnMonth,int* pnDay);
// Get the logical value of the logical type "field"
BOOL GetFieldsLogic(int nIndex);
// Get the memo value of the memo type "field"
BOOL GetFieldsMemo(int nIndex,char* pStr);

// Locate the record
BOOL Seek(long nIndex,int nMode);
// Determine if the database is already open
BOOL IsOpen();
// Refresh the database to disk
BOOL Flush();
// Add a new record
BOOL AddNew();
// Prepare to modify the current record
BOOL Edit();
// Make the current changes effective
BOOL Update();
// Insert a record in the current record
BOOL Insert();
// Delete the current record (with a delete mark)
BOOL Delete();
// Delete the current record
BOOL DeleteEx();
// Delete records with deletion criteria
BOOL Zap();
public:
void CoFoxpro();// Constructor call
// Constructor
CFoxpro();
// Constructor (open the specified database)
CFoxpro(char* strFile);
// Destructor
~CFoxpro();
};



#endif
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
zzz19760225 +2 2018-07-14 16:52
Floor 4 Posted 2018-07-14 12:45 ·  中国 广东 深圳 电信
高级用户
★★
Credits 668
Posts 295
Joined 2005-07-26 00:00
21-year member
UID 41110
Gender Male
From 广东深圳
Status Offline
```cpp
// The following file is Foxpro.Cpp file
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <bios.h>
#include <dir.h>
#include <conio.h>
#include <string.h>
#include <sys\\stat.h>
#include <io.h>

#include "symbol2.h"
#include "Foxpro.h"


//#include <Myframe.h>

void CFoxpro::CoFoxpro()
{
m_bNew = FALSE;
m_lRecordPoint = 0;
m_pRecordBuf = NULL;
m_pFile = NULL;
m_pFoxproFields = NULL;
_fmemset(&m_FoxproHead ,0,sizeof(FOXPRO_HEAD));
_fmemset(&m_FoxproFields,0,sizeof(FOXPRO_FIELDS));
_fmemset(&m_strFile,0,256);
}

CFoxpro::CFoxpro()
{
this->CoFoxpro();
}

CFoxpro::CFoxpro(char* strFile)
{

this->CoFoxpro();
this->OpenFoxpro(strFile);
}

CFoxpro::~CFoxpro()
{
if (m_pFile != NULL)
{
fclose(m_pFile);
}
m_pFile = NULL;

DELETE(m_pRecordBuf);
DELETE(m_pFoxproFields);

}

void CFoxpro::Debug()
{
char buf[512];
if(!m_pRecordBuf) return;

int nFields = this->GetFieldsCount();
printf("\n-------------------------------------------------");
for(int i=0;i<nFields;i++)
{
m_FoxproFields = *(this->GetFoxproFields(i));

printf("\n%10s--",m_FoxproFields.strName);
if(m_FoxproFields.byStyle=='C')
{
this->GetFieldsString(i,buf);
this->TrimLeft(buf);
this->TrimRight(buf);
printf("%s",buf);
}
else if(m_FoxproFields.byStyle=='L')
{
BOOL bLogic = this->GetFieldsLogic(i);
printf("%d",bLogic);
}
else if(m_FoxproFields.byStyle=='D')
{
int nYear,nMonth,nDay;
this->GetFieldsDate(i,&nYear,&nMonth,&nDay);
printf("%04d-%02d-%02d",nYear,nMonth,nDay);
}
else if(m_FoxproFields.byStyle=='N')
{
float fValue = this->GetFieldsNumber(i);
if(m_FoxproFields.byDotCount==0) printf("%.0f",fValue);
else if(m_FoxproFields.byDotCount==1) printf("%.1f",fValue);
else if(m_FoxproFields.byDotCount==2) printf("%.2f",fValue);
else if(m_FoxproFields.byDotCount==3) printf("%.3f",fValue);
else if(m_FoxproFields.byDotCount==4) printf("%.4f",fValue);
else if(m_FoxproFields.byDotCount==5) printf("%.5f",fValue);
else printf("%.6f",fValue);

}
else if(m_FoxproFields.byStyle=='M')
{
}
if(i&& !(i%24))
{
getch();
}

}
}

BOOL CFoxpro::CreateFoxpro(const char *pStr)
{
if(m_pFile) return(FALSE);

m_pFile = NULL;

CoFoxpro();

int nFields = 0;

date mDate;
::getdate(&mDate);

m_FoxproHead.byFlags = 0x03;
m_FoxproHead.byDay = (BYTE)mDate.da_day;
m_FoxproHead.byMonth = (BYTE)mDate.da_mon;
m_FoxproHead.byYear = (BYTE)(WORD)(mDate.da_year-1900);
m_FoxproHead.dwRecordCount = 0x00;
m_FoxproHead.wRecordAddress = sizeof(FOXPRO_HEAD)+1;
m_FoxproHead.wRecordLength = 0x01;
m_FoxproHead.dwReserve1 = 0x00;
m_FoxproHead.dwReserve2 = 0x00;
m_FoxproHead.dwReserve3 = 0x00;
m_FoxproHead.dwReserve4 = 0x00;
m_FoxproHead.dwReserve5 = 0x00;

_fmemset(&m_FoxproFields.strName,0,10);
m_FoxproFields.byFill = 0;
m_FoxproFields.byStyle = (BYTE)'C';
m_FoxproFields.dwDataAddress = 0;
m_FoxproFields.byLength = 0;
m_FoxproFields.byDotCount = 0;
m_FoxproFields.wReserve1 = 0;
m_FoxproFields.byFlags = 0;
_fmemset(&m_FoxproFields.wsReserve,0,11);

if(!(m_pFile=fopen(pStr,"w+b")))
{
return(FALSE);
}

fwrite(&m_FoxproHead ,sizeof(FOXPRO_HEAD) ,1,m_pFile);
fwrite(&m_FoxproFields,sizeof(FOXPRO_FIELDS),nFields,m_pFile);
fwrite("\x0D",1,1,m_pFile);
fwrite("\x1A",1,1,m_pFile);

//File Name
_fstrcpy(m_strFile,pStr);
//Malloc RecordBuf
m_pRecordBuf = new BYTE[this->GetRecordLength()+1024];
_fmemset(m_pRecordBuf,0,this->GetRecordLength()+1024);
//Malloc Foxpro_Fields
m_pFoxproFields = new FOXPRO_FIELDS[1+this->GetFieldsCount()];
_fmemset(m_pFoxproFields,0,sizeof(FOXPRO_FIELDS)*(1+this->GetFieldsCount()));
//Fill FoxproFields
::fseek(m_pFile,sizeof(FOXPRO_HEAD),SEEK_SET);
nFields = this->GetFieldsCount();
for(int i=0;i<nFields;i++)
{
::fread(&m_pFoxproFields,sizeof(FOXPRO_FIELDS),1,m_pFile);
}
return(TRUE);
}


BOOL CFoxpro::OpenFoxpro(const char* pStr)
{
if (m_pFile != NULL)
{
return(FALSE);
}

CoFoxpro();

if (!(m_pFile = fopen(pStr,"r+b")))
{
return(FALSE);
}

long lFileLength = filelength(fileno(m_pFile));
if(lFileLength<1L*sizeof(FOXPRO_HEAD)+2)
{
::fclose(m_pFile);
m_pFile=NULL;
return(FALSE);
}

rewind(m_pFile);
fread(&m_FoxproHead ,sizeof(FOXPRO_HEAD) ,1,m_pFile);
//Flags
if(m_FoxproHead.byFlags!=0x03 && m_FoxproHead.byFlags!=0xF5)
{
::fclose(m_pFile);
m_pFile=NULL;
return(FALSE);
}

BYTE nCh=0;
fseek(m_pFile,-1,SEEK_END);
fread(&nCh,1,1,m_pFile);
// End Is 0x1A?
if(nCh!=0x1A)
{
fclose(m_pFile);
m_pFile=NULL;
return(FALSE);
}
//---------------------------
//File Name
_fstrcpy(m_strFile,pStr);
//Malloc Record Buffer
m_pRecordBuf = new BYTE[this->GetRecordLength()+1024];
if (m_pRecordBuf == NULL)
{
fclose(m_pFile);
m_pFile=NULL;

return (FALSE);
}
_fmemset(m_pRecordBuf,0,this->GetRecordLength()+1024);
//Malloc Foxpro_Fields
m_pFoxproFields = new FOXPRO_FIELDS[1+this->GetFieldsCount()];
if (m_pFoxproFields == NULL)
{
delete m_pRecordBuf;
m_pRecordBuf = NULL;

fclose(m_pFile);
m_pFile=NULL;

return (FALSE);
}

_fmemset(m_pFoxproFields,0,sizeof(FOXPRO_FIELDS)*(1+this->GetFieldsCount()));
//Fill FoxproFields
::fseek(m_pFile,sizeof(FOXPRO_HEAD),SEEK_SET);
int nFields = this->GetFieldsCount();
for(int i=0;i<nFields;i++)
{
::fread(&m_pFoxproFields,sizeof(FOXPRO_FIELDS),1,m_pFile);
}
return(TRUE);
}

BOOL CFoxpro::CloseFoxpro()
{
if (NULL != m_pFile)
{
fclose(m_pFile);
}
m_pFile = NULL;

DELETE(m_pRecordBuf);
DELETE(m_pFoxproFields);

return(TRUE);
}

BOOL CFoxpro::AddFields(const char* strName,BYTE byStyle,int nLength,int nDot)
{
if(!m_pFile) return(FALSE);

if(byStyle=='C') {nDot = 0;}
else if(byStyle=='N') {}
else if(byStyle=='D') {nLength = 8;nDot = 0;}
else if(byStyle=='L') {nLength=1;nDot =0;}
else if(byStyle=='M') {nLength = 10;nDot = 0;}
else {return(0);}

_fmemset(&m_FoxproFields,0,sizeof(FOXPRO_FIELDS));
//----------------------------------------------------
if(byStyle=='M')
{
m_FoxproHead.byFlags=0xF5;
fseek(m_pFile,0,SEEK_SET);
fwrite(&m_FoxproHead ,sizeof(FOXPRO_HEAD) ,1,m_pFile);
}
//Calc Fields
int nFields=(m_FoxproHead.wRecordAddress-sizeof(FOXPRO_HEAD))/sizeof(FOXPRO_FIELDS);
//Modify Head
date mDate;
::getdate(&mDate);
m_FoxproHead.byDay = (BYTE)mDate.da_day;
m_FoxproHead.byMonth = (BYTE)mDate.da_mon;
m_FoxproHead.byYear = (BYTE)(WORD)(mDate.da_year-1900);
m_FoxproHead.wRecordAddress+= sizeof(FOXPRO_FIELDS);
m_FoxproHead.wRecordLength += (WORD)nLength;
//Write Head Again
fseek(m_pFile,0,SEEK_SET);
fwrite(&m_FoxproHead,sizeof(FOXPRO_HEAD),1,m_pFile);
//
int nFieldsAddress = 1;
fseek(m_pFile,sizeof(FOXPRO_HEAD),SEEK_SET);
for(int i=0;i<nFields;i++)
{
::fread(&m_FoxproFields,sizeof(FOXPRO_FIELDS),1,m_pFile);
nFieldsAddress+=m_FoxproFields.byLength;
}
//Fill in Field
if (::_fstrlen(strName) < 10)
{
::_fstrcpy((char*)&m_FoxproFields.strName,(char*)strName);
}
else
{
::_fmemcpy((BYTE*)&m_FoxproFields.strName, (char*)strName, 10);
}

m_FoxproFields.byStyle = (BYTE)byStyle;
m_FoxproFields.dwDataAddress = nFieldsAddress;
m_FoxproFields.byLength = (BYTE)nLength;
m_FoxproFields.byDotCount = nDot;
//Write Field
::fseek(m_pFile,sizeof(FOXPRO_HEAD)+nFields*sizeof(FOXPRO_FIELDS),SEEK_SET);
::fwrite(&m_FoxproFields,sizeof(FOXPRO_FIELDS),1,m_pFile);
//Write End
fwrite("\x0D",1,1,m_pFile);
fwrite("\x1A",1,1,m_pFile);

//Malloc Record BUf
DELETE(m_pRecordBuf);
m_pRecordBuf = new BYTE[this->GetRecordLength()+1024];
_fmemset(m_pRecordBuf,' ',this->GetRecordLength()+1024);
m_pRecordBuf[GetRecordLength()]='\0';
//Reset Record Point
m_lRecordPoint=0;
//Malloc Foxpro Fields
DELETE(m_pFoxproFields);
m_pFoxproFields = new FOXPRO_FIELDS[1+this->GetFieldsCount()];
_fmemset(m_pFoxproFields,0,sizeof(FOXPRO_FIELDS)*(1+this->GetFieldsCount()));
//Fill FoxproFields
::fseek(m_pFile,sizeof(FOXPRO_HEAD),SEEK_SET);
nFields = this->GetFieldsCount();
for(i=0;i<nFields;i++)
{
::fread(&m_pFoxproFields,sizeof(FOXPRO_FIELDS),1,m_pFile);
}

return(TRUE);
}

char* CFoxpro::TrimLeft(char *pStr)
{
//if(_fstrlen(pStr)>=256) return(pStr);
while(pStr[0]==' '||pStr[0]=='\x9')
{
int nLength=_fstrlen(pStr);
if(!nLength) break;
if(nLength) _fmemcpy(&pStr[0],&pStr[1],nLength);
}
return(pStr);
}

char* CFoxpro::TrimRight(char *pStr)
{
//if(_fstrlen(pStr)>=256) return(pStr);
int nLength=_fstrlen(pStr);
if(!nLength) return(pStr);
for(int i=nLength-1;i>=0;i--)
{
if(pStr!=' '&&pStr!='\x9') break;
pStr='\0';
}

return(pStr);
}

FOXPRO_FIELDS* CFoxpro::GetFoxproFields(int nIndex)
{
::_fmemset(&m_FoxproFields,0,sizeof(FOXPRO_FIELDS));
//----------------------------------------------------
//Calc Fields
int nFields=this->GetFieldsCount();
if(nIndex>=nFields||nFields<0) return(&m_FoxproFields);
return(&m_pFoxproFields[nIndex]);
}

BOOL CFoxpro::GetFieldsString(int nIndex,char* pStr)
{
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);

m_FoxproFields = *(this->GetFoxproFields(nIndex));
if(m_FoxproFields.byStyle!='C') return(FALSE);
if(!m_pRecordBuf) return(FALSE);

int nAddress = (WORD)m_FoxproFields.dwDataAddress;
_fmemcpy(pStr,&m_pRecordBuf[nAddress],m_FoxproFields.byLength);
if(m_FoxproFields.byLength)
{
pStr[m_FoxproFields.byLength] = '\0';
}
this->TrimRight(pStr);

return(TRUE);
}

float CFoxpro::GetFieldsNumber(int nIndex)
{
char buf[120];
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);

m_FoxproFields = *(this->GetFoxproFields(nIndex));

if(m_FoxproFields.byStyle!='N') return(FALSE);
if(!m_pRecordBuf) return(FALSE);

int nAddress = (WORD)m_FoxproFields.dwDataAddress;
_fmemcpy(buf,&m_pRecordBuf[nAddress],m_FoxproFields.byLength);
buf[m_FoxproFields.byLength] = '\0';
return(atof(buf));
}

BOOL CFoxpro::GetFieldsDate(int nIndex,int* pnYear,int* pnMonth,int* pnDay)
{
char buf1[20],buf2[20];
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);

m_FoxproFields = *(this->GetFoxproFields(nIndex));

if(m_FoxproFields.byStyle!='D') return(FALSE);
if(!m_pRecordBuf) return(FALSE);

int nAddress = (WORD)m_FoxproFields.dwDataAddress;
_fmemcpy(buf1,&m_pRecordBuf[nAddress],m_FoxproFields.byLength);
buf1[m_FoxproFields.byLength] = '\0';

buf2[0]=buf1[0];
buf2[1]=buf1[1];
buf2[2]=buf1[2];
buf2[3]=buf1[3];
buf2[4]='\0';
*pnYear =(int)(atof(buf2)+0.5f);

buf2[0]=buf1[4];
buf2[1]=buf1[5];
buf2[2]='\0';
*pnMonth =(int)(atof(buf2)+0.5f);

buf2[0]=buf1[6];
buf2[1]=buf1[7];
buf2[2]='\0';
*pnDay =(int)(atof(buf2)+0.5f);

return(TRUE);
}

BOOL CFoxpro::GetFieldsLogic(int nIndex)
{
char buf[10];
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);

m_FoxproFields = *(this->GetFoxproFields(nIndex));

if(m_FoxproFields.byStyle!='L') return(FALSE);
if(!m_pRecordBuf) return(FALSE);

int nAddress = (WORD)m_FoxproFields.dwDataAddress;
_fmemcpy(buf,&m_pRecordBuf[nAddress],m_FoxproFields.byLength);
buf[m_FoxproFields.byLength] = '\0';
if(buf[0]=='T') return(TRUE);
return(FALSE);
}

BOOL CFoxpro::GetFieldsMemo(int /*nIndex*/,char* /*pStr*/)
{
return(TRUE);
}

BOOL CFoxpro::SetFieldsString(int nIndex, const char* pStr)
{
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);

m_FoxproFields = *(this->GetFoxproFields(nIndex));

if(m_FoxproFields.byStyle!='C') return(FALSE);
if(!m_pRecordBuf) return(FALSE);

int nAddress = (WORD)m_FoxproFields.dwDataAddress;
int nLength1 = _fstrlen(pStr);
int nLength2 = (WORD)m_FoxproFields.byLength;
for(int i=0;i<nLength2;i++)
{
if(i>=nLength1)
{
m_pRecordBuf[nAddress+i]=' ';
}
else
{
m_pRecordBuf[nAddress+i]=*(pStr+i);
}
}


//::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
//::fseek(m_pFile,m_lRecordPoint*GetRecordLength(),SEEK_CUR);
//::fseek(m_pFile,nAddress,SEEK_CUR);
return(TRUE);
}
BOOL CFoxpro::SetFieldsNumber(int nIndex,float fValue)
{
char buf[100];
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);

m_FoxproFields = *(this->GetFoxproFields(nIndex));

if(m_FoxproFields.byStyle!='N') return(FALSE);
if(!m_pRecordBuf) return(FALSE);


if(m_FoxproFields.byDotCount==0) sprintf(buf,"%.0f",fValue);
else if(m_FoxproFields.byDotCount==1) sprintf(buf,"%.1f",fValue);
else if(m_FoxproFields.byDotCount==2) sprintf(buf,"%.2f",fValue);
else if(m_FoxproFields.byDotCount==3) sprintf(buf,"%.3f",fValue);
else if(m_FoxproFields.byDotCount==4) sprintf(buf,"%.4f",fValue);
else if(m_FoxproFields.byDotCount==5) sprintf(buf,"%.5f",fValue);
else sprintf(buf,"%.6f",fValue);

int nAddress = (WORD)m_FoxproFields.dwDataAddress;
int nLength1 = _fstrlen(buf);
int nLength2 = (WORD)m_FoxproFields.byLength;
for(int i=0;i<nLength2;i++)
{
if(i>=nLength1)
{
m_pRecordBuf[nAddress+i]=' ';
}
else
{
m_pRecordBuf[nAddress+i]=buf;
}
}

return(TRUE);
}
BOOL CFoxpro::SetFieldsDate(int nIndex,int nYear,int nMonth,int nDay)
{
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);

m_FoxproFields = *(this->GetFoxproFields(nIndex));

if(m_FoxproFields.byStyle!='D') return(FALSE);
if(!m_pRecordBuf) return(FALSE);

int nAddress = (WORD)m_FoxproFields.dwDataAddress;
//sprintf((char *)&m_pRecordBuf[nAddress],"%4d%2d%2d%",nYear,nMonth,nDay);
//m_pRecordBuf[nAddress+8]=' ';
char buf[40];
sprintf(buf,"%4d%2d%2d",nYear,nMonth,nDay);
for(int i=0;i<8;i++)
{
m_pRecordBuf[nAddress+i] = buf;
}


return(TRUE);
}
BOOL CFoxpro::SetFieldsLogic(int nIndex,BOOL bLogic)
{
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);

m_FoxproFields = *(this->GetFoxproFields(nIndex));

if(m_FoxproFields.byStyle!='L') return(FALSE);
if(!m_pRecordBuf) return(FALSE);

int nAddress = (WORD)m_FoxproFields.dwDataAddress;
if(bLogic) m_pRecordBuf[nAddress]='T';
else m_pRecordBuf[nAddress]='F';

return(TRUE);
}
BOOL CFoxpro::SetFieldsMemo(int nIndex, const char* /*pStr*/)
{
int nFields=this->GetFieldsCount();
if(nIndex<0||nIndex>=nFields||nFields<0) return(FALSE);

m_FoxproFields = *(this->GetFoxproFields(nIndex));

if(m_FoxproFields.byStyle!='M') return(FALSE);
if(!m_pRecordBuf) return(FALSE);

//int nAddress = (WORD)m_FoxproFields.dwDataAddress;
//((DWORD *)&m_pRecordBuf[nAddress])=(BYTE)chLogic;

return(TRUE);
}

BOOL CFoxpro::Seek(long nIndex,int nMode)
{
if(!m_pFile) return(FALSE);

if(nMode==SEEK_SET) m_lRecordPoint = nIndex;
else if(nMode==SEEK_CUR) m_lRecordPoint+= nIndex;
else if(nMode==SEEK_END)
{
m_lRecordPoint = GetRecordCount();
m_lRecordPoint+= nIndex;
}
::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
::fseek(m_pFile,m_lRecordPoint*GetRecordLength(),SEEK_CUR);

if(m_lRecordPoint<this->GetRecordCount())
{
::fread(m_pRecordBuf,this->GetRecordLength(),1,m_pFile);
m_pRecordBuf[GetRecordLength()]='\0';
::fseek(m_pFile,0L-GetRecordLength(),SEEK_CUR);
}

return(TRUE);
}

BOOL CFoxpro::Delete()
{
if(!m_pFile) return(FALSE);
if(!m_pRecordBuf) return(FALSE);

if(m_lRecordPoint>=this->GetRecordCount()) return(FALSE);

this->Edit();
m_pRecordBuf[0]='*';
this->Update();

return(TRUE);
}

BOOL CFoxpro::Zap()
{
if(!m_pFile) return(FALSE);
if(!this->GetRecordCount()) return(TRUE);


long lRecordCount = this->GetRecordCount();
long lRecordLength = this->GetRecordLength();

long lLocation=0;
for(long i=0;i<lRecordCount;i++)
{
::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
::fseek(m_pFile,i*lRecordLength,SEEK_CUR);

::fread(m_pRecordBuf,lRecordLength,1,m_pFile);
if(m_pRecordBuf[0] != '*')
{
::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
::fseek(m_pFile,lLocation*lRecordLength,SEEK_CUR);
::fwrite(m_pRecordBuf,lRecordLength,1,m_pFile);
lLocation++;
}
}
this->m_FoxproHead.dwRecordCount=lLocation;

::fseek(m_pFile,0,SEEK_SET);
::fwrite(&m_FoxproHead,sizeof(FOXPRO_HEAD),1,m_pFile);

long lFileLength = sizeof(FOXPRO_HEAD)+
1L*sizeof(FOXPRO_FIELDS)*this->GetFieldsCount()+
1L+
1L*this->m_FoxproHead.dwRecordCount*lRecordLength;


::chsize(fileno(m_pFile),lFileLength);
::fseek(m_pFile,0,SEEK_END);
::fwrite("\x1A",1,1,m_pFile);

m_lRecordPoint=0L;

this->Seek(m_lRecordPoint,SEEK_SET);
this->Edit();
return(TRUE);
}

BOOL CFoxpro::DeleteEx()
{
if(!m_pFile) return(FALSE);

if(m_lRecordPoint>=this->GetRecordCount()) return(FALSE);


long lRecordCount = this->GetRecordCount();
long lRecordLength = this->GetRecordLength();

for(long i=m_lRecordPoint+1;i<lRecordCount;i++)
{
::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
::fseek(m_pFile,i*lRecordLength,SEEK_CUR);

::fread(m_pRecordBuf,lRecordLength,1,m_pFile);

::fseek(m_pFile,-2L*lRecordLength,SEEK_CUR);
::fwrite(m_pRecordBuf,lRecordLength,1,m_pFile);
}

this->m_FoxproHead.dwRecordCount-=1;
::fseek(m_pFile,0,SEEK_SET);
::fwrite(&m_FoxproHead,sizeof(FOXPRO_HEAD),1,m_pFile);
long lFileLength = sizeof(FOXPRO_HEAD)+
1L*sizeof(FOXPRO_FIELDS)*this->GetFieldsCount()+
1L+
1L*this->m_FoxproHead.dwRecordCount*lRecordLength;


::chsize(fileno(m_pFile),lFileLength);
::fseek(m_pFile,0,SEEK_END);
::fwrite("\x1A",1,1,m_pFile);

if(m_lRecordPoint && m_lRecordPoint==this->GetRecordCount())
{
m_lRecordPoint-=1;
}
//------------------------------------------------------------
this->Seek(m_lRecordPoint,SEEK_SET);
this->Edit();
return(TRUE);
}

BOOL CFoxpro::Insert()
{
return(TRUE);
}

BOOL CFoxpro::AddNew()
{
if (NULL == m_pFile)
{
return(FALSE);
}
m_lRecordPoint = this->GetRecordCount();
_fmemset(m_pRecordBuf,' ',this->GetRecordLength()+1024);
this->Seek(0,SEEK_END);
m_bNew = TRUE;

return(TRUE);
}

BOOL CFoxpro::Edit()
{
if(NULL == m_pFile)
{
return(FALSE);
}
if (m_bNew)
{
return(TRUE);
}

this->Seek(m_lRecordPoint,SEEK_SET);
::fread(m_pRecordBuf,this->GetRecordLength(),1,m_pFile);
m_pRecordBuf[GetRecordLength()]='\0';

return(TRUE);
}
BOOL CFoxpro::Update()
{
if (NULL == m_pFile) return(FALSE);

if(m_bNew)
{
m_lRecordPoint = this->GetRecordCount();
}

::fseek(m_pFile,this->GetRecordAddress(),SEEK_SET);
::fseek(m_pFile,m_lRecordPoint*GetRecordLength(),SEEK_CUR);
::fwrite(m_pRecordBuf,this->GetRecordLength(),1,m_pFile);

if (m_bNew)
{
//::fseek(m_pFile, 0, SEEK_END);
fwrite("\x1A",1,1,m_pFile);

rewind(m_pFile);
m_FoxproHead.dwRecordCount += 1;
::fwrite(&m_FoxproHead,sizeof(FOXPRO_HEAD),1,m_pFile);
}
m_bNew = FALSE;
return(TRUE);
}

BOOL CFoxpro::IsOpen()
{
return(((m_pFile)?TRUE:FALSE));
}

BOOL CFoxpro::Flush()
{
if (NULL == m_pFile)
{
return(FALSE);
}
::fflush(m_pFile);

int duphandle = ::dup(fileno(m_pFile));

::close(duphandle);

return(TRUE);
}
```
Floor 5 Posted 2018-07-14 12:47 ·  中国 广东 深圳 电信
高级用户
★★
Credits 668
Posts 295
Joined 2005-07-26 00:00
21-year member
UID 41110
Gender Male
From 广东深圳
Status Offline
//Application of the database class CFoxpro

//Create a new database
BOOL BuildArmatureDbf(char* pStr)
{
char buf[50];
//Allocate a database object
CFoxpro mFoxpro;
//Create and open a new database
if (!mFoxpro.CreateFoxpro(pStr))
{
return(FALSE);
}
//Character type, maximum 24 bytes, 0 decimal places (characters naturally have no concept of decimal places!)
mFoxpro.AddFields("ModelName", 'C', 24, 0);
//Date type, 10 bytes in length (must be this value), 0 decimal places (dates naturally have no concept of decimal places!)
mFoxpro.AddFields("Date", 'D', 10, 0);
//Numeric type, 9 bytes in length (must be this value), 0 decimal places
mFoxpro.AddFields("Line", 'N', 9, 0);
//Numeric type, 2 bytes in length (must be this value), 0 decimal places
mFoxpro.AddFields("Bar", 'N', 2, 0);
//Numeric type, 5 bytes in length (must be this value), 3 decimal places
mFoxpro.AddFields("Hipot1", 'N', 5, 3);
//Numeric type, 5 bytes in length (must be this value), 3 decimal places
mFoxpro.AddFields("Hipot2", 'N', 5, 3);
//Numeric type, 5 bytes in length (must be this value), 3 decimal places
mFoxpro.AddFields("Hipot3", 'N', 5, 3);
//Numeric type, 4 bytes in length (must be this value), 0 decimal places
mFoxpro.AddFields("Insulation", 'N', 4, 0);
//Close the database
mFoxpro.CloseFoxpro();
return(TRUE);
}
//Write record
BOOL WriteMapFile()
{
CFoxpro mFoxpro;
if (!mFoxpro.OpenFoxpro("MyName.Dbf"))
{
return(FALSE);
}
//Create a new record
mFoxpro.AddNew();
mFoxpro.Edit();

//Write model
mFoxpro.SetFieldsString(0, "COIN-003");
//Write date
mFoxpro.SetFieldsDate(1, 2004, 10, 30);
//Write an integer
mFoxpro.SetFieldsNumber(2, 3275);
//Write an integer
mFoxpro.SetFieldsNumber(3, 24);
//Update new effect
mFoxpro.Update();

//Close file
mFoxpro.Flush();
mFoxpro.CloseFoxpro();
return(TRUE);
}

//Read record
BOOL ReadMapFile()
{
CFoxpro mFoxpro;
if (!mFoxpro.OpenFoxpro("MyName.Dbf"))
{
return(FALSE);
}

//Locate to the 8th record
mFoxpro.Seek(7, SEEK_SET);

//Read string
char buf[256];
mFoxpro.GetFieldsString(0, buf);

//Read date
int nYear, nMonth, nDay;
mFoxpro.GetFieldsDate(1, &nYear, &nMonth, &nDay);

//Read data
float fData = mFoxpro.GetFieldsNumber(2);

//Close file
mFoxpro.CloseFoxpro();
return(TRUE);
}
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
zzz19760225 +2 2018-07-14 16:52
Floor 6 Posted 2018-07-14 17:51 ·  中国 广东 深圳 电信
高级用户
★★
Credits 668
Posts 295
Joined 2005-07-26 00:00
21-year member
UID 41110
Gender Male
From 广东深圳
Status Offline
The above code compiles successfully in Borland C++ 3.1 version. If you want to port it to Visual C++ 6.0, you just need to make a little modification!
Forum Jump: