[vConnect/trunk/stand2.0] add: Path
@@ -0,0 +1,34 @@ | ||
1 | +#ifndef TEST_PathTest | |
2 | +#define TEST_PathTest | |
3 | +#include "cppunit/extensions/HelperMacros.h" | |
4 | +#include "../Path.h" | |
5 | + | |
6 | +using namespace std; | |
7 | +using namespace vconnect; | |
8 | + | |
9 | +class PathTest : public CppUnit::TestFixture | |
10 | +{ | |
11 | +public: | |
12 | + void getFullPath() | |
13 | + { | |
14 | + string expected = "/bin/sh"; | |
15 | + string actual = Path::getFullPath( "/bin/../bin/../bin/sh" ); | |
16 | + CPPUNIT_ASSERT_EQUAL( expected, actual ); | |
17 | + }; | |
18 | + | |
19 | + void combine() | |
20 | + { | |
21 | + string separator = Path::getDirectorySeparator(); | |
22 | + string actual = Path::combine( "abc" + separator, separator + "def" ); | |
23 | + string expected = "abc" + separator + "def"; | |
24 | + CPPUNIT_ASSERT_EQUAL( expected, actual ); | |
25 | + } | |
26 | + | |
27 | + CPPUNIT_TEST_SUITE( PathTest ); | |
28 | + CPPUNIT_TEST( getFullPath ); | |
29 | + CPPUNIT_TEST( combine ); | |
30 | + CPPUNIT_TEST_SUITE_END(); | |
31 | +}; | |
32 | + | |
33 | +CPPUNIT_TEST_SUITE_REGISTRATION( PathTest ); | |
34 | +#endif |
@@ -1,31 +1,32 @@ | ||
1 | -#include <cppunit/TestRunner.h> | |
2 | -#include <cppunit/TestResult.h> | |
3 | -#include <cppunit/TestResultCollector.h> | |
4 | -#include <cppunit/CompilerOutputter.h> | |
5 | -#include <cppunit/extensions/HelperMacros.h> | |
6 | -#include <cppunit/BriefTestProgressListener.h> | |
7 | -#include <cppunit/extensions/TestFactoryRegistry.h> | |
8 | - | |
9 | -#include "EncodingConverterTest.h" | |
10 | -#include "TextReaderTest.h" | |
11 | -#include "TextWriterTest.h" | |
12 | -#include "StringUtilTest.h" | |
13 | - | |
14 | -int main( int argc, char* argv[] ) | |
15 | -{ | |
16 | - CppUnit::TestResult controller; | |
17 | - CppUnit::TestResultCollector results; | |
18 | - controller.addListener( &results ); | |
19 | - | |
20 | - CppUnit::BriefTestProgressListener progress; | |
21 | - controller.addListener( &progress ); | |
22 | - | |
23 | - CppUnit::TestRunner runner; | |
24 | - runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() ); | |
25 | - runner.run( controller ); | |
26 | - | |
27 | - CppUnit::CompilerOutputter outputter( &results, CppUnit::stdCOut() ); | |
28 | - outputter.write(); | |
29 | - | |
30 | - return results.wasSuccessful() ? 0 : 1; | |
31 | -} | |
1 | +#include <cppunit/TestRunner.h> | |
2 | +#include <cppunit/TestResult.h> | |
3 | +#include <cppunit/TestResultCollector.h> | |
4 | +#include <cppunit/CompilerOutputter.h> | |
5 | +#include <cppunit/extensions/HelperMacros.h> | |
6 | +#include <cppunit/BriefTestProgressListener.h> | |
7 | +#include <cppunit/extensions/TestFactoryRegistry.h> | |
8 | + | |
9 | +#include "EncodingConverterTest.h" | |
10 | +#include "TextReaderTest.h" | |
11 | +#include "TextWriterTest.h" | |
12 | +#include "StringUtilTest.h" | |
13 | +#include "PathTest.h" | |
14 | + | |
15 | +int main( int argc, char* argv[] ) | |
16 | +{ | |
17 | + CppUnit::TestResult controller; | |
18 | + CppUnit::TestResultCollector results; | |
19 | + controller.addListener( &results ); | |
20 | + | |
21 | + CppUnit::BriefTestProgressListener progress; | |
22 | + controller.addListener( &progress ); | |
23 | + | |
24 | + CppUnit::TestRunner runner; | |
25 | + runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() ); | |
26 | + runner.run( controller ); | |
27 | + | |
28 | + CppUnit::CompilerOutputter outputter( &results, CppUnit::stdCOut() ); | |
29 | + outputter.write(); | |
30 | + | |
31 | + return results.wasSuccessful() ? 0 : 1; | |
32 | +} |
@@ -0,0 +1,94 @@ | ||
1 | +/* | |
2 | + * Path.h | |
3 | + * Copyright © 2012 kbinani | |
4 | + * | |
5 | + * This file is part of vConnect-STAND. | |
6 | + * | |
7 | + * vConnect-STAND is free software; you can redistribute it and/or | |
8 | + * modify it under the terms of the GPL License. | |
9 | + * | |
10 | + * vConnect-STAND is distributed in the hope that it will be useful, | |
11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
13 | + */ | |
14 | +#ifndef __Path_h__ | |
15 | +#define __Path_h__ | |
16 | + | |
17 | +#ifdef _WIN32 | |
18 | +#include <windows.h> | |
19 | +#endif | |
20 | +#include <string> | |
21 | + | |
22 | +namespace vconnect | |
23 | +{ | |
24 | + | |
25 | + using namespace std; | |
26 | + | |
27 | + class Path | |
28 | + { | |
29 | + public: | |
30 | + /** | |
31 | + * 絶対パスを取得する | |
32 | + * @param path パス | |
33 | + * @return path の絶対パス | |
34 | + */ | |
35 | + static string getFullPath( string path ) | |
36 | + { | |
37 | +#ifdef _WIN32 | |
38 | + char resolvedPath[MAX_PATH]; | |
39 | + GetFullPathNameA( path.c_str(), MAX_PATH * sizeof( char ), resolvedPath, NULL ); | |
40 | + string result = resolvedPath; | |
41 | + return result; | |
42 | +#else | |
43 | + char resolvedPath[PATH_MAX]; | |
44 | + string result = ""; | |
45 | + if( realpath( path.c_str(), resolvedPath ) ){ | |
46 | + result = resolvedPath; | |
47 | + } | |
48 | + return result; | |
49 | +#endif | |
50 | + } | |
51 | + | |
52 | + /** | |
53 | + * パス文字列をつなげる | |
54 | + * @param path1 パス | |
55 | + * @param path2 パス | |
56 | + * @return 連結されたパス文字列 | |
57 | + */ | |
58 | + static string combine( string path1, string path2 ) | |
59 | + { | |
60 | + string separator = getDirectorySeparator(); | |
61 | + if( path1.length() > 0 && path1.rfind( separator ) == path1.length() - separator.length() ){ | |
62 | + path1 = path1.substr( 0, path1.length() - separator.length() ); | |
63 | + } | |
64 | + if( path2.rfind( separator ) == 0 ){ | |
65 | + path2 = path2.substr( separator.length() ); | |
66 | + } | |
67 | + return path1 + separator + path2; | |
68 | + } | |
69 | + | |
70 | + /** | |
71 | + * ディレクトリの区切り文字を取得する | |
72 | + * @return 区切り文字 | |
73 | + */ | |
74 | + static string getDirectorySeparator() | |
75 | + { | |
76 | + #ifdef _WIN32 | |
77 | + return "\\"; | |
78 | + #else | |
79 | + return "/"; | |
80 | + #endif | |
81 | + } | |
82 | + | |
83 | + private: | |
84 | + /** | |
85 | + * 隠蔽されたコンストラクタ | |
86 | + */ | |
87 | + Path() | |
88 | + { | |
89 | + } | |
90 | + }; | |
91 | + | |
92 | +} | |
93 | + | |
94 | +#endif |