home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / util / edit / jade / src / makedoc.c < prev    next >
C/C++ Source or Header  |  1994-04-28  |  3KB  |  119 lines

  1. /* makedoc.c -- Program to strip doc-strings from C source
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4.    This file is part of Jade.
  5.  
  6.    Jade is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    Jade is distributed in the hope that it will be useful, but
  12.    WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with Jade; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. extern void exit(int);
  24.  
  25. static void
  26. usage(void)
  27. {
  28.     fputs("usage: makedoc [-a] doc-file header-file [src-files...]\n", stderr);
  29.     exit(1);
  30. }
  31.  
  32. static int
  33. scanfile(FILE *src, FILE *docf, FILE *hdrf, int docindex)
  34. {
  35.     char buf[512];
  36.     while(fgets(buf, 512, src))
  37.     {
  38.     char *start = strstr(buf, "::doc:");
  39.     if(start)
  40.     {
  41.         int startcol = start - buf;
  42.         start += 6;
  43.         while(*start && *start != ':')
  44.         start++;
  45.         *start = 0;
  46.         fprintf(hdrf, "#define DOC_%s %d\n", buf + startcol + 6,  docindex);
  47.         while(fgets(buf, 512, src))
  48.         {
  49.         int linelen = strlen(buf) - startcol;
  50.         if(linelen > 0)
  51.         {
  52.             if(buf[startcol] == ':')
  53.             break;
  54.             fputs(buf + startcol, docf);
  55.             docindex += linelen;
  56.         }
  57.         else
  58.         {
  59.             fputc('\n', docf);
  60.             docindex++;
  61.         }
  62.         }
  63.         /* back over trailing newline */
  64.         fseek(docf, -1, 1 /*SEEK_CUR*/);
  65.         fputc('\f', docf);
  66.     }
  67.     }
  68.     return(docindex);
  69. }
  70.  
  71. int
  72. main(int ac, char **av)
  73. {
  74.     FILE *docfile, *hdrfile;
  75.     char append_p = 0;
  76.     ac--;
  77.     av++;
  78.     if(ac >= 1 && !strcmp("-a", av[0]))
  79.     {
  80.     append_p = 1;
  81.     ac--; av++;
  82.     }
  83.     if(ac < 2)
  84.     usage();
  85.     docfile = fopen(*av++, append_p ? "r+" : "w");
  86.     hdrfile = fopen(*av++, append_p ? "r+" : "w");
  87.     ac -= 2;
  88.     if(!(docfile && hdrfile))
  89.     {
  90.     fprintf(stderr, "can't open output files.\n");
  91.     exit(2);
  92.     }
  93.     if(append_p)
  94.     {
  95.     fseek(docfile, 0, 2 /*SEEK_END*/);
  96.     fseek(hdrfile, 0, 2 /*SEEK_END*/);
  97.     }
  98.     else
  99.     fputs("/* GENERATED BY MAKEDOC -- DO NOT EDIT! */\n\n", hdrfile);
  100.     if(!ac)
  101.     scanfile(stdin, docfile, hdrfile, 0);
  102.     else
  103.     {
  104.     int docindex = ftell(docfile);
  105.     while(ac)
  106.     {
  107.         FILE *file = fopen(*av, "r");
  108.         if(file)
  109.         {
  110.         docindex = scanfile(file, docfile, hdrfile, docindex);
  111.         fclose(file);
  112.         }
  113.         ac--;
  114.         av++;
  115.     }
  116.     }
  117.     exit(0);
  118. }
  119.