source: dev/trunk/ab5.0/jenga/src/common/Directory.cpp@ 804

Last change on this file since 804 was 718, checked in by dai, 16 years ago
  • Web設定をオプションダイアログから除去した(不要な機能)。
  • Jenga::Common::Environment::GetUserAppDirメソッドを追加。
  • Jenga::Common::Directoryクラスコンストラクタを修正。
File size: 3.6 KB
Line 
1#include "stdafx.h"
2
3using namespace Jenga::Common;
4
5Directory::Directory( const std::string &path, bool isMake )
6 : path( path )
7{
8 if ( isMake )
9 {
10 std::string tempPath = path;
11 if( !tempPath.empty() && tempPath[tempPath.size()-1] != '\\' )
12 {
13 tempPath += "\\";
14 }
15 if (!::MakeSureDirectoryPathExists(tempPath.c_str()))
16 {
17 Jenga::Throw( "MakeSureDirectoryPathExists failed!" );
18 }
19 }
20}
21Directory::Directory( const Directory &dir )
22 : path( dir.path )
23{
24}
25
26std::string Directory::GetFullPath( const std::string &relationPath ) const
27{
28 std::string resultPath = relationPath;
29
30 // '/'→'\'
31 BOOST_FOREACH( char &c, resultPath )
32 {
33 if( c == '/' )
34 {
35 c = '\\';
36 }
37 }
38
39 if( resultPath.find( ":" ) != std::string::npos || resultPath.find( "\\\\" ) != std::string::npos )
40 {
41 // フルパスが引き渡されていたとき
42 return resultPath;
43 }
44
45 int i=0,i2=0;
46 while(1){
47 if(resultPath[i]=='.'&&resultPath[i+1]=='\\') i+=2;
48 if(resultPath[i]=='.'&&resultPath[i+1]=='.'&&resultPath[i+2]=='\\'){
49 i2++;
50 i+=3;
51 }
52 else break;
53 }
54
55 int i3 = (int)path.size(),i4=0;
56 while(i4<i2){
57 for(i3--;;i3--){
58 if(path[i3-1]=='\\'){
59 i4++;
60 break;
61 }
62 }
63 }
64
65 char temporary[MAX_PATH];
66 memcpy(temporary,path.c_str(),i3);
67 temporary[i3]=0;
68 lstrcat(temporary,resultPath.c_str()+i);
69
70 return temporary;
71}
72
73void _GetRelationalPath(char *path,const char *dir){
74 //相対パスを取得
75 int i,i2,i3,i4,i5;
76 char temporary[MAX_PATH],temp2[MAX_PATH],temp3[MAX_PATH],temp4[MAX_PATH];
77
78 //ドライブ名をチェック
79 _splitpath(path,temporary,0,0,0);
80 _splitpath(dir,temp2,0,0,0);
81 if(lstrcmpi(temporary,temp2)!=0) return;
82
83 _splitpath(path,0,temporary,0,0);
84 _splitpath(dir,0,temp2,0,0);
85 i=1;i2=1;
86 while(1){
87 i4=i;
88 if(temporary[i-1]=='\\'&&temporary[i]){ //path側
89 for(i3=0;;i++,i3++){
90 if(temporary[i]=='\\'){
91 temp3[i3]=0;
92 i++;
93 break;
94 }
95 temp3[i3]=temporary[i];
96 }
97 }
98 else temp3[0]=0;
99
100 i5=i2;
101 if(temp2[i2-1]=='\\'&&temp2[i2]){ //dir側
102 for(i3=0;;i2++,i3++){
103 if(temp2[i2]=='\\'){
104 temp4[i3]=0;
105 i2++;
106 break;
107 }
108 temp4[i3]=temp2[i2];
109 }
110 }
111 else temp4[0]=0;
112
113 if(temp3[0]=='\0'&&temp4[0]=='\0'){
114 lstrcpy(temp3,".\\");
115 break;
116 }
117
118 if(lstrcmpi(temp3,temp4)!=0){
119 for(i3=0;;i5++){
120 if(temp2[i5]=='\0') break;
121 if(temp2[i5]=='\\') i3++;
122 }
123 if(i3==0) lstrcpy(temp3,".\\");
124 else{
125 temp3[0]=0;
126 for(i2=0;i2<i3;i2++) lstrcat(temp3,"..\\");
127 }
128 lstrcat(temp3,temporary+i4);
129 break;
130 }
131 }
132 _splitpath(path,0,0,temporary,temp2);
133 lstrcat(temp3,temporary);
134 lstrcat(temp3,temp2);
135 lstrcpy(path,temp3);
136}
137
138std::string Directory::GetRelationalPath( const std::string &fullPath ) const
139{
140 char temp[1024];
141 lstrcpy( temp, fullPath.c_str() );
142 _GetRelationalPath( temp, path.c_str() );
143 return temp;
144}
145
146void Directory::SearchFiles( Jenga::Common::Strings &resultOfFullPath, const std::string &findStr, bool isRecuresive ) const
147{
148 WIN32_FIND_DATA wfd;
149 HANDLE hFind=FindFirstFile( ( this->path + "\\" + findStr ).c_str(), &wfd );
150 if( hFind != INVALID_HANDLE_VALUE )
151 {
152 do
153 {
154 if( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
155 {
156 // ディレクトリのとき
157 if( isRecuresive )
158 {
159 Directory tempDir( this->path + "\\" + wfd.cFileName );
160 tempDir.SearchFiles( resultOfFullPath, findStr, isRecuresive );
161 }
162 }
163 else
164 {
165 //ファイルのとき
166 resultOfFullPath.push_back( this->path + "\\" + wfd.cFileName );
167 }
168 } while( FindNextFile( hFind, &wfd ) );
169
170 FindClose( hFind );
171 }
172}
Note: See TracBrowser for help on using the repository browser.