Skip to content

Commit

Permalink
Merge 29173b4 into 8f58ec8
Browse files Browse the repository at this point in the history
  • Loading branch information
berryzplus authored Oct 3, 2020
2 parents 8f58ec8 + 29173b4 commit a837a5d
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 51 deletions.
2 changes: 2 additions & 0 deletions sakura/sakura.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@
<ClInclude Include="..\sakura_core\io\CStream.h" />
<ClInclude Include="..\sakura_core\io\CTextStream.h" />
<ClInclude Include="..\sakura_core\io\CZipFile.h" />
<ClInclude Include="..\sakura_core\io\FilePathTooLongError.h" />
<ClInclude Include="..\sakura_core\macro\CCookieManager.h" />
<ClInclude Include="..\sakura_core\macro\CEditorIfObj.h" />
<ClInclude Include="..\sakura_core\macro\CIfObj.h" />
Expand Down Expand Up @@ -796,6 +797,7 @@
<ClCompile Include="..\sakura_core\io\CStream.cpp" />
<ClCompile Include="..\sakura_core\io\CTextStream.cpp" />
<ClCompile Include="..\sakura_core\io\CZipFile.cpp" />
<ClCompile Include="..\sakura_core\io\FilePathTooLongError.cpp" />
<ClCompile Include="..\sakura_core\macro\CCookieManager.cpp" />
<ClCompile Include="..\sakura_core\macro\CEditorIfObj.cpp" />
<ClCompile Include="..\sakura_core\macro\CIfObj.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions sakura/sakura.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,9 @@
<ClInclude Include="..\sakura_core\GrepInfo.h">
<Filter>Cpp Source Files</Filter>
</ClInclude>
<ClInclude Include="..\sakura_core\io\FilePathTooLongError.h">
<Filter>Cpp Source Files\io</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\resource\auto_scroll_center.cur">
Expand Down Expand Up @@ -2276,6 +2279,9 @@
<ClCompile Include="..\sakura_core\GrepInfo.cpp">
<Filter>Cpp Source Files</Filter>
</ClCompile>
<ClCompile Include="..\sakura_core\io\FilePathTooLongError.cpp">
<Filter>Cpp Source Files\io</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Image Include="..\resource\auto_scroll_center.bmp">
Expand Down
94 changes: 43 additions & 51 deletions sakura_core/_main/CCommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
#include <tchar.h>
#include <io.h>
#include <string.h>
#include <memory>
#include "debug/CRunningTimer.h"
#include "charset/charcode.h" // 2006.06.28 rastiv
#include "io/CTextStream.h"
#include "io/FilePathTooLongError.h"
#include "util/shell.h"
#include "util/file.h"
#include "env/CSakuraEnvironment.h"
Expand Down Expand Up @@ -244,40 +246,18 @@ void CCommandLine::ParseCommandLine( LPCWSTR pszCmdLineSrc, bool bResponse )
{
MY_RUNNINGTIMER( cRunningTimer, "CCommandLine::Parse" );

WCHAR szPath[_MAX_PATH];
bool bFind = false; // ファイル名発見フラグ
bool bParseOptDisabled = false; // 2007.09.09 genta オプション解析を行わなず,ファイル名として扱う
int nPos;
bool bParseOptDisabled = false; // 2007.09.09 genta オプション解析を行なわず,ファイル名として扱う
int nPos = 0;
int i = 0;
if( pszCmdLineSrc[0] != L'-' ){
for( i = 0; i < _countof( szPath ); ++i ){
if( pszCmdLineSrc[i] == L' ' || pszCmdLineSrc[i] == L'\0' ){
/* ファイルの存在をチェック */
szPath[i] = L'\0'; // 終端文字
if( fexist(szPath) ){
bFind = true;
break;
}
if( pszCmdLineSrc[i] == L'\0' ){
break;
}
}
szPath[i] = pszCmdLineSrc[i];
}
}
if( bFind ){
CSakuraEnvironment::ResolvePath(szPath);
wcscpy( m_fi.m_szPath, szPath ); /* ファイル名 */
nPos = i + 1;
}else{
m_fi.m_szPath[0] = L'\0';
nPos = 0;
}

CNativeW cmResponseFile = L"";
LPWSTR pszCmdLineWork = new WCHAR[lstrlen( pszCmdLineSrc ) + 1];
wcscpy( pszCmdLineWork, pszCmdLineSrc );
int nCmdLineWorkLen = lstrlen( pszCmdLineWork );

const int nCmdLineWorkLen = static_cast<int>( ::wcsnlen( pszCmdLineSrc, INT_MAX ) );

auto cmdLineWork = std::make_unique<WCHAR[]>( nCmdLineWorkLen + 1 );
LPWSTR pszCmdLineWork = cmdLineWork.get();
::wcscpy_s( pszCmdLineWork, nCmdLineWorkLen + 1, pszCmdLineSrc );

LPWSTR pszToken = my_strtok<WCHAR>( pszCmdLineWork, nCmdLineWorkLen, &nPos, L" " );
while( pszToken != NULL )
{
Expand All @@ -286,9 +266,10 @@ void CCommandLine::ParseCommandLine( LPCWSTR pszCmdLineSrc, bool bResponse )
// 2007.09.09 genta オプション判定ルール変更.オプション解析停止と""で囲まれたオプションを考慮
if( ( bParseOptDisabled ||
! (pszToken[0] == '-' || pszToken[0] == '"' && pszToken[1] == '-' ) )){
// ファイルパス解析用バッファ
WCHAR szPath[_MAX_PATH]{ 0 };

if( pszToken[0] == L'\"' ){
CNativeW cmWork;
// Nov. 3, 2005 genta
// 末尾のクォーテーションが無い場合を考慮して,
// 最後がダブルクォートの場合のみ取り除く
Expand All @@ -298,19 +279,22 @@ void CCommandLine::ParseCommandLine( LPCWSTR pszCmdLineSrc, bool bResponse )
// 引数がダブルクォート1つの場合に,その1つを最初と最後の1つずつと
// 見間違えて,インデックス-1にアクセスしてしまうのを防ぐために長さをチェックする
// ファイル名の後ろにあるOptionを解析するため,ループは継続
int len = lstrlen( pszToken + 1 );
if( len > 0 ){
cmWork.SetString( &pszToken[1], len - ( pszToken[len] == L'"' ? 1 : 0 ));
cmWork.Replace( L"\"\"", L"\"" );
wcscpy_s( szPath, _countof(szPath), cmWork.GetStringPtr() ); /* ファイル名 */
const size_t nTokenLen = ::wcsnlen( &pszToken[1], INT_MAX );
CNativeW cmWork;
cmWork.SetString( &pszToken[1], (int) nTokenLen - ( nTokenLen != 0 && pszToken[nTokenLen] == L'\"' ? 1 : 0 ) );
cmWork.Replace( L"\"\"", L"\"" );
if( _countof(szPath) == ::wcsnlen( cmWork.GetStringPtr(), _countof(szPath) ) ){
FilePathTooLongError( std::wstring_view( cmWork.GetStringPtr() ) ).ShowMessage();
}else{
::wcscpy_s( szPath, cmWork.GetStringPtr() );
}
else {
szPath[0] = L'\0';
}else{
if( _countof(szPath) == ::wcsnlen( pszToken, _countof(szPath) ) ){
FilePathTooLongError( std::wstring_view( pszToken ) ).ShowMessage();
}else{
::wcscpy_s( szPath, pszToken );
}
}
else{
wcscpy_s( szPath, _countof(szPath), pszToken ); /* ファイル名 */
}

// Nov. 11, 2005 susu
// 不正なファイル名のままだとファイル保存時ダイアログが出なくなるので
Expand All @@ -335,14 +319,9 @@ void CCommandLine::ParseCommandLine( LPCWSTR pszCmdLineSrc, bool bResponse )
i += nChars;
}

if (szPath[0] != L'\0') {
CSakuraEnvironment::ResolvePath(szPath);
if (m_fi.m_szPath[0] == L'\0') {
wcscpy(m_fi.m_szPath, szPath );
}
else {
m_vFiles.push_back( szPath );
}
if( szPath[0] != L'\0' ){
CSakuraEnvironment::ResolvePath( szPath );
m_vFiles.push_back( szPath );
}
}
else{
Expand Down Expand Up @@ -524,7 +503,6 @@ void CCommandLine::ParseCommandLine( LPCWSTR pszCmdLineSrc, bool bResponse )
}
pszToken = my_strtok<WCHAR>( pszCmdLineWork, nCmdLineWorkLen, &nPos, L" " );
}
delete [] pszCmdLineWork;

// レスポンスファイル解析
if( cmResponseFile.GetStringLength() && bResponse ){
Expand All @@ -539,5 +517,19 @@ void CCommandLine::ParseCommandLine( LPCWSTR pszCmdLineSrc, bool bResponse )
ParseCommandLine( responseData.c_str(), false );
}

// このエディタプロセスで開くファイルパスを決定する
if( m_fi.m_szPath[0] == L'\0' && !m_vFiles.empty() ){
// オプションでない引数のうち、最初に見つかった存在するファイルを検索
const auto cend = m_vFiles.cend();
auto it = std::find_if( m_vFiles.cbegin(), cend, [] ( const std::wstring& s ) { return fexist( s.c_str() ); } );
if( it == cend ){
// 見つからないときは、先頭要素を使う
it = m_vFiles.cbegin();
}
const std::wstring& firstFile = *it;
::wcscpy_s( m_fi.m_szPath, firstFile.c_str() );
m_vFiles.erase( it );
}

return;
}
52 changes: 52 additions & 0 deletions sakura_core/io/FilePathTooLongError.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*! @file */
/*
Copyright (C) 2018-2020 Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "StdAfx.h"
#include "io/FilePathTooLongError.h"
#include "util/MessageBoxF.h"
#include "CSelectLang.h"
#include "String_define.h"

/*!
* コンストラクタ
*
* @param path [in] 長過ぎるファイルパス
*/
FilePathTooLongError::FilePathTooLongError( const std::wstring_view& path )
: std::runtime_error("file path is too long.")
, path_buff(std::make_unique<wchar_t[]>(path.length() + 1))
{
path.copy( path_buff.get(), path.length() );
}

/*!
* エラーメッセージを表示する
*
* @param hwndParent [in,opt] ウインドウハンドル
*/
void FilePathTooLongError::ShowMessage( HWND hwndParent ) const
{
// L"%ls\nというファイルを開けません。\nファイルのパスが長すぎます。"
ErrorMessage( hwndParent, LS(STR_ERR_FILEPATH_TOO_LONG), path_buff.get() );
}
54 changes: 54 additions & 0 deletions sakura_core/io/FilePathTooLongError.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*! @file */
/*
Copyright (C) 2018-2020 Sakura Editor Organization
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#pragma once

#include <tchar.h>
#include <Windows.h>

#include <stdexcept>
#include <memory>
#include <string>

/*!
* ファイルパスが長過ぎるエラー
*/
class FilePathTooLongError : std::runtime_error {
std::unique_ptr<wchar_t[]> path_buff;

public:
/*!
* コンストラクタ
*
* @param path [in] 長過ぎるファイルパス
*/
FilePathTooLongError( const std::wstring_view& path );

/*!
* エラーメッセージを表示する
*
* @param hwndParent [in,opt] ウインドウハンドル
*/
void ShowMessage( HWND hwndParent = NULL ) const;
};

0 comments on commit a837a5d

Please sign in to comment.