Revision: 8994 https://osdn.net/projects/ttssh2/scm/svn/commits/8994 Author: zmatsuo Date: 2020-11-02 00:35:00 +0900 (Mon, 02 Nov 2020) Log Message: ----------- ファイル名arrayを作成 Modified Paths: -------------- branches/proto_unicode/teraterm/teraterm/filesys_proto.cpp branches/proto_unicode/teraterm/teraterm/filesys_proto.h branches/proto_unicode/teraterm/ttpfile/ftlib.c -------------- next part -------------- Modified: branches/proto_unicode/teraterm/teraterm/filesys_proto.cpp =================================================================== --- branches/proto_unicode/teraterm/teraterm/filesys_proto.cpp 2020-11-01 15:34:47 UTC (rev 8993) +++ branches/proto_unicode/teraterm/teraterm/filesys_proto.cpp 2020-11-01 15:35:00 UTC (rev 8994) @@ -48,6 +48,7 @@ #include "helpid.h" #include "layer_for_unicode.h" #include "codeconv.h" +#include "asprintf.h" #include "filesys_log_res.h" @@ -155,10 +156,10 @@ fv->Close(fv); fv->FileOpen = FALSE; } +#if 0 if (fv->FnStrMem != NULL) { free(fv->FnStrMem); } -#if 0 if (fv->FnStrMemHandle != 0) { GlobalUnlock(fv->FnStrMemHandle); @@ -165,6 +166,9 @@ GlobalFree(fv->FnStrMemHandle); } #endif + if (fv->FileNames != NULL) { + free(fv->FileNames); + } fv->FileSysDestroy(fv); free(fv); @@ -494,8 +498,87 @@ return fileA; } -static BOOL _GetXFname(HWND HWin, BOOL Receive, const char *caption, LPLONG Option, PFileVarProto fv) +char **MakeStrArrayFromArray(char **strs) { + // \x90\x94\x82𐔂\xA6\x82\xE9 + size_t strs_count = 0; + size_t strs_len = 0; + while(1) { + const char *f = strs[strs_count]; + if (f == NULL) { + break; + } + strs_count++; + size_t len = strlen(f) + 1; // len = 1 when "\0" + strs_len += len; + } + + // 1\x97̈\xE6\x82ɕۑ\xB6 + size_t ptrs_len = sizeof(char *) * (strs_count + 1); + char *pool = (char *)malloc(ptrs_len + strs_len); + char **ptrs = (char **)pool; + char *strpool = pool + ptrs_len; + for (int i = 0 ; i < strs_count; i++) { + size_t len = strlen(strs[i]) + 1; + memcpy(strpool, strs[i], len); + ptrs[i] = strpool; + strpool += len; + } + ptrs[strs_count] = NULL; + return ptrs; +} + +char **MakeFileArrayMultiSelect(const char *lpstrFile) +{ + // \x90\x94\x82𐔂\xA6\x82\xE9 + size_t file_count = 0; + const char *p = lpstrFile; + const char *path = p; + size_t len = strlen(p); + p += len + 1; + while(1) { + len = strlen(p); + if (len == 0) { + break; + } + p += len + 1; + file_count++; + } + + // \x83p\x83X + \x83t\x83@\x83C\x83\x8B\x96\xBC \x88ꗗ\x8D쐬 + size_t ptr_len = sizeof(char *) * (file_count + 1); + char **filenames = (char **)malloc(ptr_len); + len = strlen(path); + p = path + (len + 1); + size_t filelen_sum = 0; + for (int i = 0 ; i < file_count; i++) { + size_t filelen = asprintf(&filenames[i], "%s\\%s", path, p); + filelen_sum += filelen + 1; + len = strlen(p); + p += len + 1; + } + filenames[file_count] = NULL; + + char **ret = MakeStrArrayFromArray(filenames); + + for (int i = 0 ; i < file_count; i++) { + free(filenames[i]); + } + + return ret; +} + +char **MakeFileArrayFromStr(char *str) +{ + char *strs[2]; + strs[0] = str; + strs[1] = NULL; + char **ret = MakeStrArrayFromArray(strs); + return ret; +} + +static char **_GetXFname(HWND HWin, BOOL Receive, const char *caption, LPLONG Option) +{ char FileDirExpanded[MAX_PATH]; ExpandEnvironmentStrings(ts.FileDir, FileDirExpanded, sizeof(FileDirExpanded)); PCHAR CurDir = FileDirExpanded; @@ -502,11 +585,12 @@ char *FNFilter = GetCommonDialogFilterA(!Receive ? ts.FileSendFilter : NULL, ts.UILanguageFile); - fv->FullName[0] = 0; + char FullName[MAX_PATH]; + FullName[0] = 0; if (!Receive) { char *default_filename = GetCommonDialogDefaultFilenameA(CurDir); if (default_filename != NULL) { - strncpy_s(fv->FullName, _countof(fv->FullName), default_filename, _TRUNCATE); + strncpy_s(FullName, _countof(FullName), default_filename, _TRUNCATE); free(default_filename); } } @@ -516,8 +600,8 @@ ofn.hwndOwner = HWin; ofn.lpstrFilter = FNFilter; ofn.nFilterIndex = 1; - ofn.lpstrFile = fv->FullName; - ofn.nMaxFile = _countof(fv->FullName); + ofn.lpstrFile = FullName; + ofn.nMaxFile = _countof(FullName); ofn.lpstrInitialDir = CurDir; LONG opt = *Option; if (! Receive) @@ -550,17 +634,20 @@ free(FNFilter); _SetCurrentDirectoryW(TempDir); + char **ret = NULL; if (Ok) { - fv->DirLen = ofn.nFileOffset; - fv->FnPtr = ofn.nFileOffset; + //fv->DirLen = ofn.nFileOffset; + //fv->FnPtr = ofn.nFileOffset; if (Receive) *Option = opt; else *Option = MAKELONG(LOWORD(*Option),HIWORD(opt)); + + ret = MakeFileArrayFromStr(FullName); } - return Ok; + return ret; } /** @@ -741,7 +828,7 @@ #define FnStrMemSize 4096 -static BOOL _GetMultiFname(PFileVarProto fv, WORD FuncId, LPWORD Option) +static char **_GetMultiFname(HWND hWnd, WORD FuncId, const char *caption, LPWORD Option) { OPENFILENAME ofn; wchar_t TempDir[MAX_PATH]; @@ -756,48 +843,29 @@ /* save current dir */ _GetCurrentDirectoryW(_countof(TempDir), TempDir); - fv->NumFname = 0; - - /* moemory should be zero-initialized */ -#if 0 - fv->FnStrMemHandle = GlobalAlloc(GHND, FnStrMemSize); - if (fv->FnStrMemHandle == NULL) { + char *FnStrMem = (char *)malloc(FnStrMemSize); + if (FnStrMem == NULL) { MessageBeep(0); return FALSE; } - else { - fv->FnStrMem = (char *)GlobalLock(fv->FnStrMemHandle); - if (fv->FnStrMem == NULL) { - GlobalFree(fv->FnStrMemHandle); - fv->FnStrMemHandle = 0; - MessageBeep(0); - return FALSE; - } - } -#endif + FnStrMem[0] = 0; - fv->FnStrMem = (char *)malloc(FnStrMemSize); - if (fv->FnStrMem == NULL) { - MessageBeep(0); - return FALSE; - } - char *FNFilter = GetCommonDialogFilterA(FileSendFilter, UILanguageFile); char *default_filename = GetCommonDialogDefaultFilenameA(CurDir); if (default_filename != NULL) { - strncpy_s(fv->FnStrMem, _countof(fv->FullName), default_filename, _TRUNCATE); + strncpy_s(FnStrMem, FnStrMemSize / sizeof(char), default_filename, _TRUNCATE); free(default_filename); } memset(&ofn, 0, sizeof(OPENFILENAME)); ofn.lStructSize = get_OPENFILENAME_SIZE(); - ofn.hwndOwner = fv->HMainWin; + ofn.hwndOwner = hWnd; ofn.lpstrFilter = FNFilter; ofn.nFilterIndex = 1; - ofn.lpstrFile = fv->FnStrMem; - ofn.nMaxFile = FnStrMemSize; - ofn.lpstrTitle= fv->DlgCaption; + ofn.lpstrFile = FnStrMem; + ofn.nMaxFile = FnStrMemSize / sizeof(char); + ofn.lpstrTitle= caption; ofn.lpstrInitialDir = CurDir; ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.Flags |= OFN_ALLOWMULTISELECT | OFN_EXPLORER; @@ -818,19 +886,30 @@ Ok = GetOpenFileName(&ofn); free(FNFilter); + char **ret; if (Ok) { int i, len; + int NumFname = 0; /* count number of file names */ - len = strlen(fv->FnStrMem); + len = strlen(FnStrMem); i = 0; while (len>0) { i = i + len + 1; - fv->NumFname++; - len = strlen(&fv->FnStrMem[i]); + NumFname++; + len = strlen(&FnStrMem[i]); } - fv->NumFname--; + NumFname--; + if (NumFname<1) { + // single selection + ret = MakeFileArrayFromStr(FnStrMem); + } + else { + // multiple selection + ret = MakeFileArrayMultiSelect(FnStrMem); + } +#if 0 if (fv->NumFname<1) { // single selection fv->NumFname = 1; fv->DirLen = ofn.nFileOffset; @@ -842,10 +921,19 @@ AppendSlash(fv->FullName,sizeof(fv->FullName)); fv->DirLen = strlen(fv->FullName); fv->FnPtr = strlen(fv->FnStrMem)+1; + file_array = MakeFileArrayMultiSelect(fv->FnStrMem); + + { + int a = 0; + } } +#endif - fv->FNCount = 0; +// fv->FNCount = 0; } + else { + ret = NULL; + } #if 0 GlobalUnlock(fv->FnStrMemHandle); @@ -854,15 +942,19 @@ fv->FnStrMemHandle = NULL; } #endif + +#if 0 if (! Ok) { free(fv->FnStrMem); fv->FnStrMem = NULL; } +#endif + free(FnStrMem); /* restore dir */ _SetCurrentDirectoryW(TempDir); - return Ok; + return ret; } static void _SetFileVar(PFileVarProto fv) @@ -896,7 +988,7 @@ if (strlen(&(FileVar->FullName[FileVar->DirLen]))==0) { - if (!_GetMultiFname(FileVar, GMF_KERMIT, &w) || + if (!_GetMultiFname(fv->HMainWin, GMF_KERMIT, fv->DlgCaption, &w) || (FileVar->NumFname==0)) { ProtoEnd(); @@ -1042,12 +1134,14 @@ if (strlen(&(FileVar->FullName[FileVar->DirLen]))==0) { Option = MAKELONG(ts.XmodemBin,ts.XmodemOpt); - if (! _GetXFname(FileVar->HMainWin, - mode==IdXReceive, fv->DlgCaption, &Option,FileVar)) - { + char **filenames = _GetXFname(FileVar->HMainWin, + mode==IdXReceive, fv->DlgCaption, &Option); + if (filenames == NULL) { ProtoEnd(); return; } + fv->FileNames = filenames; + GetNextFname(fv); tmp = HIWORD(Option); if (mode == IdXReceive) { if (IsXoptCRC(tmp)) { @@ -1187,7 +1281,7 @@ FileVar->OpId = OpYSend; if (strlen(&(FileVar->FullName[FileVar->DirLen]))==0) { - if (! _GetMultiFname(FileVar, GMF_Y,&Opt) || + if (! _GetMultiFname(fv->HMainWin, GMF_Y, fv->DlgCaption, &Opt) || (FileVar->NumFname==0)) { ProtoEnd(); @@ -1270,9 +1364,8 @@ FileVar->OpId = OpZSend; if (strlen(&(FileVar->FullName[FileVar->DirLen]))==0) { - if (! _GetMultiFname(FileVar, GMF_Z,&Opt) || - (FileVar->NumFname==0)) - { + char **filenames = _GetMultiFname(fv->HMainWin, GMF_Z, fv->DlgCaption, &Opt); + if (filenames == NUL) { if (mode == IdZAutoS) { CommRawOut(&cv, "\030\030\030\030\030\030\030\030\b\b\b\b\b\b\b\b\b\b", 18); } @@ -1279,6 +1372,8 @@ ProtoEnd(); return; } + fv->FileNames = filenames; + GetNextFname(fv); ts.XmodemBin = Opt; } else @@ -1482,7 +1577,7 @@ FileVar->OpId = OpQVSend; if (strlen(&(FileVar->FullName[FileVar->DirLen]))==0) { - if (! _GetMultiFname(FileVar, GMF_QV, &W) || + if (! _GetMultiFname(fv->HMainWin, GMF_QV, fv->DlgCaption, &W) || (FileVar->NumFname==0)) { ProtoEnd(); Modified: branches/proto_unicode/teraterm/teraterm/filesys_proto.h =================================================================== --- branches/proto_unicode/teraterm/teraterm/filesys_proto.h 2020-11-01 15:34:47 UTC (rev 8993) +++ branches/proto_unicode/teraterm/teraterm/filesys_proto.h 2020-11-01 15:35:00 UTC (rev 8994) @@ -37,10 +37,13 @@ char FullName[MAX_PATH]; int DirLen; - int NumFname, FNCount; + char **FileNames; + + int NumFname; + int FNCount; // HANDLE FnStrMemHandle; - PCHAR FnStrMem; - int FnPtr; +// PCHAR FnStrMem; +// int FnPtr; BOOL FileOpen; LONG FileSize, ByteCount; Modified: branches/proto_unicode/teraterm/ttpfile/ftlib.c =================================================================== --- branches/proto_unicode/teraterm/ttpfile/ftlib.c 2020-11-01 15:34:47 UTC (rev 8993) +++ branches/proto_unicode/teraterm/ttpfile/ftlib.c 2020-11-01 15:35:00 UTC (rev 8994) @@ -54,26 +54,21 @@ BOOL GetNextFname(PFileVarProto fv) { - /* next file name exists? */ + int i; - if (fv->FNCount >= fv->NumFname) - return FALSE; /* no more file name */ + const char *f = fv->FileNames[fv->FNCount]; + if (f == NULL) { + return FALSE; /* no more file name */ + } + fv->FNCount++; - fv->FNCount++; - if (fv->NumFname==1) return TRUE; + strncpy_s(fv->FullName, sizeof(fv->FullName), f, _TRUNCATE); + GetFileNamePos(fv->FullName, &(fv->DirLen), &i); + fv->DirLen++; // \x88\xEA\x94ԍŌ\xE3\x82\xCC '\\' \x82\xF0\x8Ew\x82\xB5\x82Ă\xA2\x82\xE9\x82̂\xC5 +1 \x82\xB5\x82Ă\xA8\x82\xAD -// GlobalLock(fv->FnStrMemHandle); - - strncpy_s(&fv->FullName[fv->DirLen],sizeof(fv->FullName) - fv->DirLen, - &fv->FnStrMem[fv->FnPtr],_TRUNCATE); - fv->FnPtr = fv->FnPtr + strlen(&fv->FnStrMem[fv->FnPtr]) + 1; - -// GlobalUnlock(fv->FnStrMemHandle); - - return TRUE; + return TRUE; } - WORD UpdateCRC(BYTE b, WORD CRC) { int i;