📄 db_create.c
字号:
#include <pr2k.h>
#include <database.h>
#include <db_create.h>
//#include <fatsys.h>
#include <filesys.h>
#include <string.h>
enum{TY_SIGNED_CHAR=1,TY_SIGNED_SHORT,TY_SIGNED_INT,TY_SIGNED_LONG};
enum{TY_UNSIGNED_CHAR=5,TY_UNSIGNED_SHORT,TY_UNSIGNED_INT,TY_UNSIGNED_LONG};
static char* __strupr(char* str);
SDB_Field m_Field[_DB_FIELD_MAX];
SDB_Database m_DbdHeader;
int m_field_idx;
int m_FileNum;
int l;
int m_Align;
int m_IsTaret;
int m_pre_flag;
int bvarFlag;
char gstrddl[1024];
char strddl[1024];//--my01.ddl
char strdbd[300];//--my01.dbd
char strTmp3[1024];
int dbdpos;
#define CHAR_SIZE (sizeof(char))
#define SHORT_SIZE (sizeof(short))
#define INT_SIZE (sizeof(int))
#define LONG_SIZE (sizeof(long))
#define FLOAT_SIZE (sizeof(float))
#define DOUBLE_SIZE (sizeof(double))
#define LONG_FLOAT_SIZE (sizeof(float))
#define LONG_DOUBLE_SIZE (sizeof(long double))
#define KEY_COUNT (sizeof(KeyName)/sizeof(KeyName[0]))
const unsigned char KeyName[][16] =
{
"database", //0
"define",
"record",
"struct",
"char",
"short",
"int",
"long", //combin key
"signed", //combin key
"unsigned", //combin key //9
"uchar",
"ushort",
"uint",
"ulong",
"float",
"double",
"{",
"}",
"primary",
"alternate", //19
"//",
"\0",
"\n",
"\r",
"\t",
"on",
"foreign",
"varchar",//27-undefined length string--myh2002-08-19
"varbyte"//28-myh2002-11-07
};
const unsigned char KeyType_[][16] =
{
" char ",
" short ",
" int ",
" long ", //combin key
" float ",
" double ",
};
const unsigned char KeyType[][16] =
{
" char*",
" short*",
" int*",
" long*", //combin key
" float*",
" double*",
};
const unsigned short type_size[13][2] = //pc, target
{
{0,0},
{CHAR_SIZE,CHAR_SIZE},//char
{SHORT_SIZE,SHORT_SIZE},//short
{INT_SIZE,INT_SIZE},//int
{LONG_SIZE,LONG_SIZE},//long
{CHAR_SIZE,CHAR_SIZE},//unsigned char
{SHORT_SIZE,SHORT_SIZE},//unsigned short
{INT_SIZE,INT_SIZE},//unsigned int
{LONG_SIZE,LONG_SIZE},//unsigned long
{FLOAT_SIZE,FLOAT_SIZE},//float
{DOUBLE_SIZE,DOUBLE_SIZE},//double
{LONG_FLOAT_SIZE,LONG_FLOAT_SIZE},//long float
{LONG_DOUBLE_SIZE,LONG_DOUBLE_SIZE},//long double
};
DLL_EXP(int) CreateDB(char* strdb,char* phead)
{
//因为牵涉到pc和pda内存处理上的区别,但是对NF来说不存在在pda上
//存储,而在pc机上操作的情况
//因此考虑对齐方式是按照4位---对32位cpu都是按4对齐**** m_Align =4
//存储不分目标和pc机----------即就是按照pc机处理****** m_IsTaret=0
// char strTmp3[1024];
char* strTmp,strTmp2,strTmp4;
char *str1;
char *pFieldList;//include end flag
int str_len,spacePos;
int fn,rn;
int enterpos;
int j;
unsigned char ch;
int bFlag;
//char database[20];
char database[256];
int fHandle1=-1,fHandle2=-1;
short offset;
int linenum;
linenum=0;
offset=0;
pFieldList=0;
enterpos=0;
bFlag=0;
ch=0x7e;
m_pre_flag=0;
bvarFlag=0;
dbdpos=0;
m_Align=4; //4 align
m_IsTaret=0;//pc format
m_field_idx=-1;//initialize
m_FileNum=0;//used or not used???
memset(gstrddl,0,sizeof(gstrddl));
memset(strdbd,0,sizeof(strdbd));
memset(&m_DbdHeader,0,sizeof(m_DbdHeader));
strcpy(gstrddl,phead);//
strcpy(strddl,phead);
strTmp=strstr(strddl,"\r\n");
memset(strTmp3,0,sizeof(strTmp3));
if(strTmp)
{
memcpy(strTmp3,strddl,(unsigned int)(strlen(strddl)-strlen(strTmp)));
strcpy(strddl,strTmp+2);
}
else
strcpy(strTmp3,strddl);
while(strlen(strTmp3)>0)
{
if(-1 == DealDllFile((char*)strTmp3))
{
return -1;
}
strTmp=strstr(strddl,"\r\n");
if(strTmp)
{
memset(strTmp3,0,sizeof(strTmp3));
memcpy(&strTmp3[0],&strddl[0],strlen(strddl)-strlen(strTmp));
strcpy(strddl,strTmp+2);
}
else
{
if(bFlag)
strcpy(strTmp3,"");
else
strcpy(strTmp3,strddl);
bFlag=1;
}
linenum++;
}
m_DbdHeader.Record_Size++;
pFieldList = (char*)malloc(m_DbdHeader.Field_Count*sizeof(SDB_Field)+7);
memset(pFieldList,0,m_DbdHeader.Field_Count*sizeof(SDB_Field)+2);
fn=m_DbdHeader.Field_Count;
rn=m_DbdHeader.Record_Size;
for(j=0;j<fn;j++)
{
m_Field[j].wType2=m_Field[j].wSize;
m_Field[j].wSize=offset;
offset+=m_Field[j].wType2;
}
if(m_IsTaret)
{
for(j=0;j<fn;j++)
{
m_Field[j].wType2 = Swap2(m_Field[j].wType2);
m_Field[j].wSize = Swap2(m_Field[j].wSize);
m_Field[j].wType = Swap2(m_Field[j].wType);
m_Field[j].WReserve = Swap2(m_Field[j].WReserve);
}
}
memcpy(pFieldList,m_Field,fn*sizeof(SDB_Field));
m_DbdHeader.sFlag=0x1228;
m_DbdHeader.version=0x0100;
if(m_IsTaret)
{
m_DbdHeader.Field_Count = Swap4(m_DbdHeader.Field_Count);
m_DbdHeader.Record_Size = Swap4(m_DbdHeader.Record_Size);
m_DbdHeader.version = Swap2(m_DbdHeader.version);
}
memset(m_DbdHeader.Reserve,0xff,20);
memcpy(&strdbd[dbdpos],&m_DbdHeader,sizeof(SDB_Database));
dbdpos+=(sizeof(SDB_Database));
// memcpy(&strdbd[dbdpos],pFieldList,fn*sizeof(SDB_Field));
memcpy(&strdbd[dbdpos],m_Field,fn*sizeof(SDB_Field));
dbdpos+=fn*sizeof(SDB_Field);
free(pFieldList);
strdbd[dbdpos]=ch;
dbdpos++;
if(bvarFlag)
strdbd[0]=0xBB;
strcpy(database, strdb);
strcat(database, ".DBD");
fHandle1=fs_open((char*)database, O_RDWR|O_CREATE);
if(fHandle1!=-1)
{
if(bvarFlag)
{
strcpy(database,strdb);
strcat(database,".MEM");
fHandle2=fs_open((char*)database,O_RDWR|O_CREATE);
if(fHandle2==-1)
{
fs_close(fHandle1);
return -1;
}
}
fs_write(fHandle1,strdbd,dbdpos);
}
else
return -1;
if(fHandle1>=0)
fs_close(fHandle1);
if(fHandle2>=0)
fs_close(fHandle2);
return 1;
}
//return -1:err;1:ok
int DealDllFile(char *pstr)
{
char *psize1,*psize2;
char strTmp2[200];
char strTemp[200];
int i,deal_ok;
int ret,type_count;
int type;
int j,k;
int p;
ret= 1;
type_count=1;
deal_ok=-1;
type=0;
j=0;
k=0;
p=KEY_COUNT;
if(strlen(pstr)==0)
{
deal_ok=-1; m_FileNum++;
return ret;
}
__trimleft(pstr);
// pstr->TrimLeft();
__trimright(pstr);
// pstr->TrimRight();
if(strstr(pstr,(char*)KeyName[27])==(char*)pstr)
{
deal_ok=27;
bvarFlag++;
}
else if(strstr(pstr,(char*)KeyName[28])==(char*)pstr)
{
deal_ok=28;
bvarFlag++;
}
else
{
for(i=0;i<KEY_COUNT;i++)
{
// if(pstr->Find((LPCTSTR)KeyName[i],0) == 0 )
if(strstr(pstr,(char*)KeyName[i])==(char*)pstr)
{
deal_ok = i;
break;
}
}
}
if(strlen(pstr)==0)
{
deal_ok = 21;
}
if(deal_ok >= 0)
{
switch(deal_ok)
{
case 0://database
m_pre_flag = 0;
ret = 1;
break;
case 1://define
// strTemp.Format("#%s\n",*pstr);
m_pre_flag = 0;
ret = 1;
break;
case 2://record
// strTemp = *pstr;
// strTemp.Replace("record","struct");
// strTemp2 = strTemp;
// strTemp.Format("%s\n",strTemp2);
m_pre_flag = 1;
ret = 1;
break;
case 3://struct
ret = -1;
break;
case 4://char
Lab_si_char: if(m_pre_flag != 1)
break;
m_field_idx ++;
// strTemp = *pstr;
// strTemp2 = strTemp;
// strTemp.Format(" %s\n",strTemp2);
type_count = GetTypeCount(pstr);
m_Field[m_field_idx].wType = TY_CHAR;
// if(pstr->Find("*",0) > 0)
if(strstr(pstr,"*"))
{
m_Field[m_field_idx].wSize = 4*type_count;
m_Field[m_field_idx].wType2 |= 0x02;
if(m_Align==4)
if(m_DbdHeader.Record_Size%4!=0)
{
m_Field[m_field_idx-1].wSize += (m_DbdHeader.Record_Size+3)/4*4-m_DbdHeader.Record_Size;
m_DbdHeader.Record_Size = (m_DbdHeader.Record_Size+3)/4*4;
}
else
if(m_DbdHeader.Record_Size%2!=0)
{
m_Field[m_field_idx-1].wSize++;
m_DbdHeader.Record_Size++;
}
}
else
m_Field[m_field_idx].wSize = type_size[m_Field[m_field_idx].wType][m_IsTaret]*type_count;
if(m_Field[m_field_idx].wSize>1)
m_Field[m_field_idx].wSize--;
m_DbdHeader.Record_Size += m_Field[m_field_idx].wSize;
m_DbdHeader.Field_Count++;
ret = 1;
break;
case 5://short
Lab_si_short: if(m_pre_flag != 1)
break;
m_field_idx ++;
// strTemp = *pstr;
// strTemp2 = strTemp;
// strTemp.Format(" %s\n",strTemp2);
type_count = GetTypeCount(pstr);
m_Field[m_field_idx].wType = TY_SHORT;
// if(pstr->Find("*",0) > 0)
if(strstr(pstr,"*"))
{
m_Field[m_field_idx].wSize = 4*type_count;
m_Field[m_field_idx].wType2 |= 0x02;
if(m_Align==4)
if(m_DbdHeader.Record_Size%4!=0)
{
m_Field[m_field_idx-1].wSize += (m_DbdHeader.Record_Size+3)/4*4-m_DbdHeader.Record_Size;
m_DbdHeader.Record_Size = (m_DbdHeader.Record_Size+3)/4*4;
}
else
if(m_DbdHeader.Record_Size%2!=0)
{
m_Field[m_field_idx-1].wSize++;
m_DbdHeader.Record_Size++;
}
}
else
m_Field[m_field_idx].wSize = type_size[m_Field[m_field_idx].wType][m_IsTaret]*type_count;
m_DbdHeader.Record_Size += m_Field[m_field_idx].wSize;
m_DbdHeader.Field_Count++;
ret = 1;
break;
case 6://int
Lab_si_int: if(m_pre_flag != 1)
break;
m_field_idx ++;
// strTemp = *pstr;
// strTemp2 = strTemp;
// strTemp.Format(" %s\n",strTemp2);
type_count = GetTypeCount(pstr);
m_Field[m_field_idx].wType = TY_INT;
// if(pstr->Find("*",0) > 0)
if(strstr(pstr,"*"))
{
m_Field[m_field_idx].wSize = 4*type_count;
m_Field[m_field_idx].wType2 |= 0x02;
}
else
m_Field[m_field_idx].wSize = type_size[m_Field[m_field_idx].wType][m_IsTaret]*type_count;
m_DbdHeader.Record_Size += m_Field[m_field_idx].wSize;
m_DbdHeader.Field_Count++;
ret = 1;
break;
case 7://long
if(m_pre_flag != 1)
break;
// strTemp = *pstr;
// strTemp.Delete(0,strlen((char*)KeyName[7]));
// strTemp.TrimLeft();
// strTemp.TrimRight();
strcpy(strTemp,pstr+strlen((char*)KeyName[7]));
__trimleft(strTemp);
__trimright(strTemp);
if(strstr(strTemp,(char*)KeyName[14])==(char*)strTemp)//long float
{
type = TY_LONG_FLOAT;
}
else if(strstr(strTemp,(char*)KeyName[15])==(char*)strTemp)//long double
{
type = TY_LONG_DOUBLE;
}
else //long int
{
Lab_si_long: if(m_pre_flag != 1)
break;
type = TY_LONG;
}
m_field_idx ++;
// strTemp = *pstr;
// strTemp2 = strTemp;
// strTemp.Format(" %s\n",strTemp2);
strcpy(strTemp,pstr);
type_count = GetTypeCount(pstr);
m_Field[m_field_idx].wType = (int)type;
if(strstr(pstr,"*"))
{
m_Field[m_field_idx].wSize = 4*type_count;
m_Field[m_field_idx].wType2 |= 0x02;
}
else
m_Field[m_field_idx].wSize = type_size[m_Field[m_field_idx].wType][m_IsTaret]*type_count;
m_DbdHeader.Record_Size += m_Field[m_field_idx].wSize;
m_DbdHeader.Field_Count++;
ret = 1;
break;
case 8://signed
// strTemp = *pstr;
// strTemp.Delete(0,strlen((char*)KeyName[8]));
// strTemp.TrimLeft();
// strTemp.TrimRight();
strcpy(strTemp,pstr+strlen((char*)KeyName[8]));
__trimleft(strTemp);
__trimright(strTemp);
if(strstr(strTemp,(char*)KeyName[4])==(char*)strTemp)//signed char
{
goto Lab_si_char;
}
else if(strstr(strTemp,(char*)KeyName[5])==(char*)strTemp)//signed short
{
goto Lab_si_short;
}
else if(strstr(strTemp,(char*)KeyName[6])==(char*)strTemp)//signed int
{
goto Lab_si_int;
}
else if(strstr(strTemp,(char*)KeyName[7])==(char*)strTemp)//signed long
{
goto Lab_si_long;
}
break;
case 9://unsigned
// strTemp = *pstr;
// strTemp.Delete(0,strlen((char*)KeyName[9]));
// strTemp.TrimLeft();
// strTemp.TrimRight();
strcpy(strTemp,pstr+strlen((char*)KeyName[9]));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -