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

  1. #include "global.h"
  2. #include "misc.h"
  3. #include "adfile.h"
  4.  
  5. /***********************************************/
  6.  
  7. void FreeList(struct AnyNode *List)
  8.  
  9. {
  10.    struct AnyNode *NextNode;
  11.  
  12.    while (List)
  13.       {
  14.          NextNode=List->Next;
  15.          FreeVec(List);
  16.          List=NextNode;
  17.       }
  18. }
  19.  
  20. /***********************************************/
  21.  
  22. char *FindSlash(char *String)
  23.  
  24. {
  25.    while (*String && *String!='/') String++;
  26.    return(String);
  27. }
  28.  
  29. /***********************************************/
  30.  
  31. BOOL Break(void)
  32.  
  33. {
  34.    if (SetSignal(0,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  35.       {
  36.          printf("*** User abort.\n");
  37.          return(TRUE);
  38.       }
  39.    return(FALSE);
  40. }
  41.  
  42. /***********************************************/
  43.  
  44. void WriteError(char *FormatString, ...)
  45.  
  46. {
  47.    VPrintf(FormatString,((LONG *)&FormatString)+1);
  48.    if (ErrorFile)
  49.       {
  50.          VFPrintf(ErrorFile,FormatString,((LONG *)&FormatString)+1);
  51.       }
  52. }
  53.  
  54. /***********************************************/
  55.  
  56. struct AnyNode **ListToArray(struct AnyNode *List)
  57.  
  58. {
  59.    int ArrayEntries;
  60.    int Index, Current;
  61.    struct AnyNode *Node;
  62.    struct AnyNode **Array;
  63.  
  64.    Node=List;
  65.    ArrayEntries=0;
  66.    while (Node)
  67.       {
  68.          ArrayEntries++;
  69.          Node=Node->Next;
  70.       }
  71.    if (Array=AllocVec((ArrayEntries+1)*4,0))
  72.       {
  73.          Array[0]=(struct AnyNode *)ArrayEntries;
  74.          Node=List;
  75.          for (Index=1; Node; Index++)
  76.             {
  77.                Array[Index]=Node;
  78.                Node=Node->Next;
  79.             }
  80.          for (Current=1; Current<ArrayEntries; Current++)
  81.             {
  82.                for (Index=Current+1; Index<=ArrayEntries; Index++)
  83.                   {
  84.                      if (strcmp(Array[Index]->Name,Array[Current]->Name)<0)
  85.                         {
  86.                            Node=Array[Index];
  87.                            Array[Index]=Array[Current];
  88.                            Array[Current]=Node;
  89.                         }
  90.                   }
  91.                if (Break())
  92.                   {
  93.                      FreeVec(Array);
  94.                      return(NULL);
  95.                   }
  96.             }
  97.       }
  98.    else
  99.       {
  100.          PrintFault(ERROR_NO_FREE_STORE,NULL);
  101.       }
  102.    return(Array);
  103. }
  104.  
  105. /***********************************************/
  106.  
  107. BOOL ReadLine(BOOL AcceptEOF, BOOL StoreLF)
  108.  
  109. {
  110.    int Index;
  111.    long Character;
  112.  
  113.    Index=0;
  114.    while (TRUE)
  115.       {
  116.          if (Break())
  117.             {
  118.                return(FALSE);
  119.             }
  120.          Character=FGetC(CurrentFile);
  121.          switch(Character)
  122.             {
  123.                case -1:     if (IoErr())
  124.                                {
  125.                                   PrintFault(IoErr(),CurrentSource);
  126.                                }
  127.                             else
  128.                                {
  129.                                   if (!AcceptEOF)
  130.                                      {
  131.                                         WriteError("Error: Unexpected end of file %s\n"
  132.                                                    "       Operation aborted.\n",CurrentSource);
  133.                                      }
  134.                                }
  135.                             return(FALSE);
  136.  
  137.                case '\n':   if (StoreLF)
  138.                                {
  139.                                   Buffer[Index++]='\n';
  140.                                }
  141.                             Buffer[Index]='\0';
  142.                             CurrentLine++;
  143.                             return(TRUE);
  144.  
  145.                case '\x0c': if (Arguments.CED)
  146.                                {
  147.                                   CurrentLine++;
  148.                                }
  149.                             if (Index)
  150.                                {
  151.                                   WriteError("Error: Unexpected FormFeed in file %s"
  152.                                              "       Operation aborted.\n",CurrentSource);
  153.                                   return(FALSE);
  154.                                }
  155.                             else
  156.                                {
  157.                                   Buffer[0]=Character;
  158.                                   return(TRUE);
  159.                                }
  160.  
  161.                default:     Buffer[Index++]=Character;
  162.                             if (Index==MAX_LEN)
  163.                                {
  164.                                   WriteError("Error: Line too long in file %s\n"
  165.                                              "       Operation aborted.\n",CurrentSource);
  166.                                   return(FALSE);
  167.                                }
  168.                             break;
  169.             }
  170.       }
  171. }
  172.  
  173. /***********************************************/
  174.  
  175. BOOL WriteSomething(char *FormatString, ...)
  176.  
  177. {
  178.    if (VFPrintf(HypertextFile,FormatString,((LONG *)&FormatString)+1)==-1)
  179.       {
  180.          PrintFault(IoErr(),HypertextFilename);
  181.          return(FALSE);
  182.       }
  183.    return(TRUE);
  184. }
  185.  
  186. /***********************************************/
  187.  
  188. void DosError(char *Header)
  189.  
  190. {
  191.    PrintFault(IoErr(),Header);
  192.    CloseAll();
  193.    exit(10);
  194. }
  195.  
  196. /***********************************************/
  197.  
  198. long fgetc(void)
  199.  
  200. {
  201.    while (TRUE)
  202.       {
  203.          if (Break())
  204.             {
  205.                return(-1);
  206.             }
  207.          if (Buffer[BufferIndex])
  208.             {
  209.                return((long)((unsigned char)(Buffer[BufferIndex++])));
  210.             }
  211.          if (!ReadLine(TRUE,TRUE)) return(-1);
  212.          BufferIndex=0;
  213.       }
  214. }
  215.  
  216. /***********************************************/
  217.  
  218. struct AnyNode *SearchNameCase(struct AnyNode **Array, char *Name)
  219.  
  220. {
  221.    int OldIndex;
  222.    int Upper, Lower;
  223.    int Index;
  224.    int Result;
  225.  
  226.    Lower=1;
  227.    Upper=(int)Array[0]+1;
  228.    Index=0;
  229.    do
  230.       {
  231.          OldIndex=Index;
  232.          Index=(Upper+Lower)/2;
  233.          if (!(Result=strcmp(Array[Index]->Name,Name)))
  234.             {
  235.                return(Array[Index]);
  236.             }
  237.          if (Result<0)
  238.             {
  239.                Lower=Index;
  240.             }
  241.          else
  242.             {
  243.                Upper=Index;
  244.             }
  245.       }
  246.    while (Index!=OldIndex);
  247.    return(NULL);
  248. }
  249.  
  250. /***********************************************/
  251.  
  252. struct AnyNode *SearchNameNoCase(struct AnyNode **Array, char *Name)
  253.  
  254. {
  255.    int Upper, Lower;
  256.    int Index;
  257.    int OldIndex;
  258.    int Result;
  259.  
  260.    Lower=1;
  261.    Upper=(int)Array[0]+1;
  262.    Index=0;
  263.    do
  264.       {
  265.          OldIndex=Index;
  266.          Index=(Upper+Lower)/2;
  267.          if (!(Result=Stricmp(Array[Index]->Name,Name)))
  268.             {
  269.                return(Array[Index]);
  270.             }
  271.          if (Result<0)
  272.             {
  273.                Lower=Index;
  274.             }
  275.          else
  276.             {
  277.                Upper=Index;
  278.             }
  279.       }
  280.    while (Index!=OldIndex);
  281.    return(NULL);
  282. }
  283.  
  284. /***********************************************/
  285.  
  286. BPTR CreateFile(char *Name)
  287.  
  288. {
  289.    char *Temp;
  290.    BPTR DirLock;
  291.  
  292.    Temp=Name;
  293.    while (*Temp)
  294.       {
  295.          if (*Temp=='/')
  296.             {
  297.                *Temp='\0';
  298.                if (!(DirLock=Lock(Name,ACCESS_READ)))
  299.                   {
  300.                      if (!(DirLock=CreateDir(Name)))
  301.                         {
  302.                            *Temp='/';
  303.                            return(NULL);
  304.                         }
  305.                   }
  306.                UnLock(DirLock);
  307.                *Temp='/';
  308.             }
  309.          Temp++;
  310.       }
  311.    return(Open(Name,MODE_NEWFILE));
  312. }
  313.