OSDN Git Service

[VM][General] EMU/COMMON : Fix bugs around handling filename and directories.
authorK.Ohta <whatisthis.sowhat@gmail.com>
Wed, 22 Mar 2017 12:43:03 +0000 (21:43 +0900)
committerK.Ohta <whatisthis.sowhat@gmail.com>
Wed, 22 Mar 2017 12:43:03 +0000 (21:43 +0900)
[FILEIO] Fix FTBFS with ZLIB 1.2.8 or earlier.
[CONFIG] Sync to upstream 2017-03-20.

source/build-cmake/cmake/config_tk80.cmake
source/src/common.cpp
source/src/config.h
source/src/emu.cpp
source/src/fileio.cpp

index 0b6fb72..d714519 100644 (file)
@@ -31,7 +31,7 @@ if(BUILD_TK80BS)
     set(EXEC_TARGET emutk80bs)
     set(VM_NAME tk80bs)
     set(VMFILES_LIB ${VMFILES_LIB} i8251.cpp)
-    set(VMFILES_BASE ${VMFILES_BASE} io.cpp)
+    set(VMFILES_BASE ${VMFILES_BASE} datarec.cpp io.cpp)
     set(RESOURCE ${CMAKE_SOURCE_DIR}/../../src/qt/common/qrc/tk80bs.qrc)
 elseif(BUILD_TK80)
     add_definitions(-D_TK80)
index 728382d..da726e2 100644 (file)
@@ -23,6 +23,7 @@
        #include <algorithm>
        #include <cctype>
        #include <QDir>
+       #include <QFileInfo>
 #elif defined(_WIN32)
        #include <shlwapi.h>
        #pragma comment(lib, "shlwapi.lib")
@@ -502,11 +503,18 @@ const _TCHAR *DLL_PREFIX get_file_path_without_extensiton(const _TCHAR *file_pat
        my_tcscpy_s(path[output_index], _MAX_PATH, file_path);
 #if defined(_WIN32) && defined(_MSC_VER)
        PathRemoveExtension(path[output_index]);
-#else
-       _TCHAR *p = _tcsrchr(path[output_index], _T('.'));
-       if(p != NULL) {
-               *p = _T('\0');
+#elif defined(_USE_QT)
+       QString delim;
+       delim = QString::fromUtf8(".");
+       QString tmp_path = QString::fromUtf8(file_path);
+       int n = tmp_path.lastIndexOf(delim);
+       if(n > 0) {
+               tmp_path = tmp_path.left(n);
        }
+       //printf("%s\n", tmp_path.toUtf8().constData());
+       memset(path[output_index], 0x00, sizeof(_TCHAR) * _MAX_PATH);
+       strncpy(path[output_index], tmp_path.toUtf8().constData(), _MAX_PATH - 1);
+#else
 #endif
        return (const _TCHAR *)path[output_index];
 }
@@ -521,9 +529,9 @@ void DLL_PREFIX get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t d
                my_tcscpy_s(dst, dst_len, tmp);
        }
 #elif defined(_USE_QT)
-       QDir _dir = QDir(QString::fromLocal8Bit(src));
-       QString tmp_path = _dir.absolutePath();
-       strncpy(dst, tmp_path.toUtf8().constData(), dst_len);
+       QString tmp_path = QString::fromUtf8(src);
+       QFileInfo info(tmp_path);
+       my_tcscpy_s(dst, dst_len, info.absoluteFilePath().toLocal8Bit().constData());
 #else
        // write code for your environment
        
@@ -543,8 +551,19 @@ const _TCHAR* DLL_PREFIX get_parent_dir(const _TCHAR* file)
                *ptr = _T('\0');
        }
 #elif defined(_USE_QT)
-       QDir _dir = QDir(QString::fromLocal8Bit(file));
-       QString tmp_path = _dir.dirName();
+       QString delim;
+#if defined(Q_OS_WIN)
+       delim = QString::fromUtf8("\\");
+#else
+       delim = QString::fromUtf8("/");
+#endif
+       QString tmp_path = QString::fromUtf8(file);
+       int n = tmp_path.lastIndexOf(delim);
+       if(n > 0) {
+               tmp_path = tmp_path.left(n);
+               tmp_path.append(delim);
+       }
+       //printf("%s\n", tmp_path.toUtf8().constData());
        memset(path[output_index], 0x00, sizeof(_TCHAR) * _MAX_PATH);
        strncpy(path[output_index], tmp_path.toUtf8().constData(), _MAX_PATH - 1);
 #else
index 3e777fe..1ec8c4a 100644 (file)
@@ -89,7 +89,7 @@ typedef struct {
        bool correct_disk_timing[/*MAX_FD_TMP*/ 16];
        bool ignore_disk_crc[/*MAX_FD_TMP*/ 16];
 #endif
-#if defined(USE_SHARED_DLL) || defined(USE_TAPE)
+#if defined(USE_SHARED_DLL) || defined(USE_TAPE1)
        bool wave_shaper[MAX_TAPE_TMP];
        bool direct_load_mzt[MAX_TAPE_TMP];
        bool baud_high[MAX_TAPE_TMP];
index 1cf713a..df48d2e 100644 (file)
@@ -1425,108 +1425,166 @@ uint32_t EMU::is_quick_disk_accessed()
 }
 #endif
 
-#ifdef USE_TAPE
-void EMU::play_tape(const _TCHAR* file_path)
+#ifdef USE_TAPE1
+void EMU::play_tape(int drv, const _TCHAR* file_path)
 {
-       if(vm->is_tape_inserted()) {
-               vm->close_tape();
-               // wait 0.5sec
+       if(drv < MAX_TAPE) {
+               if(vm->is_tape_inserted(drv)) {
+                       vm->close_tape(drv);
+                       // wait 0.5sec
 #ifdef SUPPORT_VARIABLE_TIMING
-               tape_status.wait_count = (int)(vm->get_frame_rate() / 2);
+                       tape_status[drv].wait_count = (int)(vm->get_frame_rate() / 2);
 #else
-               tape_status.wait_count = (int)(FRAMES_PER_SEC / 2);
+                       tape_status[drv].wait_count = (int)(FRAMES_PER_SEC / 2);
 #endif
-               out_message(_T("CMT: Ejected"));
-       } else if(tape_status.wait_count == 0) {
-               vm->play_tape(file_path);
-               out_message(_T("CMT: %s"), file_path);
+#if MAX_TAPE > 1
+                       out_message(_T("CMT%d: Ejected"), drv + TAPE_BASE_NUMBER);
+#else
+                       out_message(_T("CMT: Ejected"));
+#endif
+               } else if(tape_status[drv].wait_count == 0) {
+                       vm->play_tape(drv, file_path);
+#if MAX_TAPE > 1
+                       out_message(_T("CMT%d: %s"), drv + TAPE_BASE_NUMBER, file_path);
+#else
+                       out_message(_T("CMT: %s"), file_path);
+#endif
+               }
+               my_tcscpy_s(tape_status[drv].path, _MAX_PATH, file_path);
+               tape_status[drv].play = true;
        }
-       my_tcscpy_s(tape_status.path, _MAX_PATH, file_path);
-       tape_status.play = true;
 }
 
-void EMU::rec_tape(const _TCHAR* file_path)
+void EMU::rec_tape(int drv, const _TCHAR* file_path)
 {
-       if(vm->is_tape_inserted()) {
-               vm->close_tape();
-               // wait 0.5sec
+       if(drv < MAX_TAPE) {
+               if(vm->is_tape_inserted(drv)) {
+                       vm->close_tape(drv);
+                       // wait 0.5sec
 #ifdef SUPPORT_VARIABLE_TIMING
-               tape_status.wait_count = (int)(vm->get_frame_rate() / 2);
+                       tape_status[drv].wait_count = (int)(vm->get_frame_rate() / 2);
 #else
-               tape_status.wait_count = (int)(FRAMES_PER_SEC / 2);
+                       tape_status[drv].wait_count = (int)(FRAMES_PER_SEC / 2);
 #endif
-               out_message(_T("CMT: Ejected"));
-       } else if(tape_status.wait_count == 0) {
-               vm->rec_tape(file_path);
-               out_message(_T("CMT: %s"), file_path);
+#if MAX_TAPE > 1
+                       out_message(_T("CMT%d: Ejected"), drv + TAPE_BASE_NUMBER);
+#else
+                       out_message(_T("CMT: Ejected"));
+#endif
+               } else if(tape_status[drv].wait_count == 0) {
+                       vm->rec_tape(drv, file_path);
+#if MAX_TAPE > 1
+                       out_message(_T("CMT%d: %s"), drv + TAPE_BASE_NUMBER, file_path);
+#else
+                       out_message(_T("CMT: %s"), file_path);
+#endif
+               }
+               my_tcscpy_s(tape_status[drv].path, _MAX_PATH, file_path);
+               tape_status[drv].play = false;
        }
-       my_tcscpy_s(tape_status.path, _MAX_PATH, file_path);
-       tape_status.play = false;
 }
 
-void EMU::close_tape()
+void EMU::close_tape(int drv)
 {
-       vm->close_tape();
-       clear_media_status(&tape_status);
-       out_message(_T("CMT: Ejected"));
+       if(drv < MAX_TAPE) {
+               vm->close_tape(drv);
+               clear_media_status(&tape_status[drv]);
+#if MAX_TAPE > 1
+               out_message(_T("CMT%d: Ejected"), drv + TAPE_BASE_NUMBER);
+#else
+               out_message(_T("CMT: Ejected"));
+#endif
+       }
 }
 
-bool EMU::is_tape_inserted()
+bool EMU::is_tape_inserted(int drv)
 {
-       return vm->is_tape_inserted();
+       if(drv < MAX_TAPE) {
+               return vm->is_tape_inserted(drv);
+       } else {
+               return false;
+       }
 }
 
 #ifndef TAPE_BINARY_ONLY
-bool EMU::is_tape_playing()
+bool EMU::is_tape_playing(int drv)
 {
-       return vm->is_tape_playing();
+       if(drv < MAX_TAPE) {
+               return vm->is_tape_playing(drv);
+       } else {
+               return false;
+       }
 }
 
-bool EMU::is_tape_recording()
+bool EMU::is_tape_recording(int drv)
 {
-       return vm->is_tape_recording();
+       if(drv < MAX_TAPE) {
+               return vm->is_tape_recording(drv);
+       } else {
+               return false;
+       }
 }
 
-int EMU::get_tape_position()
+int EMU::get_tape_position(int drv)
 {
-       return vm->get_tape_position();
+       if(drv < MAX_TAPE) {
+               return vm->get_tape_position(drv);
+       } else {
+               return 0;
+       }
 }
 
-const _TCHAR* EMU::get_tape_message()
+const _TCHAR* EMU::get_tape_message(int drv)
 {
-       return vm->get_tape_message();
+       if(drv < MAX_TAPE) {
+               return vm->get_tape_message(drv);
+       } else {
+               return NULL;
+       }
 }
 #endif
 
 #ifdef USE_TAPE_BUTTON
-void EMU::push_play()
+void EMU::push_play(int drv)
 {
-       vm->push_play();
+       if(drv < MAX_TAPE) {
+               vm->push_play(drv);
+       }
 }
 
-void EMU::push_stop()
+void EMU::push_stop(int drv)
 {
-       vm->push_stop();
+       if(drv < MAX_TAPE) {
+               vm->push_stop(drv);
+       }
 }
 
-void EMU::push_fast_forward()
+void EMU::push_fast_forward(int drv)
 {
-       vm->push_fast_forward();
+       if(drv < MAX_TAPE) {
+               vm->push_fast_forward(drv);
+       }
 }
 
-void EMU::push_fast_rewind()
+void EMU::push_fast_rewind(int drv)
 {
-       vm->push_fast_rewind();
+       if(drv < MAX_TAPE) {
+               vm->push_fast_rewind(drv);
+       }
 }
 
-void EMU::push_apss_forward()
+void EMU::push_apss_forward(int drv)
 {
-       vm->push_apss_forward();
+       if(drv < MAX_TAPE) {
+               vm->push_apss_forward(drv);
+       }
 }
 
-void EMU::push_apss_rewind()
+void EMU::push_apss_rewind(int drv)
 {
-       vm->push_apss_rewind();
+       if(drv < MAX_TAPE) {
+               vm->push_apss_rewind(drv);
+       }
 }
 #endif
 #endif
@@ -1770,7 +1828,7 @@ void EMU::save_state_tmp(const _TCHAR* file_path)
 #ifdef USE_QD1
                fio->Fwrite(&quick_disk_status, sizeof(quick_disk_status), 1);
 #endif
-#ifdef USE_TAPE
+#ifdef USE_TAPE1
                fio->Fwrite(&tape_status, sizeof(tape_status), 1);
 #endif
 #ifdef USE_COMPACT_DISC
@@ -1810,7 +1868,7 @@ bool EMU::load_state_tmp(const _TCHAR* file_path)
 #ifdef USE_QD1
                                fio->Fread(&quick_disk_status, sizeof(quick_disk_status), 1);
 #endif
-#ifdef USE_TAPE
+#ifdef USE_TAPE1
                                fio->Fread(&tape_status, sizeof(tape_status), 1);
 #endif
 #ifdef USE_COMPACT_DISC
index 1b911d3..cc150a0 100644 (file)
        #ifdef _WIN32
                #define ZLIB_WINAPI
        #endif
-       #include "zlib-1.2.11/zlib.h"
-       #include "zlib-1.2.11/zconf.h"
+       #if defined(USE_QT)
+               #include <zlib.h>
+               #include <zconf.h>
+       #else
+               #include "zlib-1.2.11/zlib.h"
+               #include "zlib-1.2.11/zconf.h"
+       #endif
 #endif
 
+#if ZLIB_VERNUM < 0x1290
+inline size_t gzfread(void *buffer, size_t size, size_t count, gzFile file)
+{
+       uint8_t *p = (uint8_t *)buffer;
+       int s = 0;
+       int i = 0;
+       for(i = 0; i < count; i++) {
+               for(int j = 0; j < size; j++) {
+                       s = gzgetc(file);
+                       if(s < 0) return 0; // EOF
+                       *p++ = (uint8_t)s; 
+               }
+       }
+       return i + 1;
+}
+inline size_t gzfwrite(void *buffer, size_t size, size_t count, gzFile file)
+{
+       uint8_t *p = (uint8_t *)buffer;
+       uint8_t n;
+       int s = 0;
+       int i = 0;
+       for(i = 0; i < count; i++) {
+               for(int j = 0; j < size; j++) {
+                       n = *p++; 
+                       s = gzputc(file, n);
+                       if(s < 0) return 0; // EOF
+               }
+       }
+       return i + 1;
+}
+#endif                 
 FILEIO::FILEIO()
 {
 #ifdef USE_ZLIB
@@ -171,7 +207,18 @@ bool FILEIO::Fopen(const _TCHAR *file_path, int mode)
                }
                switch(mode) {
                case FILEIO_READ_BINARY:
-                       return ((gz = gzopen(file_path, _T("rb"))) != NULL);
+                       {
+                               //memset(path, 0x00, _MAX_PATH);
+                               gz = gzopen(file_path, _T("rb"));
+                               if(gz != NULL) {
+                                       my_tcscpy_s(path, _MAX_PATH, get_file_path_without_extensiton(file_path));
+                                       return true;
+                               } else {
+                                       my_tcscpy_s(path, _MAX_PATH, file_path);
+                                       return false;
+                               }                                       
+                       }
+                       break;
 //             case FILEIO_WRITE_BINARY:
 //                     return ((gz = gzopen(file_path, _T("wb"))) != NULL);
 //             case FILEIO_READ_WRITE_BINARY:
@@ -179,7 +226,17 @@ bool FILEIO::Fopen(const _TCHAR *file_path, int mode)
 //             case FILEIO_READ_WRITE_NEW_BINARY:
 //                     return ((gz = gzopen(file_path, _T("w+b"))) != NULL);
                case FILEIO_READ_ASCII:
-                       return ((gz = gzopen(file_path, _T("r"))) != NULL);
+                       {
+                               gz = gzopen(file_path, _T("r"));
+                               if(gz != NULL) {
+                                       my_tcscpy_s(path, _MAX_PATH, get_file_path_without_extensiton(file_path));
+                                       return true;
+                               } else {
+                                       my_tcscpy_s(path, _MAX_PATH, file_path);
+                                       return false;
+                               }
+                       }
+                       break;
 //             case FILEIO_WRITE_ASCII:
 //                     return ((gz = gzopen(file_path, _T("w"))) != NULL);
 //             case FILEIO_WRITE_APPEND_ASCII:
@@ -660,7 +717,13 @@ int FILEIO::Fgetc()
                return gzgetc(gz);
        } else
 #endif
-       return fgetc(fp);
+       {
+               if(fp != NULL) {
+                       return getc(fp);
+               } else {
+                       return 0;
+               }
+       }
 }
 
 int FILEIO::Fputc(int c)
@@ -670,7 +733,13 @@ int FILEIO::Fputc(int c)
                return gzputc(gz, c);
        } else
 #endif
-       return fputc(c, fp);
+       {
+               if(fp != NULL) {
+                       return fputc(c, fp);
+               } else {
+                       return 0;
+               }
+       }
 }
 
 char *FILEIO::Fgets(char *str, int n)
@@ -680,7 +749,13 @@ char *FILEIO::Fgets(char *str, int n)
                return gzgets(gz, str, n);
        } else
 #endif
-       return fgets(str, n, fp);
+       {
+               if(fp != NULL) {
+                       return fgets(str, n, fp);
+               } else {
+                       return 0;
+               }
+       }
 }
 
 int FILEIO::Fprintf(const char* format, ...)
@@ -697,7 +772,13 @@ int FILEIO::Fprintf(const char* format, ...)
                return gzprintf(gz, "%s", buffer);
        } else
 #endif
-       return my_fprintf_s(fp, "%s", buffer);
+       {
+               if(fp != NULL) {
+                       return my_fprintf_s(fp, "%s", buffer);
+               } else {
+                       return 0;
+               }
+       }
 }
 
 size_t FILEIO::Fread(void* buffer, size_t size, size_t count)
@@ -707,7 +788,13 @@ size_t FILEIO::Fread(void* buffer, size_t size, size_t count)
                return gzfread(buffer, size, count, gz);
        } else
 #endif
-       return fread(buffer, size, count, fp);
+       {
+               if(fp != NULL) {
+                       return fread(buffer, size, count, fp);
+               } else {
+                       return 0;
+               }
+       }
 }
 
 size_t FILEIO::Fwrite(void* buffer, size_t size, size_t count)
@@ -717,7 +804,13 @@ size_t FILEIO::Fwrite(void* buffer, size_t size, size_t count)
                return gzfwrite(buffer, size, count, gz);
        } else
 #endif
-       return fwrite(buffer, size, count, fp);
+       {
+               if(fp != NULL) {
+                       return fwrite(buffer, size, count, fp);
+               } else {
+                       return 0;
+               }
+       }
 }
 
 int FILEIO::Fseek(long offset, int origin)
@@ -734,13 +827,16 @@ int FILEIO::Fseek(long offset, int origin)
                }
        } else
 #endif
-       switch(origin) {
-       case FILEIO_SEEK_CUR:
-               return fseek(fp, offset, SEEK_CUR);
-       case FILEIO_SEEK_END:
-               return fseek(fp, offset, SEEK_END);
-       case FILEIO_SEEK_SET:
-               return fseek(fp, offset, SEEK_SET);
+       {
+               if(fp == NULL) return -1;
+               switch(origin) {
+               case FILEIO_SEEK_CUR:
+                       return fseek(fp, offset, SEEK_CUR);
+               case FILEIO_SEEK_END:
+                       return fseek(fp, offset, SEEK_END);
+               case FILEIO_SEEK_SET:
+                       return fseek(fp, offset, SEEK_SET);
+               }
        }
        return -1;
 }
@@ -752,6 +848,12 @@ long FILEIO::Ftell()
                return gztell(gz);
        } else
 #endif
-       return ftell(fp);
+       {
+               if(fp != NULL) {
+                       return ftell(fp);
+               } else {
+                       return 0;
+               }
+       }
 }