GnuCOBOL  2.0
A free COBOL compiler
ishelper.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 /* Global functions */
23 
24 int
25 iscluster (const int ihandle, struct keydesc *pskeydesc)
26 {
27  /* BUG Write iscluster() and don't forget to call ivbtranscluster */
28  if (ihandle < 0 || ihandle > ivbmaxusedhandle) {
29  iserrno = EBADARG;
30  return -1;
31  }
32  return 0;
33 }
34 
35 int
36 iserase (char *pcfilename)
37 {
38  int ihandle;
39  char cbuffer[1024];
40 
41  for (ihandle = 0; ihandle <= ivbmaxusedhandle; ihandle++) {
42  if (psvbfile[ihandle] != NULL) {
43  if (!strcmp (psvbfile[ihandle]->cfilename, pcfilename)) {
44  isclose (ihandle);
45  ivbclose3 (ihandle);
46  break;
47  }
48  }
49  }
50  sprintf (cbuffer, "%s.idx", pcfilename);
51  unlink (cbuffer);
52  sprintf (cbuffer, "%s.dat", pcfilename);
53  unlink (cbuffer);
54  return ivbtranserase (pcfilename);
55 }
56 
57 int
58 isflush (const int ihandle)
59 {
60  struct DICTINFO *psvbptr;
61 
62  if (ihandle < 0 || ihandle > ivbmaxusedhandle) {
63  iserrno = EBADARG;
64  return -1;
65  }
66  psvbptr = psvbfile[ihandle];
67  if (!psvbptr || psvbptr->iisopen) {
68  iserrno = ENOTOPEN;
69  return -1;
70  }
71  if (psvbptr->iindexhandle >= 0) {
72  fsync (psvbptr->iindexhandle);
73  }
74  if (psvbptr->idatahandle >= 0) {
75  fsync (psvbptr->idatahandle);
76  }
77  return 0;
78 }
79 
80 int
81 islock (const int ihandle)
82 {
83  struct DICTINFO *psvbptr;
84 
85  if (ihandle < 0 || ihandle > ivbmaxusedhandle) {
86  iserrno = EBADARG;
87  return -1;
88  }
89  psvbptr = psvbfile[ihandle];
90  if (!psvbptr || psvbptr->iisopen) {
91  iserrno = ENOTOPEN;
92  return -1;
93  }
94  return ivbdatalock (ihandle, VBWRLOCK, (off_t)0);
95 }
96 
97 int
98 isrelcurr (const int ihandle)
99 {
100  struct DICTINFO *psvbptr;
101 
102  if (ihandle < 0 || ihandle > ivbmaxusedhandle) {
103  iserrno = EBADARG;
104  return -1;
105  }
106  psvbptr = psvbfile[ihandle];
107  if (!psvbptr || psvbptr->iisopen) {
108  iserrno = ENOTOPEN;
109  return -1;
110  }
111  if (ivbintrans != VBNOTRANS) {
112  return 0;
113  }
114  if (!psvbptr->trownumber) {
115  iserrno = ENOREC;
116  return -1;
117  }
118  iserrno = ivbdatalock (ihandle, VBUNLOCK, psvbptr->trownumber);
119  if (iserrno) {
120  return -1;
121  }
122  return 0;
123 }
124 
125 int
126 isrelease (const int ihandle)
127 {
128  struct DICTINFO *psvbptr;
129 
130  if (ihandle < 0 || ihandle > ivbmaxusedhandle) {
131  iserrno = EBADARG;
132  return -1;
133  }
134  psvbptr = psvbfile[ihandle];
135  if (!psvbptr || psvbptr->iisopen) {
136  iserrno = ENOTOPEN;
137  return -1;
138  }
139  if (ivbintrans != VBNOTRANS) {
140  return 0;
141  }
142  ivbdatalock (ihandle, VBUNLOCK, (off_t)0); /* Ignore the return */
143  return 0;
144 }
145 
146 int
147 isrelrec (const int ihandle, const vbisam_off_t trownumber)
148 {
149  struct DICTINFO *psvbptr;
150 
151  if (ihandle < 0 || ihandle > ivbmaxusedhandle) {
152  iserrno = EBADARG;
153  return -1;
154  }
155  psvbptr = psvbfile[ihandle];
156  if (!psvbptr || psvbptr->iisopen) {
157  iserrno = ENOTOPEN;
158  return -1;
159  }
160  iserrno = ivbdatalock (ihandle, VBUNLOCK, trownumber);
161  if (iserrno) {
162  return -1;
163  }
164  return 0;
165 }
166 
167 int
168 isrename (char *pcoldname, char *pcnewname)
169 {
170  int iresult;
171  char cbuffer[2][1024];
172 
173  sprintf (cbuffer[0], "%s.idx", pcoldname);
174  sprintf (cbuffer[1], "%s.idx", pcnewname);
175  iresult = rename (cbuffer[0], cbuffer[1]);
176  if (iresult == -1) {
177  goto renameexit;
178  }
179  sprintf (cbuffer[0], "%s.dat", pcoldname);
180  sprintf (cbuffer[1], "%s.dat", pcnewname);
181  iresult = rename (cbuffer[0], cbuffer[1]);
182  if (iresult == -1) {
183  sprintf (cbuffer[0], "%s.idx", pcoldname);
184  sprintf (cbuffer[1], "%s.idx", pcnewname);
185  rename (cbuffer[1], cbuffer[0]);
186  goto renameexit;
187  }
188  return ivbtransrename (pcoldname, pcnewname);
189 renameexit:
190  iserrno = errno;
191  return -1;
192 }
193 
194 int
195 issetunique (const int ihandle, const vbisam_off_t tuniqueid)
196 {
197  struct DICTINFO *psvbptr;
198  off_t tvalue;
199  int iresult, iresult2;
200 
201  if (ivbenter (ihandle, 1, 0)) {
202  return -1;
203  }
204  psvbptr = psvbfile[ihandle];
205  iserrno = 0;
206  if (!psvbptr->iisdictlocked) {
207  iserrno = EBADARG;
208  return -1;
209  }
210  tvalue = inl_ldquad (psvbptr->sdictnode.cuniqueid);
211  if (tuniqueid > tvalue) {
212  inl_stquad (tuniqueid, psvbptr->sdictnode.cuniqueid);
213  psvbptr->iisdictlocked |= 0x02;
214  }
215 
216  iresult = ivbtranssetunique (ihandle, tuniqueid);
217  psvbptr->iisdictlocked |= 0x02;
218  iresult2 = ivbexit (ihandle);
219  if (iresult) {
220  return -1;
221  }
222  return iresult2;
223 }
224 
225 int
226 isuniqueid (const int ihandle, vbisam_off_t *ptuniqueid)
227 {
228  struct DICTINFO *psvbptr;
229  off_t tvalue;
230  int iresult;
231  int iresult2;
232 
233  if (ivbenter (ihandle, 1, 0)) {
234  return -1;
235  }
236 
237  psvbptr = psvbfile[ihandle];
238  iserrno = 0;
239  if (!psvbptr->iisdictlocked) {
240  iserrno = EBADARG;
241  return -1;
242  }
243  tvalue = inl_ldquad (psvbptr->sdictnode.cuniqueid);
244  inl_stquad (tvalue + 1, psvbptr->sdictnode.cuniqueid);
245  psvbptr->iisdictlocked |= 0x02;
246  iresult = ivbtransuniqueid (ihandle, tvalue);
247  iresult2 = ivbexit (ihandle);
248  if (iresult) {
249  return -1;
250  }
251  *ptuniqueid = tvalue;
252  return iresult2;
253 }
254 
255 int
256 isunlock (const int ihandle)
257 {
258  struct DICTINFO *psvbptr;
259 
260  if (unlikely(ihandle < 0 || ihandle > ivbmaxusedhandle)) {
261  iserrno = EBADARG;
262  return -1;
263  }
264  psvbptr = psvbfile[ihandle];
265  if (!psvbptr || psvbptr->iisopen) {
266  iserrno = ENOTOPEN;
267  return -1;
268  }
269  return ivbdatalock (ihandle, VBUNLOCK, (off_t)0);
270 }
271 
272 char *
273 isdi_name (const int ihandle)
274 {
275  struct DICTINFO *psvbptr;
276 
277  if (unlikely(ihandle < 0 || ihandle > ivbmaxusedhandle)) {
278  iserrno = EBADARG;
279  return NULL;
280  }
281  psvbptr = psvbfile[ihandle];
282  if (!psvbptr || psvbptr->iisopen) {
283  iserrno = ENOTOPEN;
284  return NULL;
285  }
286  return strdup (psvbptr->cfilename);
287 }
288 
289 int
290 isdi_datlen (const int ihandle)
291 {
292  struct DICTINFO *psvbptr;
293 
294  if (unlikely(ihandle < 0 || ihandle > ivbmaxusedhandle)) {
295  iserrno = EBADARG;
296  return -1;
297  }
298  psvbptr = psvbfile[ihandle];
299  if (!psvbptr || psvbptr->iisopen) {
300  iserrno = ENOTOPEN;
301  return -1;
302  }
303  return psvbptr->imaxrowlength;
304 }
305 
306 int
307 isdi_idxfd (const int ihandle)
308 {
309  struct DICTINFO *psvbptr;
310 
311  if (unlikely(ihandle < 0 || ihandle > ivbmaxusedhandle)) {
312  iserrno = EBADARG;
313  return -1;
314  }
315  psvbptr = psvbfile[ihandle];
316  if (!psvbptr || psvbptr->iisopen) {
317  iserrno = ENOTOPEN;
318  return -1;
319  }
320  return psvbptr->iindexhandle;
321 }
322 
323 int
324 isdi_datfd (const int ihandle)
325 {
326  struct DICTINFO *psvbptr;
327 
328  if (unlikely(ihandle < 0 || ihandle > ivbmaxusedhandle)) {
329  iserrno = EBADARG;
330  return -1;
331  }
332  psvbptr = psvbfile[ihandle];
333  if (!psvbptr || psvbptr->iisopen) {
334  iserrno = ENOTOPEN;
335  return -1;
336  }
337  return psvbptr->idatahandle;
338 }
339 
340 int
341 isdi_curidx (const int ihandle)
342 {
343  struct DICTINFO *psvbptr;
344 
345  if (unlikely(ihandle < 0 || ihandle > ivbmaxusedhandle)) {
346  iserrno = EBADARG;
347  return -1;
348  }
349  psvbptr = psvbfile[ihandle];
350  if (!psvbptr || psvbptr->iisopen) {
351  iserrno = ENOTOPEN;
352  return -1;
353  }
354  return psvbptr->iactivekey;
355 }
356 
357 struct keydesc *
358 isdi_kdsc (const int ihandle)
359 {
360  struct DICTINFO *psvbptr;
361  struct keydesc *keydptr;
362 
363  if (unlikely(ihandle < 0 || ihandle > ivbmaxusedhandle)) {
364  iserrno = EBADARG;
365  return NULL;
366  }
367  psvbptr = psvbfile[ihandle];
368  if (!psvbptr || psvbptr->iisopen) {
369  iserrno = ENOTOPEN;
370  return NULL;
371  }
372  keydptr = pvvbmalloc (sizeof(struct keydesc));
373  if (keydptr) {
374  memcpy (keydptr, psvbptr->pskeydesc[psvbptr->iactivekey],
375  sizeof(struct keydesc));
376  }
377  return keydptr;
378 }
379 
380 void
381 ldchar (char *pcsource, int ilength, char *pcdestination)
382 {
383  char *pcdst;
384 
385  memcpy ((void *)pcdestination, (void *)pcsource, (size_t)ilength);
386  for (pcdst = pcdestination + ilength - 1; pcdst >= (char *)pcdestination; pcdst--) {
387  if (*pcdst != ' ') {
388  pcdst++;
389  *pcdst = 0;
390  return;
391  }
392  }
393  *(++pcdst) = 0;
394 }
395 
396 void
397 stchar (char *pcsource, char *pcdestination, int ilength)
398 {
399  char *pcsrc, *pcdst;
400  int icount;
401 
402  pcsrc = pcsource;
403  pcdst = pcdestination;
404  for (icount = ilength; icount && *pcsrc; icount--, pcsrc++, pcdst++) {
405  *pcdst = *pcsrc;
406  }
407  for (; icount; icount--, pcdst++) {
408  *pcdst = ' ';
409  }
410 }
411 
412 int
413 ldint (void *pclocation)
414 {
415 #ifndef WORDS_BIGENDIAN
416  return (int)VB_BSWAP_16 (*(unsigned short *)pclocation);
417 #else
418  short ivalue = 0;
419  unsigned char *pctemp = (unsigned char *)&ivalue;
420  unsigned char *pctemp2 = (unsigned char *)pclocation;
421 
422  *(pctemp + 0) = *(pctemp2 + 0);
423  *(pctemp + 1) = *(pctemp2 + 1);
424  return (int)ivalue;
425 #endif
426 }
427 
428 void
429 stint (int ivalue, void *pclocation)
430 {
431 #ifndef WORDS_BIGENDIAN
432  *(unsigned short *)pclocation = VB_BSWAP_16 ((unsigned short)ivalue);
433 #else
434  unsigned char *pctemp = (unsigned char *)&ivalue;
435 
436  *((unsigned char *)pclocation + 0) = *(pctemp + 0 + INTSIZE);
437  *((unsigned char *)pclocation + 1) = *(pctemp + 1 + INTSIZE);
438 #endif
439 }
440 
441 int
442 ldlong (void *pclocation)
443 {
444 #ifndef WORDS_BIGENDIAN
445  return VB_BSWAP_32 (*(unsigned int *)pclocation);
446 #else
447  int lvalue;
448 
449  memcpy ((unsigned char *)&lvalue, (unsigned char *)pclocation, 4);
450  return lvalue;
451 #endif
452 }
453 
454 void
455 stlong (int lvalue, void *pclocation)
456 {
457 #ifndef WORDS_BIGENDIAN
458  *(unsigned int *)pclocation = VB_BSWAP_32 ((unsigned int)lvalue);
459 #else
460  memcpy ((unsigned char *)pclocation, (unsigned char *)&lvalue, 4);
461 #endif
462 }
463 
464 double
465 ldfloat (void *pclocation)
466 {
467  float ffloat;
468  double ddouble;
469 
470  memcpy (&ffloat, pclocation, FLOATSIZE);
471  ddouble = ffloat;
472  return (double)ddouble;
473 }
474 
475 void
476 stfloat (double dsource, void *pcdestination)
477 {
478  float ffloat;
479 
480  ffloat = dsource;
481  memcpy (pcdestination, &ffloat, FLOATSIZE);
482 }
483 
484 double
485 ldfltnull (void *pclocation, short *pinullflag)
486 {
487  double dvalue;
488 
489  *pinullflag = 0;
490  dvalue = ldfloat (pclocation);
491  return (double)dvalue;
492 }
493 
494 void
495 stfltnull (double dsource, void *pcdestination, int inullflag)
496 {
497  if (inullflag) {
498  dsource = 0;
499  }
500  stfloat (dsource, pcdestination);
501 }
502 
503 double
504 lddbl (void *pclocation)
505 {
506  double ddouble;
507 
508  memcpy (&ddouble, pclocation, DOUBLESIZE);
509  return ddouble;
510 }
511 
512 void
513 stdbl (double dsource, void *pcdestination)
514 {
515  memcpy (pcdestination, &dsource, DOUBLESIZE);
516 }
517 
518 double
519 lddblnull (void *pclocation, short *pinullflag)
520 {
521  *pinullflag = 0;
522  return (lddbl (pclocation));
523 }
524 
525 void
526 stdblnull (double dsource, void *pcdestination, int inullflag)
527 {
528  if (inullflag) {
529  dsource = 0;
530  }
531  stdbl (dsource, pcdestination);
532 }
int isdi_curidx(const int ihandle)
Definition: ishelper.c:341
int ivbtranserase(const char *pcfilename)
Definition: istrans.c:777
int imaxrowlength
Definition: isinternal.h:404
int isdi_idxfd(const int ihandle)
Definition: ishelper.c:307
void ldchar(char *pcsource, int ilength, char *pcdestination)
Definition: ishelper.c:381
int ivbtranssetunique(const int ihandle, const off_t tuniqueid)
Definition: istrans.c:948
int islock(const int ihandle)
Definition: ishelper.c:81
double lddblnull(void *pclocation, short *pinullflag)
Definition: ishelper.c:519
void stdbl(double dsource, void *pcdestination)
Definition: ishelper.c:513
int isrelcurr(const int ihandle)
Definition: ishelper.c:98
void stlong(int lvalue, void *pclocation)
Definition: ishelper.c:455
int isrename(char *pcoldname, char *pcnewname)
Definition: ishelper.c:168
int isuniqueid(const int ihandle, long long *ptuniqueid)
Definition: ishelper.c:226
int isrelrec(const int ihandle, const long long trownumber)
Definition: ishelper.c:147
void stdblnull(double dsource, void *pcdestination, int inullflag)
Definition: ishelper.c:526
int iserase(char *pcfilename)
Definition: ishelper.c:36
int iindexhandle
Definition: isinternal.h:406
char cuniqueid[8]
Definition: isinternal.h:378
int ivbenter(const int ihandle, const unsigned int imodifying, const unsigned int ispecial)
Definition: vblocking.c:178
int ivbtransrename(char *pcoldname, char *pcnewname)
Definition: istrans.c:914
int isdi_datfd(const int ihandle)
Definition: ishelper.c:324
#define VBNOTRANS
Definition: isinternal.h:306
char * cfilename
Definition: isinternal.h:422
void ivbclose3(const int ihandle)
Definition: isopen.c:125
struct keydesc * isdi_kdsc(const int ihandle)
Definition: ishelper.c:358
int ivbmaxusedhandle
Definition: vblocking.c:26
int iisopen
Definition: isinternal.h:407
double ldfltnull(void *pclocation, short *pinullflag)
Definition: ishelper.c:485
void stchar(char *pcsource, char *pcdestination, int ilength)
Definition: ishelper.c:397
int isunlock(const int ihandle)
Definition: ishelper.c:256
#define unlikely(x)
Definition: common.h:437
void stfloat(double dsource, void *pcdestination)
Definition: ishelper.c:476
unsigned char iisdictlocked
Definition: isinternal.h:427
#define VB_BSWAP_32(val)
Definition: byteswap.h:185
EC ARGUMENT EC EC BOUND EC BOUND EC BOUND EC BOUND TABLE EC DATA EC DATA EC DATA PTR NULL
Definition: exception.def:95
double lddbl(void *pclocation)
Definition: ishelper.c:504
int ivbdatalock(const int ihandle, const int imode, const off_t trownumber)
Definition: vblocking.c:432
struct DICTNODE sdictnode
Definition: isinternal.h:444
int ivbtransuniqueid(const int ihandle, const off_t tuniqueid)
Definition: istrans.c:981
void stfltnull(double dsource, void *pcdestination, int inullflag)
Definition: ishelper.c:495
char * isdi_name(const int ihandle)
Definition: ishelper.c:273
void * pvvbmalloc(const size_t size)
Definition: vbmemio.c:45
#define VBWRLOCK
Definition: isinternal.h:297
double ldfloat(void *pclocation)
Definition: ishelper.c:465
int ldlong(void *pclocation)
Definition: ishelper.c:442
struct DICTINFO * psvbfile[128+1]
Definition: vblowlevel.c:23
int issetunique(const int ihandle, const long long tuniqueid)
Definition: ishelper.c:195
int iactivekey
Definition: isinternal.h:401
#define VB_BSWAP_16(val)
Definition: byteswap.h:184
int iscluster(const int ihandle, struct keydesc *pskeydesc)
Definition: ishelper.c:25
int ivbexit(const int ihandle)
Definition: vblocking.c:290
int isclose(const int ihandle)
Definition: isopen.c:182
static void inl_stquad(off_t tvalue, void *pclocation)
Definition: isinternal.h:260
int isflush(const int ihandle)
Definition: ishelper.c:58
int isdi_datlen(const int ihandle)
Definition: ishelper.c:290
int ldint(void *pclocation)
Definition: ishelper.c:413
#define VBUNLOCK
Definition: isinternal.h:294
void stint(int ivalue, void *pclocation)
Definition: ishelper.c:429
struct keydesc * pskeydesc[32]
Definition: isinternal.h:445
static off_t inl_ldquad(void *pclocation)
Definition: isinternal.h:238
int isrelease(const int ihandle)
Definition: ishelper.c:126
off_t trownumber
Definition: isinternal.h:416
int idatahandle
Definition: isinternal.h:405
int iserrno
Definition: vbmemio.c:27
int ivbintrans
Definition: istrans.c:23