home *** CD-ROM | disk | FTP | other *** search
/ The Best Internet Programs / BESTINTERNET.bin / latest / ged2ht20 / main.c < prev    next >
C/C++ Source or Header  |  1995-04-08  |  7KB  |  245 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "node.h"
  6. #include "read.h"
  7. #include "database.h"
  8. #include "output.h"
  9. #include "tags.h"
  10.  
  11. #define VERSION "2.0 (6 April 1995)"
  12. #define USAGE "Usage: %s [-Hciv][-d <max-per-directory>][-s <individual> ...][-u <URL template>][-f <file-template>][-t <individual-template>][-T <index-template>] [-- <gedcom-file> ...]\n", argv[0]
  13. #define OPTIONS " -v\t\t\tPrint version information.\n" \
  14. " -c\t\t\tDisable automatic capitalization of surnames.\n" \
  15. " -d max_per_directory\t\tSpecify number of individuals per subdirectory\n" \
  16. "\t\t\t(0 means no subdirectories)\n" \
  17. " -i\t\t\tCause an index file to be generated containing\n" \
  18. "\t\t\tall the individuals in the input.\n" \
  19. " -s individuals ...\tLimit the production of output files to a specified\n" \
  20. "\t\t\tset of zero or more selected individuals.\n" \
  21. " -u url_template\tSpecify a template string for the URL's used\n" \
  22. "\t\t\tin anchors in the output files (default '%%s.html').\n" \
  23. " -f file_template\tSpecify a template string for the names of the\n" \
  24. "\t\t\tHTML files (default '%%s.html').\n" \
  25. " -t individual_template\tSpecify an HTML template file for individuals.\n" \
  26. " -T index_template\tSpecify an HTML template file for the index.\n" \
  27. " -H\t\t\tPrint a brief message listing the available options.\n"
  28.  
  29. int generate_index;
  30. char **selected_individuals;
  31. struct node head;
  32.  
  33. main(int argc, char *argv[])
  34. {
  35.   struct node *np;
  36.   int i, optc;
  37.   extern char *optarg;
  38.   extern int optind;
  39.   FILE *gedcom_file;
  40.   int serial = 0;
  41. #ifdef MSDOS
  42.   int getopt(int argc, char *const *argv, const char *optstring);
  43.   extern char *optarg;
  44.   extern int optind;
  45. #endif
  46.  
  47.   validate_tags_tables();
  48. #ifdef MSDOS
  49.   if(argc <= 1) {
  50. #ifdef MSWINDOWS
  51.     fprintf(stderr, "Launch this program from the File Manager, using the\n");
  52.     fprintf(stderr, "'Run' item under the 'File' menu to specify command-line\n");
  53.     fprintf(stderr, "arguments.\n");
  54. #endif
  55.     fprintf(stderr, USAGE);
  56.     exit(1);
  57.   }
  58. #endif
  59.   while((optc = getopt(argc, argv, "Hviscd:u:h:f:t:T:")) != -1) {
  60.     FILE *tempf;
  61.     long size;
  62.     char *temps, *tempe, c;
  63.  
  64.     switch(optc) {
  65.     case 'v':    /* Version */
  66.       printf("GEDCOM to HTML translator, version %s by Gene Stark "
  67.          "(stark@cs.sunysb.edu)\n", VERSION);
  68.       exit(0);
  69.     case 'c':   /* Disable automatic capitalization of surnames */
  70.       capitalization = 0;
  71.       break;
  72.     case 'i':    /* Generate index */
  73.       generate_index = 1;
  74.       break;
  75.     case 's':    /* Generate record(s) for selected individual(s) */
  76.       {
  77.     int i = 0;
  78.     int j;
  79.     while(argv[optind+i] && argv[optind+i][0] != '-')
  80.       i++;
  81.     if(!(selected_individuals = malloc((i+1) * sizeof(char *))))
  82.       out_of_memory();
  83.     for (j = 0; j < i; j++)
  84.       selected_individuals[j] = argv[optind+j];
  85.     selected_individuals[j]=NULL;
  86.     optind += i;
  87.       }
  88.       break;
  89.     case 't':    /* Template file for individuals */
  90.     case 'T':    /* Template file for the index */
  91.       if((tempf = fopen(optarg, "r")) == NULL) {
  92.     fprintf(stderr, "Can't open template file '%s'\n", optarg);
  93.     break;
  94.       }
  95.       if(fseek(tempf, 0L, SEEK_END) == -1 || (size = ftell(tempf)) == -1){
  96.     fprintf(stderr, "Can't determine length of template file '%s'\n",
  97.         optarg);
  98.     fclose(tempf);
  99.     break;
  100.       }
  101.       rewind(tempf);
  102.       if((temps = malloc((size_t) size+1)) == NULL) {
  103.     fprintf(stderr, "Can't allocate memory for template string\n");
  104.     fclose(tempf);
  105.     break;
  106.       }
  107.       tempe = temps;
  108.       while((c = fgetc(tempf)) != EOF && tempe-temps <= size)
  109.     *tempe++ = c;
  110.       *tempe = '\0';
  111.       if(optc == 't')
  112.     individual_template = temps;
  113.       else
  114.     index_template = temps;
  115.       break;
  116.     case 'd':    /* Specify max per directory */
  117.       max_per_directory = atoi(optarg);
  118.       break;
  119.     case 'u':    /* Template for URL's within HTML anchors */
  120.       url_template = optarg;
  121.       break;
  122.     case 'f':    /* Template for file names */
  123.       file_template = optarg;
  124.       break;
  125.     case 'H':
  126.       printf(USAGE);
  127.       printf(OPTIONS);
  128.       exit(0);
  129.     case '?':
  130.     default:
  131.       fprintf(stderr, USAGE);
  132.       exit(1);
  133.     }
  134.   }
  135.   if(optind == argc) {
  136.     current_gedcom = "stdin";
  137.     current_lineno = 0;
  138.     read_gedcom(stdin, &head, 0);
  139.   } else {
  140.     for(np = &head ; optind < argc; optind++) {
  141.       current_gedcom = argv[optind];
  142.       current_lineno = 0;
  143.       if((gedcom_file = fopen(argv[optind], "r")) == NULL) {
  144.     fprintf(stderr, "Can't open GEDCOM file '%s'.\n", argv[optind]);
  145.     continue;
  146.       }
  147.       read_gedcom(gedcom_file, np, 0);
  148.       fclose(gedcom_file);
  149.       while(np->siblings)
  150.     np = np->siblings;
  151.     }
  152.   }
  153.   if(head.siblings == NULL) {
  154.     fprintf(stderr, "No valid GEDCOM lines found\n");
  155.     exit(1);
  156.   }
  157.   process_records(head.siblings);
  158.   link_records(head.siblings);
  159.   fprintf(stderr, "Processed %d GEDCOM lines", gedcom_lines);
  160.   if(total_individuals)
  161.     fprintf(stderr, ", %d individuals", total_individuals);
  162.   if(total_families)
  163.     fprintf(stderr, ", %d families", total_families);
  164.   if(total_events)
  165.     fprintf(stderr, ", %d events", total_events);
  166.   if(total_sources)
  167.     fprintf(stderr, ", %d sources", total_sources);
  168.   if(total_notes)
  169.     fprintf(stderr, ", %d notes", total_notes);
  170.   fprintf(stderr, "\n");
  171.   /*
  172.    * Determine individuals to be output, and assign them serial numbers.
  173.    */
  174.   for(i = 0; i < total_individuals; i++) {
  175.     char **av;
  176.  
  177.     if(selected_individuals != NULL) {
  178.       for(av = selected_individuals; *av != NULL; av++)
  179.     if(!strcmp(*av, all_individuals[i]->xref)) {
  180.       all_individuals[i]->serial = ++serial;
  181.     }
  182.     } else {
  183.       all_individuals[i]->serial = ++serial;
  184.     }
  185.   }
  186.   /*
  187.    * Generate index file
  188.    */
  189.   if (generate_index)
  190.     output_index(*all_individuals);
  191.   /*
  192.    * Output individuals
  193.    */
  194.   if(individual_template == NULL) {
  195.     if(max_per_directory)
  196.       individual_template = individual_template_subdir;
  197.     else
  198.       individual_template = individual_template_nosubdir;
  199.   }
  200.   for(i = 0; i < total_individuals; i++) {
  201.     if(all_individuals[i]->serial)
  202.       output_individual(all_individuals[i]);
  203.   }
  204.   exit(0);
  205. }
  206.  
  207. #ifdef MSDOS
  208. char *optarg;
  209. int optind = 1;
  210.  
  211. int getopt(int argc, char *const *argv, const char *optstring)
  212. {
  213.   char c;
  214.   if(optind >= argc || *argv[optind] != '-')
  215.     return(-1);
  216.   if(argv[optind][1] == '-') {
  217.     optind++;
  218.     return(-1);
  219.   }
  220.   for( ; *optstring != '\0'; optstring++) {
  221.     c = *optstring;
  222.     if(c == argv[optind][1]) {
  223.       optstring++;
  224.       optind++;
  225.       if(*optstring == ':') {
  226.     if(optind >= argc) {
  227.       fprintf(stderr, "Option '%c' requires an argument.\n", c);
  228.       return(':');
  229.     } else {
  230.       optarg = argv[optind];
  231.       optind++;
  232.       return(c);
  233.     }
  234.       } else {
  235.     return(c);
  236.       }
  237.     }
  238.   }
  239.   fprintf(stderr, "Unrecognized option: '%c'.\n", argv[optind][1]);
  240.   optind++;
  241.   return('?');
  242. }
  243. #endif
  244.  
  245.