language

some fools attempt at an interpreted language
Log | Files | Refs | README

fh.c (618B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 
      4 #include "fh.h"
      5 #include "helper.h"
      6 
      7 /* Reads n bytes, n passed as @param long, returns array of byte_t
      8  */
      9 byte_t* read_bytes(FILE* f, long bytes)
     10 {
     11 	byte_t* buffer = (byte_t*)malloc(bytes*sizeof(byte_t));
     12 
     13 	ASSERT(buffer != NULL, "Could not allocate memory\n");
     14 
     15 	fread(buffer, bytes, sizeof(byte_t), f);
     16 
     17 	return buffer;
     18 }
     19 
     20 /* Reads a byte, returns byte_t
     21  */
     22 byte_t read_byte(FILE* f)
     23 {
     24 	return fgetc(f);
     25 }
     26 
     27 /* Determines filesize, returns size as long
     28  */
     29 long read_size(FILE* f)
     30 {
     31 	fseek(f, 0, SEEK_END);
     32 
     33 	long fsize = 0;
     34 	fsize = ftell(f);
     35 	rewind(f);
     36 
     37 	return fsize;
     38 }