📄 fvsfile.cpp
字号:
#ifndef __FVSFILE_CPP__#define __FVSFILE_CPP__#include <stdio.h>#include <string.h>#include "fvstypes.h"#include "fvsfile.h"// Default constructor, set file pointer to NULL FvsFile::FvsFile(void){ // Not needed fp = NULL;}// Constructor that also opens a fileFvsFile::FvsFile(const String_t name, const uint32_t flags){ fp = NULL; // call open function, it will take care of the rest Open(name, flags);}// DestructorFvsFile::~FvsFile(void){ // call close function it will take care of freeing file pointer Close();}// Open file for reading or writingFvsError_t FvsFile::Open(const String_t name, const uint32_t flags){ char strflags[10]; // make sure file is not already open Close(); // determine file flags needed strcpy(strflags,""); if (flags&FILE_READ && flags&FILE_WRITE) strcat(strflags, "rw"); else if (flags&FILE_READ) strcat(strflags, "r"); else if (flags&FILE_WRITE) strcat(strflags, "w"); // always open as binary file strcat(strflags,"b"); // should we create file if it does not exist? if (flags&FILE_CREATE) strcat(strflags,"+"); // open and set file pointer fp = fopen(name, strflags); // make sure that the file opened correctly if(IsOpen()) return FvsOk; // the file did not open correctly return failure return FvsFailure;}// Close function, frees file pointer and allows class to be reusedFvsError_t FvsFile::Close(void){ // make sure file is open before we close it if (IsOpen()) { int nerr = fclose(fp); // set file pointer to NULL fp = NULL; // check if there was any problems if (nerr == 0) return FvsOk; else return FvsFailure; } // File is not open return FvsFailure;}// Read from fileuint32_t FvsFile::Read(pointer_t data, uint32_t len){ return fread(data, 1, len, fp);}// Write to fileuint32_t FvsFile::Write(pointer_t data, uint32_t len){ return fwrite(data, 1, len, fp);}// Read only one byteuint8_t FvsFile::GetByte(){ return fgetc(fp);}// Read two bytes (1 word)uint16_t FvsFile::GetWord(){ uint16_t w = fgetc(fp); return (w<<8) + fgetc(fp);}// Checks if file pointer points to somethingbool_t FvsFile::IsOpen(void) const{ return (fp != NULL)?TRUE:FALSE;}// Check if we have reached the end of the filebool_t FvsFile::IsAtEOF(void) const { // make sure file is open first if (!IsOpen()) return FALSE; return (feof(fp) != 0)?TRUE:FALSE;}// Commit change to fileFvsError_t FvsFile::Commit(void){ return (fflush(fp) == 0)?FvsOk:FvsFailure;}// Move to beginning of fileFvsError_t FvsFile::SeekToBegin(void){ // make sure file is open if (IsOpen()) { fseek(fp, 0, SEEK_SET); return FvsOk; } // file not open, return failure return FvsFailure;}// Move to end of fileFvsError_t FvsFile::SeekToEnd(void){ // make sure file is open if (IsOpen()) { fseek(fp, 0, SEEK_END); return FvsOk; } // file not open, return failure return FvsFailure;}// Seek to specific locationFvsError_t FvsFile::Seek(uint32_t newpos){ // make sure file is open if (IsOpen()) { fseek(fp, newpos, SEEK_SET); return FvsOk; } // file not open, return failure return FvsFailure;}// Returns current position in fileuint32_t FvsFile::GetPosition(void) const{ // make sure file is open if (IsOpen()) return ftell(fp); // file not open, return 0 return 0;}#endif // __FVSFILE_CPP_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -