home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / scm / wb1a1.lha / wb / view.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-29  |  6.2 KB  |  236 lines

  1. /* WB-tree File Based Associative String Data Base System.
  2.    Copyright (c) 1991, 1992, 1993 Holland Mark Martin
  3.  
  4. Permission to use, copy, modify, and distribute this software and its
  5. documentation for educational, research, and non-profit purposes and
  6. without fee is hereby granted, provided that the above copyright
  7. notice appear in all copies and that both that copyright notice and
  8. this permission notice appear in supporting documentation, and that
  9. the name of Holland Mark Martin not be used in advertising or
  10. publicity pertaining to distribution of the software without specific,
  11. written prior consent in each case.  Permission to incorporate this
  12. software into commercial products can be obtained from Jonathan
  13. Finger, Holland Mark Martin, 174 Middlesex Turnpike, Burlington, MA,
  14. 01803-4467, USA.  Holland Mark Martin makes no representations about
  15. the suitability or correctness of this software for any purpose.  It
  16. is provided "as is" without express or implied warranty.  Holland Mark
  17. Martin is under no obligation to provide any services, by way of
  18. maintenance, update, or otherwise. */
  19.  
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include "defs.h"
  23. #include <time.h>
  24.  
  25. #define BLOCK_SIZE 2048        /* Maximum supported block size */
  26. #define BLKSIZST 44        /* Location of Block Size in Block 0 - BAD HACK */
  27. int block_size = 2048;
  28.  
  29. FILE *stream;
  30. unsigned long blk_num,cont_ptr,tree_head;
  31. int size,level,typ;
  32. char directory;
  33.  
  34. void dspl_char(ch)
  35. int ch;
  36. {
  37.  if ((ch >= ' ') && (ch <= '~')) putchar(ch);
  38.   else printf("'%d'",ch);
  39. }
  40.  
  41.  
  42. long convert_bytes_to_long(bytes,size)
  43. unsigned char *bytes;
  44. int size;
  45. {
  46.  long value = 0;
  47.  while (size-- > 0) value = (value * 256) + *bytes++;
  48.  return(value);
  49. }
  50.  
  51. unsigned char buffer[BLOCK_SIZE],command_line[1000],*input_ptr,*input_end;
  52.  
  53. int input_read(buff,size)
  54. unsigned char *buff;
  55. int size;
  56. {
  57.  unsigned char *input_ptr,*input_end,ch;
  58.  printf("\n?");
  59.  fflush(stdout);
  60.  input_ptr = buff;
  61.  input_end = buff + size;
  62.  while ( (ch=getchar()) != 13 && (ch != 10))
  63.     {
  64.      if ((ch >= ' ') && (ch <= '~') )
  65.        {
  66.     if (input_ptr < input_end)
  67.         {
  68.          *input_ptr++ = ch;
  69.          putchar(ch);
  70.         }
  71.      else putchar(7);
  72.        }
  73.       else if ( (ch == '\b') &&
  74.           (input_ptr > buff) )
  75.        {
  76.         input_ptr--;
  77.         putchar('\b');
  78.         putchar(' ');
  79.         putchar('\b');
  80.        }
  81.      else if (ch == 17) /* ^Q */
  82.        putchar(*input_ptr++ = getchar());
  83.      }
  84.  *input_ptr = 0;
  85.  return(input_ptr - buff);
  86. }
  87.  
  88. #ifndef SEEK_SET
  89. #define SEEK_SET 0
  90. #endif
  91.  
  92. void display_block(blk)
  93. long blk;
  94. {
  95.  long i;
  96.  unsigned char *ptr1,*ptr2,*ptr3,namebuf[256],namebuf1[256];
  97.  int namelen,namelen1;
  98.  fseek(stream,blk * block_size,SEEK_SET);
  99.  i = fread(buffer,sizeof(char),block_size,stream);
  100.  if (i != block_size)
  101.    {
  102.     printf("\nERROR READING BLOCK %ld",blk);
  103.     return;
  104.    }
  105.  blk_num = convert_bytes_to_long(&buffer[blk_id_pos],4);
  106.  cont_ptr = convert_bytes_to_long(&buffer[blk_nxt_id_pos],4);
  107.  tree_head = convert_bytes_to_long(&buffer[blk_top_id_pos],4);
  108.  size = convert_bytes_to_long(&buffer[blk_end_pos],2) - blk_data_start;
  109.  level = buffer[blk_level_pos] - '0';
  110.  typ = blk_typ(buffer);
  111.  if (typ == 'D') directory = 'Y';
  112.   else directory = 'N';
  113.  if (blk != blk_num)
  114.   printf("BLOCK HEAD MISMATCH BLK: %ld != BLK_HEAD: %ld",blk,blk_num);
  115.  ptr1 = &buffer[blk_data_start];
  116.  ptr2 = ptr1 + size;
  117.  if (typ == seq_typ)
  118.     {
  119.      int j;
  120.      printf("\nSEQ BLOCK: %ld   LEVEL: %d  HEAD: %ld  CONT: %ld DIR: %c  SIZE: %d",
  121.         blk_num,level,tree_head,cont_ptr,directory,size);
  122.     {
  123.      long i = convert_bytes_to_long(&buffer[blk_time_pos],4);
  124.      printf("\nLAST WRITTEN: %s",ctime(&i));
  125.     }
  126.      while (ptr1 < ptr2)
  127.        {
  128.     unsigned char *ptr3 = ptr1 + *ptr1 + 1;
  129.         j = *ptr1;
  130.     if (j > (ptr2 - ptr1)) j = ptr2 - ptr1;
  131.         printf("\n");
  132.         ptr1++;
  133.     while (j > 0) { dspl_char(*ptr1++); j--; }
  134.     ptr1 = ptr3;
  135.        }
  136.      }
  137.  else
  138.    {
  139.     int beg = 1;
  140.     printf("\nBLOCK: %ld   LEVEL: %d  HEAD: %ld  CONT: %ld DIR: %c  SIZE: %d TYPE: %c",
  141.     blk_num,level,tree_head,cont_ptr,directory,size,(char) typ);
  142.     {
  143.      long i = convert_bytes_to_long(&buffer[blk_time_pos],4);
  144.      printf("\nLAST WRITTEN: %s",ctime(&i));
  145.     }
  146.     namelen = 0;
  147.     namelen1 = 0;
  148.     while (ptr1 < ptr2)
  149.       {
  150.        namelen = *ptr1++;
  151.        i = *ptr1++;
  152.        ptr3 = ptr1 + i;
  153.        ptr3 += *ptr3 + 1;
  154.        if (ptr3 >= ptr2) printf("\nLINK POINTER TO %ld",cont_ptr);
  155.        while (i-- > 0) namebuf[namelen++] = *ptr1++;
  156.        printf("\n");
  157.        i = 0;
  158.        while (i < namelen) dspl_char(namebuf[i++]);
  159.        i = 0;
  160.        while (  (i < namelen) && (i < namelen1)
  161.           && (namebuf[i] == namebuf1[i]) ) i++;
  162.        if ( !beg &&
  163.           ( ( (i < namelen) && (i < namelen1)
  164.           && (namebuf[i] < namebuf1[i]) )
  165.         || (i == namelen) )  )
  166.         printf("\nCOLLATION SEQUENCE ERROR");
  167.        namelen1 = 0;
  168.        beg = 0;
  169.        while (namelen1 < namelen)
  170.        {
  171.         namebuf1[namelen1] = namebuf[namelen1];
  172.         namelen1++;
  173.        }
  174.        if (ptr3 >= ptr2) ;
  175.     else {
  176.           printf("\n --> ");
  177.           i = *ptr1++;
  178.           if ( (i == 4) && ((*ptr1 < ' ') || (level > 0)))
  179.           {
  180.            printf("%ld",convert_bytes_to_long(ptr1,4));
  181.            ptr1 += 4;
  182.           }
  183.            else while (i-- > 0) dspl_char(*ptr1++);
  184.          }
  185.        }
  186.    }
  187.   if (ptr1 != ptr2) printf("\nBLOCK SIZE ERROR");
  188. }
  189.  
  190.  
  191.  
  192. int main( argc, argv )
  193. int argc;
  194. char *argv[];
  195. {
  196.  long i;
  197.  int j,k;
  198.  if ((stream = fopen(argv[1],"rb")) == NULL)
  199.            {
  200.             printf("\nUNABLE TO OPEN %s",argv[1]);
  201.             return;
  202.            }
  203.  i = fread(buffer,sizeof(char),BLKSIZST+2,stream);
  204.  if (i != BLKSIZST+2)
  205.    {
  206.     printf("\nERROR READING BLOCK 0");
  207.     return;
  208.    }
  209.  block_size = buffer[44]*256 + buffer[45];
  210.  printf("\nSUPERBLOCK (size = %d)", block_size);
  211.  display_block(0L);
  212.  while ( ( k = input_read(command_line,900) ))
  213.     {
  214.      j = 0;
  215.      if (strcmp(command_line,"L") == 0)
  216.         {
  217.      if (cont_ptr) display_block(cont_ptr);
  218.         }
  219.       else if (strcmp(command_line,"R") == 0) display_block(blk_num);
  220.       else if (strcmp(command_line,"H") == 0) display_block(tree_head);
  221.       else {
  222.             while ( (j < k) && isdigit(command_line[j])) j++;
  223.         if (j == k)
  224.           {
  225.            i = 0;
  226.            j = 0;
  227.            while (j < k) i = (i * 10) + command_line[j++] - '0';
  228.            display_block(i);
  229.           }
  230.             }
  231.     }
  232.  fclose(stream);
  233.  return 0;
  234. }
  235.  
  236.