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

Last change on this file since 680 was 680, checked in by dai_9181, 16 years ago

Directory::SearchFilesメソッドを追加。

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