GnuCOBOL  2.0
A free COBOL compiler
isdelete.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 Trevor van Bremen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1,
7  * or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; see the file COPYING.LIB. If
16  * not, write to the Free Software Foundation, 51 Franklin Street, Fifth Floor
17  * Boston, MA 02110-1301 USA
18  */
19 
20 #include "isinternal.h"
21 
22 static char *pcwritebuffer;
23 
24 /* Local functions */
25 
26 static int
27 irowdelete (const int ihandle, off_t trownumber)
28 {
29  struct DICTINFO *psvbptr;
30  int ikeynumber, iresult;
31  off_t tdupnumber[MAXSUBS];
32 
33  psvbptr = psvbfile[ihandle];
34  /*
35  * Step 1:
36  * Check each index for existance of trownumber
37  */
38  for (ikeynumber = 0; ikeynumber < psvbptr->inkeys; ikeynumber++) {
39  if (psvbptr->pskeydesc[ikeynumber]->k_nparts == 0) {
40  continue;
41  }
42  if (ivbkeylocaterow (ihandle, ikeynumber, trownumber)) {
43  iserrno = EBADFILE;
44  return -1;
45  }
46  tdupnumber[ikeynumber] = psvbptr->pskeycurr[ikeynumber]->tdupnumber;
47  }
48 
49  /*
50  * Step 2:
51  * Perform the actual deletion from each index
52  */
53  for (ikeynumber = 0; ikeynumber < psvbptr->inkeys; ikeynumber++) {
54  if (psvbptr->pskeydesc[ikeynumber]->k_nparts == 0) {
55  continue;
56  }
57  iresult = ivbkeydelete (ihandle, ikeynumber);
58  if (iresult) {
59  iserrno = iresult;
60  return -1;
61  }
62  }
63 
64  return 0;
65 }
66 
67 static int
68 iprocessdelete (const int ihandle, off_t trownumber)
69 {
70  struct DICTINFO *psvbptr;
71  int ideleted;
72 
73  psvbptr = psvbfile[ihandle];
74  if (psvbptr->iopenmode & ISTRANS) {
75  iserrno = ivbdatalock (ihandle, VBWRLOCK, trownumber);
76  if (iserrno) {
77  return -1;
78  }
79  }
80  iserrno = ivbdataread (ihandle, psvbptr->ppcrowbuffer,
81  &ideleted, trownumber);
82  if (!iserrno && ideleted) {
83  iserrno = ENOREC;
84  }
85  if (iserrno) {
86  return -1;
87  }
88  if (irowdelete (ihandle, trownumber)) {
89  return -1;
90  }
91  if (!pcwritebuffer) {
93  if (!pcwritebuffer) {
94  iserrno = EBADMEM;
95  return -1;
96  }
97  }
98  iserrno = ivbdatawrite (ihandle, pcwritebuffer, 1, trownumber);
99  if (iserrno) {
100  return -1;
101  }
102  if (!(psvbptr->iopenmode & ISTRANS) || ivbintrans == VBNOTRANS
104  iserrno = ivbdatafree (ihandle, trownumber);
105  if (iserrno) {
106  return -1;
107  }
108  }
110  if (trownumber == psvbptr->trownumber) {
111  psvbptr->trownumber = 0;
112  }
113  ivbtransdelete (ihandle, trownumber, isreclen); /* BUG - retval */
114  return 0;
115 }
116 
117 /* Global functions */
118 
119 int
120 isdelete (const int ihandle, char *pcrow)
121 {
122  struct DICTINFO *psvbptr;
123  int iresult = 0;
124  unsigned char ckeyvalue[VB_MAX_KEYLEN];
125 
126  if (ivbenter (ihandle, 1, 0)) {
127  return -1;
128  }
129 
130  psvbptr = psvbfile[ihandle];
131  if (psvbptr->pskeydesc[0]->k_flags & ISDUPS) {
132  iserrno = ENOPRIM;
133  iresult = -1;
134  } else {
135  vvbmakekey (psvbptr->pskeydesc[0], pcrow, ckeyvalue);
136  iresult = ivbkeysearch (ihandle, ISEQUAL, 0, 0, ckeyvalue, (off_t)0);
137  switch (iresult) {
138  case 1: /* Exact match */
139  iresult =
140  iprocessdelete (ihandle, psvbptr->pskeycurr[0]->trownode);
141  break;
142 
143  case 0: /* LESS than */
144  case 2: /* EMPTY file */
145  iserrno = ENOREC;
146  iresult = -1;
147  break;
148 
149  default:
150  iserrno = EBADFILE;
151  iresult = -1;
152  break;
153  }
154  }
155 
156  if (iresult == 0) {
157  psvbptr->iisdictlocked |= 0x02;
158  }
159  iresult |= ivbexit (ihandle);
160  return iresult;
161 }
162 
163 int
164 isdelcurr (const int ihandle)
165 {
166  struct DICTINFO *psvbptr;
167  int iresult = 0;
168 
169  if (ivbenter (ihandle, 1, 0)) {
170  return -1;
171  }
172  psvbptr = psvbfile[ihandle];
173 
174  if (psvbptr->trownumber > 0) {
175  iresult = iprocessdelete (ihandle, psvbptr->trownumber);
176  } else {
177  iserrno = ENOREC;
178  iresult = -1;
179  }
180 
181  if (iresult == 0) {
182  psvbptr->iisdictlocked |= 0x02;
183  }
184  iresult |= ivbexit (ihandle);
185  return iresult;
186 }
187 
188 int
189 isdelrec (const int ihandle, vbisam_off_t trownumber)
190 {
191  struct DICTINFO *psvbptr;
192  int iresult = 0;
193 
194  if (ivbenter (ihandle, 1, 0)) {
195  return -1;
196  }
197  psvbptr = psvbfile[ihandle];
198 
199  if (trownumber > 0) {
200  iresult = iprocessdelete (ihandle, trownumber);
201  } else {
202  iserrno = ENOREC;
203  iresult = -1;
204  }
205 
206  if (iresult == 0) {
207  psvbptr->iisdictlocked |= 0x02;
208  }
209  iresult |= ivbexit (ihandle);
210  return iresult;
211 }
int inkeys
Definition: isinternal.h:400
int ivbtransdelete(const int ihandle, off_t trownumber, int irowlength)
Definition: istrans.c:698
int ivbdatafree(const int ihandle, const off_t trownumber)
Definition: vbindexio.c:150
#define VBCOMMIT
Definition: isinternal.h:309
int ivbkeysearch(const int ihandle, const int imode, const int ikeynumber, int ilength, unsigned char *pckeyvalue, off_t tdupnumber)
Definition: vbkeysio.c:372
int isdelrec(const int ihandle, long long trownumber)
Definition: isdelete.c:189
int isreclen
Definition: vbmemio.c:29
int ivbkeylocaterow(const int ihandle, const int ikeynumber, off_t trownumber)
Definition: vbkeysio.c:435
int ivbdatawrite(const int ihandle, char *pcbuffer, int ideletedrow, const off_t trownumber)
Definition: vbdataio.c:611
int ivbenter(const int ihandle, const unsigned int imodifying, const unsigned int ispecial)
Definition: vblocking.c:178
#define VBNOTRANS
Definition: isinternal.h:306
int ivbkeydelete(const int ihandle, const int ikeynumber)
Definition: vbkeysio.c:777
unsigned char iisdictlocked
Definition: isinternal.h:427
off_t tdupnumber
Definition: isinternal.h:417
int isdelcurr(const int ihandle)
Definition: isdelete.c:164
int ivbdatalock(const int ihandle, const int imode, const off_t trownumber)
Definition: vblocking.c:432
struct VBKEY * pskeycurr[32]
Definition: isinternal.h:448
int isdelete(const int ihandle, char *pcrow)
Definition: isdelete.c:120
off_t trownode
Definition: isinternal.h:328
void vvbmakekey(const struct keydesc *pskeydesc, char *pcrow_buffer, unsigned char *pckeyvalue)
Definition: vbkeysio.c:212
int ivbdataread(const int ihandle, char *pcbuffer, int *pideletedrow, const off_t trownumber)
Definition: vbdataio.c:493
void * pvvbmalloc(const size_t size)
Definition: vbmemio.c:45
#define VBWRLOCK
Definition: isinternal.h:297
struct DICTINFO * psvbfile[128+1]
Definition: vblowlevel.c:23
off_t tdupnumber
Definition: isinternal.h:329
int ivbexit(const int ihandle)
Definition: vblocking.c:290
int iopenmode
Definition: isinternal.h:412
#define MAX_RESERVED_LENGTH
Definition: isinternal.h:290
#define VBROLLBACK
Definition: isinternal.h:310
struct keydesc * pskeydesc[32]
Definition: isinternal.h:445
off_t trownumber
Definition: isinternal.h:416
static int irowdelete(const int ihandle, off_t trownumber)
Definition: isdelete.c:27
char * ppcrowbuffer
Definition: isinternal.h:423
static int iprocessdelete(const int ihandle, off_t trownumber)
Definition: isdelete.c:68
int iserrno
Definition: vbmemio.c:27
int isrecnum
Definition: vbmemio.c:30
#define MAXSUBS
Definition: isinternal.h:119
int ivbintrans
Definition: istrans.c:23
static char * pcwritebuffer
Definition: isdelete.c:22