2012-03-24 22:39:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "file/easy_file.h"
|
|
|
|
|
|
|
|
LAMEFile::LAMEFile() : file_(NULL) {
|
2012-10-31 12:23:16 +00:00
|
|
|
isOpen = false;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LAMEFile::~LAMEFile() { }
|
|
|
|
|
|
|
|
bool LAMEFile::open(const char *filename, eFileMode mode) {
|
2012-10-31 12:23:16 +00:00
|
|
|
file_ = fopen(filename, mode == FILE_READ ? "rb" : "wb");
|
2012-03-24 22:39:19 +00:00
|
|
|
|
2012-10-31 12:23:16 +00:00
|
|
|
if (!file_) {
|
|
|
|
isOpen = false;
|
|
|
|
} else {
|
|
|
|
isOpen = true;
|
|
|
|
if (mode == FILE_READ) {
|
|
|
|
fseek(file_, 0, SEEK_END);
|
|
|
|
size_ = ftell(file_);
|
|
|
|
fseek(file_, 0, SEEK_SET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isOpen;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LAMEFile::close() {
|
2012-10-31 12:23:16 +00:00
|
|
|
if (isOpen) {
|
|
|
|
//close the file and reset variables
|
|
|
|
fclose(file_);
|
|
|
|
file_ = NULL;
|
|
|
|
isOpen=false;
|
|
|
|
}
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LAMEFile::fileSize() {
|
2012-10-31 12:23:16 +00:00
|
|
|
if (!isOpen) //of course
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return size_;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string LAMEFile::readAll() {
|
2012-10-31 12:23:16 +00:00
|
|
|
std::string s;
|
|
|
|
size_t size = fileSize();
|
|
|
|
s.resize(size);
|
|
|
|
read(&s[0], size);
|
|
|
|
return s;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LAMEFile::write(const void *data, int size) {
|
2012-10-31 12:23:16 +00:00
|
|
|
if (isOpen) {
|
|
|
|
return fwrite(data, 1, size, file_); //we return the number of bytes that actually got written
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LAMEFile::read(void *data, int size) {
|
2012-10-31 12:23:16 +00:00
|
|
|
if (isOpen) {
|
|
|
|
return fread(data, 1, size, file_);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int LAMEFile::readInt() {
|
2012-10-31 12:23:16 +00:00
|
|
|
int temp;
|
|
|
|
if (read(&temp, sizeof(int)))
|
|
|
|
return temp;
|
|
|
|
else
|
|
|
|
return 0;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LAMEFile::writeInt(int i) {
|
2012-10-31 12:23:16 +00:00
|
|
|
write(&i, sizeof(int));
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char LAMEFile::readChar() {
|
2012-10-31 12:23:16 +00:00
|
|
|
char temp;
|
|
|
|
if (read(&temp, sizeof(char)))
|
|
|
|
return temp;
|
|
|
|
else
|
|
|
|
return 0;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LAMEFile::writeChar(char i) {
|
2012-10-31 12:23:16 +00:00
|
|
|
write(&i,sizeof(char));
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|