Skip to content

Commit 22fc397

Browse files
authored
Merge pull request #3 from sakura-editor/master
2021-01-02 09:32:29
2 parents 383b01c + a76327d commit 22fc397

File tree

7 files changed

+138
-92
lines changed

7 files changed

+138
-92
lines changed

sakura_core/CBackupAgent.cpp

+12-17
Original file line numberDiff line numberDiff line change
@@ -445,34 +445,26 @@ bool CBackupAgent::FormatBackUpPath(
445445
}
446446

447447
}else{ // 詳細設定使用する
448-
WCHAR szFormat[1024];
449-
448+
SYSTEMTIME time = {};
450449
switch( bup_setting.GetBackupTypeAdv() ){
451450
case 4: // ファイルの日付,時刻
452451
{
453452
// 2005.10.20 ryoji FindFirstFileを使うように変更
454453
CFileTime ctimeLastWrite;
455454
GetLastWriteTimestamp( target_file, &ctimeLastWrite );
456-
if( !GetDateTimeFormat( szFormat, _countof(szFormat), bup_setting.m_szBackUpPathAdvanced , ctimeLastWrite.GetSYSTEMTIME() ) ){
457-
return false;
458-
}
455+
time = ctimeLastWrite.GetSYSTEMTIME();
459456
}
460457
break;
461458
case 2: // 現在の日付,時刻
462459
default:
463-
{
464-
// 2012.12.26 aroka 詳細設定のファイル保存日時と現在時刻で書式を合わせる
465-
SYSTEMTIME SystemTime;
466-
// 2016.07.28 UTC→ローカル時刻に変更
467-
::GetLocalTime(&SystemTime); // 現在時刻を取得
468-
469-
if( !GetDateTimeFormat( szFormat, _countof(szFormat), bup_setting.m_szBackUpPathAdvanced , SystemTime ) ){
470-
return false;
471-
}
472-
}
460+
// 2012.12.26 aroka 詳細設定のファイル保存日時と現在時刻で書式を合わせる
461+
// 2016.07.28 UTC→ローカル時刻に変更
462+
::GetLocalTime( &time ); // 現在時刻を取得
473463
break;
474464
}
475465

466+
std::wstring formatString = GetDateTimeFormat( bup_setting.m_szBackUpPathAdvanced.c_str(), time );
467+
476468
{
477469
// make keys
478470
// $0-$9に対応するフォルダ名を切り出し
@@ -503,11 +495,14 @@ bool CBackupAgent::FormatBackUpPath(
503495
{
504496
// $0-$9を置換
505497
//wcscpy( szNewPath, L"" );
506-
WCHAR *q= szFormat;
507-
WCHAR *q2 = szFormat;
498+
WCHAR *q = formatString.data();
499+
WCHAR *q2 = q;
508500
while( *q ){
509501
if( *q==L'$' ){
510502
++q;
503+
if( *q == L'\0' ){
504+
break;
505+
}
511506
if( isdigit(*q) ){
512507
q[-1] = L'\0';
513508
wcscat( szNewPath, q2 );

sakura_core/doc/CDocFileOperation.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,10 @@ bool CDocFileOperation::SaveFileDialog(
269269
if( pSaveInfo->cFilePath[0] == L'\0' ){
270270
SYSTEMTIME localTime = {};
271271
::GetLocalTime( &localTime );
272-
WCHAR dateTimeString[20] = {};
273-
if( !GetDateTimeFormat( dateTimeString, _countof(dateTimeString), L"_%Y%m%d_%H%M%S", localTime ) ){
274-
dateTimeString[0] = L'\0';
275-
}
276-
272+
auto dateTimeString = GetDateTimeFormat( L"_%Y%m%d_%H%M%S", localTime );
277273
const EditNode* node = CAppNodeManager::getInstance()->GetEditNode( m_pcDocRef->m_pcEditWnd->GetHwnd() );
278274
const int nId = (node != NULL && 0 < node->m_nId) ? node->m_nId : 0;
279-
auto_sprintf_s( pSaveInfo->cFilePath, pSaveInfo->cFilePath.GetBufferCount(), L"%s%.0d%s", LS(STR_NO_TITLE2), nId, dateTimeString );
275+
auto_sprintf_s( pSaveInfo->cFilePath, pSaveInfo->cFilePath.GetBufferCount(), L"%s%.0d%s", LS(STR_NO_TITLE2), nId, dateTimeString.c_str() );
280276
}
281277

282278
// ダイアログを表示

sakura_core/util/format.cpp

+48-65
Original file line numberDiff line numberDiff line change
@@ -26,78 +26,61 @@
2626
#include "StdAfx.h"
2727
#include "format.h"
2828

29-
/*! 日時をフォーマット
29+
/*! 書式文字列に従い日時を変換
3030
31-
@param[out] 書式変換後の文字列
32-
@param[in] バッファサイズ
33-
@param[in] format 書式
31+
@param[in] format 書式文字列
3432
@param[in] systime 書式化したい日時
35-
@return bool true
36-
37-
@note %Y %y %m %d %H %M %S の変換に対応
38-
39-
@author aroka
40-
@date 2005.11.21 新規
41-
42-
@todo 出力バッファのサイズチェックを行う
33+
@return 書式変換後の文字列
34+
35+
@note 書式文字列では以下の変換指定子が使用できます。
36+
@li "%Y" 西暦
37+
@li "%y" 下2桁の西暦
38+
@li "%m" 2桁の月
39+
@li "%d" 2桁の日
40+
@li "%H" 2桁の時
41+
@li "%M" 2桁の分
42+
@li "%S" 2桁の秒
43+
@note 書式文字列は末尾または最初のnull文字までを変換対象とします。
4344
*/
44-
bool GetDateTimeFormat( WCHAR* szResult, int size, const WCHAR* format, const SYSTEMTIME& systime )
45+
std::wstring GetDateTimeFormat( std::wstring_view format, const SYSTEMTIME& systime )
4546
{
46-
WCHAR szTime[10];
47-
const WCHAR *p = format;
48-
WCHAR *q = szResult;
49-
int len;
50-
51-
while( *p ){
52-
if( *p == L'%' ){
53-
++p;
54-
switch(*p){
55-
case L'Y':
56-
len = wsprintf(szTime,L"%d",systime.wYear);
57-
wcscpy( q, szTime );
58-
break;
59-
case L'y':
60-
len = wsprintf(szTime,L"%02d",(systime.wYear%100));
61-
wcscpy( q, szTime );
62-
break;
63-
case L'm':
64-
len = wsprintf(szTime,L"%02d",systime.wMonth);
65-
wcscpy( q, szTime );
66-
break;
67-
case L'd':
68-
len = wsprintf(szTime,L"%02d",systime.wDay);
69-
wcscpy( q, szTime );
70-
break;
71-
case L'H':
72-
len = wsprintf(szTime,L"%02d",systime.wHour);
73-
wcscpy( q, szTime );
74-
break;
75-
case L'M':
76-
len = wsprintf(szTime,L"%02d",systime.wMinute);
77-
wcscpy( q, szTime );
78-
break;
79-
case L'S':
80-
len = wsprintf(szTime,L"%02d",systime.wSecond);
81-
wcscpy( q, szTime );
82-
break;
83-
// A Z
84-
case L'%':
85-
default:
86-
*q = *p;
87-
len = 1;
88-
break;
47+
std::wstring result;
48+
wchar_t str[6] = {};
49+
bool inSpecifier = false;
50+
51+
result.reserve( format.length() * 2 );
52+
53+
for( const auto f : format ){
54+
if( inSpecifier ){
55+
inSpecifier = false;
56+
if( f == L'Y' ){
57+
swprintf( str, _countof(str), L"%d", systime.wYear );
58+
}else if( f == L'y' ){
59+
swprintf( str, _countof(str), L"%02d", systime.wYear % 100 );
60+
}else if( f == L'm' ){
61+
swprintf( str, _countof(str), L"%02d", systime.wMonth );
62+
}else if( f == L'd' ){
63+
swprintf( str, _countof(str), L"%02d", systime.wDay );
64+
}else if( f == L'H' ){
65+
swprintf( str, _countof(str), L"%02d", systime.wHour );
66+
}else if( f == L'M' ){
67+
swprintf( str, _countof(str), L"%02d", systime.wMinute );
68+
}else if( f == L'S' ){
69+
swprintf( str, _countof(str), L"%02d", systime.wSecond );
70+
}else{
71+
swprintf( str, _countof(str), L"%c", f );
8972
}
90-
q+=len;//q += strlen(szTime);
91-
++p;
92-
}
93-
else{
94-
*q = *p;
95-
q++;
96-
p++;
73+
result.append( str );
74+
}else if( f == L'%' ){
75+
inSpecifier = true;
76+
}else if( f == L'\0' ){
77+
break;
78+
}else{
79+
result.push_back( f );
9780
}
9881
}
99-
*q = *p;
100-
return true;
82+
83+
return result;
10184
}
10285

10386
/*! バージョン番号の解析

sakura_core/util/format.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727
#define SAKURA_FORMAT_A006AC9B_ADE2_499D_9CC6_00A649F32B4F_H_
2828
#pragma once
2929

30+
#include <string>
31+
#include <string_view>
32+
3033
// 20051121 aroka
31-
bool GetDateTimeFormat( WCHAR* szResult, int size, const WCHAR* format, const SYSTEMTIME& systime );
34+
std::wstring GetDateTimeFormat( std::wstring_view format, const SYSTEMTIME& systime );
3235
UINT32 ParseVersion( const WCHAR* ver ); //バージョン番号の解析
3336
int CompareVersion( const WCHAR* verA, const WCHAR* verB ); //バージョン番号の比較
3437
#endif /* SAKURA_FORMAT_A006AC9B_ADE2_499D_9CC6_00A649F32B4F_H_ */

tests/unittests/test-format.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*! @file */
2+
/*
3+
Copyright (C) 2018-2020 Sakura Editor Organization
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the authors be held liable for any damages
7+
arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any purpose,
10+
including commercial applications, and to alter it and redistribute it
11+
freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented;
14+
you must not claim that you wrote the original software.
15+
If you use this software in a product, an acknowledgment
16+
in the product documentation would be appreciated but is
17+
not required.
18+
19+
2. Altered source versions must be plainly marked as such,
20+
and must not be misrepresented as being the original software.
21+
22+
3. This notice may not be removed or altered from any source
23+
distribution.
24+
*/
25+
#include <gtest/gtest.h>
26+
#include <Windows.h>
27+
#include "util/format.h"
28+
29+
/*!
30+
* @brief GetDateTimeFormatのテスト
31+
*/
32+
TEST( format, GetDateTimeFormat )
33+
{
34+
SYSTEMTIME time = {};
35+
36+
// 変換指定子の解釈
37+
auto result1 = GetDateTimeFormat( L"%%-%1-%", time );
38+
ASSERT_STREQ( L"%-1-", result1.c_str() );
39+
40+
// 数字桁数少
41+
time.wYear = 1;
42+
time.wMonth = 2;
43+
time.wDay = 3;
44+
time.wHour = 1;
45+
time.wMinute = 2;
46+
time.wSecond = 3;
47+
auto result2 = GetDateTimeFormat( L"%Y-%y-%m-%d %H:%M:%S", time );
48+
ASSERT_STREQ( L"1-01-02-03 01:02:03", result2.c_str() );
49+
50+
// 数字桁数多
51+
time.wYear = 12345;
52+
time.wMonth = 12;
53+
time.wDay = 23;
54+
time.wHour = 12;
55+
time.wMinute = 34;
56+
time.wSecond = 56;
57+
auto result3 = GetDateTimeFormat( L"%Y-%y-%m-%d %H:%M:%S", time );
58+
ASSERT_STREQ( L"12345-45-12-23 12:34:56", result3.c_str() );
59+
60+
// 途中にnull文字
61+
auto result4 = GetDateTimeFormat( L"%Y-%y-%m-%d\0%H:%M:%S", time );
62+
ASSERT_STREQ( L"12345-45-12-23", result4.c_str() );
63+
}

tests/unittests/tests1.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
<ClCompile Include="test-cprofile.cpp" />
115115
<ClCompile Include="test-editinfo.cpp" />
116116
<ClCompile Include="test-file.cpp" />
117+
<ClCompile Include="test-format.cpp" />
117118
<ClCompile Include="test-grepinfo.cpp" />
118119
<ClCompile Include="test-int2dec.cpp" />
119120
<ClCompile Include="test-is_mailaddress.cpp" />

tests/unittests/tests1.vcxproj.filters

+8-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
<UniqueIdentifier>{0eefa0df-ca7f-489c-9844-9a182e1dba18}</UniqueIdentifier>
1212
</Filter>
1313
</ItemGroup>
14+
<ItemGroup Condition="Exists('$(VCInstallDir)\Auxiliary\VS\include\CodeCoverage\CodeCoverage.h')">
15+
<ClCompile Include="coverage.cpp">
16+
<Filter>Other Files</Filter>
17+
</ClCompile>
18+
</ItemGroup>
1419
<ItemGroup>
1520
<Text Include="CMakeLists.txt">
1621
<Filter>Other Files</Filter>
@@ -56,9 +61,6 @@
5661
<ClCompile Include="test-grepinfo.cpp">
5762
<Filter>Test Files</Filter>
5863
</ClCompile>
59-
<ClCompile Include="coverage.cpp">
60-
<Filter>Other Files</Filter>
61-
</ClCompile>
6264
<ClCompile Include="test-loadstring.cpp">
6365
<Filter>Test Files</Filter>
6466
</ClCompile>
@@ -83,6 +85,9 @@
8385
<ClCompile Include="test-file.cpp">
8486
<Filter>Test Files</Filter>
8587
</ClCompile>
88+
<ClCompile Include="test-format.cpp">
89+
<Filter>Test Files</Filter>
90+
</ClCompile>
8691
</ItemGroup>
8792
<ItemGroup>
8893
<ClInclude Include="StartEditorProcessForTest.h">

0 commit comments

Comments
 (0)