1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
template<class dest,class src>
dest *copyString(src *str,long len=0){
if (!len)
for (;str[len];len++); //strlen() wouldn't work inside a template function.
dest *res=new dest[len+1];
res[len]=0;
for (long a=0;a<len;a++)
res[a]=str[a];
return res;
}
//Looks for str1 in str0 from the back.
template<typename T>
long instrB_template(T *str0,T *str1){
long len0,len1;
for (len0=0;str0[len0];len0++); //strlen() wouldn't work inside a template function.
for (len1=0;str1[len1];len1++);
str0+=len0-1;
for (long res=len0-1;res>=0;res--,str0--){
T *str2=str0;
T *str3=str1;
for (;*str3;str2++,str3++){
if (!*str2)
return -1;
if (*str2!=*str3)
goto instrB_000; //Fuck yeah!
}
return res;
instrB_000:;
}
return -1;
}
long instrB(wchar_t *str0,wchar_t *str1){
return instrB_template<wchar_t>(str0,str1);
}
template <typename dst,typename src>
void addStringsInplace_template(dst **str1,src *str2){
ulong len1=0,len2=0;
for (;(*str1)[len1];len1++);
for (;str2[len2];len2++);
ulong len3=len1+len2;
dst *res=new dst[len3+1];
res[len3]=0;
for (ulong a=0;a<len1;a++)
res[a]=(*str1)[a];
for (ulong a=0;a<len2;a++)
res[a+len1]=str2[a];
delete[] *str1;
*str1=res;
}
void addStringsInplace(wchar_t **str1,wchar_t *str2){
addStringsInplace_template<wchar_t,wchar_t>(str1,str2);
}
void addStringsInplace(wchar_t **str1,char *str2){
addStringsInplace_template<wchar_t,char>(str1,str2);
}
void addStringsInplace(char **str1,wchar_t *str2){
addStringsInplace_template<char,wchar_t>(str1,str2);
}
void addStringsInplace(char **str1,char *str2){
addStringsInplace_template<char,char>(str1,str2);
}
/*
behavior:
xx1b: all files
x1xb: all directories
1xxb: recursive
*/
std::vector<wchar_t *> *find(wchar_t *name,char behavior=5){
WIN32_FIND_DATA ffd;
std::vector<wchar_t *> *res=new std::vector<wchar_t *>;
HANDLE hFind = FindFirstFile(name, &ffd);
if (!hFind)
return res;
wchar_t *currentPath,*currentPattern;
{
wchar_t *temp=copyString<wchar_t>(name);
for (wchar_t *a=temp;*a;a++)
if (*a=='\\')
*a='/';
long p=instrB(temp,L"/");
if (p<0){
p=0;
currentPath=copyString<wchar_t>("./");
}else{
p++;
currentPath=copyString<wchar_t>(temp,p);
}
delete[] temp;
currentPattern=copyString<wchar_t>(name+p);
}
do{
if (!wcscmp(L".",ffd.cFileName) || !wcscmp(L"..",ffd.cFileName))
continue;
if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){
if ((behavior&1)==1){
wchar_t *temp=copyString<wchar_t>(currentPath);
addStringsInplace(&temp,ffd.cFileName);
res->push_back(temp);
}
}else{
wchar_t *temp=copyString<wchar_t>(currentPath);
addStringsInplace(&temp,ffd.cFileName);
addStringsInplace(&temp,"/");
if ((behavior&2)==2)
res->push_back(temp);
if ((behavior&4)==4){
wchar_t *temp2=copyString<wchar_t>(temp);
addStringsInplace(&temp2,currentPattern);
std::vector<wchar_t *> *v=find(temp2,behavior);
delete[] temp2;
res->insert(res->end(),v->begin(),v->end());
delete v;
}
}
}while (FindNextFile(hFind, &ffd));
FindClose(hFind);
delete[] currentPath;
delete[] currentPattern;
return res;
}
|