📄 dbf.c
字号:
}
free(s);
}
free(buf);
fo->stru.record = fosta + i;
return fo->stru.record;
}
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: DB_FAPPEND *
* 参 数: starec:为被追加文件起始记录.其余参数 同 DB_CPYREC. *
* 功 能: 追加 fi 文件的记录到 fo 文件尾部,其它同 DB_CPYREC *
* 返 回 值: fo 文件实际记录数,其它同 DB_CPYREC *
***************************************************************************
*/
long db_fappend(DBFILE *fo, DBFILE *fi, long starec, long n, int *fields)
{
return(db_cpyrec(fo, fi, fo->stru.record, starec, n, fields));
}
/*** DB_CPYRE.C END ***/
/*
********************* DBASE 数据文件操作 C 库文件 ************************
* 文 件 名 : DB_CREAT.C *
* 编制人日期 : 湖北省公安县统计局 毛 泽 发 (1991.8) *
***************************************************************************
*/
#include "dbfio.h"
#ifdef TURBOC
#include <alloc.h>
#else
#include <malloc.h>
#endif
#include <string.h>
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: DB_VERIFIELD *
* 参 数: fd:外部字段结构数组首指针,末尾字段名首字符应为 "" *
* 功 能: 检验并校正字段类型.长度和小数位 *
* 返 回 值: 字段数 *
***************************************************************************
*/
int db_verifield(struct dbf *fd)
{
register int i, j;
for(i = 0; fd[i].name[0] && i < MAXFIELD; i ++){
if(fd[i].type > 'b' && fd[i].type < 'o') fd[i].type -= 32;
if(fd[i].type != 'N') fd[i].dec = 0;
if(fd[i].width == 0) fd[i].width = 1;
switch(fd[i].type){
case 'C':
if(fd[i].width > MAXFIESIZE) fd[i].width = MAXFIESIZE;
break;
case 'N':
if(fd[i].dec > MAXDEC) fd[i].dec = MAXDEC;
if(fd[i].width > MAXWIDTH) fd[i].dec = MAXWIDTH;
if((fd[i].width - fd[i].dec) < 2) fd[i].width = fd[i].dec + 2;
break;
case 'L':
fd[i].width = 1; break;
case 'D':
fd[i].width = 8; break;
case 'M':
fd[i].width = 10; break;
default:
if(fd[i].name[0]){
fd[i].type = 'C'; i -= 2;
}
}
}
return i;
}
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: db_create *
* 参 数: fname:文件名; fd:同上 *
* 功 能: 以写/读方式创建一个 DBASE 数据文件 *
* 返 回 值: 成功 DBFILE 指针, 出错 NULL *
***************************************************************************
*/
DBFILE *db_create(char *fname, struct dbf *fd)
{
DBFILE *f, *db_findfile();
register int n, i = 0;
if((f = db_findfile()) == NULL) return NULL;
if((f->fields = db_verifield(fd)) == NULL) return NULL;
n = sizeof(DBFIELD) * f->fields;
if((f->start = (DBFIELD *)malloc(n)) == NULL) return NULL;
memset(f->start, 0, n);
if((f->fdb = fopen(fname, "w+b")) == NULL){
free(f->start);
db_error = 6;
return NULL;
}
/* 将定义的外部字段信息拷贝到内部字段结构中 */
for(n = 0; i < f->fields; i ++){
strcpy(f->start[i].name, fd[i].name);
f->start[i].type = fd[i].type;
f->start[i].width = fd[i].width;
f->start[i].dec = fd[i].dec;
n += fd[i].width;
}
/* 初始化文件头结构 */
f->stru.dbf3 = DBFFILE;
f->stru.record = 0l;
f->stru.ldb = f->fields * 32 + 34;
f->stru.lrd = n + 1;
memset(f->stru.nul, 0, 20);
db_date(&f->stru);
if(!db_writestr(f, 1)){
db_close(f, 0);
remove(fname);
return NULL;
}
return f;
}
/*** DB_CREAT.C END ***/
/*
********************* DBASE 数据文件操作 C 库文件 ************************
* 文 件 名 : DB_DEL.C *
* 编制人日期 : 湖北省公安县统计局 毛 泽 发 (1991.8) *
***************************************************************************
*/
#include "dbfio.h"
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: DB_DELETE *
* 参 数: f:DBASE 数据文件指针;recs:记录号 *
* 功 能: 给记录打上删除标记 *
* 返 回 值: 成功 DELFLAG, 出错 EOF *
***************************************************************************
*/
int db_delete(DBFILE *f, long recs)
{
if(db_goto(f, recs) == -1) return EOF;
return(fputc(DELFLAG, f->fdb));
}
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: DB_RECALL *
* 参 数: 同 DB_DELETE *
* 功 能: 恢复打上删除标记的记录 *
* 返 回 值: 成功 SPACE, 出错 EOF *
***************************************************************************
*/
int db_recall(DBFILE *f, long recs)
{
if(db_goto(f, recs) == -1) return EOF;
return(fputc(32, f->fdb));
}
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: DB_PACK *
* 参 数: fname:DBASE 数据文件名 *
* 功 能: 重新组合数据库,以去掉带删除标记的记录 *
* 返 回 值: 成功 0 ,出错非零 *
***************************************************************************
*/
int db_pack(char *fname)
{
if(db_copy("$$$$$$$$.$$$", fname, 1, 0l, 0l, NULL)) return -1;
if(remove(fname) == -1) return -1;
return(rename("$$$$$$$$.$$$", fname));
}
/*** DB_DEL.C END ***/
/*
********************* DBASE 数据文件操作 C 库文件 ************************
* 文 件 名 : DB_SEEK.C *
* 编制人日期 : 湖北省公安县统计局 毛 泽 发 (1991.8) *
***************************************************************************
*/
#include "dbfio.h"
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: DB_GOTO *
* 参 数: f: DBASE 文件指针;record:记录号 = TOP 首记录, = BOTTOM 末记录*
* 功 能: 移动文件指针到指定的记录号 *
* 返 回 值: 成功:指针在文件的实际位置(字节数),出错 -1 *
***************************************************************************
*/
long db_goto(DBFILE *f, long record)
{
if(record == BOTTOM) record = f->stru.record - 1;
if(record < 0 || record > f->stru.record) record = f->stru.record;
record = record * f->stru.lrd + f->stru.ldb;
if(fseek(f->fdb, record, 0)){
db_error = 7;
return -1l;
}
return record;
}
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: DB_GETRECNUM *
* 参 数: f: DBASE 文件指针 *
* 功 能: 返回当前文件指针所在位置的记录号(注:指针不一定指在记录首字节)*
* 返 回 值: 记录号,出错 -n *
***************************************************************************
*/
long db_getrecnum(DBFILE *f)
{
long record;
if((record = ftell(f->fdb)) == -1l){
db_error = 7;
return -1l;
}
record = (record - f->stru.ldb) / f->stru.lrd;
return record;
}
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: DB_SKIP *
* 参 数: f: DBASE 文件指针; n:记录数 *
* 功 能: 文件指针从当前记录处移动 N 个记录位置 *
* 返 回 值: 同 DB_GOTO *
***************************************************************************
*/
long db_skip(DBFILE *f, long n)
{
long record;
if((record = db_getrecnum(f)) == -1l) return -1l;
return(db_goto(f, record + n));
}
/*** DB_SEEK.C END ***/
/*
********************* DBASE 数据文件操作 C 库文件 ************************
* 文 件 名 : DB_USE.C *
* 编制人日期 : 湖北省公安县统计局 毛 泽 发 (1991.8) *
***************************************************************************
*/
#include "dbfio.h"
#ifdef TURBOC
#include <alloc.h>
#else
#include <malloc.h>
#endif
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: db_getstr *
* 参 数: f 数据文件指针 *
* 功 能: 读数据文件头信息 *
* 返 回 值: 成功 1,否则 0 *
***************************************************************************
*/
int db_getstr(DBFILE *f)
{
/* 读文件头结构 */
if(!fread(&f->stru, sizeof(DBFSTR), 1, f->fdb) || f->stru.dbf3 != DBFFILE)
goto err;
/* 读字段结构 */
f->fields = db_getfields(f->stru);
if((f->start = (DBFIELD *)malloc(sizeof(DBFIELD) * f->fields)) == NULL)
goto err;
if(fread(f->start, sizeof(DBFIELD), f->fields, f->fdb) != f->fields){
free(f->start);
goto err;
}
return 1;
err:
db_error = 3;
}
/*
********************* DBASE 数据文件操作 C 库函数 ***********************
* 函 数 名: DB_USE *
* 参 数: 以读/写方式打开一个已存在的数据文件 *
* 功 能: fname 数据文件名 *
* 返 回 值: 成功 DBFILE 文件指针, 出错 NULL *
***************************************************************************
*/
DBFILE *db_use(char *fname)
{
DBFILE *f, *db_findfile();
if((f = db_findfile()) == NULL) return NULL; /* 数据文件打开数超过 MAXFILES */
if((f->fdb = fopen(fname, "r+b")) == NULL){
db_error = 2;
return NULL; /* 文件不存在 */
}
if(!db_getstr(f)){
fclose(f->fdb);
f->fdb = NULL;
return NULL; /* 非 DBASE 数据文件或读文件头信息有错 */
}
return f; /* 返回文件指针 */
}
/*** DB_USE.C END ***/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -