Skip to content

Commit

Permalink
- EvalDirなどフォルダ指定の時に絶対Pathでの指定を可能にした。
Browse files Browse the repository at this point in the history
	例)
		C:/YaneuraOu/Eval  ← Windowsのドライブレター付きの絶対Path
		\\MyNet\MyPC\Eval  ← WindowsのUNC
		~myeval            ← Linuxのhome
		/YaneuraOu/Eval    ← Windows、Linuxのroot

- Path.IsAbsolute()追加。

- namespace MiscにUnitTest追加。
  • Loading branch information
yaneurao committed Sep 15, 2021
1 parent 0fd67e8 commit a603883
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/USI拡張コマンド.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ USIプロトコルの独自拡張コマンド
EvalDir : 評価関数用のファイルの配置フォルダ(デフォルトでは eval/ )
isreadyコマンドで評価関数ファイルを読み込むので、起動後isreadyまでにsetoptionでこの設定を変更しないと効かない。

やねうら王 V6.05からフォルダは、絶対Pathでの指定が可能になりました。
例) C:/YaneuraOu/Eval

EnteringKingRule: 入玉ルール
NoEnteringKing : 入玉ルールなし
CSARule24 : 24点法。31点で宣言勝ちなので31点にならないと宣言勝ちはしない。
Expand Down
58 changes: 58 additions & 0 deletions source/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ extern "C" {
#include "misc.h"
#include "thread.h"
#include "usi.h"
#include "testcmd/unit_test.h"

using namespace std;

Expand Down Expand Up @@ -1369,6 +1370,10 @@ namespace Path
// ('/'自体は、Pathの区切り文字列として、WindowsでもLinuxでも使えるはずなので。
std::string Combine(const std::string& folder, const std::string& filename)
{
// 与えられたfileが絶対Pathであるかの判定
if (IsAbsolute(filename))
return filename;

if (folder.length() >= 1 && *folder.rbegin() != '/' && *folder.rbegin() != '\\')
return folder + "/" + filename;

Expand Down Expand Up @@ -1406,6 +1411,33 @@ namespace Path
return (length == 0) ? "" : path.substr(0,length);
}

// 絶対Pathであるかの判定。
// "\\"(WindowsのUNC)で始まるか、"/"で始まるか(Windows / Linuxのroot)、"~"で始まるか、"C:"(ドライブレター + ":")で始まるか。
bool IsAbsolute(const std::string& path)
{
// path separator
const auto path_char1 = '\\';
const auto path_char2 = '/';

// home directory
const auto home_char = '~';

// dirve letter separator
const auto drive_char = ':';

if (path.length() >= 1)
{
const char c = path[0];
if (c == path_char1 || c == path_char2 || c == home_char)
return true;

// 2文字目が":"なら1文字目をチェックしなくとも良いかな?
if (path.length() >= 2 && path[1] == drive_char)
return true;
}

return false;
}
};

// --------------------
Expand Down Expand Up @@ -1772,3 +1804,29 @@ namespace CommandLine {
}

}

namespace Misc {
// このheaderに書いてある関数のUnitTest。
void UnitTest(Test::UnitTester& tester)
{
auto section1 = tester.section("Misc");

{
auto section2 = tester.section("Path");

{
auto section3 = tester.section("Combine");

tester.test("Absolute Path Root1", Path::Combine("xxxx" , "/dir" ) == "/dir" );
tester.test("Absolute Path Root2", Path::Combine("xxxx" , "\\dir" ) == "\\dir" );
tester.test("Absolute Path Home", Path::Combine("xxxx" , "~dir" ) == "~dir" );
tester.test("Absolute Path Drive Letter", Path::Combine("xxxx" , "c:\\dir") == "c:\\dir" );
tester.test("Absolute Path UNC", Path::Combine("xxxx" , "\\\\dir") == "\\\\dir" );
tester.test("Relative Path1", Path::Combine("xxxx" , "yyy" ) == "xxxx/yyy" );
tester.test("Relative Path2", Path::Combine("xxxx/" , "yyy" ) == "xxxx/yyy" );
tester.test("Relative Path3", Path::Combine("xxxx\\", "yyy" ) == "xxxx\\yyy");
}

}
}
}
21 changes: 21 additions & 0 deletions source/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -745,13 +745,30 @@ namespace Path
{
// path名とファイル名を結合して、それを返す。
// folder名のほうは空文字列でないときに、末尾に'/'か'\\'がなければそれを付与する。
// 与えられたfilenameが絶対Pathである場合、folderを連結せずに単にfilenameをそのまま返す。
// 与えられたfilenameが絶対Pathであるかの判定は、内部的にはPath::IsAbsolute()を用いて行う。
//
// 実際の連結のされ方については、UnitTestに例があるので、それも参考にすること。
extern std::string Combine(const std::string& folder, const std::string& filename);

// full path表現から、(フォルダ名をすべて除いた)ファイル名の部分を取得する。
extern std::string GetFileName(const std::string& path);

// full path表現から、(ファイル名だけを除いた)ディレクトリ名の部分を取得する。
extern std::string GetDirectoryName(const std::string& path);

// 絶対Pathであるかの判定。
// ※ std::filesystem::absolute() は MSYS2 で Windows の絶対パスの判定に失敗するらしいので自作。
//
// 絶対Pathの条件 :
// "\\"(WindowsのUNC)で始まるか、"/"で始まるか(Windows / Linuxのroot)、"~"で始まるか、"C:"(ドライブレター + ":")で始まるか。
//
// 絶対Pathの例)
// C:/YaneuraOu/Eval ← Windowsのドライブレター付きの絶対Path
// \\MyNet\MyPC\Eval ← WindowsのUNC
// ~myeval ← Linuxのhome
// /YaneuraOu/Eval ← Windows、Linuxのroot
extern bool IsAbsolute(const std::string& path);
};

// --------------------
Expand Down Expand Up @@ -832,5 +849,9 @@ namespace CommandLine {
extern std::string workingDirectory; // path of the working directory
}

namespace Misc {
// このheaderに書いてある関数のUnitTest。
extern void UnitTest(Test::UnitTester& tester);
}

#endif // #ifndef MISC_H_INCLUDED
4 changes: 4 additions & 0 deletions source/testcmd/unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../position.h"
#include "../usi.h"
#include "../search.h"
#include "../misc.h"

using namespace std;

Expand Down Expand Up @@ -121,6 +122,9 @@ namespace Test
// Position class
tester.run(Position::UnitTest);

// Misc tools
tester.run(Misc::UnitTest);

}

}
Expand Down

0 comments on commit a603883

Please sign in to comment.