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 / pureind.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-20  |  3.4 KB  |  120 lines

  1. /********************************************************************/
  2. /* pureind.c - build index for a group of pure-c help source files  */
  3. /*                                                                  */
  4. /* Copyright (c) 1993 by Hildo Biersma - Evil Eye Software          */
  5. /*                            e-mail: boender@dutiws.twi.tudelft.nl */
  6. /*        Evil Eye Software - ``Software with a Purpose''           */
  7. /*                                                                  */
  8. /* Freeware - do with this what you like, but leave my name.        */
  9. /********************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "pureind.h"
  15. #include "pure_lib.h"
  16.  
  17. /* Local function prototypes */
  18. void index_file(const char *filename);
  19. int  compare_pagenames(const void *page1, const void *page2);
  20. void add_page_entry(const char *name, size_t pos);
  21.  
  22. /* Global variables */
  23. struct file_record files[MAX_FILES];
  24. struct page_record *pages = NULL;
  25. long   no_files = 0, no_pages = 0, alloced_pages = 0;
  26. char   *program_name;
  27.  
  28. /* Index a single pure-c help source file */
  29. void index_file(const char *filename)
  30. {
  31.   char **header_names;
  32.   
  33.   if (open_source(filename) != 0)
  34.     return;
  35.   strncpy(files[no_files++].name, filename, FILENAME_SIZE);
  36.  
  37.   while ((header_names = get_next_header()) != NULL)
  38.   {
  39.     int counter = 0;
  40.     size_t pos;
  41.  
  42.     while (header_names[counter] != NULL)
  43.     {
  44.         pos = get_position();
  45.       add_page_entry(header_names[counter++], pos);
  46.      }
  47.   }
  48.   
  49.   close_source();
  50. } /* End of index_file() */
  51.  
  52. /* Compare two page names */
  53. int compare_pagenames(const void *page1,
  54.                       const void *page2)
  55. {
  56.   return(strncmp(((struct page_record *)page1)->name,
  57.                  ((struct page_record *)page2)->name, PAGENAME_SIZE));
  58. } /* End of compare_pagenames() */
  59.  
  60. /* Add a page entry */
  61. void add_page_entry(const char *name, size_t position)
  62. {
  63.   char buf[PAGENAME_SIZE + 1];
  64.   
  65.   strncpy(buf, name, PAGENAME_SIZE);
  66.   buf[PAGENAME_SIZE] = 0x00;
  67.   strlwr(buf);
  68.   
  69.   if (alloced_pages == no_pages)
  70.   {
  71.     alloced_pages += 100;
  72.     if ((pages = realloc(pages, sizeof(struct page_record) *
  73.         alloced_pages)) == NULL)
  74.     {
  75.       fprintf(stderr, "%s: out of memory\n", program_name);
  76.       exit(1);
  77.     }
  78.   }
  79.   strncpy(pages[no_pages].name, buf, PAGENAME_SIZE);
  80.   pages[no_pages].fileno = no_files - 1;
  81.   pages[no_pages].pos = position;
  82.   no_pages++;
  83. } /* End of add_page_entry() */
  84.  
  85. void main(int argc, char *argv[])
  86. {
  87.   int  counter = 1;
  88.   FILE *index;
  89.  
  90.   program_name = argv[0];
  91.   if (argc > MAX_FILES + 1)
  92.   {
  93.     fprintf(stderr, "%s: too many files\n", program_name);
  94.     exit(1);
  95.   }
  96.   
  97.   while (counter < argc)
  98.     index_file(argv[counter++]);
  99.  
  100.   /* Yes, this is a *lot* slower than sorting pointers to the records */
  101.   qsort(pages, no_pages, sizeof(struct page_record), 
  102.         compare_pagenames);
  103.  
  104.   if ((index = fopen(INDEXFILE_NAME, "wb")) == NULL)
  105.   {
  106.     fprintf(stderr, "%s: could not open file %s\n",
  107.             program_name, INDEXFILE_NAME);
  108.     exit(1);
  109.   }
  110.  
  111.   fwrite(INDEXFILE_HEADER, sizeof(char), 4, index);
  112.   fwrite(&no_files, 1, sizeof(long), index);
  113.   fwrite(&no_pages, 1, sizeof(long), index);
  114.   fwrite(files, no_files, sizeof(struct file_record), index);
  115.   fwrite(pages, no_pages, sizeof(struct page_record), index);
  116.   fclose(index);
  117.  
  118.   exit(0);
  119. } /* End of main() */
  120.