home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / database / mbase.zip / BUILD.C next >
C/C++ Source or Header  |  1991-02-11  |  10KB  |  418 lines

  1. #include "stdinc.h"
  2.  
  3. #define cr(x) ((x) == 0) ? "\n\n" : "\n"
  4.  
  5. static char *justword[] = { "L-just", "R-just" };
  6.  
  7. char *word (fle)
  8. int         fle;
  9. {
  10.    int         i;
  11.    char        a[2];
  12.    static char buffer [81];
  13.  
  14.    i = 0;
  15.  
  16.    while (read (fle, a, 1) == 1)
  17.     {
  18.       if (a[0] == ' ' || a[0] == '\n')
  19.        { if (i != 0)
  20.             break;
  21.        }
  22.       else
  23.          buffer[i++] = a[0];
  24.     };
  25.  
  26.    buffer [i] = 0;
  27.  
  28.    return buffer;
  29. }
  30.  
  31. char *repeat (ch, nm)
  32. int           ch, nm;
  33. {
  34.    char  buf [MAX_RPT];
  35.  
  36.    buf[(nm = (nm < 0) ? 0 : nm)] = 0;
  37.  
  38.    for (nm--; nm >= 0; nm--)  buf[nm] = ch;
  39.  
  40.    return buf;
  41. }
  42.  
  43. pattern (whole,  match, skip)
  44. char    *whole, *match;
  45. int                     skip;
  46. {
  47.    int  i, j, m, w;
  48.  
  49.    m = strlen (match);  w = strlen (whole);
  50.  
  51.    for (i = 0; i <= w-m; i++)
  52.     {
  53.       for (j = 0; match [j] != 0 && whole[i+j] == match[j]; j++)
  54.        ;
  55.  
  56.       if (match[j] == 0)  if (skip-- == 0)  return i;
  57.     };
  58.  
  59.    return -1;
  60. }
  61.  
  62. /*  Encode and Eval perform base 10 to base 255 conversion, with a bottom
  63.     value of 1 (As opposed to 0) for the base-255 numbers.  Perfect for
  64.     encoding BIG numbers (See chart) and storing them in 5 bytes on disk, eh?
  65.  
  66.     # of characters used______________Maximum integer representable
  67.     1                                          254
  68.     2                                        65024
  69.     3                                     16581374
  70.     4                                   4228250624
  71.     5                                 107820390900.... etc.                  */
  72.  
  73. long _eval (st, x)
  74. char       *st;
  75. int             x;
  76. {
  77.    long int  j;
  78.    long int  n;
  79.    int       a;
  80.  
  81.    j = 0;  n = 1;
  82.  
  83.    while (x > 0) { a = st[--x];  a = fix (a);  j += n * (a - 1);  n *= 255; };
  84.  
  85.    return j;
  86. }
  87.  
  88. char *_encode (num, len)
  89. long int       num;
  90. int                 len;
  91. {
  92.    register int  i;
  93.             int  a;
  94.    static   char buf[10];
  95.  
  96.    len = (len < 9) ? len : 9;
  97.  
  98.    for (i = len-1; i >= 0; i--)
  99.     { a = num % 255;
  100.  
  101.       num = (num - a) / 255;
  102.  
  103.       buf[i] = a + 1;
  104.     };
  105.  
  106.    buf[len] = 0;
  107.  
  108.    return buf;
  109. }
  110.  
  111. main  (argc, argv)
  112. int    argc;
  113. char **argv;
  114. {
  115.    char   flds[384], lens[384], idxs[20][50];
  116.    char   rel[80],   temp[80],  tmp2[80];
  117.    int    stage,     column,    num_i,         num_f,    R;
  118.    int    n,     i,  fle,       st,    dups,   s,        r_len,   isright;
  119.  
  120.    if (argc != 2)
  121.     { printf ("%s: Format = '%s%s%s schema.s'\n", argv[0], BOLD, argv[0], NORM);
  122.       exit   (1);
  123.     };
  124.  
  125.    n = strlen (argv[1]);
  126.    if (argv[1][n-1] != 's' || argv[1][n-2] != '.')  strcat (argv[1], ".s");
  127.  
  128.    sprintf (flds, "|");  sprintf (rel, "%s", argv[1]);  sprintf (lens, "");
  129.  
  130.    for (i = strlen(argv[1])-1; i > -1 && argv[1][i] != ':'
  131.                                       && argv[1][i] != '/'; i--);
  132.  
  133.    r_len = rel[i+1] = 0;
  134.  
  135.    if ((fle = open (argv[1], O_RDONLY)) == -1)
  136.     { fprintf (stderr, "\nCannot open %s%s%s.\n", BOLD, argv[1], NORM);
  137.       exit    (1);
  138.     };
  139.  
  140.    skip (fle, "relation");
  141.  
  142.    sprintf (temp, "%s", word (fle));
  143.  
  144.    if (strlen (temp) == 0)
  145.     { fprintf (stderr, "\nFile %s%s%s holds no schema definition.\n",
  146.                        BOLD, argv[1], NORM);
  147.       exit    (1);
  148.     };
  149.  
  150.    printf ("%s", CLS);
  151.    printf ("Building relation %s%s%s", BOLD, temp, NORM);
  152.  
  153.    if (rel[0] != 0)
  154.       printf (" under directory %s%s%s\n\n", BOLD, rel, NORM);
  155.    else
  156.       printf (" under current directory\n\n");
  157.  
  158.    sprintf (rel, "%s%s.rel", rel, temp);
  159.  
  160.    num_f  = num_i = 0;
  161.    column = stage = 1;
  162.  
  163.    printf ("%s%-60.60s%s\n", UNDR, "Fields", NORM);
  164.  
  165.    do
  166.     { sprintf (temp, "%s", word (fle));
  167.  
  168.       if (strcmp (temp, "field") == 0)
  169.        { if (stage == 2)
  170.           { fprintf (stderr, "%sField %s%s%s declared after indicies.\n",
  171.                      cr (column), BOLD, word (fle), NORM);
  172.             exit    (1);
  173.           };
  174.  
  175.          sprintf (temp, "%s", word (fle));
  176.  
  177.          if (temp [strlen(temp)-1] == '|')  temp[strlen(temp)-1] = 0;
  178.  
  179.          sprintf (tmp2, "|%s|", temp);
  180.  
  181.          if (pattern (flds, tmp2, 0) != -1)
  182.           { fprintf (stderr, "%sField %s%s%s declared twice.\n",
  183.                      cr (column), BOLD, temp, NORM);
  184.             exit    (1);
  185.           };
  186.  
  187.          strcat  (flds, temp);  strcat  (flds, "|");
  188.  
  189.          skip    (fle, "length");
  190.  
  191.          sprintf (tmp2, "%03s", word (fle));
  192.  
  193.          strcat  (lens, tmp2);
  194.  
  195.          if ((isright = skip (fle, "right")) == 1)
  196.             strcat (lens, "r|");
  197.          else
  198.           { strcat (lens, "l|");
  199.             skip   (fle,  "left");
  200.           };
  201.  
  202.          r_len += atoi (tmp2) + 1;
  203.  
  204.          sprintf (temp, "%s [%d] %s", temp, atoi (tmp2), justword[isright]);
  205.  
  206.          if ((column = 1-column) == 0)
  207.             printf ("%s%-30.30s%s", SUBD, temp, NORM);
  208.          else
  209.             printf ("%s%s%s\n",     SUBD, temp, NORM);
  210.  
  211.          num_f ++;
  212.  
  213.          if (skip (fle, ";") == 1)
  214.           { do     sprintf (temp, "%s", word (fle));
  215.             until (strcmp (temp, ";") == 0 || temp[0] == 0);
  216.  
  217.             if (temp[0] == 0)
  218.              { printf ("%s", cr (column));
  219.                endoffile (num_f, num_i);
  220.                stage = 3;
  221.              };
  222.           };
  223.        }
  224.       else
  225.          if (strcmp (temp, "index") == 0)
  226.           {
  227.             if (stage == 1)
  228.              { if (column == 0)  printf ("\n");
  229.  
  230.                if (num_f == 0)
  231.                 { fprintf (stderr, "\nNo fields declared before indicies.\n");
  232.                   exit    (1);
  233.                 };
  234.  
  235.                printf ("\n%s%-60.60s%s\n", UNDR, "Indicies", NORM);
  236.  
  237.                stage  = 2;
  238.                column = 1;
  239.              };
  240.  
  241.             skip (fle, "on");
  242.  
  243.             sprintf (temp, "%s", word (fle));
  244.  
  245.             if (temp[strlen(temp)-1] == '|')  temp[strlen(temp)-1] = 0;
  246.  
  247.             printf  ("%s%s%s%s", SUBD, temp, NORM,
  248.                                        repeat ('.', 36-strlen (temp)));
  249.             dups = 0;
  250.  
  251.             if (skip (fle, "with") == 1)
  252.              { if (skip (fle, "duplicates") == 0)
  253.                 { fprintf (stderr, "?\n\nIncorrect syntax\n");
  254.                   exit    (1);
  255.                 }
  256.                else
  257.                 { printf  ("Duplicates allowed\n");
  258.                   dups = 1;
  259.                 };
  260.              }
  261.             else
  262.                printf ("Duplicates not allowed\n");
  263.  
  264.             strcat  (temp, "|");  sprintf (tmp2, "%s", "");
  265.  
  266.             s = idxs[num_i][0] = 0;
  267.  
  268.             while (temp[s] != 0)
  269.              { for (i = s, tmp2[0] = '|'; temp[i] != '|'; i++)
  270.                   tmp2 [i-s+1] = temp [i];
  271.  
  272.                tmp2[i-s+1] = '|';  tmp2[i-s+2] = 0; s = i+1;
  273.  
  274.                if ((i = pattern (flds, tmp2, 0)) == -1)
  275.                 {
  276.                   sprintf (temp, "%s", &tmp2[1]);  temp[strlen(temp)-1] = 0;
  277.  
  278.                   fprintf (stderr, "\nField %s%s%s undefined\n", BOLD,
  279.                            temp, NORM);
  280.                   exit    (1);
  281.                 };
  282.  
  283.                for (st = 0, i++; i >= 0; i--)  if (flds[i] == '|')  st++;
  284.  
  285.                sprintf (tmp2, "%03d|", st);
  286.                strcat  (idxs[num_i], tmp2);
  287.              };
  288.  
  289.             if (dups == 1)  strcat (idxs[num_i], "d");
  290.  
  291.             num_i ++;
  292.  
  293.             if (skip (fle, ";") == 1)
  294.              { do     sprintf (temp, "%s", word (fle));
  295.                until (strcmp (temp, ";") == 0 || temp[0] == 0);
  296.  
  297.                if (temp[0] == 0)
  298.                 { printf ("%s", cr (column));
  299.                   endoffile (num_f, num_i);
  300.                   stage = 3;
  301.                 };
  302.              };
  303.           }
  304.          else
  305.             if (strcmp (temp, "end") == 0 || temp[0] == 0)
  306.              { printf ("%s", cr (column));
  307.                endoffile (num_f, num_i);
  308.                stage = 3;
  309.              }
  310.             else
  311.              { fprintf (stderr, "%sIdentifier %s%s%s not recognized.\n",
  312.                                 cr (column), BOLD, temp, NORM);
  313.                exit    (1);
  314.              };
  315.  
  316.     } while (stage != 3);
  317.  
  318.    R = open (rel, O_RDWR);
  319.  
  320.    if (R != -1)
  321.     { if (read (R, temp, 1) != -1)
  322.          if (temp[0] != 1)
  323.           { fprintf (stderr, "\n%s%s%32.32s%-28.28s%s\n", SUBD, INVR,
  324.                              "*** ERR", "OR ***", NORM);
  325.             fprintf (stderr, "\n   This relation is busy.  It cannot be");
  326.             fprintf (stderr, " built during use.\n\n");
  327.  
  328.             close (R);  exit (1);
  329.           };
  330.  
  331.       printf ("%s%s%32.32s%-28.28s%s\n", SUBD, INVR, "** WARN", "ING **", NORM);
  332.       printf ("\n   The file about to be created already exists under the\n");
  333.       printf ("         target directory!  This data will be lost!\n\n");
  334.  
  335.       close (R);
  336.     };
  337.  
  338.    printf ("Continue with the creation of the relation [Y/n] ? ");
  339.  
  340.    i = getchar ();
  341.  
  342.    printf ("\n");
  343.  
  344.    if (i == 'n' || i == 'N' || i == 'q' || i == 'Q')  exit (0);
  345.  
  346.    remove (rel);
  347.  
  348.    if ((R = open (rel, O_RDWR | O_CREAT)) == -1)
  349.     { fprintf (stderr, "\n\nCannot open relation -- Aborted\n");
  350.       exit    (-1);
  351.     };
  352.  
  353. #ifdef UNIX
  354.    sprintf (temp, "chmod 755 %s", rel);
  355.    system  (temp);
  356. #endif
  357.  
  358.    write (R, _encode (0,     1), 1);
  359.    write (R, _encode (num_i, 1), 1);
  360.    write (R, _encode (r_len, 2), 2);
  361.  
  362.    for (i = 0; i < num_i; i++)  write (R, _encode (0, 4), 4);
  363.  
  364.    write (R, ".\001\001\001\001.\n", 7);
  365.  
  366.    write (R, lens, strlen(lens));    write (R, "\n", 1);
  367.  
  368.    for (i = 0; i < num_i; i++)
  369.     { write (R, idxs[i], strlen (idxs[i]));  write (R, "\n", 1);
  370.     };
  371.  
  372.    close (R);
  373.  
  374.    printf ("Relation created -- zero entries.\n");
  375. }
  376.  
  377. skip (f, s)  /* 0 means didn't skip the word, 1 means we did. */
  378. int   f;
  379. char    *s;
  380. {
  381.    int   i;
  382.    char  a[2];
  383.  
  384.    i = 0;
  385.  
  386.    while (s[i] != 0)
  387.     {
  388.       if (read (f, a, 1) != 1)  return -1;
  389.  
  390.       if (i != 0 || (i == 0 && a[0] != ' ' && a[0] != '\n'))
  391.        { if (s[i] != a[0])
  392.           { lseek (f, -1, 1);
  393.             break;
  394.           }
  395.          else
  396.             i++;
  397.        };
  398.     }
  399.  
  400.    if (s[i] == 0)  return 1;
  401.  
  402.    return 0;
  403. }
  404.  
  405. endoffile (num_f, num_i)
  406. int            num_f, num_i;
  407. {
  408.    if (num_f == 0)
  409.     { fprintf (stderr, "No fields declared before end reached\n");
  410.       exit    (1);
  411.     };
  412.    if (num_i == 0)
  413.     { fprintf (stderr, "No indicies declared before end reached\n");
  414.       exit    (1);
  415.     };
  416. }
  417.  
  418.