room101/FILE.CPP

208 lines
5.5 KiB
C++

#include "file.hpp"
#include "main.hpp"
#include "string.hpp"
#include <assert.h>
int
IntRead(FILE* file)
{
int a;
fread(&a , sizeof(a), 1, file);
return a;
}
void
IntWrite(int a, FILE* file)
{
fwrite(&a, sizeof(a), 1, file);
}
/*******************************************************************************
** FileOpen
*******************************************************************************/
FILE*
FileOpen(char* name, char* mode)
{
FILE* file = fopen(name, mode);
ThrowIf(!file, "Could not open file");
return file;
}
/*******************************************************************************
** FileClose
*******************************************************************************/
void
FileClose(FILE* file)
{
fclose(file);
}
/*******************************************************************************
** FileEatWhite
*******************************************************************************/
int
FileEatWhite(FILE* file)
{
String s;
return fscanf(file, "%[ \n\t\r]", s);
}
/*******************************************************************************
** FileEatLine
*******************************************************************************/
int
FileEatLine(FILE* file)
{
String s;
fscanf(file, "%[^\n]", s);
return fscanf(file, "%c", s);
}
/*******************************************************************************
** FileReadNonDigit
*******************************************************************************/
int
FileReadNonDigit(FILE* file, String s)
{
return fscanf(file, "%[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]", s) == 1;
}
/*******************************************************************************
** FileReadDigit
*******************************************************************************/
int
FileReadDigit(FILE* file, String s)
{
return fscanf(file, "%[0123456789]", s) == 1;
}
/*******************************************************************************
** FileGetIdent
*******************************************************************************/
int
FileReadIdent(FILE* file, String s)
{
FileEatWhite(file);
if (FileReadNonDigit(file, s))
{
while (FileReadDigit(file, strchr(s, 0)))
while(FileReadNonDigit(file, strchr(s, 0)));
return 1;
}
return 0;
}
/*******************************************************************************
** FileReadPunct
*******************************************************************************/
int
FileReadPunct(FILE* file, String s)
{
assert(file);
s[0] = fgetc(file);
s[1] = fgetc(file);
s[2] = 0;
ThrowIf(s[0] == 0xFF, "Unexpected EOF");
if (s[0] == '\\' ) return 2;
if (!strcmp(s, "/*")) return 3;
if (!strcmp(s, "*/")) return 4;
if (!strcmp(s, "//")) return 5;
if (!strcmp(s, "++")) return 6;
if (!strcmp(s, "--")) return 7;
if (!strcmp(s, "+=")) return 8;
if (!strcmp(s, "-=")) return 9;
if (!strcmp(s, "*=")) return 10;
if (!strcmp(s, "/=")) return 11;
if (!strcmp(s, "==")) return 12;
if (!strcmp(s, "!=")) return 13;
ungetc(s[1], file);
s[1] = 0;
return s[0];
}
/*******************************************************************************
** FileSize
*******************************************************************************/
int
FileSize(FILE* file)
{
int pos = ftell(file);
fseek(file, 0, SEEK_END);
int size = ftell(file);
fseek(file, pos, SEEK_SET);
return size;
}
/*******************************************************************************
** FileLoad
*******************************************************************************/
void*
FileLoad(char* filename)
{
FILE* file = FileOpen(filename, "rb");
int size = FileSize(file);
void* mem = malloc(size);
ThrowIf(!mem, "Could not allocate memory");
fread(mem, size, 1, file);
FileClose(file);
return mem;
}
/*******************************************************************************
** FileName
*******************************************************************************/
char*
FileName(char* s)
{
char* p;
for (p = strchr(s, 0); p != s; p--)
if (*p == '/' || *p == '\\')
return p + 1;
return p;
}
/*******************************************************************************
** FilePath
*******************************************************************************/
char*
FilePath(char* dst, char* src)
{
*FileName(strcpy(dst, src)) = '\0';
return dst;
}
/*******************************************************************************
** FileNameExt
*******************************************************************************/
char*
FileNameExt(char* s)
{
char* p = strrchr(FileName(s), '.');
return p ? p : strchr(s, 0);
}
/*******************************************************************************
** FileNameExtChange
*******************************************************************************/
char*
FileNameExtChange(char* s, char* ext)
{
strcat(strcpy(FileNameExt(s), "."), ext);
return s;
}
/*******************************************************************************
** FileNameExtRemove
*******************************************************************************/
char*
FileNameExtRemove(char* s)
{
*FileNameExt(s) = '\0';
return s;
}