GnuCOBOL  2.0
A free COBOL compiler
iswrite.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 int
23 irowinsert (const int ihandle, char *pcrow_buffer, off_t trownumber)
24 {
25  struct VBKEY *pskey;
26  struct DICTINFO *psvbptr;
27  struct keydesc *pskptr;
28  off_t tdupnumber[MAXSUBS];
29  int ikeynumber, iresult;
30  unsigned char ckeyvalue[VB_MAX_KEYLEN];
31 
32  psvbptr = psvbfile[ihandle];
33  /*
34  * Step 1:
35  * Check each index for a potential ISNODUPS error (EDUPL)
36  * Also, calculate the duplicate number as needed
37  */
38  for (ikeynumber = 0; ikeynumber < psvbptr->inkeys; ikeynumber++) {
39  pskptr = psvbptr->pskeydesc[ikeynumber];
40  if (pskptr->k_nparts == 0) {
41  continue;
42  }
43  vvbmakekey (psvbptr->pskeydesc[ikeynumber], pcrow_buffer, ckeyvalue);
44  iresult = ivbkeysearch (ihandle, ISGREAT, ikeynumber, 0, ckeyvalue, (off_t)0);
45  tdupnumber[ikeynumber] = 0;
46  if (iresult >= 0 && !ivbkeyload (ihandle, ikeynumber, ISPREV, 0, &pskey)
47  && !memcmp (pskey->ckey, ckeyvalue, (size_t)pskptr->k_len)) {
48  iserrno = EDUPL;
49  if (pskptr->k_flags & ISDUPS) {
50  tdupnumber[ikeynumber] = pskey->tdupnumber + 1;
51  } else {
52  return -1;
53  }
54  }
55  iresult = ivbkeysearch (ihandle, ISGTEQ, ikeynumber, 0, ckeyvalue,
56  tdupnumber[ikeynumber]);
57  }
58 
59  /* Step 2: Perform the actual insertion into each index */
60  for (ikeynumber = 0; ikeynumber < psvbptr->inkeys; ikeynumber++) {
61  if (psvbptr->pskeydesc[ikeynumber]->k_nparts == 0) {
62  continue;
63  }
64  vvbmakekey (psvbptr->pskeydesc[ikeynumber], pcrow_buffer, ckeyvalue);
65  iresult = ivbkeyinsert (ihandle, NULL, ikeynumber, ckeyvalue,
66  trownumber, tdupnumber[ikeynumber], NULL);
67  if (iresult) {
68 /* BUG - do something SANE here */
69  /* Eeek, an error occured. Let's remove what we added */
70  /* while (ikeynumber >= 0) */
71  /* { */
72  /* ivbkeydelete (ihandle, ikeynumber); */
73  /* ikeynumber--; */
74  /* } */
75  return iresult;
76  }
77  }
78 
79  return 0;
80 }
81 
82 /* Global functions */
83 
84 int
85 ivbwriterow (const int ihandle, char *pcrow, const off_t trownumber)
86 {
87  struct DICTINFO *psvbptr;
88  int iresult = 0;
89 
90  psvbptr = psvbfile[ihandle];
92  if (psvbptr->iopenmode & ISTRANS) {
93  iserrno = ivbdatalock (ihandle, VBWRLOCK, trownumber);
94  if (iserrno) {
95  return -1;
96  }
97  }
98  iresult = irowinsert (ihandle, pcrow, trownumber);
99  if (!iresult) {
100  iserrno = 0;
101  psvbptr->tvarlennode = 0; /* Stop it from removing */
102  iresult = ivbdatawrite (ihandle, (void *)pcrow, 0, trownumber);
103  if (iresult) {
104  iserrno = iresult;
105  if (psvbptr->iopenmode & ISTRANS) {
106  ivbdatalock (ihandle, VBUNLOCK, trownumber);
107  }
108  return -1;
109  }
110  if (psvbptr->iopenmode & ISVARLEN) {
111  iresult = ivbtransinsert (ihandle, trownumber,
112  isreclen, pcrow);
113  } else {
114  iresult = ivbtransinsert (ihandle, trownumber,
115  psvbptr->iminrowlength, pcrow);
116  }
117  }
118  return iresult;
119 }
120 
121 int
122 iswrcurr (const int ihandle, char *pcrow)
123 {
124  struct DICTINFO *psvbptr;
125  off_t trownumber;
126  int iresult;
127 
128  if (ivbenter (ihandle, 1, 0)) {
129  return -1;
130  }
131  psvbptr = psvbfile[ihandle];
132 
133  if ((psvbptr->iopenmode & ISVARLEN) && (isreclen > psvbptr->imaxrowlength
134  || isreclen < psvbptr->iminrowlength)) {
135  iserrno = EBADARG;
136  return -1;
137  }
138 
139  trownumber = tvbdataallocate (ihandle);
140  if (trownumber == -1) {
141  return -1;
142  }
143 
144  iresult = ivbwriterow (ihandle, pcrow, trownumber);
145  if (!iresult) {
146  psvbptr->trownumber = trownumber;
147  } else {
148  ivbdatafree (ihandle, trownumber);
149  }
150 
151  ivbexit (ihandle);
152  return iresult;
153 }
154 
155 int
156 iswrite (const int ihandle, char *pcrow)
157 {
158  struct DICTINFO *psvbptr;
159  off_t trownumber;
160  int iresult, isaveerror;
161 
162  if (ivbenter (ihandle, 1, 0)) {
163  return -1;
164  }
165  psvbptr = psvbfile[ihandle];
166 
167  if ((psvbptr->iopenmode & ISVARLEN) && (isreclen > psvbptr->imaxrowlength
168  || isreclen < psvbptr->iminrowlength)) {
169  iserrno = EBADARG;
170  return -1;
171  }
172 
173  trownumber = tvbdataallocate (ihandle);
174  if (trownumber == -1) {
175  return -1;
176  }
177 
178  iresult = ivbwriterow (ihandle, pcrow, trownumber);
179  if (iresult) {
180  isaveerror = iserrno;
181  ivbdatafree (ihandle, trownumber);
182  iserrno = isaveerror;
183  }
184 
185  ivbexit (ihandle);
186  return iresult;
187 }
int inkeys
Definition: isinternal.h:400
int imaxrowlength
Definition: isinternal.h:404
int iswrcurr(const int ihandle, char *pcrow)
Definition: iswrite.c:122
int ivbdatafree(const int ihandle, const off_t trownumber)
Definition: vbindexio.c:150
int ivbkeysearch(const int ihandle, const int imode, const int ikeynumber, int ilength, unsigned char *pckeyvalue, off_t tdupnumber)
Definition: vbkeysio.c:372
unsigned char ckey[1]
Definition: isinternal.h:334
int isreclen
Definition: vbmemio.c:29
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
int ivbwriterow(const int ihandle, char *pcrow, const off_t trownumber)
Definition: iswrite.c:85
int ivbkeyload(const int ihandle, const int ikeynumber, const int imode, const int isetcurr, struct VBKEY **ppskey)
Definition: vbkeysio.c:503
int ivbtransinsert(const int ihandle, const off_t trownumber, int irowlength, char *pcrow)
Definition: istrans.c:878
int iminrowlength
Definition: isinternal.h:403
int iswrite(const int ihandle, char *pcrow)
Definition: iswrite.c:156
off_t tvarlennode
Definition: isinternal.h:421
off_t tvbdataallocate(const int ihandle)
Definition: vbindexio.c:284
EC ARGUMENT EC EC BOUND EC BOUND EC BOUND EC BOUND TABLE EC DATA EC DATA EC DATA PTR NULL
Definition: exception.def:95
int ivbdatalock(const int ihandle, const int imode, const off_t trownumber)
Definition: vblocking.c:432
void vvbmakekey(const struct keydesc *pskeydesc, char *pcrow_buffer, unsigned char *pckeyvalue)
Definition: vbkeysio.c:212
#define VBWRLOCK
Definition: isinternal.h:297
struct DICTINFO * psvbfile[128+1]
Definition: vblowlevel.c:23
off_t tdupnumber
Definition: isinternal.h:329
int ivbkeyinsert(const int ihandle, struct VBTREE *pstree, const int ikeynumber, unsigned char *pckeyvalue, off_t trownode, off_t tdupnumber, struct VBTREE *pschild)
Definition: vbkeysio.c:727
int ivbexit(const int ihandle)
Definition: vblocking.c:290
int iopenmode
Definition: isinternal.h:412
static int irowinsert(const int ihandle, char *pcrow_buffer, off_t trownumber)
Definition: iswrite.c:23
#define VBUNLOCK
Definition: isinternal.h:294
struct keydesc * pskeydesc[32]
Definition: isinternal.h:445
off_t trownumber
Definition: isinternal.h:416
int iserrno
Definition: vbmemio.c:27
int isrecnum
Definition: vbmemio.c:30
#define MAXSUBS
Definition: isinternal.h:119