Path.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifdef _WIN32
  17. #include <windows.h>
  18. #include <shlwapi.h>
  19. #endif
  20. #include <string>
  21. #include <sys/stat.h>
  22. #include "./StringUtil.h"
  23. #ifndef PATH_MAX
  24. #define PATH_MAX 1024
  25. #endif
  26. namespace vconnect
  27. {
  28. using namespace std;
  29. class Path
  30. {
  31. public:
  32. /**
  33. * 絶対パスを取得する
  34. * @param path パス
  35. * @return path の絶対パス
  36. */
  37. static string getFullPath( string path )
  38. {
  39. #ifdef _WIN32
  40. char resolvedPath[MAX_PATH];
  41. GetFullPathNameA( path.c_str(), MAX_PATH * sizeof( char ), resolvedPath, NULL );
  42. string result = resolvedPath;
  43. if( exists( result ) ){
  44. return result;
  45. }else{
  46. return "";
  47. }
  48. #else
  49. char resolvedPath[PATH_MAX];
  50. string result = "";
  51. if( realpath( path.c_str(), resolvedPath ) ){
  52. result = resolvedPath;
  53. }
  54. return result;
  55. #endif
  56. }
  57. /**
  58. * パス文字列をつなげる
  59. * @param path1 パス
  60. * @param path2 パス
  61. * @return 連結されたパス文字列
  62. */
  63. static string combine( string path1, string path2 )
  64. {
  65. string separator = getDirectorySeparator();
  66. if( path1.length() > 0 && path1.rfind( separator ) == path1.length() - separator.length() ){
  67. path1 = path1.substr( 0, path1.length() - separator.length() );
  68. }
  69. if( path2.rfind( separator ) == 0 ){
  70. path2 = path2.substr( separator.length() );
  71. }
  72. return path1 + separator + path2;
  73. }
  74. /**
  75. * ディレクトリの区切り文字を取得する
  76. * @return 区切り文字
  77. */
  78. static string getDirectorySeparator()
  79. {
  80. char ch = getDirectorySeparatorChar();
  81. return string(&ch, 1);
  82. }
  83. static char getDirectorySeparatorChar()
  84. {
  85. #ifdef _WIN32
  86. return '\\';
  87. #else
  88. return '/';
  89. #endif
  90. }
  91. /**
  92. * ディレクトリのパスを取得する
  93. * @param path パス
  94. * @return ディレクトリのパス
  95. */
  96. static string getDirectoryName( string path )
  97. {
  98. string separator= getDirectorySeparator();
  99. string::size_type index = path.rfind( separator );
  100. if( index != string::npos ){
  101. return path.substr( 0, index );
  102. }else{
  103. return path;
  104. }
  105. }
  106. /**
  107. * パスが存在するかどうか調べる
  108. * @param path パス
  109. * @return ファイルまたはディレクトリが存在すれば true を返す
  110. */
  111. static bool exists( string path )
  112. {
  113. #ifdef _WIN32
  114. return PathFileExistsA( path.c_str() ) ? true : false;
  115. #else
  116. struct stat statResult;
  117. return (stat( path.c_str(), & statResult ) == 0) ? true : false;
  118. #endif
  119. }
  120. /**
  121. * パス区切り文字を、システムデフォルトのものに変換する
  122. * @param path パス
  123. * @return パス
  124. */
  125. static string normalize( string path )
  126. {
  127. string separator = getDirectorySeparator();
  128. string result = StringUtil::replace( path, "¥", separator );
  129. result = StringUtil::replace( result, "₩", separator );
  130. result = StringUtil::replace( result, "/", separator );
  131. result = StringUtil::replace( result, "\\", separator );
  132. return result;
  133. }
  134. static string getExtension(string const& path)
  135. {
  136. auto last_period = path.rfind('.');
  137. if (last_period != std::string::npos) {
  138. return path.substr(last_period);
  139. } else {
  140. return "";
  141. }
  142. }
  143. protected:
  144. /**
  145. * 隠蔽されたコンストラクタ
  146. */
  147. Path()
  148. {
  149. }
  150. };
  151. }
  152. #endif