picoc/platform/platform_unix.c
zik.saleeba 0fc32f5bc7 A bit of a reorganisation to make adding new platforms and
C standard library modules neater.


git-svn-id: http://picoc.googlecode.com/svn/trunk@427 21eae674-98b7-11dd-bd71-f92a316d2d60
2010-06-13 09:17:42 +00:00

86 lines
1.9 KiB
C

#include "../picoc.h"
/* a source file we need to clean up */
static char *CleanupText = NULL;
/* deallocate any storage */
void PlatformCleanup()
{
if (CleanupText != NULL)
free(CleanupText);
}
/* get a line of interactive input */
char *PlatformGetLine(char *Buf, int MaxLen)
{
fflush(stdout);
return fgets(Buf, MaxLen, stdin);
}
/* get a character of interactive input */
int PlatformGetCharacter()
{
fflush(stdout);
return getchar();
}
/* write a character to the console */
void PlatformPutc(unsigned char OutCh, union OutputStreamInfo *Stream)
{
putchar(OutCh);
}
/* read a file into memory */
char *PlatformReadFile(const char *FileName)
{
struct stat FileInfo;
char *ReadText;
FILE *InFile;
int BytesRead;
if (stat(FileName, &FileInfo))
ProgramFail(NULL, "can't read file %s\n", FileName);
ReadText = malloc(FileInfo.st_size + 1);
if (ReadText == NULL)
ProgramFail(NULL, "out of memory\n");
InFile = fopen(FileName, "r");
if (InFile == NULL)
ProgramFail(NULL, "can't read file %s\n", FileName);
BytesRead = fread(ReadText, 1, FileInfo.st_size, InFile);
if (BytesRead == 0)
ProgramFail(NULL, "can't read file %s\n", FileName);
ReadText[BytesRead] = '\0';
fclose(InFile);
return ReadText;
}
/* read and scan a file for definitions */
void PlatformScanFile(const char *FileName)
{
char *SourceStr = PlatformReadFile(FileName);
char *OrigCleanupText = CleanupText;
if (CleanupText == NULL)
CleanupText = SourceStr;
Parse(FileName, SourceStr, strlen(SourceStr), TRUE);
free(SourceStr);
if (OrigCleanupText == NULL)
CleanupText = NULL;
}
/* mark where to end the program for platforms which require this */
jmp_buf ExitBuf;
/* exit the program */
void PlatformExit()
{
longjmp(ExitBuf, 1);
}