home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / papers / RadBib.shar / roffbib.c < prev   
C/C++ Source or Header  |  1991-02-06  |  2KB  |  107 lines

  1. /*
  2.  * A very simple version of ROFFBIB
  3.  *
  4.  * Paul Heckbert, 1985
  5.  */
  6.  
  7. #include <stdio.h>
  8.  
  9. typedef struct {
  10.     char *code,*pref,*post;
  11. } reftrans;
  12.  
  13. static reftrans reftab[] = {
  14.     "%A",    "",        ",",        /* author (possibly several) */
  15.     "%B",    "\\fI",        "\\fP,",    /* book title */
  16.     "%C",    "",        ",",        /* city */
  17.     "%D",    "",        ",",        /* date */
  18.     "%E",    "",        " ed.,",    /* editor of book */
  19.     "%G",    "NTIS ",    ",",        /* government order number */
  20.     "%I",    "",        ",",        /* issuer (publisher) */
  21.     "%J",    "\\fI",        "\\fP,",    /* journal/magazine */
  22.     "%K",    "{",        "},",        /* keywords */
  23.     "%N",    "no. ",        ",",        /* number (vol 5 no 3) */
  24.     "%O",    "(",        "),",        /* other (misc info) */
  25.     "%P",    "pp. ",        ",",        /* pages */
  26.     "%R",    "",        ",",        /* report number */
  27.     "%T",    "``",        "'',",        /* title */
  28.     "%V",    "vol. ",    ",",        /* volume */
  29.     "%Y",    "[",        "],",        /* owner(s) of copy */
  30.     "%Z",    "\\fI",        "\\fP,"        /* comments */
  31. };
  32.  
  33. #define NREF (sizeof reftab / sizeof reftab[0])
  34. static int lno;
  35.  
  36. main(ac,av)
  37. int ac;
  38. char **av;
  39. {
  40.     char line[500];
  41.     int i,j;
  42.     FILE *fp;
  43.  
  44.     if (ac==1) fp = stdin;
  45.     else if ((fp = fopen(av[1],"r"))==NULL) {
  46.     fprintf(stderr,"Can't find %s\n",av[1]);
  47.     exit(1);
  48.     }
  49.     lno = 0;
  50.     while (!feof(fp)) {
  51.     for (j=0; getline(fp,line)>0; j++) {
  52.         if (strlen(line)<4 || line[0]!='%' || line[2]!=' ') {
  53.         fprintf(stderr,"line %d is (%s)\n",lno,line);
  54.         printf("\n%s\n",line);
  55.         continue;
  56.         }
  57.         for (i=0; i<NREF && line[1]!=reftab[i].code[1]; i++);
  58.         if (i>=NREF) {
  59.         fprintf(stderr,"line %d bad code (%s)\n",lno,line);
  60.         printf("\n%s\n",line);
  61.         continue;
  62.         }
  63.         if (j==0 && (line[1]=='A' || line[1]=='E'))
  64.         surfirst(line);
  65.         printf("%s%s%s\n",reftab[i].pref,line+3,reftab[i].post);
  66.     }
  67.     printf("\n");
  68.     }
  69.     fclose(fp);
  70. }
  71.  
  72. getline(fp,str)
  73. FILE *fp;
  74. char *str;
  75. {
  76.     char *s;
  77.     int c;
  78.  
  79.     s = str;
  80.     do {
  81.     while ((c = getc(fp))!='\n' && c!=EOF) *s++ = c;
  82.     *s++ = ' ';
  83.     lno++;
  84.     } while ((*s++ = c = getc(fp))!='%' && c!='\n' && c!=EOF);
  85.     ungetc(c,fp);
  86.     s[-2] = 0;
  87.     return strlen(str);
  88. }
  89.  
  90. surfirst(str0)                    /* put surname first */
  91. char *str0;
  92. {
  93.     char *str,*s,buf[500];
  94.  
  95.     str = str0+3;
  96.     s = str+strlen(str)-1;
  97.     do {
  98.     while (s>=str && *s!=' ') s--;
  99.     } while (s>str && *--s==',');
  100.     if (s>str) {
  101.     s += 2;
  102.     s[-1] = 0;
  103.     sprintf(buf,"%%%c \\fB%s\\fP, %s",str0[1],s,str);
  104.     strcpy(str0,buf);
  105.     }
  106. }
  107.