lsinsert.cpp
来自「CBASE v1.01 采用Borland公司TC++编写的数据库管理源程序库」· C++ 代码 · 共 152 行
CPP
152 行
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "@(#)lsinsert.c 1.4 - 90/06/20" */
/* ansi headers */
#include <errno.h>
/*#include <string.h>*/
/* library headers */
#include <blkio.h>
/* local headers */
#include "lseq_.h"
/*man---------------------------------------------------------------------------
NAME
lsinsert - insert record
SYNOPSIS
#include <lseq.h>
int lsinsert(lsp, buf)
lseq_t *lsp;
const void *buf;
DESCRIPTION
The lsinsert function creates a new record following the current
record and writes the data pointed to by buf into that record.
The cursor is set to the inserted record.
lsinsert will fail if one or more of the following is true:
[EINVAL] lsp is not a valid lseq pointer.
[EINVAL] buf is the NULL pointer.
[LSELOCK] lsp is not write locked.
[LSENOPEN] lsp is not open.
SEE ALSO
lsdelcur, lsinsert, lssearch.
DIAGNOSTICS
Upon successful completion, a value of 0 is returned. Otherwise,
a value of -1 is returned, and errno set to indicate the error.
------------------------------------------------------------------------------*/
int lsinsert(lseq_t *lsp, const void *buf)
{
int terrno = 0;
bpos_t bpos = NIL;
/* validate arguments */
if (!ls_valid(lsp) || buf == NULL) {
errno = EINVAL;
return -1;
}
/* check if not open */
if (!(lsp->flags & LSOPEN)) {
errno = LSENOPEN;
return -1;
}
/* check if not write locked */
if (!(lsp->flags & LSWRLCK)) {
errno = LSELOCK;
return -1;
}
/* build record to insert */
lsp->clsrp->prev = lsp->clspos;
if (lsp->clspos == NIL) {
lsp->clsrp->next = lsp->lshdr.first;
}
memcpy(lsp->clsrp->recbuf, buf, lsp->lshdr.recsize);
/* set modify bit in header */
lsp->lshdr.flags |= LSHMOD;
if (bputhf(lsp->bp, sizeof(bpos_t),
(void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
sizeof(lshdr_t) - sizeof(bpos_t)) == -1) {
LSEPRINT;
return -1;
}
if (bsync(lsp->bp) == -1) {
LSEPRINT;
return -1;
}
/* get empty block from free list */
if (bflpop(lsp->bp, &bpos) == -1) {
LSEPRINT;
return -1;
}
lsp->clspos = bpos;
/* write new record */
if (ls_rcput(lsp, lsp->clspos, lsp->clsrp) == -1) {
LSEPRINT;
terrno = errno;
bpos = lsp->clspos;
bflpush(lsp->bp, &bpos);
errno = terrno;
return -1;
}
/* increment record count */
lsp->lshdr.reccnt++;
/* fix links */
if (lsp->clsrp->next == NIL) {
lsp->lshdr.last = lsp->clspos;
} else {
if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->next, offsetof(lsrec_t, prev), &lsp->clspos, sizeof(lsp->clsrp->prev)) == -1) {
LSEPRINT;
terrno = errno;
bpos = lsp->clspos;
bflpush(lsp->bp, &bpos);
errno = terrno;
return -1;
}
}
if (lsp->clsrp->prev == NIL) {
lsp->lshdr.first = lsp->clspos;
} else {
if (bputbf(lsp->bp, (bpos_t)lsp->clsrp->prev, offsetof(lsrec_t, next), &lsp->clspos, sizeof(lsp->clsrp->next)) == -1) {
LSEPRINT;
terrno = errno;
bpos = lsp->clspos;
bflpush(lsp->bp, &bpos);
errno = terrno;
return -1;
}
}
/* clear modify bit in header */
lsp->lshdr.flags &= ~LSHMOD;
if (bputhf(lsp->bp, sizeof(bpos_t),
(void *)((char *)&lsp->lshdr + sizeof(bpos_t)),
sizeof(lshdr_t) - sizeof(bpos_t)) == -1) {
LSEPRINT;
return -1;
}
if (bsync(lsp->bp) == -1) {
LSEPRINT;
return -1;
}
errno = 0;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?