home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / clibs / HeapGraph / !HeapDisp / c / ScanFile < prev    next >
Encoding:
Text File  |  1994-09-02  |  2.4 KB  |  104 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. #include "HeapGraph.HeapGraph.h"
  10.  
  11. #include "Structs.h"
  12. #include "ScanFile.h"
  13. #include "StartApp.h"
  14. #include "AddBlock.h"
  15. #include "MoveBlock.h"
  16. #include "RemoveBloc.h"
  17. #include "StatDispl.h"
  18.  
  19. #define BUFFERLEN    256
  20. #define code_MAXLENGTH    (4*3+1)
  21.  
  22.  
  23.  
  24. void    ReadFromFile(
  25.         file_handle file, const char *filename, int filelen, apps_anchorblock *apps
  26.         )
  27. {
  28. app_block    *app;
  29. char        buffer[ 1+BUFFERLEN];
  30. int        bufferpos;
  31. BOOL        statschanged = FALSE;
  32. UNUSED( filelen);
  33.  
  34. /*Shell_Printf( "Reading from file '%s', file handle %i\n", filename, file);*/
  35.  
  36. for ( app=LinkList_FirstItem( &apps->anchor); app; app = LinkList_NextItem( app))    {
  37.     if ( !strcmp( app->filename, filename))    break;
  38.     }
  39.  
  40. if (!app)    app = StartApp( filename, apps);
  41.  
  42.  
  43. bufferpos = 0;
  44. for(;;)    {
  45.     int    byte = File_ReadByte( file);
  46.     /*Shell_Printf( "Read byte %i, file_lasterror=%p\n", byte, file_lasterror);*/
  47.  
  48.     if (file_lasterror)    break;
  49.  
  50.     if ( byte==HeapGraph_COLOUR)    {    /* Code used by ACELib to change colour of    */
  51.         File_ReadByte( file);        /* text. Not possible with Shell.        */
  52.         }
  53.  
  54.     if ( byte==HeapGraph_MALLOC)    {
  55.         int    pos    = File_ReadInt( file);
  56.         int    size    = File_ReadInt( file);
  57.         int    ref    = File_ReadInt( file);
  58.         if (!file_lasterror && pos)    {
  59.             AddBlock( app, pos, size, ref);
  60.             statschanged = TRUE;
  61.             /*Shell_TextRectPrintf( app->textrect, "MALLOC\n");*/
  62.             }
  63.         }
  64.  
  65.     else if ( byte==HeapGraph_REALLOC)    {
  66.         int    oldpos    = File_ReadInt( file);
  67.         int    newpos    = File_ReadInt( file);
  68.         int    newsize    = File_ReadInt( file);
  69.         if (!file_lasterror && newpos)    {
  70.             if ( oldpos != 0)    MoveBlock( app, oldpos, newpos, newsize);
  71.             else            AddBlock( app, newpos, newsize, 0);
  72.             statschanged = TRUE;
  73.             /*Shell_TextRectPrintf( app->textrect, "REALLOC\n");*/
  74.             }
  75.         }
  76.  
  77.     else if ( byte==HeapGraph_FREE)    {
  78.         int    oldpos    = File_ReadInt( file);
  79.         if (!file_lasterror)    {
  80.             if ( oldpos != 0)    RemoveBlock( app, oldpos);
  81.             statschanged = TRUE;
  82.             /*Shell_TextRectPrintf( app->textrect, "FREE\n");*/
  83.             }
  84.         }
  85.  
  86.     else    {
  87.         buffer[ bufferpos++] = byte;
  88.         if ( bufferpos == BUFFERLEN)    {
  89.             buffer[ bufferpos] = 0;
  90.             Shell_TextRectPrint( app->textrect, buffer);
  91.             bufferpos = 0;
  92.             }
  93.         }
  94.     }
  95.  
  96. buffer[ bufferpos] = 0;
  97. if ( bufferpos>0)    buffer[ bufferpos-1] = 0;    /* Last chr from File_ReadInt is rubbish*/
  98. Shell_TextRectPrint( app->textrect, buffer);
  99. /*fprintf( stderr, buffer);*/
  100. if ( statschanged)    UpdateAppStatsDisplay( app);
  101. return;
  102. }
  103.  
  104.