home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / utilities / hypertext / adtoht / source / convertad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-22  |  19.3 KB  |  512 lines

  1. /* Convert all autodoc files to hypertext files */
  2.  
  3. #include "global.h"
  4. #include "misc.h"
  5. #include "readdir.h"
  6. #include "incfile.h"
  7. #include "adfile.h"
  8. #include "shortad.h"
  9. #include "convertad.h"
  10.  
  11. /***********************************************/
  12.  
  13. static BOOL ConvertContents(void)
  14.  
  15. {
  16.    BOOL FirstLine;
  17.    struct AutodocNode *AutodocNode;
  18.    char *Temp;
  19.  
  20.    do
  21.       {
  22.          if (!ReadLine(FALSE,FALSE)) return(FALSE);
  23.       }
  24.    while (Stricmp(Buffer,"TABLE OF CONTENTS"));
  25.    FirstLine=TRUE;
  26.    do
  27.       {
  28.          if (!ReadLine(FALSE,FALSE)) return(FALSE);
  29.          if (Buffer[0]!='\x0c' && Buffer[0])
  30.             {
  31.                if (FirstLine && !WriteSomething("@NODE Main \x22")) return(FALSE);
  32.                for (Temp=Buffer; *Temp && *Temp!='/'; Temp++)
  33.                   {
  34.                      if (FirstLine && !WriteSomething("%lc",*Temp)) return(FALSE);
  35.                   }
  36.                if (FirstLine && !WriteSomething("\x22\n")) return(FALSE);
  37.                if (!*Temp)
  38.                   {
  39.                      WriteError("Error: Missing '/' in file %s, line %ld\n"
  40.                                 "       Operation aborted.\n",CurrentSource,CurrentLine);
  41.                      return(FALSE);
  42.                   }
  43.                FirstLine=FALSE;
  44.                if (!(AutodocNode=(struct AutodocNode *)SearchNameCase(AutodocArray,Buffer)))
  45.                   {
  46.                      WriteError("Error: File %s changed.\n"
  47.                                 "       Operation aborted.\n",CurrentSource);
  48.                      return(FALSE);
  49.                   }
  50.                Temp=AutodocNode->Node.Name;
  51.                if (Arguments.ShortTOC)
  52.                   {
  53.                      if (Temp=FindSlash(Temp))
  54.                         {
  55.                            Temp++;
  56.                         }
  57.                      else
  58.                         {
  59.                            Temp=AutodocNode->Node.Name;
  60.                         }
  61.                   }
  62.                if (!WriteSomething("    @{\x22 %s",Temp)) return(FALSE);
  63.                if (AutodocNode->Type==1 && !WriteSomething("()")) return(FALSE);
  64.                if (!WriteSomething(" \x22 LINK \x22%s\x22}\n",FindSlash(AutodocNode->Node.Name)+1)) return(FALSE);
  65.             }
  66.       }
  67.    while (Buffer[0]!='\x0c');
  68.    if (!WriteSomething("@ENDNODE\n\n")) return(FALSE);
  69.    return(TRUE);
  70. }
  71.  
  72. /***********************************************/
  73.  
  74. static BOOL ConvertNode(void)
  75.  
  76. {
  77.    int Index;
  78.    int OldBufferIndex;
  79.    struct AnyNode *AnyNode;
  80.    char *Temp;
  81.    char *Temp2;
  82.    int Periods;
  83.    int Slashes;
  84.    long Character;
  85.    int UppercaseLetters;
  86.    int Underscores;
  87.    BOOL StructMode;
  88.    int SeeAlsoMode;
  89.  
  90.    StructMode=FALSE;
  91.    SeeAlsoMode=0;
  92.    Temp=FindSlash(Buffer);
  93.    if (!*Temp)
  94.       {
  95.          WriteError("Error: Missing '/' in file %s, line %ld\n"
  96.                     "       Operation aborted.",CurrentSource,CurrentLine);
  97.          return(FALSE);
  98.       }
  99.    *Temp='\0';
  100.    stpcpy(CurrentUnit,Buffer);
  101.    *Temp='/';
  102.    Temp2=Temp;
  103.    do
  104.       Temp++;
  105.    while (*Temp && *Temp!=' ' && *Temp!='\t' && *Temp!='/');
  106.    if (*Temp=='/')
  107.       {
  108.          stpcpy(Temp2,Temp);
  109.       }
  110.    else
  111.       {
  112.          *Temp='\0';
  113.       }
  114.    if (!(AnyNode=SearchNameCase(AutodocArray,Buffer)))
  115.       {
  116.          if (!(AnyNode=SearchNameNoCase(AutodocArray,Buffer)))
  117.             {
  118.                WriteError("Error: Node %s not present in table of contents.\n"
  119.                           "       File: %s\n"
  120.                           "       Operation aborted.\n",Buffer,CurrentSource);
  121.                return(FALSE);
  122.             }
  123.          WriteError("Warning: Node %s not present in table of contents.\n"
  124.                     "         File %s\n"
  125.                     "         Assuming %s instead.\n",Buffer,CurrentSource,AnyNode->Name);
  126.       }
  127.    CurrentAutodocNode=(struct AutodocNode *)AnyNode;
  128.    if (CurrentAutodocNode->Type==1)
  129.       {
  130.          if (!WriteSomething("@NODE \x22%s\x22 \x22%s()\x22\n",FindSlash(CurrentAutodocNode->Node.Name)+1,CurrentAutodocNode->Node.Name)) return(FALSE);
  131.       }
  132.    else
  133.       {
  134.          if (!WriteSomething("@NODE \x22%s\x22 \x22%s\x22\n",FindSlash(CurrentAutodocNode->Node.Name)+1,CurrentAutodocNode->Node.Name)) return(FALSE);
  135.       }
  136.    BufferIndex=0;
  137.    Buffer[0]='\0';
  138.    while (TRUE)
  139.       {
  140.          if (Break()) return(FALSE);
  141.          OldBufferIndex=BufferIndex;
  142.          Character=fgetc();
  143.          WordBuffer[0]='\0';
  144.          if ((Character>='a' && Character<='z') || (Character>='A' && Character<='Z'))
  145.             {
  146.                Underscores=0;
  147.                UppercaseLetters=0;
  148.                Index=0;
  149.                Periods=0;
  150.                Slashes=0;
  151.                while ((Character>='a' && Character<='z') ||
  152.                       (Character>='A' && Character<='Z') ||
  153.                       (Character>='0' && Character<='9') ||
  154.                       (Character=='_') || 
  155.                       (Character=='/') ||
  156.                       (Character=='.'))
  157.                   {
  158.                      WordBuffer[Index++]=Character;
  159.                      if (Index==MAX_LEN)
  160.                         {
  161.                            WriteError("Error, line %ld: line is too long.\n"
  162.                                       "       Operation aborted.\n",CurrentLine);
  163.                            return(FALSE);
  164.                         }
  165.                      if (Character=='.')
  166.                         {
  167.                            Periods++;
  168.                         }
  169.                      else if (Character=='/')
  170.                         {
  171.                            Slashes++;
  172.                         }
  173.                      else if (Character=='_')
  174.                         {
  175.                            Underscores++;
  176.                         }
  177.                      else if ((Character>='A' && Character<='Z') || (Character>='0' && Character<='9'))
  178.                         {
  179.                            UppercaseLetters++;
  180.                         }
  181.                      Character=fgetc();
  182.                   }
  183.                if (WordBuffer[Index-1]=='.')
  184.                   {
  185.                      Index--;
  186.                      BufferIndex--;
  187.                      Character='.';
  188.                   }
  189.                if (Character=='(')
  190.                   {
  191.                      StructMode=FALSE;
  192.                   }
  193.                WordBuffer[Index]='\0';
  194.                if (StructMode)
  195.                   {
  196.                      if (AnyNode=(struct AnyNode *)SearchNameCase(StructArray,WordBuffer))
  197. LinkStructure:          {
  198.                            if (!WriteSomething("@{\x22%s\x22 LINK \x22%s/MAIN\x22 %ld}",WordBuffer,((struct StructNode *)AnyNode)->File,((struct StructNode *)AnyNode)->Line)) return(FALSE);
  199.                            IncludeLinks++;
  200.                            WordBuffer[0]='\0';
  201.                            Index=0;
  202.                         }
  203.                      StructMode=FALSE;
  204.                   }
  205.                if (Index>=2)
  206.                   {
  207.                      if (!strcmp(WordBuffer,"struct"))
  208.                         {
  209.                            StructMode=TRUE;
  210.                            if (SeeAlsoMode==1) SeeAlsoMode=0;
  211.                         }
  212.                      else if (!Stricmp(WordBuffer+Index-2,".h"))
  213.                         {
  214.                            if (SeeAlsoMode==1) SeeAlsoMode=0;
  215.                            if (AnyNode=(struct AnyNode *)SearchNameNoCase(IncludeFileArray,WordBuffer))
  216.                               {
  217.                                  if (!WriteSomething("@{\x22%s\x22 LINK \x22%s/MAIN\x22 0}",WordBuffer,((struct FileNode *)AnyNode)->Node.Name)) return(FALSE);
  218.                                  IncludeLinks++;
  219.                                  WordBuffer[0]='\0';
  220.                               }
  221.                         }
  222.                      else if (Periods==1 && Slashes==1)
  223.                         {
  224.                            if (SeeAlsoMode==1) SeeAlsoMode=0;
  225.                            if (AnyNode=SearchNameCase(AutodocArray,WordBuffer))
  226. LinkAutodoc:                  {
  227.                                  if (CurrentAutodocNode!=(struct AutodocNode *)AnyNode)
  228.                                     {
  229.                                        if (!(Temp=FindSlash(((struct AutodocNode *)AnyNode)->Node.Name)))
  230.                                           {
  231.                                              WriteError("INTERNAL ERROR #1\n");
  232.                                              return(FALSE);
  233.                                           }
  234.                                        if (Character=='(')
  235.                                           {
  236.                                              if (fgetc()==')')
  237.                                                 {
  238.                                                    WordBuffer[Index++]='(';
  239.                                                    WordBuffer[Index++]=')';
  240.                                                    WordBuffer[Index]='\0';
  241.                                                    Character=-2;
  242.                                                 }
  243.                                              else
  244.                                                 {
  245.                                                    BufferIndex--;
  246.                                                 }
  247.                                           }
  248.                                        if (!strcmp(HypertextFilename,((struct AutodocNode *)AnyNode)->File))
  249.                                           {
  250.                                              if (!WriteSomething("@{\x22%s\x22 LINK \x22%s\x22 0}",WordBuffer,Temp+1)) return(FALSE);
  251.                                              InternalLinks++;
  252.                                              WordBuffer[0]='\0';
  253.                                           }
  254.                                        else
  255.                                           {
  256.                                              if (!WriteSomething("@{\x22%s\x22 LINK \x22%s%s\x22 0}",WordBuffer,((struct AutodocNode *)AnyNode)->File,Temp)) return(FALSE);
  257.                                              ExternalLinks++;
  258.                                              WordBuffer[0]='\0';
  259.                                           }
  260.                                     }
  261.                               }
  262.                            else
  263.                               {
  264.                                  Temp=WordBuffer+strlen(WordBuffer);
  265.                                  stpcpy(Temp,"A");
  266.                                  if (AnyNode=SearchNameCase(AutodocArray,WordBuffer))
  267.                                     {
  268.                                        *Temp='\0';
  269.                                        goto LinkAutodoc;
  270.                                     }
  271.                                  stpcpy(Temp,"Tags");
  272.                                  if (AnyNode=SearchNameCase(AutodocArray,WordBuffer))
  273.                                     {
  274.                                        *Temp='\0';
  275.                                        goto LinkAutodoc;
  276.                                     }
  277.                                  stpcpy(Temp,"TagList");
  278.                                  if (AnyNode=SearchNameCase(AutodocArray,WordBuffer))
  279.                                     {
  280.                                        *Temp='\0';
  281.                                        goto LinkAutodoc;
  282.                                     }
  283.                                  *Temp='\0';
  284.                               }
  285.                         }
  286.                      else if (!strcmp(WordBuffer,"SEE") && !SeeAlsoMode)
  287.                         {
  288.                            SeeAlsoMode=1;
  289.                         }
  290.                      else if (SeeAlsoMode==1)
  291.                         {
  292.                            if (!strcmp(WordBuffer,"ALSO"))
  293.                               {
  294.                                  SeeAlsoMode=2;
  295.                               }
  296.                            else
  297.                               {
  298.                                  SeeAlsoMode=0;
  299.                               }
  300.                         }
  301.                      else if (Character=='(' || UppercaseLetters+Underscores>1)
  302.                         {
  303.                            if (AnyNode=(struct AnyNode *)SearchShortAutodoc(WordBuffer))
  304.                               {
  305.                                  goto LinkAutodoc;
  306.                               }
  307.                            if (AnyNode=(struct AnyNode *)SearchNameCase(StructArray,WordBuffer))
  308.                               {
  309.                                  goto LinkStructure;
  310.                               }
  311.                            if (AnyNode=(struct AnyNode *)SearchNameCase(TypedefArray,WordBuffer))
  312.                               {
  313.                                  goto LinkStructure;
  314.                               }
  315.                            Temp=WordBuffer+strlen(WordBuffer);
  316.                            stpcpy(Temp,"A");
  317.                            if (AnyNode=SearchShortAutodoc(WordBuffer))
  318.                               {
  319.                                  *Temp='\0';
  320.                                  goto LinkAutodoc;
  321.                               }
  322.                            stpcpy(Temp,"Tags");
  323.                            if (AnyNode=SearchShortAutodoc(WordBuffer))
  324.                               {
  325.                                  *Temp='\0';
  326.                                  goto LinkAutodoc;
  327.                               }
  328.                            stpcpy(Temp,"TagList");
  329.                            if (AnyNode=SearchShortAutodoc(WordBuffer))
  330.                               {
  331.                                  *Temp='\0';
  332.                                  goto LinkAutodoc;
  333.                               }
  334.                            *Temp='\0';
  335.                         }
  336.                      else if (SeeAlsoMode==2)
  337.                         {
  338.                            if ((AnyNode=(struct AnyNode *)SearchShortAutodoc(WordBuffer)) && AnyNode!=(struct AnyNode *)CurrentAutodocNode)
  339.                               {
  340.                                  goto LinkAutodoc;
  341.                               }
  342.                         }
  343.                   }
  344.             }
  345.          if (WordBuffer[0] && !WriteSomething("%s",WordBuffer)) return(FALSE);
  346.          switch(Character)
  347.             {
  348.                case -1:     if (IoErr())
  349.                                {
  350.                                   return(FALSE);
  351.                                }
  352.                case '\x0c': return(WriteSomething("@ENDNODE\n\n"));
  353.  
  354.                case '\n':   for (Index=0; Index<BufferIndex-1; Index++)
  355.                                {
  356.                                   if (Buffer[Index]!=' ' && Buffer[Index]!='\t')
  357.                                      {
  358.                                         goto HandleSpace;
  359.                                      }
  360.                                }
  361.                             SeeAlsoMode=0;
  362.                             goto HandleSpace;
  363.  
  364.                default:     StructMode=FALSE;
  365.                             if (SeeAlsoMode==1) SeeAlsoMode=0;
  366. HandleSpace:   case ' ':
  367.                case '\t':   if (Character!=-2 && !WriteSomething("%lc",Character)) return(FALSE);
  368.                             break;
  369.             }
  370.       }
  371. }
  372.  
  373. /***********************************************/
  374.  
  375. static BOOL ConvertAutodoc(void)
  376.  
  377. {
  378.    char *AutodocName;
  379.    long AutodocNameLen;
  380.  
  381.    CurrentLine=0;
  382.    AutodocNameLen=500;
  383.    while (TRUE)
  384.       {
  385.          if (AutodocName=AllocVec(AutodocNameLen,0))
  386.             {
  387.                if (NameFromFH(CurrentFile,AutodocName,AutodocNameLen))
  388.                   {
  389.                      break;
  390.                   }
  391.                FreeVec(AutodocName);
  392.                if (IoErr()==ERROR_LINE_TOO_LONG)
  393.                   {
  394.                      AutodocNameLen<<=1;
  395.                   }
  396.                else
  397.                   {
  398.                      PrintFault(IoErr(),CurrentSource);
  399.                      return(FALSE);
  400.                   }
  401.             }
  402.          else
  403.             {
  404.                PrintFault(ERROR_NO_FREE_STORE,NULL);
  405.                return(FALSE);
  406.             }
  407.       }
  408.    AutodocNameLen=WriteSomething("@DATABASE \x22%s\x22\n"
  409.                                  "@MASTER \x22%s\x22\n"
  410.                                  "@REMARK created by ADtoHT V1.2 © 1993 Christian Stieber\n\n",HypertextFilename,AutodocName);
  411.    FreeVec(AutodocName);
  412.    if (AutodocNameLen)
  413.       {
  414.          if (!ConvertContents())
  415.             {
  416.                return(FALSE);
  417.             }
  418.          ExternalLinks=0;
  419.          IncludeLinks=0;
  420.          InternalLinks=0;
  421.          while (TRUE)
  422.             {
  423.                do
  424.                   {
  425.                      if (!ReadLine(TRUE,FALSE))
  426.                         {
  427.                            if (IoErr())
  428.                               {
  429.                                  return(FALSE);
  430.                               }
  431.                            else
  432.                               {
  433.                                  printf("         %ld links were created.\n"
  434.                                         "            %ld links within the database\n"
  435.                                         "            %ld links to other databases\n"
  436.                                         "            %ld links to include files\n",InternalLinks+ExternalLinks+IncludeLinks,InternalLinks,ExternalLinks,IncludeLinks);
  437.                                  return(TRUE);
  438.                               }
  439.                         }
  440.                      if (Buffer[0]=='\x0c')
  441.                         {
  442.                            WriteError("Error: Unexpected FormFeed in file %s\n"
  443.                                       "       Operation aborted.\n",CurrentSource);
  444.                            return(FALSE);
  445.                         }
  446.                   }
  447.                while (!Buffer[0]);
  448.                if (!ConvertNode()) return(FALSE);
  449.             }
  450.       }
  451.    else
  452.       {
  453.          return(FALSE);
  454.       }
  455. }
  456.  
  457. /***********************************************/
  458.  
  459. BOOL ConvertAutodocs(void)
  460.  
  461. {
  462.    BPTR OldCurrentDir;
  463.    struct FileNode *NextNode;
  464.    int Length;
  465.    BOOL Success;
  466.  
  467.    Success=TRUE;
  468.    while (AutodocFileList && Success)
  469.       {
  470.          CurrentSource=AutodocFileList->Node.Name;
  471.          Length=strlen(CurrentSource)-4;
  472.          if (HypertextFilename=AllocVec(Length+strlen(Arguments.Extension)+1,0))
  473.             {
  474.                __builtin_memcpy(HypertextFilename,CurrentSource,Length);
  475.                stpcpy(HypertextFilename+Length,Arguments.Extension);
  476.                printf("      Converting %s to %s\n",CurrentSource,HypertextFilename);
  477.                OldCurrentDir=CurrentDir(HypertextDir);
  478.                if (HypertextFile=CreateFile(HypertextFilename))
  479.                   {
  480.                      CurrentDir(AutodocDir);
  481.                      if (CurrentFile=Open(CurrentSource,MODE_OLDFILE))
  482.                         {
  483.                            Success=ConvertAutodoc();
  484.                            Close(CurrentFile);
  485.                         }
  486.                      else
  487.                         {
  488.                            PrintFault(IoErr(),CurrentSource);
  489.                            Success=FALSE;
  490.                         }
  491.                      Close(HypertextFile);
  492.                   }
  493.                else
  494.                   {
  495.                      PrintFault(IoErr(),HypertextFilename);
  496.                      Success=FALSE;
  497.                   }
  498.                CurrentDir(OldCurrentDir);
  499.                FreeVec(HypertextFilename);
  500.                NextNode=(struct FileNode *)(AutodocFileList->Node.Next);
  501.                FreeVec(AutodocFileList);
  502.                AutodocFileList=NextNode;
  503.             }
  504.          else
  505.             {
  506.                PrintFault(ERROR_NO_FREE_STORE,NULL);
  507.                Success=FALSE;
  508.             }
  509.       }
  510.    return(Success);
  511. }
  512.