home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / text / adformat / adformat.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  9KB  |  273 lines

  1. /****************************************************************************
  2.  *                                                                          *
  3.  *  Program     :   ADFormat                                                *
  4.  *  Revision    :   1.0                                                     *
  5.  *  Version     :   $VER: ADFormat.c                                        *
  6.  *  Copyright   :   © 1993 DIgital DImensions                               *
  7.  *  Author      :   Hans-Jörg Frieden, of DIgital DImensions                *
  8.  *                                                                          *
  9.  *  Revision History                                                        *
  10.  *  Date        Revision    Comment                                         *
  11.  *  ----------------------------------------------------------------------  *
  12.  *  15-Aug-93   1.0         Created file. First working version             *
  13.  *                                                                          *
  14.  ****************************************************************************/
  15.  
  16. #include "global.h"
  17.  
  18. static char *version = "$VER: ADFormat 1.0";
  19.  
  20. static char *BackLinkChars = " @{\" Back to Autodoc-it \" Link \"Autodoc-it/main\"}\n"
  21.                              "---------------------------------------------------------------------\n";
  22.  
  23. char in[256],out[256];
  24. int col;
  25. BOOL verbose;
  26. BOOL backlink;
  27. BOOL NormConv;
  28.  
  29. #define TEMPLATE (APTR)"FROM/A,TO/K,COL/K/N,VERBOSE/S,BACKLINK/S"
  30. #define T_FROM      0
  31. #define T_TO        1
  32. #define T_COL       2
  33. #define T_VERBOSE   3
  34. #define T_BACKLINK  4
  35. #define T_COUNT     5
  36. LONG opts[T_COUNT];
  37.  
  38. #define GETNEXT c=FGetC(fhi); x=c; Write(fho,&x,1);
  39.  
  40. struct ADEntry { 
  41.     struct ADEntry *next;
  42.     char Title[80];
  43.     UWORD TLeng;
  44.     char Link[80];
  45.     UWORD LLeng;
  46. };
  47.  
  48. struct ADEntry root,*list;
  49.  
  50. struct ADEntry *MakeEntry(char *title, char *link) {
  51.     struct ADEntry *ret;
  52.     ret=(struct ADEntry *)AllocVec(sizeof(struct ADEntry),MEMF_ANY);
  53.     if (ret) {
  54.         ret->next=NULL;
  55.         strncpy(ret->Title,title,80);
  56.         ret->TLeng=strlen(title);
  57.         strncpy(ret->Link,link,80);
  58.         ret->LLeng=strlen(link);
  59.         return ret;
  60.     } else return ret;
  61. }
  62.  
  63. BOOL BuildList(BPTR fh) {
  64.     ULONG c;
  65.     char title[80]; char link[80];
  66.     int t,l;
  67.     struct ADEntry *ad;
  68.     list=&root; root.next=NULL;
  69.     do {
  70.         c=0;
  71.         do {
  72.             while (c!='@') c=FGetC(fh);
  73.             c=FGetC(fh);
  74.             if (c=='e' || c=='E') {
  75.                 c=FGetC(fh);
  76.                 if (c=='n' || c=='N') {
  77.                     c=FGetC(fh);
  78.                     if (c=='d' || c=='D') {
  79.                         c=FGetC(fh);
  80.                         if (c=='n' || c=='N') {
  81.                             c=FGetC(fh);
  82.                             if (c=='o' || c=='O') {
  83.                                 c=FGetC(fh);
  84.                                 if (c=='d' || c=='D') {
  85.                                     c=FGetC(fh);
  86.                                     if (c=='e' || c=='E') {
  87.                                         return TRUE;
  88.                                     }
  89.                                 }
  90.                             }
  91.                         }
  92.                     }
  93.                 }
  94.             } else {
  95.                 while (c!='"') c=FGetC(fh);
  96.                 t=0;
  97.                 c=FGetC(fh);
  98.                 while (c!='"') {
  99.                     title[t++]=(char)c;
  100.                     c=FGetC(fh);
  101.                 }
  102.                 title[t]=0;
  103.                 c=FGetC(fh); c=FGetC(fh);
  104.                 while (c!='"') c=FGetC(fh);
  105.                 l=0;
  106.                 c=FGetC(fh);
  107.                 while (c!='"') {
  108.                     link[l++]=(char)c;
  109.                     c=FGetC(fh);
  110.                 }
  111.                 link[l]=0;
  112.                 ad=MakeEntry(title,link);
  113.                 if (!ad) return FALSE;
  114.                 list->next=ad;
  115.                 list=list->next;
  116.             }
  117.         } while (c!=-1);
  118.     } while (c!=-1);
  119.     if (c==-1) return FALSE;
  120.     else return TRUE;
  121. }
  122.  
  123. BOOL SeekMainNode(BPTR fhi, BPTR fho) {
  124.     ULONG c;
  125.     char x;
  126.     BOOL ret=FALSE;
  127.     do {
  128.         GETNEXT
  129.         if (c=='@') {
  130.             GETNEXT 
  131.             if (c=='N' || c=='n') {
  132.                 GETNEXT
  133.                 if (c=='O' || c=='o') {
  134.                     GETNEXT
  135.                     if (c=='D' || c=='d') {
  136.                         GETNEXT
  137.                         if (c=='E' || c=='e') {
  138.                             GETNEXT
  139.                             while (c==' ') GETNEXT
  140.                             if (c=='M' || c=='m') {
  141.                                 GETNEXT
  142.                                 if (c=='A' || c=='a') {
  143.                                     GETNEXT
  144.                                     if (c=='I' || c=='i') {
  145.                                         GETNEXT
  146.                                         if (c=='N' || c=='n') {
  147.                                             ret=TRUE;
  148.                                             while (c!='\n') {GETNEXT}
  149.                                         }
  150.                                     }
  151.                                 }
  152.                             }
  153.                         }
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.         if (c==-1) {
  159.             ret=FALSE;
  160.             break;
  161.         }
  162.     } while (ret!=TRUE);
  163.     return ret;
  164. }
  165.  
  166. void CopyRest(BPTR fhi, BPTR fho) {
  167.     ULONG c=0;
  168.     while (c!=-1) {
  169.         c=FGetC(fhi);
  170.         FPutC(fho,(char)c);
  171.     }
  172. }
  173.  
  174. void DoConvert(char *in, char *out) {
  175.     BPTR fhi,fho;
  176.     BOOL bl;
  177.     struct ADEntry *max;
  178.     struct ADEntry *ad;
  179.     char buffer[256];
  180.     ULONG ColWidth;
  181.     ULONG colnr;
  182.     fhi=Open((APTR)in,MODE_OLDFILE);
  183.     if (fhi) {
  184.         fho=Open((APTR)out,MODE_NEWFILE);
  185.         if (fho) {
  186.             bl=SeekMainNode(fhi,fho);
  187.             if (bl) {
  188.                 if (backlink) Write(fho,BackLinkChars,strlen(BackLinkChars));
  189.                 bl=BuildList(fhi);
  190.                 if (bl) {
  191.                     ad=root.next;
  192.                     root.TLeng=0;
  193.                     max=&root;
  194.                     while (ad->next) {
  195.                         if ( (ad->TLeng) > (max->TLeng) ) max=ad;
  196.                         ad=ad->next;
  197.                     }
  198.                     ColWidth=max->TLeng+1;
  199.                     if (verbose==TRUE) printf("Longest: %s\n",max->Title);
  200.                     ad=root.next;
  201.                     colnr=0;
  202.                     while (ad->next) {
  203.                         sprintf(buffer," @{\"%-*s\" Link \"%s\"}",ColWidth,ad->Title,ad->Link);
  204.                         FWrite(fho,buffer,strlen(buffer),1);
  205.                         colnr++;
  206.                         if (colnr==col) {
  207.                             FPutC(fho,'\n');
  208.                             colnr=0;
  209.                         }
  210.                         ad=ad->next;
  211.                     }
  212.                     FPutC(fho,'\n');
  213.                     FWrite(fho,(APTR)"@ENDNODE\n",8,1);
  214.                     CopyRest(fhi,fho);
  215.                 } else printf("Error reading %s\n",in);
  216.             } else printf("Cannot locate MAIN node\n");
  217.             Close(fho);
  218.         } else printf("Cannot open Output file\n");
  219.         Close(fhi);
  220.     } else printf("File %s not found\n",in);
  221. }
  222.  
  223. void CopyFile(char *to, char *from) {
  224.     BPTR fhi,fho;
  225.     ULONG c=0;
  226.     fhi=Open((APTR)to,MODE_OLDFILE);
  227.     if (verbose) printf("Copying: %s -> %s\n",to,from);
  228.     if (fhi) {
  229.         fho=Open((APTR)from,MODE_NEWFILE);
  230.         if (fho) {
  231.             while (c!=-1) {
  232.                 c=FGetC(fhi);
  233.                 FPutC(fho,(char)c);
  234.             }
  235.             Close(fho);
  236.         } else printf("Cannot overwrite/create %s\n",to);
  237.         Close(fhi);
  238.     } else printf("File %s not found\n",from);
  239. }
  240.  
  241. void main(void) {
  242.     struct RDArgs *rda;
  243.     rda=ReadArgs(TEMPLATE,opts,NULL);
  244.     NormConv=TRUE;
  245.     if (rda) {
  246.         if (opts[T_COL]) col=*(LONG *)(opts[T_COL]); else col=1;
  247.         strncpy(in,(char *)opts[T_FROM],256);
  248.         if (opts[T_VERBOSE]) verbose=TRUE; else verbose=FALSE;
  249.         if (opts[T_BACKLINK]) backlink=TRUE; else backlink=FALSE;
  250.         if (opts[T_TO]) strncpy(out,(char *)opts[T_TO],256);
  251.         else {
  252.             strcpy(out,"adfXXX.XXX");
  253.             mktemp(out);
  254.             NormConv=FALSE;
  255.         }
  256.         FreeArgs(rda);
  257.         if (strcmp(in,out)==0) {
  258.             strcpy(out,"adfXXX.XXX");
  259.             mktemp(out);
  260.             NormConv=FALSE;
  261.         }
  262.         DoConvert(in,out);
  263.         if (NormConv==FALSE) {
  264.             CopyFile(out,in);
  265.             DeleteFile((APTR)out);
  266.         }
  267.     } else {
  268.         PrintFault(IoErr(),NULL);
  269.         exit(IoErr());
  270.     }
  271.     exit(0);
  272. }
  273.