//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include <stdio.h>
#include <stdlib.h>

#include "FGUISourceViewer.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TGUIViewSource *GUIViewSource;
//---------------------------------------------------------------------------
static int filesize(FILE *fd)
{
    int iPos = ftell(fd);
    fseek(fd,-1,SEEK_END);
    int iSize = ftell(fd);
	fseek(fd,iPos,SEEK_SET);
    return(iSize+1);
}
//---------------------------------------------------------------------------
__fastcall TGUIViewSource::TGUIViewSource(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void gui_ViewSource(AnsiString _asFilename)
{
	Application->CreateForm(__classid(TGUIViewSource), &GUIViewSource);

    GUIViewSource->Caption = "View Source: " + _asFilename;


    FILE* fd = fopen(_asFilename.c_str(),"rb");
    if (fd != NULL)
    {
    	int iLen = filesize(fd);
    	char* szBuffer = (char*)malloc(iLen);
        fread(szBuffer,iLen,1,fd);
    	fclose(fd);

        	GUIViewSource->mem_SourceViewer->Text = szBuffer;

        free(szBuffer);
    }


	GUIViewSource->ShowModal();
	delete GUIViewSource;
}
//---------------------------------------------------------------------------
