📄 select.c
字号:
void Select(DBF * dbf_name, char * commond, FILE ** opened_fp)
{
RecUnion * recIn = (RecUnion *)malloc(sizeof(RecUnion));
RecUnion * tempRec = (RecUnion *)malloc(sizeof(RecUnion));
char yes = 'n';
int q = 0;
int i = 0;
int xiaoshu = FALSE;
int tempInt = 0;
int fpi;
int op = -1;
int isString = FALSE;
int pos1 = -1;
int pos2 = -1;
int type1 = -1;
int type2 = -1;
int offset1 = 0;
int offset2 = 0;
int show[MAX_FILED_COUNT] = {0};/*标记各个属性是否被选择,为1被选择,显示*/
int selected = FALSE;/*用于标志是否选择了属性列,若为0则显示出所有字段否则根据show[i]的值来确定是否显示*/
int limited = FALSE;/*用于表示是否有加入选择条件*/
int q1 = -1;/*记录关系运算符第一个运算对象,字段的字段号*/
int q2 = -1;/*如果关系运算符第一个运算对象为字段则记下其字段号*/
int compare = ERROR;
unsigned j = 0;
float div = 1;
float tempFloat = 0;
commond = strtok(commond," ");
commond = strtok(NULL," ");
fpi = dbf_name->dhID;
if (strcmp(dbf_name->dhName,"\0") == 0)
{
puts(" 您还没有打开过文件,当前文件为空!!!");
/*free(tempRec);
free(recIn);*/
return;
}
if (dbf_name->dhRecCount == 0)
{
puts(" 表中无记录!!!");
/*free(tempRec);
free(recIn);*/
return;
}
fseek(opened_fp[fpi],sizeof(DBF),0);
while ((commond != NULL) && (strcmpi(commond,"WHERE") != 0))/*读入要显示的属性列名*/
{
for (q=0; q<dbf_name->dhFieldCount; q++)
{
if (strcmpi(dbf_name->dbf_struct[q].dhFieldName,commond) == 0)
{
if (show[q] == 1)
{
printf(" 错误,该字段%s已输入过\n",commond);
/*free(tempRec);
free(recIn);*/
return;
}
show[q] = 1;
break;
}
}
if ((q >= dbf_name->dhFieldCount) && (strcmpi(dbf_name->dbf_struct[q-1].dhFieldName,"commond") != 0))
{
printf(" 格式错误,不存在 %s 属性,可能为缺乏where\n", commond);
/*free(tempRec);
free(recIn);*/
return;
}
commond = strtok(NULL, " ");
selected = TRUE;
}/*end of while*/
if ((commond != NULL) && (strcmpi(commond,"WHERE") == 0))/*读选择条件*/
{
commond = strtok(NULL, " ");
if (commond == NULL)
{
puts(" 请输入选择条件");
/*free(tempRec);
free(recIn);*/
return;
}
for (q=0; q<dbf_name->dhFieldCount; q++)
{
if (strcmpi(dbf_name->dbf_struct[q].dhFieldName,commond) == 0)
{
pos1 = dbf_name->dbf_struct[q].dhFieldPos;
type1 = dbf_name->dbf_struct[q].dhFieldType;
offset1 = dbf_name->dbf_struct[q].dhFieldLen;
q1 = q;
break;
}
}
if (pos1 == -1)
{
printf(" 该关系不存在字段 %s\n",commond);
/*free(tempRec);
free(recIn);*/
return;
}
commond = strtok(NULL," ");
/*读关系运算符*/
if (commond == NULL)
{
puts(" 命令格式错误,请输入关系运算符");
/*free(tempRec);
free(recIn);*/
return;
}
if (strcmpi(commond,"<") == 0)
op = LT;
else if (strcmpi(commond,"<=") == 0)
op = LE;
else if (strcmpi(commond,"=") == 0)
op = EQ;
else if (strcmpi(commond,">") == 0)
op = GT;
else if (strcmpi(commond,">=") == 0)
op = GE;
else if (strcmpi(commond,"<>") == 0)
op = NE;
else
{
puts(" 输入的关系运算符错误!!!");
return;
}
/*读关系运算符的第二个比较对象*/
commond = strtok(NULL," ");
if (commond == NULL)
{
puts(" 命令格式错误,请输入关系运算符的下个运算对象");
return;
}
/*判断第二个运算对象类型,并记录其值*/
if ((commond[0] == '0') && (strlen(commond) > 1))
{
isString = TRUE;
type2 = String;
}
else
{
for (j=0; j<strlen(commond); j++)
{
if (isdigit(commond[j]))
{
tempFloat = tempFloat * 10 + (int)commond[j] - 48;
tempInt = tempInt * 10 + (int)commond[j] - 48;
if (xiaoshu == TRUE)
div *= 10;
}
else if (commond[j] == '.')
{
if (xiaoshu == TRUE)
{
isString = TRUE;
break;
}
xiaoshu = TRUE;
}
else
{
isString = TRUE;
break;
}
}
}
if (isString)
{
tempRec->string = (char *)malloc(strlen(commond) * sizeof(char));
strcpy(tempRec->string,commond);
/*如果关系运算符的第二个对象为字段名,用pos2记录其偏移位置*/
for (q=0; q<dbf_name->dhFieldCount; q++)
{
if (strcmpi(dbf_name->dbf_struct[q].dhFieldName,commond) == 0)
{
pos2 = dbf_name->dbf_struct[q].dhFieldPos;
type2 = dbf_name->dbf_struct[q].dhFieldType;
offset2 = dbf_name->dbf_struct[q].dhFieldLen;
q2 = q;
break;
}
}
type2 = String;
}
else if (xiaoshu)
{
tempRec->floatType = tempFloat / div;
type2 = Float;
}
else
{
tempRec->intType = tempInt;
type2 = Int;
}
limited = TRUE;/*标记输入了条件*/
}/*end of if commond != NULL && strcmpi(commond,"WHERE")*/
/*打印需要显示的属性名*/
printf(" ");
for (q=0; q<dbf_name->dhFieldCount; q++)
{
if (show[q] || !selected)
printf("\t%s\t",dbf_name->dbf_struct[q].dhFieldName);
}
puts("");
if (!limited)
{
fseek(opened_fp[fpi],dbf_name->dhRecNO*dbf_name->dhRecLen,1);
i = dbf_name->dhRecNO;
}
for (; i<dbf_name->dhRecCount; i++)
{
if (feof(opened_fp[fpi]))
{
puts(" end of file");
break;
}
if (limited)
{ /*比较看是否满足显示条件*/
fseek(opened_fp[fpi],pos1,1);
switch (type1) {
case Int:
fread(recIn,sizeof(int),1,opened_fp[fpi]);
break;
case Float:
fread(recIn,sizeof(float),1,opened_fp[fpi]);
break;
case String:
recIn->string = (char *)malloc(dbf_name->dbf_struct[q1].dhFieldLen * sizeof(char));
fread(recIn->string,dbf_name->dbf_struct[q1].dhFieldLen,1,opened_fp[fpi]);
recIn->string[dbf_name->dbf_struct[q1].dhFieldLen] = '\0';
break;
default:
break;
}
fseek(opened_fp[fpi],-offset1-pos1,1);
/*如果第二个运算对象是某个字段,则取出该字段的数据到tempRec*/
if (pos2 != -1)
{
fseek(opened_fp[fpi],pos2,1);
switch (type2) {
case Int:
fread(tempRec,sizeof(int),1,opened_fp[fpi]);
break;
case Float:
fread(tempRec,sizeof(float),1,opened_fp[fpi]);
break;
case String:
tempRec->string = (char *)malloc(dbf_name->dbf_struct[q2].dhFieldLen * sizeof(char));
fread(tempRec->string,dbf_name->dbf_struct[q2].dhFieldLen,1,opened_fp[fpi]);
tempRec->string[dbf_name->dbf_struct[q2].dhFieldLen] = '\0';
break;
default:
break;
}
fseek(opened_fp[fpi],-offset2-pos2,1);
}
compare = Compare(recIn,tempRec,type1,type2);
if (compare == ERROR)
return;
switch (op) {/*不满足条件则文件指针指到下一条记录*/
case LT:
if (compare != LT)
{
fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
continue;
}
break;
case LE:
if (compare == GT)
{
fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
continue;
}
break;
case EQ:
if (compare != EQ)
{
fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
continue;
}
break;
case GT:
if (compare != GT)
{
fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
continue;
}
break;
case GE:
if (compare == LT)
{
fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
continue;
}
break;
case NE:
if (compare == EQ)
{
fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
continue;
}
break;
default:
puts("ERROR");
return ;
break;
}/*end of switch*/
/*if (tempRec->string != NULL)
{
free(tempRec);
tempRec = NULL;
}*/
}/*end of limited*/
/*显示记录*/
for (q=0; q<dbf_name->dhFieldCount; q++)
{
switch (dbf_name->dbf_struct[q].dhFieldType) {
case Int:
fread(recIn,sizeof(int),1,opened_fp[fpi]);
if (show[q] || !selected)
printf("\t%d\t",recIn->intType);
break;
case Float:
fread(recIn,sizeof(float),1,opened_fp[fpi]);
if (show[q] || !selected)
printf("\t%.4f\t",recIn->floatType);
break;
case String:
recIn->string = (char *)malloc(dbf_name->dbf_struct[q].dhFieldLen * sizeof(char));
fread(recIn->string,dbf_name->dbf_struct[q].dhFieldLen,1,opened_fp[fpi]);
if (show[q] || !selected)
printf("\t%s",recIn->string);
break;
default:
break;
}
/*if (recIn->string != NULL)
{
free(recIn);
recIn = NULL;
}*/
}/*end of for q=0*/
puts("");
dbf_name->dhRecNO = i;
}/*end of for (; i<dbf_name->dhRecCount; i++,dbf_name->dhRecNO++)*/
/*free(tempRec);
free(recIn);
tempRec = NULL;
recIn = NULL;*/
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -