OSDN Git Service

[VM][Qt][OSD][Genaral] Sync to upstream ; phase 1: Merege upstream 20151217.
[csp-qt/common_source_project-fm7.git] / source / src / vm / tk80bs / cmt.cpp
index f65dfd6..ee6232b 100644 (file)
@@ -50,6 +50,7 @@ void CMT::play_tape(const _TCHAR* file_path)
                fio->Fseek(0, FILEIO_SEEK_SET);
                memset(buffer, 0, sizeof(buffer));
                fio->Fread(buffer, sizeof(buffer), 1);
+               fio->Fclose();
                
                // send data to sio
                // this implement does not care the sio buffer size... :-(
@@ -65,6 +66,7 @@ void CMT::rec_tape(const _TCHAR* file_path)
        close_tape();
        
        if(fio->Fopen(file_path, FILEIO_WRITE_BINARY)) {
+               my_tcscpy_s(rec_file_path, _MAX_PATH, file_path);
                bufcnt = 0;
                rec = true;
        }
@@ -82,12 +84,71 @@ void CMT::close_tape()
 void CMT::release_tape()
 {
        // close file
-       if(rec && bufcnt) {
-               fio->Fwrite(buffer, bufcnt, 1);
-       }
-       if(play || rec) {
+       if(fio->IsOpened()) {
+               if(rec && bufcnt) {
+                       fio->Fwrite(buffer, bufcnt, 1);
+               }
                fio->Fclose();
        }
        play = rec = false;
 }
 
+#define STATE_VERSION  1
+
+void CMT::save_state(FILEIO* state_fio)
+{
+       state_fio->FputUint32(STATE_VERSION);
+       state_fio->FputInt32(this_device_id);
+       
+       state_fio->FputBool(play);
+       state_fio->FputBool(rec);
+       state_fio->Fwrite(rec_file_path, sizeof(rec_file_path), 1);
+       if(rec && fio->IsOpened()) {
+               int length_tmp = (int)fio->Ftell();
+               fio->Fseek(0, FILEIO_SEEK_SET);
+               state_fio->FputInt32(length_tmp);
+               while(length_tmp != 0) {
+                       uint8 buffer_tmp[1024];
+                       int length_rw = min(length_tmp, (int)sizeof(buffer_tmp));
+                       fio->Fread(buffer_tmp, length_rw, 1);
+                       state_fio->Fwrite(buffer_tmp, length_rw, 1);
+                       length_tmp -= length_rw;
+               }
+       } else {
+               state_fio->FputInt32(0);
+       }
+       state_fio->FputInt32(bufcnt);
+       state_fio->Fwrite(buffer, sizeof(buffer), 1);
+}
+
+bool CMT::load_state(FILEIO* state_fio)
+{
+       release_tape();
+       
+       if(state_fio->FgetUint32() != STATE_VERSION) {
+               return false;
+       }
+       if(state_fio->FgetInt32() != this_device_id) {
+               return false;
+       }
+       play = state_fio->FgetBool();
+       rec = state_fio->FgetBool();
+       state_fio->Fread(rec_file_path, sizeof(rec_file_path), 1);
+       int length_tmp = state_fio->FgetInt32();
+       if(rec) {
+               fio->Fopen(rec_file_path, FILEIO_READ_WRITE_NEW_BINARY);
+               while(length_tmp != 0) {
+                       uint8 buffer_tmp[1024];
+                       int length_rw = min(length_tmp, (int)sizeof(buffer_tmp));
+                       state_fio->Fread(buffer_tmp, length_rw, 1);
+                       if(fio->IsOpened()) {
+                               fio->Fwrite(buffer_tmp, length_rw, 1);
+                       }
+                       length_tmp -= length_rw;
+               }
+       }
+       bufcnt = state_fio->FgetInt32();
+       state_fio->Fread(buffer, sizeof(buffer), 1);
+       return true;
+}
+