/* perform export command */ int doexport(const char *name, int bin){ CURIA *curia; char *kbuf, *vbuf, *tmp; int err, ksiz, vsiz; /* open a database */ if(!(curia = cropen(name, CR_OREADER, -1, -1))){ pdperror(name); return 1; } /* initialize the iterator */ criterinit(curia); /* loop for each key */ err = FALSE; while((kbuf = criternext(curia, &ksiz)) != NULL){ /* retrieve a value with a key */ if(!(vbuf = crget(curia, kbuf, ksiz, 0, -1, &vsiz))){ pdperror(name); free(kbuf); err = TRUE; break; } /* output data */ if(bin){ tmp = cbbaseencode(kbuf, ksiz); printf("%s\t", tmp); free(tmp); tmp = cbbaseencode(vbuf, vsiz); printf("%s\n", tmp); free(tmp); } else { printf("%s\t%s\n", kbuf, vbuf); } /* free resources */ free(vbuf); free(kbuf); } /* check whether all records were retrieved */ if(dpecode != DP_ENOITEM){ pdperror(name); err = TRUE; } /* close the database */ if(!crclose(curia)){ pdperror(name); return 1; } return err ? 1 : 0; }
/* perform import command */ int doimport(const char *name, int bnum, int dnum, int bin){ CURIA *curia; char *buf, *kbuf, *vbuf, *ktmp, *vtmp; int i, err, ktsiz, vtsiz; /* open a database */ if(!(curia = cropen(name, CR_OWRITER | CR_OCREAT, bnum, dnum))){ pdperror(name); return 1; } /* loop for each line */ err = FALSE; for(i = 1; (buf = getl()) != NULL; i++){ kbuf = buf; if((vbuf = strchr(buf, '\t')) != NULL){ *vbuf = '\0'; vbuf++; /* store a record */ if(bin){ ktmp = cbbasedecode(kbuf, &ktsiz); vtmp = cbbasedecode(vbuf, &vtsiz); if(!crput(curia, ktmp, ktsiz, vtmp, vtsiz, CR_DOVER)){ pdperror(name); err = TRUE; } free(vtmp); free(ktmp); } else { if(!crput(curia, kbuf, -1, vbuf, -1, CR_DOVER)){ pdperror(name); err = TRUE; } } } else { fprintf(stderr, "%s: %s: invalid format in line %d\n", progname, name, i); } free(buf); if(err) break; } /* close the database */ if(!crclose(curia)){ pdperror(name); return 1; } return err ? 1 : 0; }
P0(PUBLIC pascal trap, LONGINT, UnloadScrap) { OSErr retval; INTEGER f; LONGINT l = Cx(ScrapSize); if (Cx(ScrapState) > 0) { retval = cropen(&f); if (retval != noErr) /*-->*/ return(retval); HLock(MR(ScrapHandle)); retval = FSWriteAll(f, &l, STARH(MR(ScrapHandle))); HUnlock(MR(ScrapHandle)); if (retval != noErr) /*-->*/ return(retval); retval = FSClose(f); if (retval != noErr) /*-->*/ return(retval); ScrapState = 0; } return noErr; }
/* Get a database handle after the fashion of QDBM. */ GDBM_FILE gdbm_open2(char *name, int read_write, int mode, int bnum, int dnum, int align){ GDBM_FILE dbf; int dpomode, cromode, flags, fd; struct stat sbuf; DEPOT *depot; CURIA *curia; assert(name); dpomode = DP_OREADER; cromode = CR_OREADER; flags = O_RDONLY; if(read_write & GDBM_READER){ dpomode = DP_OREADER; cromode = CR_OREADER; if(read_write & GDBM_NOLOCK){ dpomode |= DP_ONOLCK; cromode |= CR_ONOLCK; } if(read_write & GDBM_LOCKNB){ dpomode |= DP_OLCKNB; cromode |= CR_OLCKNB; } flags = O_RDONLY; } else if(read_write & GDBM_WRITER){ dpomode = DP_OWRITER; cromode = CR_OWRITER; if(read_write & GDBM_NOLOCK){ dpomode |= DP_ONOLCK; cromode |= CR_ONOLCK; } if(read_write & GDBM_LOCKNB){ dpomode |= DP_OLCKNB; cromode |= CR_OLCKNB; } flags = O_RDWR; } else if(read_write & GDBM_WRCREAT){ dpomode = DP_OWRITER | DP_OCREAT; cromode = CR_OWRITER | CR_OCREAT; if(read_write & GDBM_NOLOCK){ dpomode |= DP_ONOLCK; cromode |= CR_ONOLCK; } if(read_write & GDBM_LOCKNB){ dpomode |= DP_OLCKNB; cromode |= CR_OLCKNB; } if(read_write & GDBM_SPARSE){ dpomode |= DP_OSPARSE; cromode |= CR_OSPARSE; } flags = O_RDWR | O_CREAT; } else if(read_write & GDBM_NEWDB){ dpomode = DP_OWRITER | DP_OCREAT | DP_OTRUNC; cromode = CR_OWRITER | CR_OCREAT | CR_OTRUNC; if(read_write & GDBM_NOLOCK){ dpomode |= DP_ONOLCK; cromode |= CR_ONOLCK; } if(read_write & GDBM_LOCKNB){ dpomode |= DP_OLCKNB; cromode |= CR_OLCKNB; } if(read_write & GDBM_SPARSE){ dpomode |= DP_OSPARSE; cromode |= CR_OSPARSE; } flags = O_RDWR | O_CREAT | O_TRUNC; } else { gdbm_errno = GDBM_ILLEGAL_DATA; return NULL; } if(lstat(name, &sbuf) != -1){ if(S_ISDIR(sbuf.st_mode)){ if(dnum < 1) dnum = 1; } else { dnum = 0; } } depot = NULL; curia = NULL; if(dnum > 0){ mode |= 00700; if((cromode & CR_OCREAT)){ if(mkdir(name, mode) == -1 && errno != EEXIST){ gdbm_errno = GDBM_FILE_OPEN_ERROR; return NULL; } } if(!(curia = cropen(name, cromode, bnum, dnum))){ gdbm_errno = gdbm_geterrno(dpecode); return NULL; } if(cromode & CR_OWRITER) crsetalign(curia, align); if((cromode & CR_OWRITER) && (read_write & GDBM_SYNC)) crsync(curia); } else { mode |= 00600; if(dpomode & DP_OWRITER){ if((fd = open(name, flags, mode)) == -1 || close(fd) == -1){ gdbm_errno = GDBM_FILE_OPEN_ERROR; return NULL; } } if(!(depot = dpopen(name, dpomode, bnum))){ gdbm_errno = gdbm_geterrno(dpecode); return NULL; } if(dpomode & DP_OWRITER) dpsetalign(depot, align); if((dpomode & DP_OWRITER) && (read_write & GDBM_SYNC)) dpsync(depot); } if(!(dbf = malloc(sizeof(GDBM)))){ gdbm_errno = GDBM_MALLOC_ERROR; if(depot) dpclose(depot); if(curia) crclose(curia); return NULL; } dbf->depot = depot; dbf->curia = curia; dbf->syncmode = (dpomode & DP_OWRITER) && (read_write & GDBM_SYNC) ? TRUE : FALSE; return dbf; }
A0(PUBLIC, LONGINT, ROMlib_ZeroScrap) { OSErr retval; INTEGER f; THz saveZone; if (Cx(ScrapState) < 0) { ScrapCount = 0; saveZone = TheZone; TheZone = SysZone; ScrapHandle = RM(NewHandle((Size)0)); TheZone = saveZone; ScrapState = CWC (1); ScrapName = RM((StringPtr) "\016Clipboard File"); } else if (Cx(ScrapState) == 0) { retval = cropen(&f); if (retval != noErr) return retval; retval = SetEOF(f, (LONGINT)0); if (retval != noErr) return retval; FSClose(f); } else if (Cx(ScrapState) > 0) SetHandleSize(MR(ScrapHandle), (Size)0); ScrapSize = 0; ScrapCount = CW(CW(ScrapCount) + 1); return noErr; }