Emotiv EPOC plugin 1.0

D:/SVN/Scol/trunk/plugins/Epoc/src/Profile.cpp

00001 //###################################################################################
00002 //#                                                             Definition Of Class Profile                                                                         #
00003 //#                                               Used to create a binary handle to handle a user profile             #
00004 //#                                                                       Author :                                                                            #
00005 //#                                                                   Aymeric SUTEAU                                                                                    #
00006 //#                                                                    LISA - ANGERS                                                                                    #
00007 //###################################################################################
00008 
00009 
00010 /*------------------------------------ INCLUDES -----------------------------------*/
00011 #include "Profile.h"
00012 
00013 
00014 /*------------------------------- GLOBAL VARIABLES --------------------------------*/
00015 int length;
00016 char* buffer;
00017 unsigned int currentSize = 0;
00018 
00019 
00020 /*---------------------------- CONSTRUCTOR / DESTRUCTOR ---------------------------*/
00021 Profile::Profile(char* fileName)
00022 {
00023   // Try to open a user profile from system file (both Read and Write modes)
00024   if (IsReadable(fileName))
00025   {
00026     MMechostr(MSKDEBUG, ">>> File already exists and is readable.\n");
00027     ioFile.open(fileName, ios::in | ios::out | ios::binary);
00028     if (ioFile.is_open()) 
00029     {
00030       MMechostr(MSKDEBUG, ">>> Binary file [%s] created in [IO] mode...\n", fileName);
00031       SetFileName(fileName);
00032     }
00033     else
00034       MMechostr(MSKDEBUG, ">>> Error when creating [%s] !\n", fileName);
00035   }
00036 
00037   // Else open it as Write only (new user profile)
00038   else
00039   {
00040     MMechostr(MSKDEBUG, ">>> File doesn't exist or is not readable.\n");
00041     oFile.open(fileName, ios::out | ios::trunc | ios::binary);
00042     if (oFile.is_open()) 
00043     {
00044       MMechostr(MSKDEBUG, ">>> Binary file [%s] created in [O] mode...\n", fileName);
00045       SetFileName(fileName);
00046     }
00047     else
00048       MMechostr(MSKDEBUG, ">>> Error when creating [%s] !\n", fileName);
00049   }
00050 
00051   // Initialize file size and binary content
00052   SetFileLength(0);
00053   SetFileData(NULL);
00054 }
00055 
00056 Profile::~Profile(void)
00057 {
00058   // Delete memory allocated for buffer data
00059   delete [] buffer;
00060 
00061   if (oFile.is_open())
00062     oFile.close();
00063   else if (ioFile.is_open())
00064     ioFile.close();
00065 }
00066 
00067 
00068 /*-------------------------------- GETTERS / SETTERS ------------------------------*/
00069 char* Profile::GetFileName()
00070 {
00071   return this->sFileName;
00072 }
00073 
00074 void Profile::SetFileName(char* fileName)
00075 {
00076   this->sFileName = fileName;
00077 }
00078 
00079 int Profile::GetFileLength()
00080 {
00081   return this->iFileSize;
00082 }
00083 
00084 void Profile::SetFileLength(int fileSize)
00085 {
00086   this->iFileSize = fileSize;
00087 }
00088 
00089 char* Profile::GetFileData()
00090 {
00091   return this->sFileData;
00092 }
00093 
00094 void Profile::SetFileData(char* fileData)
00095 {
00096   this->sFileData = fileData;
00097 }
00098 
00099 
00100 /*------------------------------------ WRITE DATA ---------------------------------*/
00101 // Write new text data to file
00102 void Profile::WriteString(string fileLine)
00103 {
00104   if (oFile.is_open())
00105     oFile << fileLine << endl;
00106   else if (ioFile.is_open())
00107     ioFile << fileLine << endl;
00108 }
00109 
00110 
00111 // Write new binary data to file
00112 void Profile::WriteBinaryData(char* data, unsigned int size)
00113 {
00114   if (oFile.is_open())
00115   {
00116     // Get current Output file size
00117     oFile.seekp(0, ios::cur);
00118     currentSize = oFile.tellp();    //unsigned int currentSize = oFile.tellp();
00119     MMechostr(MSKDEBUG, ">>> Old [O] file size = %u\n", currentSize);
00120     oFile.seekp(0, ios::beg);
00121 
00122     // Write additional data only if new buffer size is greater than current buffer size
00123     if (size >= currentSize) 
00124     {
00125       oFile.seekp(0);             // Rewind to the beginning of the binary file
00126       oFile.write(data, size);    // Write binary file
00127     }
00128 
00129     // Else, file must be re-written
00130     else
00131     {
00132       // Close file, and open it again to rewrite it from the beginning
00133       MMechostr(MSKDEBUG, "> File [O] must be re-written...\n");
00134       oFile.close();
00135       oFile.open(GetFileName(), ios::out | ios::trunc | ios::binary);
00136       if (oFile)
00137         oFile.write(data, size);
00138     }
00139   }
00140   else if (ioFile.is_open())
00141   {
00142     // Get current Input / Output file size
00143     ioFile.seekg(0, ios::cur);
00144     currentSize = ioFile.tellg();   //unsigned int currentSize = ioFile.tellg();
00145     MMechostr(MSKDEBUG, ">>> Old [IO] file size = %u\n", currentSize);
00146     ioFile.seekg(0, ios::beg);
00147 
00148     // Write additional data only if new buffer size is greater than current buffer size
00149     if (size >= currentSize) 
00150     {
00151       ioFile.seekp(0);            // Rewind to the beginning of the binary file
00152       ioFile.write(data, size);   // Write binary file
00153     }
00154 
00155     // Else, file must be re-written
00156     else 
00157     {
00158       // Close file, and open it again to rewrite it from the beginning
00159       MMechostr(MSKDEBUG, "> File [IO] must be re-written...\n");
00160       ioFile.close();
00161       ioFile.open(GetFileName(), ios::in | ios::out | ios::trunc | ios::binary);
00162       if (ioFile)
00163         ioFile.write(data, size);
00164     }
00165   }
00166   MMechostr(MSKDEBUG, ">>> Binary data written to file...\n");
00167 }
00168 
00169 
00170 /*------------------------------------- READ DATA ---------------------------------*/
00171 // Read binary data from file
00172 bool Profile::ReadBinaryData()
00173 {
00174   if (ioFile.is_open())
00175   {
00176     // Get length of data stream
00177     ioFile.seekg(0, ios::end);
00178     length = ioFile.tellg();
00179     MMechostr(MSKDEBUG, ">>> Data length = %d\n", length);
00180     ioFile.seekg(0, ios::beg);
00181 
00182     // If size equals 0, user profile has just been created so we don't have to read any data from file
00183     if (length > 0)
00184     {
00185       // Initialize buffer according to file size
00186       buffer = new char[length];
00187 
00188       // Read data as a block
00189       ioFile.read(buffer, length);
00190       
00191       // Update file size and binary data
00192       SetFileLength(length);
00193       SetFileData(buffer);
00194     }
00195     return true;
00196   }
00197   else
00198     return false;
00199 }
00200 
00201 // Make sure the file can be read
00202 bool Profile::IsReadable(const string& file)
00203 { 
00204   ifstream fichier(file.c_str()); 
00205   return !fichier.fail(); 
00206 }