1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "io.h"
#include "general.h"
void setUpFile ( FILE* fp )
{
//Go to beginning of file
fseek ( fp, 0, SEEK_SET );
//Write for this systems endianness ( 4-byte integer to start of file )
unsigned int iPut = 1;
fwrite ( &iPut, sizeof ( int ), 1, fp );
//Write 0 as a 4-byte integer afterwards ( for error checking )
iPut = 0;
fwrite ( &iPut, sizeof ( int ), 1, fp );
}
IO_File* IO_Append_File ( char* fileName, unsigned int sugAlloc )
{
//Open for reading and writing in binary mode
int isNew = 0;
FILE* fp = fopen ( fileName, "rb+" );
if ( fp == NULL )
{
fp = fopen ( fileName, "wb+" );
isNew = 1;
}
if ( fp == NULL )
{
printf ( "IO File Error: File Error\n" );
return NULL;
}
//Allocate Struct needed for this
IO_File* filePtr = malloce ( sizeof ( IO_File ) );
memset ( filePtr, 0, sizeof ( IO_File ) );
//Transfer File Pointer
filePtr->fp = fp;
//Get endianness of this machine
int lilEnd = isLittleEndian();
//If the file is not newly created, read all data into the IO_File struct
if ( isNew == 0 )
{
//Get first 4 bytes as an integer to check endianness of stored file
//---
//Read the integer
unsigned long endianInt = 0;
fread ( &endianInt, sizeof ( long ), 1, fp );
//Check to see if fread failed ( cut short ) by reaching end of file
if ( feof ( fp ) )
{
//If so, fix the file ( Set up Endianness and empty int )
setUpFile ( fp );
}
else
{
if ( endianInt == 1 )
{
//Is the Endianness of this machine
filePtr->littleEndian = lilEnd;
}
else if ( endianInt == 16777216 )
{
//Opposite of the endianness of this machine
filePtr->littleEndian = !lilEnd;
}
//Anything else means an error for the file
else
{
//Unload all allocations and file pointers
printf ( "IO File Error: Endian Incorrect\n" );
IO_Close_File ( filePtr );
return NULL;
}
//Make sure the second 4 byte integer is empty ( just a simple check )
//---
//Load what should be an empty value
unsigned int emptyVal = 1;
fread ( &emptyVal, sizeof ( int ), 1, fp );
//Make sure its an empty value
if ( emptyVal != 0 )
{
printf ( "IO File Error: Empty Value not Empty\n" );
IO_Close_File ( filePtr );
return NULL;
}
//Load all variables within the files
//---
//Count how many variables there are
//Note: A Null pointer will signify the end of the header
unsigned int varPtr = 0;
int varAmount = 0;
do
{
fread ( &varPtr, sizeof ( int ), 1, fp );
varAmount++;
} while ( varPtr != 0 && !feof ( fp ) );
filePtr->varAmount = varAmount;
//LEFT OFF
//Allocate the
}
}
//If the file is new, Initialise it
else
{
//Set up the file with Endian indicator and empty Int
setUpFile ( fp );
}
//Initialise Variable array
filePtr->varAmount = 0;
filePtr->fileData = NULL;
return filePtr;
}
| |