home *** CD-ROM | disk | FTP | other *** search
/ Amiga GigaPD 3 / Amiga_GigaPD_v3_3of3.iso / fred_fish / fish_601-700_lha / fish_674.lha / HEXTRACT / SOURCE / extract.c < prev    next >
C/C++ Source or Header  |  1993-06-02  |  8KB  |  268 lines

  1. /****************************************************************************
  2. *   Extract.c                                    *
  3. *                                           *
  4. *   Chas A. Wyndham     15th May 1992.                         *
  5. *                                        *    
  6. *   Compiles under Lattice 5 and links with Decomp.o to make Hextract, a    *
  7. *   programme for accessing header-file data.                    *
  8. *                                        *
  9. *   This code is Freeware like Hextract itself.  You are welcome to improve *
  10. *   it - but please send me a copy.                        *
  11. *                                        *
  12. ****************************************************************************/
  13.           
  14. #include "exec/types.h"
  15. #include "exec/memory.h"
  16. #include "graphics/gfxbase.h"
  17. #include "libraries/dos.h"
  18. #include "intuition/intuition.h"
  19. #include "intuition/intuitionbase.h"
  20. #include "string.h"
  21. #include "stdio.h"
  22.  
  23. struct IntuitionBase *IntuitionBase ;
  24. struct GfxBase     *GfxBase ;
  25. struct Screen      *screendata ; 
  26. FILE               *fe ;
  27. struct FileHandle  *window, *fi, *fo ;
  28. char               buffer[100], line[256] ;
  29. int                size, setcase = 0, found = 0, located = 0 ;
  30. USHORT chip ptrImage[] = {  
  31.      0x0000, 0x0000,
  32.      0x6018, 0x0000,
  33.      0xc00c, 0x0000,
  34.      0xc30c, 0x0000,
  35.      0xc78c, 0x0000,
  36.      0xcccc, 0x0000,
  37.      0x7878, 0x0000,
  38.      0x3030, 0x0000,
  39.      0x0000, 0x0000,  };
  40.  
  41. struct FileLock *lock ;  
  42. void   search(), showlib() ;
  43. int    cleanup() ;
  44.  
  45. main(argc, argv)
  46. int   argc ;
  47. char  *argv[] ;
  48. {
  49.    char         ch = NULL, header[2], *ibuf, path[100] ;
  50.    int          form, i = 0, result = 0, length ;
  51.    
  52.    
  53.    IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0) ;
  54.    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0) ;
  55.  
  56.    screendata = (struct Screen *)AllocMem(30, MEMF_PUBLIC) ;
  57.    GetScreenData (screendata, 30, WBENCHSCREEN, NULL) ;
  58.         
  59.    stcgfp (path, argv[0]) ;
  60.    strcat (path, "headers.z") ;
  61.    
  62.    form = GfxBase->NormalDisplayRows ;
  63.    if (screendata->Height > 511)
  64.     window = (struct FileHandle *)Open("CON:0/0/640/512/Hextract V1.1", MODE_NEWFILE) ;
  65.    else if (screendata->Height >399)
  66.     window = (struct FileHandle *)Open("CON:0/0/640/400/Hextract V1.1", MODE_NEWFILE) ;
  67.    else if (screendata->Height >255)
  68.     window = (struct FileHandle *)Open("CON:0/0/640/256/Hextract V1.1", MODE_NEWFILE) ;
  69.    else 
  70.     window = (struct FileHandle *)Open("CON:0/0/640/200/Hextract V1.1", MODE_NEWFILE) ;
  71.  
  72.    if ((fi = (struct FileHandle *)Open(path, MODE_OLDFILE)) == NULL) 
  73.      { puts ("No headers.z file\n") ;
  74.        cleanup()  ;
  75.      }
  76.  
  77.    if ((fo = (struct FileHandle *)Open ("ram:headers.t", MODE_NEWFILE)) == NULL)
  78.      { puts ("Can't create ram: file\n") ;
  79.        Close(fi) ;
  80.        cleanup() ;
  81.      }
  82.     
  83.     Read(fi, header, 2) ;
  84.       
  85.     if ((header[0] != 'L') || (header[1] != 'H'))
  86.       {
  87.         if((ibuf = (char *)AllocMem(4096, MEMF_PUBLIC)) == NULL)
  88.           cleanup() ;
  89.         do
  90.          { length = Read (fi, ibuf, 4096) ;
  91.            Write (fo, ibuf, length) ;
  92.          } while (length) ;
  93.         FreeMem (ibuf, 4096) ;
  94.       }
  95.  
  96.     else result = decompress (fi, fo) ;
  97.  
  98.     Close (fi) ;
  99.     Close (fo) ;
  100.     if      (result == 1)  puts ("Decompression failed\n") ;
  101.     else if (result == 2)  puts ("Out of memory\n") ;
  102.     
  103.    fe = fopen("ram:headers.t", "r+") ;
  104.    Write (window, "                   Enter 'quit' to exit\n\n", 41) ;
  105.    Write (window, "  Enter symbol\n\n   ", 18) ;
  106.    onbreak(&cleanup) ;
  107.       
  108.    while (1)  
  109.     { if (WaitForChar (window, 999999999)) 
  110.       { Read (window, &ch, 1) ;
  111.         buffer[i++] = ch ;
  112.         if (!strcmp (buffer, "quit"))   break ;
  113.         if (ch == '\n')
  114.         { 
  115.          buffer[--i] = '\0' ;
  116.          if (i)
  117.           { SetPointer (IntuitionBase->ActiveWindow, &ptrImage, 7, 16, -4, -4) ;
  118.             search () ; 
  119.             memset (buffer, '\0', 100) ;
  120.             ClearPointer(IntuitionBase->ActiveWindow) ;
  121.             Write (window, "\n  Enter symbol\n\n   ", 19) ;
  122.           }
  123.          i = 0 ;
  124.         }
  125.       }
  126.     }
  127.    cleanup() ;
  128. }
  129.  
  130.  
  131. int cleanup ()
  132. {
  133.      if (fe)     { fclose (fe) ;  fe = 0 ;  }
  134.                    DeleteFile ("ram:headers.t") ; 
  135.      if (lock)  UnLock(lock) ;
  136.      if (window)  Close (window) ;
  137.      if (GfxBase != NULL) CloseLibrary(GfxBase);
  138.      if (IntuitionBase != NULL) CloseLibrary(IntuitionBase);
  139.      exit(0);
  140. }
  141.  
  142.  
  143. fgts(buf)
  144. char  *buf ;
  145. {
  146.    register char   c ;
  147.    register int    i = 0 ;
  148.  
  149.    memset (buf, '\0', 256) ;
  150.    c = getc (fe) ;
  151.    while ((c != '\n') && (c != EOF))
  152.     { buf[i++] = c ;
  153.       c = getc (fe) ;
  154.     }
  155.    buf [i] = '\n' ;
  156.    if (c == EOF)  return(0);
  157.    else  return(1) ;
  158. }
  159.  
  160.  
  161. void search()
  162. {  
  163.    char   *tok, *tok2, temp[250], *match, *buf1, source[40], change = 0 ; 
  164.    int    numbrackets = 0, libh = 0 ;
  165.  
  166.    size = 0 ;  located = 0 ;
  167.    while (fgts (line) != 0)
  168.     { buf1 = stpblk(line) ;
  169.       if (!change)
  170.         if (stcpma (buf1, "FFFFFF"))   change = 1 ; 
  171.       if (change)
  172.        { if (stcpm (buffer, ".resource", &match) || stcpm (buffer, ".device", &match)
  173.                                          || stcpm (buffer, ".library", &match))
  174.           { showlib() ;
  175.             break ;
  176.           }  
  177.          if (stcpm (buf1, ".resource", &match) || stcpm (buf1, ".device", &match)
  178.                                          || stcpm (buf1, ".library", &match))
  179.           { tok = strtok (buf1, " \n") ;
  180.             strcpy (source, tok) ;
  181.             libh = 1 ;
  182.             continue ;
  183.        }  }
  184.      
  185.       strcpy (temp, buf1) ;
  186.       tok = strtok (temp, " \n") ;
  187.       if (astcsma(temp, buffer)) 
  188.        { located = 1 ;        
  189.          strcpy (temp, line) ;
  190.          tok = strtok (line, " \n") ;
  191.          tok2 = strtok (NULL, "\n") ;
  192.          Write (window, "\n   ", 4) ; 
  193.          Write (window, tok2, strlen(tok2)) ;
  194.          if (libh)
  195.           { Write (window, "  ", 2) ;
  196.             Write (window, source, strlen(source)) ;
  197.           }
  198.          Write (window, "\n", 1) ;
  199.  
  200.          if (stcpm (temp, "struct", &match)) 
  201.           { fgts (temp) ;
  202.             if (strchr (temp, '{'))
  203.              { numbrackets++ ;
  204.                Write (window, "    ", 4) ; 
  205.                Write (window, temp, strlen(temp)) ;
  206.                while  (numbrackets) 
  207.                 { fgts(temp) ;
  208.  
  209.                   if (strchr (temp, '{'))
  210.                    { numbrackets++ ;
  211.                      Write (window, "    ", 4) ;
  212.                      Write (window, temp, strlen(temp)) ;
  213.                      continue ;
  214.                    }
  215.                   else if (strchr (temp, '}'))
  216.                    { numbrackets-- ;
  217.                      Write (window, temp, strlen(temp)) ;
  218.                      continue ;
  219.                    }
  220.  
  221.                   tok = strtok (temp, " \0") ;
  222.                   tok2 = strtok (NULL, " \0") ;
  223.                   Write (window, "    ", 4) ;
  224.                   Write (window, tok2, strlen(tok2)) ;
  225.  
  226.                   do
  227.                    { tok2 = strtok (NULL, " \0") ;
  228.                      if (stcpm (tok2, "struct", &match))
  229.                        { Write (window, "\n", 1) ;
  230.                          break ;
  231.                        } 
  232.                      Write (window, " ", 1) ;
  233.                      Write (window, tok2, strlen(tok2)) ;
  234.                    } while (tok2) ; 
  235.              }  }
  236.           }
  237.  
  238.          tok = strtok (line, " \n") ;
  239.          tok2 = strtok (NULL, "\n") ;
  240.          Write (window, "\n       ", 8) ; 
  241.          Write (window, tok2, strlen(tok2)) ;
  242.     }  }
  243.       
  244.    if (!located)  Write (window, "        Not found\n", 18) ;
  245.    rewind (fe) ; 
  246. }
  247.  
  248.  
  249. void showlib()
  250. {
  251.   char  *tok1, *tok2, buf[256] ;
  252.   int num, i ;
  253.  
  254.   while (!stcpma (line, buffer))
  255.     fgts (line) ;
  256.   tok1 = strtok (line, " \n") ;
  257.   tok2 = strtok (NULL, " \n") ;
  258.   num = atoi (tok2) ;
  259.   for (i = 0 ; i < num ; ++i)
  260.    { fgts (buf) ;
  261.      tok1 = strtok (buf, " \n") ;
  262.      tok2 = strtok (NULL, "\0") ;
  263.      Write (window, "   ", 3) ;
  264.      Write (window, tok2, strlen(tok2)) ;
  265.    }
  266.   located = 1 ;
  267. }
  268.