home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / code / stubshack / HeapGraph / !HeapDisp / c / ScanFile < prev    next >
Encoding:
Text File  |  1994-11-24  |  2.8 KB  |  109 lines

  1. #include <string.h>
  2.  
  3. #include "DeskLib:File.h"
  4. #include "DeskLib:LinkList.h"
  5.  
  6. #include "Shell.TextRect.h"
  7. #include "Shell.Printf.h"
  8.  
  9. /*#define HEAPGRAPH*/
  10. #include "HeapGraph.HeapGraph.h"
  11.  
  12. #include "Structs.h"
  13. #include "ScanFile.h"
  14. #include "StartApp.h"
  15. #include "AddBlock.h"
  16. #include "MoveBlock.h"
  17. #include "RemoveBloc.h"
  18. #include "StatDispl.h"
  19.  
  20. #define BUFFERLEN    256
  21. #define code_MAXLENGTH    (4*3+1)
  22.  
  23. /*char HeapGraph_filename[ 256];*/
  24.     /* This is only included here because when amu-ing a ddt version    */
  25.     /* of this program, the linker expects every extern to be present.    */
  26.     /* In HeapGraph.h, HeapGraph_filename is declared, but !HeapDisp isn't    */
  27.     /* linked with HeapGraph, so we need to make our own HeapGraph_filename    */
  28.  
  29. void    ReadFromFile(
  30.         file_handle file, const char *filename, int filelen, apps_anchorblock *apps
  31.         )
  32. {
  33. app_block    *app;
  34. char        buffer[ 1+BUFFERLEN];
  35. int        bufferpos;
  36. BOOL        statschanged = FALSE;
  37. UNUSED( filelen);
  38.  
  39. /*Shell_Printf( "Reading from file '%s', file handle %i\n", filename, file);*/
  40.  
  41. for ( app=LinkList_FirstItem( &apps->anchor); app; app = LinkList_NextItem( app))    {
  42.     if ( !strcmp( app->filename, filename))    break;
  43.     }
  44.  
  45. if (!app)    app = StartApp( filename, apps);
  46.  
  47.  
  48. bufferpos = 0;
  49. for(;;)    {
  50.     int    byte = File_ReadByte( file);
  51.     /*Shell_Printf( "Read byte %i, file_lasterror=%p\n", byte, file_lasterror);*/
  52.  
  53.     if (file_lasterror)    break;
  54.  
  55.     if ( byte==HeapGraph_COLOUR)    {    /* Code used by ACELib to change colour of    */
  56.         File_ReadByte( file);        /* text. Not possible with Shell.        */
  57.         }
  58.  
  59.     if ( byte==HeapGraph_MALLOC)    {
  60.         int    pos    = File_ReadInt( file);
  61.         int    size    = File_ReadInt( file);
  62.         int    ref    = File_ReadInt( file);
  63.         if (!file_lasterror && pos)    {
  64.             AddBlock( app, pos, size, ref);
  65.             statschanged = TRUE;
  66.             /*Shell_TextRectPrintf( app->textrect, "MALLOC\n");*/
  67.             }
  68.         }
  69.  
  70.     else if ( byte==HeapGraph_REALLOC)    {
  71.         int    oldpos    = File_ReadInt( file);
  72.         int    newpos    = File_ReadInt( file);
  73.         int    newsize    = File_ReadInt( file);
  74.         if (!file_lasterror && newpos)    {
  75.             if ( oldpos != 0)    MoveBlock( app, oldpos, newpos, newsize);
  76.             else            AddBlock( app, newpos, newsize, 0);
  77.             statschanged = TRUE;
  78.             /*Shell_TextRectPrintf( app->textrect, "REALLOC\n");*/
  79.             }
  80.         }
  81.  
  82.     else if ( byte==HeapGraph_FREE)    {
  83.         int    oldpos    = File_ReadInt( file);
  84.         if (!file_lasterror)    {
  85.             if ( oldpos != 0)    RemoveBlock( app, oldpos);
  86.             statschanged = TRUE;
  87.             /*Shell_TextRectPrintf( app->textrect, "FREE\n");*/
  88.             }
  89.         }
  90.  
  91.     else    {
  92.         buffer[ bufferpos++] = byte;
  93.         if ( bufferpos == BUFFERLEN)    {
  94.             buffer[ bufferpos] = 0;
  95.             Shell_TextRectPrint( app->textrect, buffer);
  96.             bufferpos = 0;
  97.             }
  98.         }
  99.     }
  100.  
  101. buffer[ bufferpos] = 0;
  102. if ( bufferpos>0)    buffer[ bufferpos-1] = 0;    /* Last chr from File_ReadInt is rubbish*/
  103. Shell_TextRectPrint( app->textrect, buffer);
  104. /*fprintf( stderr, buffer);*/
  105. if ( statschanged)    UpdateAppStatsDisplay( app);
  106. return;
  107. }
  108.  
  109.