home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / PROCWRKB.ZIP / BENCH1.ZIP / BENCH / PTMISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-27  |  7.6 KB  |  257 lines

  1. /***( ptmisc.c )*************************************************************
  2. *               Miscellaneous PRO-TREE Functions (low level)                *
  3. *****************************************************************************
  4. *                                                                           *
  5. *  PRO-TREE v1.21  -  Copyright (c) 1988, 1990 Vestronix Inc.               *
  6. *                                                                           *
  7. *  Features:                                                                *
  8. *     - Dynamic leveling                                                    *
  9. *     - Free list for data file and each key in the index file              *
  10. *     - Single-user or Multi-user with appropriate index file locking       *
  11. *     - Single record locking on the data file                              *
  12. *                                                                           *
  13. *  Original : NIG 24-Feb-88, GEO 23-Mar-88, VVA Dec-88 (indexing functs)    *
  14. *  Changed  : JZ Apr-89                                                     *
  15. *  Changed  : BRC 22-Aug-90 (record packing and memo fields)                *
  16. *                                                                           *
  17. ****************************************************************************/
  18.  
  19. /*
  20. **  Modifications
  21. **
  22. **  22-Aug-90  BRC - record packing and memo fields
  23. **  12-Feb-90  BRC - IOGEN'd version
  24. **   4-Dec-89  BRC - multi-user, dynamic levels, free space reuse
  25. **     Apr-89  JZ  - Rewrote the indexing functions for single user
  26. **     Dec-88  VvA - Wrote indexing functions for ASCII file manager
  27. **  23-Mar-88  GEO - Changes ASCII file manager
  28. **  24-Feb-88  NIG - Wrote ASCII file manager
  29. */
  30.  
  31. #include <protree.h>
  32.  
  33.  
  34. /*************************************************************************
  35. *  Write out a number, doing any neccessary byte/word/longword swapping  *
  36. *************************************************************************/
  37. int
  38. PT_writenum(fd, buf, len)
  39.    int fd;
  40.    char *buf;
  41.    int len;
  42. {
  43.    int iostat = IOGOOD;
  44.  
  45.    PT_swapper(buf, len);
  46.    if (write(fd, buf, len) != len)
  47.       iostat = IOIDXWRITE;
  48.    PT_swapper(buf, len);
  49.  
  50.    return(iostat);
  51. }
  52.  
  53.  
  54. /***********************************************************************
  55. *  Read in a number, doing any neccessary byte/word/longword swapping  *
  56. ***********************************************************************/
  57. int
  58. PT_readnum(fd, buf, len)
  59.    int fd;
  60.    char *buf;
  61.    int len;
  62. {
  63.    int iostat = IOGOOD;
  64.  
  65.    if (read(fd, buf, len) != len)
  66.       iostat = IOIDXREAD;
  67.  
  68.    PT_swapper(buf, len);
  69.  
  70.    return(iostat);
  71. }
  72.  
  73.  
  74. /****************************************************************************
  75. *  This bit here does byte/word/longword swapping for binary data types to  *
  76. *  get it into Intel form.  Since most Intel machines are running DOS the   *
  77. *  extra processing overhead is thrust upon the bigger/faster machines.     *
  78. *                                                                           *
  79. *  NOTE: warnings about unused PARAMETERS or LOCALS should be ignored here  *
  80. ****************************************************************************/
  81. #if !defined(SWAP_WORD8) || !defined(SWAP_WORD16) || !defined(SWAP_WORD32)
  82. void
  83. PT_swapper(buf, len)
  84.    char *buf;
  85.    int len;
  86. {
  87. #if !defined(SWAP_WORD8)
  88.    {
  89.       register int i;
  90.  
  91.       for (i = 0; i + 1 < len; i += 2)
  92.      lswap(char, buf[i], buf[i + 1]) /* NO SEMICOLON! */
  93.    }
  94. #endif /* !SWAP_WORD8 */
  95.  
  96. #if !defined(SWAP_WORD16)
  97.    {
  98.       register int i, j;
  99.  
  100.       for (i = 0; i + 3 < len; i += 4)
  101.      for (j = 0; j < 2; j++)
  102.         lswap(char, buf[i + j], buf[i + j + 2]) /* NO SEMICOLON! */
  103.    }
  104. #endif /* !SWAP_WORD16 */
  105.  
  106. #if !defined(SWAP_WORD32)
  107.    {
  108.       register int i, j;
  109.  
  110.       for (i = 0; i + 7 < len; i += 8)
  111.      for (j = 0; j < 4; j++)
  112.         lswap(char, buf[i + j], buf[i + j + 4]) /* NO SEMICOLON! */
  113.    }
  114. #endif /* !SWAP_WORD32 */
  115. }
  116. #endif
  117.  
  118.  
  119. #if defined(PTDEBUG)
  120. #if defined(UNIX)
  121. void CDECL PT_debug(va_alist)
  122.    va_dcl
  123. #else
  124. void CDECL PT_debug(char *va_alist, ...)
  125. #endif
  126. {
  127.    static char *Logname, Output, Error, Nolog;
  128. #if defined(MSDOS)
  129.    static char Terminal, Printer;
  130. #endif
  131.    static int mdebug;
  132.    va_list ap;
  133.    char *fmt;
  134.    FILE *Fp;
  135.    time_t clock;
  136.    int structured = TRUE;
  137.  
  138.    if (!mdebug)
  139.    {
  140.       mdebug = 1;
  141.       Chk("PT_debug");
  142.       mdebug = 0;
  143.    }
  144.  
  145.    if (Logname == &Nolog || (Logname == NULL &&
  146.      (Logname = getenv("PT_DEBUG")) == NULL))
  147.       return;
  148.    else if (Logname == &Output || !strcmp(Logname, "stdout"))
  149.       Logname = &Output, Fp = stdout;
  150.    else if (Logname == &Error || !strcmp(Logname, "stderr"))
  151.       Logname = &Error, Fp = stderr;
  152. #if defined(MSDOS)
  153.    else if (Logname == &Terminal || !strcmp(Logname, "stdaux"))
  154.       Logname = &Terminal, Fp = stdaux;
  155.    else if (Logname == &Terminal || !strcmp(Logname, "stdprn"))
  156.       Logname = &Printer, Fp = stdaux;
  157. #endif
  158.    else if ((Fp = fopen(Logname, "a")) == NULL)
  159.    {
  160.       Logname = &Nolog;
  161.       return;
  162.    }
  163.  
  164. #if defined(UNIX)
  165.    va_start(ap);
  166.    fmt = va_arg(ap, char *);
  167. #else
  168.    va_start(ap, va_alist);
  169.    fmt = va_alist;
  170. #endif
  171.    if (!fmt)
  172.       structured = FALSE, fmt = va_arg(ap, char *);
  173.  
  174.    if (fmt)
  175.    {
  176.       time(&clock);
  177.       if (structured)
  178.      fprintf(Fp, "%s: %.19s ", ttyname(0), ctime(&clock));
  179.       vfprintf(Fp, fmt, ap);
  180.       if (structured)
  181.      fputc('\n', Fp);
  182.       fflush(Fp);
  183.    }
  184.  
  185.    va_end(ap);
  186.  
  187.    if (Logname != &Output && Logname != &Error
  188. #if defined(MSDOS)
  189.      && Logname != &Terminal && Logname != &Printer
  190. #endif
  191.      )
  192.       fclose(Fp);
  193. }
  194. #endif
  195.  
  196.  
  197. /*
  198. ** Convert error codes into readable strings
  199. */
  200. char *
  201. PT_stat(stat)
  202.    int stat;
  203. {
  204.    char *msg;
  205.    static char foobie[20];
  206.  
  207.    switch (stat)
  208.    {
  209.    case IOGOOD:            msg = "IOGOOD";            break;
  210.    case IOERROR:        msg = "IOERROR";        break;
  211.    case IONOFILE:        msg = "IONOFILE";        break;
  212.    case IOBADOPEN:        msg = "IOBADOPEN";        break;
  213.    case IOINDEXPOSN:        msg = "IOINDEXPOSN";        break;
  214.    case IONOKEY:        msg = "IONOKEY";        break;
  215.    case IODUP:            msg = "IODUP";            break;
  216.    case IOTOF:            msg = "IOTOF";            break;
  217.    case IOEOF:            msg = "IOEOF";            break;
  218.    case IOLOCKED:        msg = "IOLOCKED";        break;
  219.    case IONOLOCK:        msg = "IONOLOCK";        break;
  220.    case IONONEXT:        msg = "IONONEXT";        break;
  221.    case IONOLOGON:        msg = "IONOLOGON";        break;
  222.    case IO_NO_ADMIN:        msg = "IO_NO_ADMIN";        break;
  223.    case IOADD:            msg = "IOADD";            break;
  224.  
  225. #if IODATSEEK != IOERROR
  226.    case IODATSEEK:        msg = "IODATSEEK";        break;
  227.    case IOIDXSEEK:        msg = "IOIDXSEEK";        break;
  228.    case IODATWRITE:        msg = "IODATWRITE";        break;
  229.    case IOIDXWRITE:        msg = "IOIDXWRITE";        break;
  230.    case IODATREAD:        msg = "IODATREAD";        break;
  231.    case IOIDXREAD:        msg = "IOIDXREAD";        break;
  232.    case IOOLDVER:        msg = "IOOLDVER";        break;
  233.    case IONEWVER:        msg = "IONEWVER";        break;
  234.    case IODATOPEN:        msg = "IODATOPEN";        break;
  235.    case IOIDXOPEN:        msg = "IOIDXOPEN";        break;
  236.    case IOFLDDEF:        msg = "IOFLDDEF";        break;
  237.    case IOKEYDEF:        msg = "IOKEYDEF";        break;
  238.    case IOFLDPERM:        msg = "IOFLDPERM";        break;
  239.    case IOIDXPERM:        msg = "IOIDXPERM";        break;
  240.    case IODATPERM:        msg = "IODATPERM";        break;
  241.    case IOFILLOCK:        msg = "IOFILLOCK";        break;
  242.    case IONOSERVER:        msg = "IONOSERVER";        break;
  243.    case IOCONFIG:        msg = "IOCONFIG";        break;
  244.    case IOFTABLE:        msg = "IOFTABLE";        break;
  245.    case IOILOCKED:        msg = "IOILOCKED";        break;
  246.    case IOINOLOCK:        msg = "IOINOLOCK";        break;
  247. #endif
  248.  
  249.    default:
  250.       msg = foobie;
  251.       sprintf(msg, "<stat=%d>", stat);
  252.       break;
  253.    }
  254.  
  255.    return(msg);
  256. }
  257.