⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compile.h

📁 一个用c开发的小型数据库管理系统dbms
💻 H
📖 第 1 页 / 共 5 页
字号:
#include<stdio.h>
#include<string.h>
#include <stdlib.h>

/*公有变量

1 字段长度
2 元祖数
3 seek函数寻找的记录行号,默认为-1
4 打开文件数目

*/
int fieldlength;
int lines;
int seeklocation=-1;
int openfilenum=0;
/*结构类型

1 字段类型
2 辅助表记录
3 数据记录
4 打开文件记录

*/
struct relation{
	char wordname[9];
	unsigned int typesign:2;//int:0 float:1 char:2
	unsigned int length;
	unsigned int key:8;//default:0 primarykey:1 notnull:2 unique:3 13:4
	long locate;
}rel[128];

struct jointable{
	struct relation join[128];
	int fieldlenght;
	int lines;
}to,from,tmp,from1;

struct value{
	char fieldvalue[256];
}values[128];

struct table_opened{
	char relname[9];
	char filename[9];
	unsigned int station:2;//default:0 current 1;
	unsigned int openstyle:2;//r:0 w:1;
	FILE *currentfp;
}file_opened[10];

/*结构类型长度

1 字段类型长度
2 数据记录长度

*/

#define LENGTH sizeof(struct relation)
#define VALUE_LENGTH sizeof(struct value)

/*
函数声明
*/
int condition(int con,int id,FILE *f);
////////////////////////////////////////////////////
/*
* Throwerror Function 错误处理函数
*/
///////////////////////////////////////////////////

void throwerror(int errid)
{
	char errstring[80];
	switch(errid)
	{
	case 1:strcpy(errstring,"error 0001:Syntax Error!");break;
	case 2:strcpy(errstring,"error 0002:Table estiblish Error!");break;
	case 3:strcpy(errstring,"error 0003:The tablename is null");break;
	case 4:strcpy(errstring,"error 0004:This table name already exist,please choose another name!");break;
	case 5:strcpy(errstring,"error 0005:The fieldname rename!");break;
	case 6:strcpy(errstring,"error 0006:Table not found!");break;
	case 7:strcpy(errstring,"error 0007:No open mode!");break;
	case 8:strcpy(errstring,"error 0008:Error open mode!");break;
	case 9:strcpy(errstring,"error 0009:Command error or not use capitalization!");break;
	case 10:strcpy(errstring,"error 0010:No Table opened!");break;
	case 11:strcpy(errstring,"error 0011:There have already 10 tables opened!Open failed!");break;
	case 12:strcpy(errstring,"error 0012:This table has already opened!");break;
	case 13:strcpy(errstring,"error 0013:Table not opened!");break;
	case 14:strcpy(errstring,"error 0014:Table not used!");break;
	case 15:strcpy(errstring,"error 0015:The Current Table Read Only!Not allowed modify!");break;
	case 16:strcpy(errstring,"error 0016:Type disaccord!");break;
	case 17:strcpy(errstring,"error 0017:Fieldname error!");break;
	case 18:strcpy(errstring,"error 0018:Please use seek command or add [where] parameter!");break;
	case 19:strcpy(errstring,"error 0019:Data not found!");break;
	case 20:strcpy(errstring,"error 0020:The fieldname is null");break;
	case 21:strcpy(errstring,"error 0021:Condition is null after WHERE");break;
	case 22:strcpy(errstring,"error 0022:Fieldname conflict!");break;
	case 23:strcpy(errstring,"error 0023:Parameter too many!");break;
	case 24:strcpy(errstring,"error 0024:The number of fields too many!");break;
	case 25:strcpy(errstring,"error 0025:Tablename conflict!");break;
	case 26:strcpy(errstring,"error 0026:Insert Error!");break;
	case 27:strcpy(errstring,"error 0027:Update Error!");break;
	default:strcpy(errstring,"Illegal!");break;
	};
    printf("\n%s\n",errstring);
}
////////////////////////////////////////////////////
/*
* isAlpNum Function 判断是否为字符、数字和特殊字符函数
*/
///////////////////////////////////////////////////

int isAlpNum(char c)
{
	if(c>='a'&&c<='z'||c>='A'&&c<='Z'||c>='0'&&c<='9'||c=='_')
	{
		return (1);
	}else if(c==' ')
	{
		return (2);
	}else
	{
		return (0);
	}
}

////////////////////////////////////////////////////
/*
* isNum Function 判断是否为数字
*/
///////////////////////////////////////////////////

int isNum(char c)
{
	if(c>='0'&&c<='9')
	{
		return (1);
	}else
	{
		return (0);
	}
}

////////////////////////////////////////////////////
/*
* Sign Function 判断逻辑字符
*/
///////////////////////////////////////////////////

int sign(char *c)
{
	if(strcmp(c,">")==0) return 1;
	if(strcmp(c,">=")==0) return 2;
	if(strcmp(c,"=")==0) return 3;
	if(strcmp(c,"<=")==0) return 4;
	if(strcmp(c,"<")==0) return 5;
	return 0;
}

////////////////////////////////////////////////////
/*
* CREATE Function 建立表
*/
///////////////////////////////////////////////////

int create(char *temp)
{
	int lll=1;
	FILE *fp;
	int i=0,j=0,k=0,l=0,m;
	int len=strlen(temp);
	int err=0;
    char token[20];
	char filename[20];
//	puts(temp);
    i=6;
	j=0;
    while(i<=len&&temp[i]!='/'){token[j]=temp[i];i++;j++;}
	token[j]='\0';
    if((fp=fopen(token,"r"))!=NULL)
	{
		throwerror(4);
		fclose(fp);
	}else
	{
		strcpy(filename,token);
//		puts(filename);
		i++;
		if(temp[i]=='('){
			i=i+2;
            j=0;
			k=0;
            while(i<=len&&temp[i]!=';')
			{
				j=0;
				l=i;
                while(l<=len&&temp[l]!='/'){
					token[j]=temp[l];
					j++;
					l++;
				}
				token[j]='\0';
                for(m=0;m<k;m++)
				{
					if(strcmp(token,rel[m].wordname)==0)
					{
						throwerror(5);
						err=1;
						break;
					}
				}
				strcpy(rel[k].wordname,token);
				rel[k].key=0;
//				puts(rel[k].wordname);
				///////////////////////////
				l++;
				j=0;
                while(l<=len&&temp[l]!='/'){
					token[j]=temp[l];
					j++;
					l++;
				}
				token[j]='\0';
				strlwr(token);
//				puts(token);
				if(strcmp(token,"int")==0)
				{
					rel[k].typesign=0;
					rel[k].length=16;
					l++;
				}else if(strcmp(token,"float")==0){
                    rel[k].typesign=1;
					rel[k].length=16;
				    l++;
				}else if(strcmp(token,"char")==0){
                    rel[k].typesign=2;
				    l++;
				   if(temp[l]!='('){err=1;break;}
				   else{
					   l++;
					   l++;
                       j=0;
                       while(l<=len&&temp[l]!='/'){
					        token[j]=temp[l];
				           	j++;
				        	l++;
					   }
				      token[j]='\0';
                      rel[k].length=atoi(token);
//					  printf("%d\n",rel[k].length);
					  l++;
				   }
				   if(temp[l]!=')'){err=1;break;}
				   else{l++;l++;}
                }else{err=1;break;}
				if(temp[l]=='P'||temp[l]=='N'||temp[l]=='U'){
					j=0;
					while(l<=len&&temp[l]!='/'){
					token[j]=temp[l];
					j++;
					l++;
					}
				    token[j]='\0';
				    strlwr(token);
//				    puts(token);
                    if(strcmp(token,"primarykey")==0)
					{
						rel[k].key=1;
						l++;
					}
					if(strcmp(token,"notnull")==0)
					{
						rel[k].key=2;
						l++;
					}
					if(strcmp(token,"unique")==0)
					{
						rel[k].key=3;
						l++;
					}
					if(strcmp(token,"notnullunique")==0)
					{
						rel[k].key=4;
						l++;
					}
				}
				if(temp[l]==')')
				{
					l++;
					l++;
					k++;
					i=l;
					break;
				}else if(temp[l]==',')
				{
					k++;
				    i=l;
					i++;
					i++;
				    continue;
				}else{
					err=1;break;
				}
				///////////////////////////
			}
			   if(temp[i]!=';')
			   {
				   err=1;
				   throwerror(1);
				   return lll;
			   }
			   if(!err){
				   if((fp=fopen(filename,"wb+"))!=NULL)
				   { 
					   fputc(k,fp);
					   fputc(0,fp);
					   for(j=0;j<k;j++)
					   {
						   rel[j].locate=j+1;
					   }
                       for(j=0;j<128;j++)
					   fwrite(&rel[j],LENGTH,1,fp);
		               fclose(fp);
					   printf("\nEstablish File success!\n");
					   lll=0;
				   }else
				   {
					   throwerror(2);
				   }
			   }
		}else
		{
			throwerror(1);
		}
	}
	return lll;
}

////////////////////////////////////////////////////
/*
* OPEN Function 打开表
*/
///////////////////////////////////////////////////

int openrel(char *temp)
{
	int lll=1;
    FILE *fp;
	int i=0,j=0,k=0,l=0,m=0;
	int len=strlen(temp);
	int err=0;
    char token[20];
	char mode[4];
//	printf("open command\n");
//	puts(temp);

	seeklocation=-1;

	if(openfilenum>=10)
	{
		throwerror(11);
		return lll;
	}
	if(len==5){
		throwerror(3);
		return lll;
	}
    i=5;
	j=0;
	while(i<=len&&temp[i]!='/'){token[j]=temp[i];i++;j++;}
	token[j]='\0';
//	puts(token);
	
	i++;
	
    if(i<len){
		if(temp[i]==',')
		{
			i+=2;
			if((i+2)<len){throwerror(23);return lll;}
			for(j=0;j<openfilenum;j++)
			{
				if(stricmp(file_opened[j].filename,token)==0)
				{
					throwerror(12);
					return lll;
				}
			}
			if(i<len)
			{
				if(temp[i]=='r'){
					strcpy(mode,"rb");
					file_opened[openfilenum].openstyle=0;
					m=1;
				}else if(temp[i]=='w'){
					strcpy(mode,"ab+");
					file_opened[openfilenum].openstyle=1;
					m=1;
				}else{
						throwerror(8);
				}
			}
		}else
		{
			throwerror(1);
		}
	}else
	{
		throwerror(7);	
	}
	
	if(m==1)
	{
		if((fp=fopen(token,mode))==NULL)
		{
			throwerror(6);
		}else
		{
		printf("%s open success!\n",token);
		lll=0;
		////////////////////////////////////
		rewind(fp);
		from.fieldlenght=fgetc(fp);
		from.lines=fgetc(fp);
        strcpy(file_opened[openfilenum].filename,token);
		strcpy(file_opened[openfilenum].relname,token);
///////////////modify the current filestation=1
		for(i=0;i<openfilenum;i++)file_opened[i].station=0;
		file_opened[openfilenum].station=1;
		file_opened[openfilenum].currentfp=fp;
		for(i=0;i<from.fieldlenght;i++)
		{
            fread(&(from.join[i]),LENGTH,1,file_opened[openfilenum].currentfp);
		}
	/*	printf("\n*------------------------------------------------------------*\n");
		fseek(fp,128*LENGTH+2,0);
		for(j=0;j<from.lines*from.fieldlenght;j++){
			fread(&values[0],VALUE_LENGTH,1,fp);
			printf("%20s",values[0].fieldvalue);
			if((j+1)%from.fieldlenght==0) printf("\n|------------------------------------------------------------|\n");
		}
		*/
		openfilenum++;
		printf("\nThere is %d files opened!\n",openfilenum);
		lll=0;
		}
	}
	return lll;
}

////////////////////////////////////////////////////
/*
* CLOSE Function 关闭表
*/
///////////////////////////////////////////////////

int closerel(char *temp)
{
	int lll=1;
    FILE *fp;
	int i=0,j=0,k=0;
	int len=strlen(temp);
	int err=0;
    char token[20];
    if(len==6){
		throwerror(3);
		return lll;
	}
	i=6;
	j=0;
	while(i<=len&&temp[i]!='/'){token[j]=temp[i];i++;j++;}
	token[j]='\0';
//	puts(token);
	if(openfilenum==0) {
		throwerror(10);
		return lll;
	}
	else{
		for(i=0;i<openfilenum;i++)
		{
			if(stricmp(token,file_opened[i].filename)==0)
			{
				k=1;
				fp=file_opened[i].currentfp;
				fclose(fp);
				fp=NULL;
				break;
			}
		}
		if(!k)throwerror(6);
		if(i<openfilenum)
		{
			j=i;
		    for(i=j+1;i<openfilenum;i++,j++)
			{
				file_opened[j].currentfp=file_opened[i].currentfp;
			    strcpy(file_opened[j].filename,file_opened[i].filename);
			    file_opened[j].openstyle=file_opened[i].openstyle;
			    strcpy(file_opened[j].relname,file_opened[i].relname);
			    file_opened[j].station=file_opened[i].station;
		    	/////////////////////
			    file_opened[i].currentfp=NULL;
			}
		    openfilenum--;
	        printf("\nThere is %d files left!\nClose Success!\n",openfilenum);
			lll=0;
		}
		
	}
	return lll;
}

////////////////////////////////////////////////////
/*
* USE Function 选择当前表
*/
///////////////////////////////////////////////////

int use(char *temp)
{
	int lll=1;
	int i=0,j=0,k=0;
	int len=strlen(temp);
	int err=0;
    char token[20];
	
	seeklocation=-1;

    if(len==4){
		throwerror(3);
		return lll;
	}

	if(openfilenum==0) {
		throwerror(10);
		return lll;
	}

	i=4;
	j=0;
	while(i<=len&&temp[i]!='/'){token[j]=temp[i];i++;j++;}
	token[j]='\0';
//	puts(token);
	i++;
	if(i<len){throwerror(23);return lll;}
    for(i=0;i<openfilenum;i++)
	{
		if(stricmp(token,file_opened[i].filename)==0)
		{
			k=1;
			break;
		}
	}
	if(k)
	{
		for(j=0;j<openfilenum;j++)file_opened[j].station=0;
		file_opened[i].station=1;
		rewind(file_opened[i].currentfp);
		from.fieldlenght=fgetc(file_opened[i].currentfp);
		from.lines=fgetc(file_opened[i].currentfp);
		for(j=0;j<from.fieldlenght;j++)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -