/*
    ####             #    #     # #
    #   #            #    #       #          The FreeWare C library for 
    #   #  ##   ###  #  # #     # ###             RISC OS machines
    #   # #  # #     # #  #     # #  #   ___________________________________
    #   # ####  ###  ##   #     # #  #                                      
    #   # #        # # #  #     # #  #    Please refer to the accompanying
    ####   ### ####  #  # ##### # ###    documentation for conditions of use
    ________________________________________________________________________

    File:    File.AllocLoad.c
    Author:  Copyright  1995 Julian Smith
    Version: 1.00 (19 Nov 1995)
*/

#include <stdlib.h>

#include "Desk.File.h"
#include "Desk.DeskMem.h"
#include "Desk.Error2.h"
#include "Desk.JumpRaw.h"




char*	Desk_File_AllocLoad( const char* filename, int* lengthptr)
{
char	*buffer = NULL;
int	length;

if ( !Desk_File_Exists( filename))	Desk_Error2_HandleTextf( "File '%s' doesn't exist", filename);

Desk_JumpAuto_Try	{
	length = Desk_File_GetLength( filename);
	buffer = Desk_DeskMem_Malloc( 1+length);	/* Add one to length so Desk_File_AllocLoad0 can add a terminating 0	*/
	Desk_File_LoadTo( filename, buffer, NULL);
	if (lengthptr)	*lengthptr = length;
	
	}

Desk_JumpAuto_Catch	{
	Desk_DeskMem_Free( buffer);
	Desk_Error2_ReThrow();
	}
Desk_JumpAuto_EndCatch

return buffer;
}
