home *** CD-ROM | disk | FTP | other *** search
- /***( ptmisc.c )*************************************************************
- * Miscellaneous PRO-TREE Functions (low level) *
- *****************************************************************************
- * *
- * PRO-TREE v1.21 - Copyright (c) 1988, 1990 Vestronix Inc. *
- * *
- * Features: *
- * - Dynamic leveling *
- * - Free list for data file and each key in the index file *
- * - Single-user or Multi-user with appropriate index file locking *
- * - Single record locking on the data file *
- * *
- * Original : NIG 24-Feb-88, GEO 23-Mar-88, VVA Dec-88 (indexing functs) *
- * Changed : JZ Apr-89 *
- * Changed : BRC 22-Aug-90 (record packing and memo fields) *
- * *
- ****************************************************************************/
-
- /*
- ** Modifications
- **
- ** 22-Aug-90 BRC - record packing and memo fields
- ** 12-Feb-90 BRC - IOGEN'd version
- ** 4-Dec-89 BRC - multi-user, dynamic levels, free space reuse
- ** Apr-89 JZ - Rewrote the indexing functions for single user
- ** Dec-88 VvA - Wrote indexing functions for ASCII file manager
- ** 23-Mar-88 GEO - Changes ASCII file manager
- ** 24-Feb-88 NIG - Wrote ASCII file manager
- */
-
- #include <protree.h>
-
-
- /*************************************************************************
- * Write out a number, doing any neccessary byte/word/longword swapping *
- *************************************************************************/
- int
- PT_writenum(fd, buf, len)
- int fd;
- char *buf;
- int len;
- {
- int iostat = IOGOOD;
-
- PT_swapper(buf, len);
- if (write(fd, buf, len) != len)
- iostat = IOIDXWRITE;
- PT_swapper(buf, len);
-
- return(iostat);
- }
-
-
- /***********************************************************************
- * Read in a number, doing any neccessary byte/word/longword swapping *
- ***********************************************************************/
- int
- PT_readnum(fd, buf, len)
- int fd;
- char *buf;
- int len;
- {
- int iostat = IOGOOD;
-
- if (read(fd, buf, len) != len)
- iostat = IOIDXREAD;
-
- PT_swapper(buf, len);
-
- return(iostat);
- }
-
-
- /****************************************************************************
- * This bit here does byte/word/longword swapping for binary data types to *
- * get it into Intel form. Since most Intel machines are running DOS the *
- * extra processing overhead is thrust upon the bigger/faster machines. *
- * *
- * NOTE: warnings about unused PARAMETERS or LOCALS should be ignored here *
- ****************************************************************************/
- #if !defined(SWAP_WORD8) || !defined(SWAP_WORD16) || !defined(SWAP_WORD32)
- void
- PT_swapper(buf, len)
- char *buf;
- int len;
- {
- #if !defined(SWAP_WORD8)
- {
- register int i;
-
- for (i = 0; i + 1 < len; i += 2)
- lswap(char, buf[i], buf[i + 1]) /* NO SEMICOLON! */
- }
- #endif /* !SWAP_WORD8 */
-
- #if !defined(SWAP_WORD16)
- {
- register int i, j;
-
- for (i = 0; i + 3 < len; i += 4)
- for (j = 0; j < 2; j++)
- lswap(char, buf[i + j], buf[i + j + 2]) /* NO SEMICOLON! */
- }
- #endif /* !SWAP_WORD16 */
-
- #if !defined(SWAP_WORD32)
- {
- register int i, j;
-
- for (i = 0; i + 7 < len; i += 8)
- for (j = 0; j < 4; j++)
- lswap(char, buf[i + j], buf[i + j + 4]) /* NO SEMICOLON! */
- }
- #endif /* !SWAP_WORD32 */
- }
- #endif
-
-
- #if defined(PTDEBUG)
- #if defined(UNIX)
- void CDECL PT_debug(va_alist)
- va_dcl
- #else
- void CDECL PT_debug(char *va_alist, ...)
- #endif
- {
- static char *Logname, Output, Error, Nolog;
- #if defined(MSDOS)
- static char Terminal, Printer;
- #endif
- static int mdebug;
- va_list ap;
- char *fmt;
- FILE *Fp;
- time_t clock;
- int structured = TRUE;
-
- if (!mdebug)
- {
- mdebug = 1;
- Chk("PT_debug");
- mdebug = 0;
- }
-
- if (Logname == &Nolog || (Logname == NULL &&
- (Logname = getenv("PT_DEBUG")) == NULL))
- return;
- else if (Logname == &Output || !strcmp(Logname, "stdout"))
- Logname = &Output, Fp = stdout;
- else if (Logname == &Error || !strcmp(Logname, "stderr"))
- Logname = &Error, Fp = stderr;
- #if defined(MSDOS)
- else if (Logname == &Terminal || !strcmp(Logname, "stdaux"))
- Logname = &Terminal, Fp = stdaux;
- else if (Logname == &Terminal || !strcmp(Logname, "stdprn"))
- Logname = &Printer, Fp = stdaux;
- #endif
- else if ((Fp = fopen(Logname, "a")) == NULL)
- {
- Logname = &Nolog;
- return;
- }
-
- #if defined(UNIX)
- va_start(ap);
- fmt = va_arg(ap, char *);
- #else
- va_start(ap, va_alist);
- fmt = va_alist;
- #endif
- if (!fmt)
- structured = FALSE, fmt = va_arg(ap, char *);
-
- if (fmt)
- {
- time(&clock);
- if (structured)
- fprintf(Fp, "%s: %.19s ", ttyname(0), ctime(&clock));
- vfprintf(Fp, fmt, ap);
- if (structured)
- fputc('\n', Fp);
- fflush(Fp);
- }
-
- va_end(ap);
-
- if (Logname != &Output && Logname != &Error
- #if defined(MSDOS)
- && Logname != &Terminal && Logname != &Printer
- #endif
- )
- fclose(Fp);
- }
- #endif
-
-
- /*
- ** Convert error codes into readable strings
- */
- char *
- PT_stat(stat)
- int stat;
- {
- char *msg;
- static char foobie[20];
-
- switch (stat)
- {
- case IOGOOD: msg = "IOGOOD"; break;
- case IOERROR: msg = "IOERROR"; break;
- case IONOFILE: msg = "IONOFILE"; break;
- case IOBADOPEN: msg = "IOBADOPEN"; break;
- case IOINDEXPOSN: msg = "IOINDEXPOSN"; break;
- case IONOKEY: msg = "IONOKEY"; break;
- case IODUP: msg = "IODUP"; break;
- case IOTOF: msg = "IOTOF"; break;
- case IOEOF: msg = "IOEOF"; break;
- case IOLOCKED: msg = "IOLOCKED"; break;
- case IONOLOCK: msg = "IONOLOCK"; break;
- case IONONEXT: msg = "IONONEXT"; break;
- case IONOLOGON: msg = "IONOLOGON"; break;
- case IO_NO_ADMIN: msg = "IO_NO_ADMIN"; break;
- case IOADD: msg = "IOADD"; break;
-
- #if IODATSEEK != IOERROR
- case IODATSEEK: msg = "IODATSEEK"; break;
- case IOIDXSEEK: msg = "IOIDXSEEK"; break;
- case IODATWRITE: msg = "IODATWRITE"; break;
- case IOIDXWRITE: msg = "IOIDXWRITE"; break;
- case IODATREAD: msg = "IODATREAD"; break;
- case IOIDXREAD: msg = "IOIDXREAD"; break;
- case IOOLDVER: msg = "IOOLDVER"; break;
- case IONEWVER: msg = "IONEWVER"; break;
- case IODATOPEN: msg = "IODATOPEN"; break;
- case IOIDXOPEN: msg = "IOIDXOPEN"; break;
- case IOFLDDEF: msg = "IOFLDDEF"; break;
- case IOKEYDEF: msg = "IOKEYDEF"; break;
- case IOFLDPERM: msg = "IOFLDPERM"; break;
- case IOIDXPERM: msg = "IOIDXPERM"; break;
- case IODATPERM: msg = "IODATPERM"; break;
- case IOFILLOCK: msg = "IOFILLOCK"; break;
- case IONOSERVER: msg = "IONOSERVER"; break;
- case IOCONFIG: msg = "IOCONFIG"; break;
- case IOFTABLE: msg = "IOFTABLE"; break;
- case IOILOCKED: msg = "IOILOCKED"; break;
- case IOINOLOCK: msg = "IOINOLOCK"; break;
- #endif
-
- default:
- msg = foobie;
- sprintf(msg, "<stat=%d>", stat);
- break;
- }
-
- return(msg);
- }
-