home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mntdoc01.zoo / mintdoc / c_src / pure_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-21  |  6.4 KB  |  266 lines

  1. /********************************************************************/
  2. /* pure_lib.c - library of support routines for programs processing */
  3. /*              Pure-C help source files.                           */
  4. /*                                                                  */
  5. /* Copyright (c) 1993 by Hildo Biersma - Evil Eye Software          */
  6. /*                            e-mail: boender@dutiws.twi.tudelft.nl */
  7. /*        Evil Eye Software - ``Software with a Purpose''           */
  8. /*                                                                  */
  9. /* Freeware - do with this what you like, but leave my name.        */
  10. /********************************************************************/
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "pure_lib.h"
  16.  
  17. /* Local function prototypes */
  18. static void dispose_names(void);
  19. static void add_name(const char *page);
  20. static void dispose_lines(void);
  21. static void add_line(const char *line);
  22.  
  23. /* Locally defined constants */
  24. #define STATE_SKIP    0
  25. #define STATE_HEADER  1
  26. #define STATE_BODY    2
  27.  
  28. /* Local variables */
  29. static FILE *source = NULL;
  30. static int  state = STATE_SKIP;
  31. static int  names_alloced = 0;
  32. static int  no_names = 0;
  33. static char **names = NULL;
  34. static int  lines_alloced = 0;
  35. static int  no_lines = 0;
  36. static char **lines = NULL;
  37.  
  38. /* Open a Pure-C help source file */
  39. int open_source(const char *filename)
  40. {
  41.   if (source != NULL)
  42.     fclose(source);
  43.   if ((source = fopen(filename, "rt")) == NULL)
  44.   {
  45.     fprintf(stderr, "could not open file %s\n", filename);
  46.     return(-1);
  47.   }
  48.   state = STATE_SKIP;
  49.   return(0);
  50. } /* End of open_source() */
  51.  
  52. /* Read in the current file; search for the next header and return */
  53. /* the page names of the new page just found.                      */
  54. char **get_next_header(void)
  55. {
  56.   char line[BUFSIZ];
  57.   int  done = 0, paren_level = 0;
  58.  
  59.   if (source == NULL)
  60.     return(NULL);
  61.   
  62.   dispose_names();
  63.   
  64.   fgets(line, BUFSIZ, source);
  65.   while (!feof(source) && !done)
  66.   {
  67.     char *ptr1 = line, *ptr2;
  68.     
  69.     switch(state)
  70.     {
  71.       case STATE_SKIP:
  72.         if (strstr(ptr1,  "screen") == NULL)
  73.           break;
  74.         ptr1 += strlen("screen");
  75.         state = STATE_HEADER;
  76.         /* FALL-THROUGH */
  77.       case STATE_HEADER:
  78.         while (*ptr1 != 0x00)
  79.         {
  80.           if (*ptr1 == '"')
  81.           {
  82.             if ((ptr2 = strchr(ptr1 + 1, '"')) == NULL)
  83.             {
  84.               fprintf(stderr, "error in source file\n");
  85.               exit(1);
  86.             }
  87.             *ptr2 = 0x00;
  88.             add_name(ptr1 + 1);
  89.             ptr1 = ptr2 + 1;
  90.           }
  91.           else if (*ptr1 == '(')
  92.           {
  93.             paren_level += 1;
  94.             ptr1 += 1;
  95.           }
  96.           else if (*ptr1 == ')')
  97.           {
  98.             paren_level -= 1;
  99.             if (paren_level == 0)
  100.             {
  101.               state = STATE_BODY;
  102.               done = 1;
  103.               break;
  104.             }
  105.             ptr1 += 1;
  106.           }
  107.           else
  108.             ptr1 += 1;
  109.         }
  110.         break;
  111.       case STATE_BODY:
  112.         if ((ptr1 = strstr(line, "\\end")) == NULL)
  113.           break;
  114.         if (ptr1 == line)
  115.         {
  116.           state = STATE_SKIP;
  117.           break;
  118.         }
  119.         while ((ptr1 > line) && (*(ptr1 - 1) == '\\'))
  120.           ptr1 -= 2;
  121.         if ((ptr1 == line) || (*(ptr1 - 1) != '\\'))
  122.           state = STATE_SKIP;
  123.         break;
  124.     } /* End of switch(state) */
  125.     if (!done)
  126.         fgets(line, BUFSIZ, source);
  127.   } /* End of while() */
  128.   
  129.   if (done)
  130.     return(names);
  131.   return(NULL);
  132. } /* End of get_next_header() */
  133.  
  134. /* Get the position of the file pointer of the source file now open */
  135. long get_position(void)
  136. {
  137.   if (source == NULL)
  138.     return(0);
  139.   else
  140.     return(ftell(source));
  141. } /* End of get_position() */
  142.  
  143. /* Return the body of the current page */
  144. char **get_body(void)
  145. {
  146.   char line[BUFSIZ];
  147.   int  done = 0;
  148.   
  149.   if ((source == NULL) || (state != STATE_BODY))
  150.     return(NULL);
  151.  
  152.   dispose_lines();
  153.  
  154.   fgets(line, BUFSIZ, source);
  155.   while (!feof(source) && !done)
  156.   {
  157.     char *ptr1 = line;
  158.     
  159.     if ((ptr1 = strstr(line, "\\end")) == NULL)
  160.       add_line(line);
  161.     else
  162.     {
  163.       if (ptr1 == line)
  164.       {
  165.         state = STATE_SKIP;
  166.         add_line(line);
  167.         done = 1;
  168.       }
  169.       else
  170.       {
  171.         char *ptr2 = ptr1;
  172.         
  173.         while ((ptr1 > line) && (*(ptr1 - 1) == '\\'))
  174.           ptr1 -= 2;
  175.         if ((ptr1 == line) || (*(ptr1 - 1) != '\\'))
  176.         {
  177.           state = STATE_SKIP;
  178.           done = 1;
  179.           *ptr2 = 0x00;
  180.         }
  181.         add_line(line);
  182.       }
  183.     } /* End of else (\\end in line) */
  184.     if (!done)
  185.         fgets(line, BUFSIZ, source);
  186.   } /* End of while() */
  187.   
  188.   if (done)
  189.     return(lines);
  190.   return(NULL);
  191. } /* End of get_body() */
  192.  
  193. /* Close a Pure-C help source file and deallocate defined strings */
  194. void close_source(void)
  195. {
  196.   if (source != NULL)
  197.   {
  198.     fclose(source);
  199.     source = NULL;
  200.     state = STATE_SKIP;
  201.     dispose_names();
  202.     dispose_lines();
  203.   }
  204. } /* End of close_source() */
  205.  
  206. /* Dispose of the contents of the names[] array (not the array itself) */
  207. static void dispose_names(void)
  208. {
  209.   int counter = 0;
  210.   
  211.   while (counter < no_names)
  212.   {
  213.     free(names[counter]);
  214.     names[counter++] = NULL;
  215.   }
  216.   no_names = 0; 
  217. } /* End of dispose_names() */
  218.  
  219. /* Add a name to the names[] array */
  220. static void add_name(const char *page)
  221. {
  222.   if (names_alloced - 1 <= no_names)
  223.   {
  224.     names_alloced += 20;
  225.     if ((names = realloc(names, names_alloced * sizeof(char *))) == NULL)
  226.     {
  227.       fprintf(stderr, "out of memory\n");
  228.       exit(1);
  229.     }
  230.   }
  231.   
  232.   names[no_names++] = strdup(page);
  233.   names[no_names] = NULL;
  234. } /* End of add_name() */
  235.  
  236. /* Dispose of the contents of the lines[] array (not the array itself) */
  237. static void dispose_lines(void)
  238. {
  239.   int counter = 0;
  240.   
  241.   while (counter < no_lines)
  242.   {
  243.     free(lines[counter]);
  244.     lines[counter++] = NULL;
  245.   }
  246.   no_lines = 0; 
  247. } /* End of dispose_lines() */
  248.  
  249. /* Add a line to the lines[] array */
  250. static void add_line(const char *line)
  251. {
  252.   if (lines_alloced - 1 <= no_lines)
  253.   {
  254.     lines_alloced += 20;
  255.     if ((lines = realloc(lines, lines_alloced * sizeof(char *))) == NULL)
  256.     {
  257.       fprintf(stderr, "out of memory\n");
  258.       exit(1);
  259.     }
  260.   }
  261.   
  262.   lines[no_lines++] = strdup(line);
  263.   lines[no_lines] = NULL;
  264. } /* End of add_line() */
  265.  
  266.