MASA-Core
FileBuffer.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "FileBuffer.hpp"
00023 
00024 #include <stdlib.h>
00025 
00026 
00027 FileBuffer::FileBuffer(string filename) {
00028     this->filename = filename;
00029 }
00030 
00031 FileBuffer::~FileBuffer() {
00032     if (file != NULL) {
00033         Buffer::destroy();
00034         //fclose(file); // TODO verificar a ordem do fclose no socket e no file.
00035         //Buffer::joinThreads();
00036         //fclose(file); // TODO verificar a ordem do fclose no socket e no file.
00037         //file == NULL;
00038     }
00039 }
00040 
00041 void FileBuffer::autoFlushThread() {
00042     char data[16*1024];
00043     
00044     while (!isDestroyed()) {
00045         int len = readBuffer(data, 1, sizeof(data));
00046         if (len <= 0) break;
00047         fwrite(data, 1, len, file);
00048     }
00049     fclose(file);
00050     file = NULL;
00051 }
00052 
00053 void FileBuffer::autoLoadThread() {
00054     char data[512];
00055     
00056     while (!isDestroyed()) {
00057         int len = fread(data, 1, sizeof(data), file);
00058         if (len <= 0) break;
00059         writeBuffer(data, 1, len);
00060     }
00061     fclose(file);
00062     file = NULL;
00063 
00064 }
00065 
00066 void FileBuffer::initAutoLoad() {
00067     file = fopen(filename.c_str(), "r");
00068     if (file == NULL) {
00069         fprintf(stderr, "File not found (%s).", filename.c_str());
00070         exit(1);
00071     }
00072 }
00073 
00074 void FileBuffer::initAutoFlush() {
00075     file = fopen(filename.c_str(), "w");
00076     if (file == NULL) {
00077         fprintf(stderr, "File not found (%s).", filename.c_str());
00078         exit(1);
00079     }
00080 }
00081 
00082 void FileBuffer::destroyAutoFlush() {
00083 }
00084 
00085 void FileBuffer::destroyAutoLoad() {
00086 }
00087 
00088