home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / test / pprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  4.0 KB  |  185 lines

  1. /*====================================================================
  2.  *
  3.  * FILE:
  4.  *    pprint.c
  5.  *
  6.  * $Header: /private/postgres/src/test/RCS/pprint.c,v 1.1 1990/11/15 16:27:24 sp Exp $
  7.  *
  8.  * DESCRIPTION:
  9.  *
  10.  * This file reads from its input a plan (genearated by 'PlanToString')
  11.  * and attempts to pretty-print it.
  12.  *
  13.  * NOTE:
  14.  *    Newlines are ignored, so you can use the xterm cut and paste...
  15.  *    The plan contents are not altered in any way. The only thing that
  16.  *    this program does is to insert some spaces and newlines.
  17.  *    Therefore its output can be re-readed by 'StringToPlan'.
  18.  */
  19. #include <stdio.h>
  20. #include <strings.h>
  21.  
  22. /*
  23.  * Is it a standalone program, or just part of postgres
  24.  * code ??
  25.  */
  26. #define STANDALONE
  27.  
  28. #ifdef STANDALONE
  29. #define MY_ALLOC(x)    malloc(x)
  30. #define MY_FREE(x)    free(x)
  31. #else
  32. #define MY_ALLOC(x)    palloc(x)
  33. #define MY_FREE(x)    pfree(x)
  34. #endif STANDALONE
  35.  
  36. char *MY_ALLOC();
  37. void MY_FREE();
  38.  
  39. char *pprint_plan();
  40. char *addCharToString();
  41.  
  42.  
  43. /*==================== ROUTINES ====================================*/
  44. #ifdef STANDALONE
  45. /*-----------------------------------------------------------------
  46.  * main()
  47.  *
  48.  */
  49. main(argc, argv)
  50. int argc;
  51. char *argv[];
  52. {
  53.     FILE *fpin, *fpout;
  54.     char *sin, *sout;
  55.     int c;
  56.     int maxlength;
  57.     int currentlength;
  58.  
  59.     /*
  60.      * Sorry, no options yet...
  61.      * Read from stdin and print to stdout...
  62.      */
  63.     fpin = stdin;
  64.     fpout = stdout;
  65.  
  66.     /*
  67.      * read the input to a string
  68.      */
  69.     maxlength = 0;
  70.     currentlength = 0;
  71.     sin = NULL;
  72.     while ((c=getc(fpin)) != EOF) {
  73.     /*
  74.      * ignore newlines
  75.      */
  76.     if (c!= '\n')
  77.         sin = addCharToString(sin, &maxlength, ¤tlength, (char)c);
  78.     }
  79.  
  80.     sout = pprint_plan(sin);
  81.  
  82.     fprintf(fpout, "%s\n", sout);
  83. }
  84. #endif STANDALONE
  85.  
  86. /*-----------------------------------------------------------------
  87.  * pprint_plan()
  88.  *
  89.  * This is the routine that does the job.
  90.  * 
  91.  *
  92.  * For every left parenthesis we find we increase indentation.
  93.  * For every right parenthesis we decrease it.
  94.  * We change to a new line every time we find a '#S(' or
  95.  * a word beginning with ':' (a node field).
  96.  */
  97.  
  98. char *
  99. pprint_plan(s)
  100. char *s;
  101. {
  102.     register int i;
  103.     int indentation;
  104.     int c;
  105.     char *res;
  106.     int maxlength, currentlength ;
  107.  
  108.     /*
  109.      * initialize output string data
  110.      */
  111.     maxlength = 0;
  112.     currentlength = 0;
  113.     res = NULL;
  114.  
  115.     indentation = 0;
  116.  
  117.     while (*s != '\0') {
  118.     if (*s=='#' && *(s+1)=='S' && *(s+2)=='(') {
  119.         res = addCharToString(res, &maxlength, ¤tlength, '\n');
  120.         for (i=0; i<indentation; i++)
  121.         res = addCharToString(res, &maxlength, ¤tlength, ' ');
  122.         res = addCharToString(res, &maxlength, ¤tlength, *s);
  123.     } else if (*s == ':') {
  124.         res = addCharToString(res, &maxlength, ¤tlength, '\n');
  125.         for (i=0; i<indentation; i++)
  126.         res = addCharToString(res, &maxlength, ¤tlength, ' ');
  127.         res = addCharToString(res, &maxlength, ¤tlength, *s);
  128.     } else if (*s == '(') {
  129.         indentation++;
  130.         res = addCharToString(res, &maxlength, ¤tlength, *s);
  131.     } else if (*s == ')') {
  132.         indentation--;
  133.         res = addCharToString(res, &maxlength, ¤tlength, *s);
  134.     } else {
  135.         res = addCharToString(res, &maxlength, ¤tlength, *s);
  136.     }
  137.     /*
  138.      * go to the next char
  139.      */
  140.     s++;
  141.     }
  142.  
  143.     return(res);
  144. }
  145.  
  146. /*-----------------------------------------------------------------
  147.  *
  148.  * addCharToString
  149.  *
  150.  * Append a character to a string and return the new string.
  151.  * It always leaves a null terminated string.
  152.  * If the string has not sufficient length,
  153.  * the original string is freed and a larger string is allocated.
  154.  */
  155. char *
  156. addCharToString(s, maxLength, currentLength, c)
  157. char *s;
  158. int *maxLength;
  159. int *currentLength;
  160. char c;
  161. {
  162.     char *res;
  163.  
  164.     if (s==NULL || (*currentLength+1) >= *maxLength) {
  165.     /*
  166.      * need to allocate some (new) space
  167.      */
  168.     *maxLength += 1000;
  169.     res = MY_ALLOC(*maxLength);
  170.     if (s!=NULL) {
  171.         bcopy(s, res, *currentLength);
  172.         MY_FREE(s);
  173.     }
  174.     } else {
  175.     res = s;
  176.     }
  177.  
  178.     res[*currentLength] = c;
  179.     *currentLength += 1;
  180.     res[*currentLength] = '\0';
  181.  
  182.     return(res);
  183. }
  184.  
  185.