00001
00008 #define DKUTIL_C_SJIS_FILESYSTEM_C
00009
00010 #ifdef WIN32
00011 #include <SYS/STAT.H>
00012 #endif
00013 #include "dkcOSIndependent.h"
00014 #include "dkcSJISFileSystem.h"
00015 #include "dkcStdio.h"
00016 #include "dkcString.h"
00017 #include "dkcThreadLock.h"
00018 #include "dkcSingleList.h"
00019 #include "dkcStream.h"
00020
00021
00022
00023 static DKC_INLINE BOOL jms1(int c){
00024 return (((((unsigned char)(c))>=0x81)&&(((unsigned char)(c))<=0x9F))||((((unsigned char)(c))>=0xE0)&&(((unsigned char)(c))<=0xFC)));
00025 }
00026 static DKC_INLINE BOOL jms2(int c){
00027 return ((((unsigned char)(c))!=0x7F)&&(((unsigned char)(c))>=0x40)&&(((unsigned char)(c))<=0xFC));
00028 }
00029
00040 DKC_EXTERN int WINAPI dkcIsShiftJIS( const char *str, int nPos )
00041 {
00042 int i;
00043 int state;
00044
00045 state = 0;
00046 for( i = 0; str[i] != '\0'; i++ )
00047 {
00048 if ( ( state == 0 ) && ( jms1( str[i] ) ) ) state = 1;
00049 else if ( ( state == 1 ) && ( jms2( str[i] ) ) ) state = 2;
00050 else if ( ( state == 2 ) && ( jms1( str[i] ) ) ) state = 1;
00051 else state = 0;
00052
00053
00054 if ( i == nPos ) return state;
00055 }
00056 return 0;
00057 }
00058 static DKC_INLINE int isJMS(const char *str, int nPos ){
00059 return dkcIsShiftJIS(str,nPos);
00060 }
00062 static DKC_INLINE char *strtail( const char *stringg )
00063 {
00064 return strchr( stringg, '\0' );
00065 }
00066
00071 const char * WINAPI dkcGetFileExtension( const char *PathName )
00072 {
00073
00074 char *p;
00075 char *get_tail;
00076
00077 get_tail = strtail( PathName );
00078 for( p = get_tail; p >= PathName; p-- )
00079 {
00080 if ( ('\\'==*p) && !isJMS(PathName,p-PathName) )
00081 return get_tail;
00082
00083 if ( '.' == *p )
00084 return p+1;
00085 }
00086 return get_tail;
00087 }
00088
00089
00090 BOOL WINAPI dkcIsEffectivePath(const char *path,size_t size){
00091 char dest[dkcdMAXPATH_BUFFER];
00092 const size_t dsize = dkcdMAXPATH_BUFFER;
00093
00094
00095 if(FALSE==dkcFileExist(path)){
00096 return FALSE;
00097 }
00098
00099 if(DKUTIL_FAILED(dkcToAbsolutelyPath(dest,dsize,path,size))){
00100 return FALSE;
00101 }
00102
00103 if(FALSE==dkcIsNativePathString(dest,strlen(dest))){
00104 return FALSE;
00105 }
00106
00107 return TRUE;
00108 }
00109 BOOL WINAPI dkcIsRelativityPath(const char *path)
00110 {
00111 int point;
00112 dkcmNOT_ASSERT(NULL==path);
00113 point = dkcSJIS_StrChrSearch(path,':');
00114 if(point == -1) return TRUE;
00115 return FALSE;
00116 }
00117 BOOL WINAPI dkcIsAbsolutelyPath(const char *path)
00118 {
00119 return !dkcIsRelativityPath(path);
00120 }
00121
00123 BOOL WINAPI dkcIsTailPathSep(const char *src,size_t dlen){
00124 int point;
00125 point = dkcSJIS_SearchPathSepLast(src);
00126
00127 if((size_t)point == dlen - 1)
00128 {
00129 return TRUE;
00130 }
00131
00132 return FALSE;
00133 }
00135 int WINAPI dkcPushBackPathSep(char *dest,size_t dlen,size_t size){
00136 if(FALSE==dkcIsTailPathSep(dest,dlen)){
00137 if(size < dlen + 2){
00138 return edk_OutputBufferWasLost;
00139 }
00140 dest[dlen ] = dkcdPATH_SEP;
00141 dest[dlen + 1] = '\0';
00142 return edk_SUCCEEDED;
00143 }
00144 return edk_EndProcess;
00145 }
00146
00147 int WINAPI dkcDirectoryConcatenate(char *dest,size_t dlen,size_t dsize,const char *src){
00148
00149
00150 dkcmNOT_ASSERT(dlen + 2 > dsize);
00151 if(dlen + 2 > dsize){
00152 return edk_FAILED;
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 dkcPushBackPathSep(dest,dlen,dsize);
00166 return dkc_strcat_amap(dest,dsize,dlen,src,strlen(src));
00167 }
00168
00169 int WINAPI dkcCurrentDirectoryConcatenate(char *dest,size_t dsize,const char *src)
00170 {
00171
00172
00173
00174 dkcmNOT_ASSERT(dsize <= dkcdMAXPATH_LEN);
00175
00176 dkcGetCurrentDirectory(dest,dsize);
00177
00178
00179 return dkcDirectoryConcatenate(dest,strlen(dest),dsize,src);
00180 }
00181 #define MAX_PATH_CHECK(dsize) \
00182 {\
00183 dkcmNOT_ASSERT(dsize < dkcdMAXPATH_BUFFER);\
00184 if(dsize < dkcdMAXPATH_BUFFER){\
00185 return edk_BufferOverFlow;\
00186 }\
00187 }
00188
00189 static int ToAbsolutelyLogic(char *dest,size_t dsize,const char *src)
00190 {
00191
00192
00193
00194 MAX_PATH_CHECK(dsize);
00195 # ifdef WIN32
00196
00197 if(NULL==_fullpath(dest,src,dsize)){
00198 return edk_FAILED;
00199 }
00200 # else//unix or linux ??
00201 if(NULL==__realpath(src,dest)){
00202 return edk_FAILED;
00203 }
00204 # endif
00205 return edk_SUCCEEDED;
00206 }
00207
00208 int WINAPI dkcToAbsolutelyPath(char *dest,size_t dsize,const char *src,size_t ssize)
00209 {
00210 char *tp = NULL;
00211 int r;
00212 MAX_PATH_CHECK(dsize);
00213
00214
00215 if(NULL==dest) return edk_FAILED;
00216
00217 if(dkcIsRelativityPath(src)==TRUE)
00218 {
00219
00220 tp = (char *)malloc(dkcdMAXPATH_BUFFER);
00221 if(NULL==tp) return edk_OutOfMemory;
00222
00223 dkcCurrentDirectoryConcatenate(tp,dkcdMAXPATH_BUFFER,src);
00224 r = ToAbsolutelyLogic(dest,dsize,tp);
00225 free(tp);
00226 return r;
00227
00228 }
00229 return ToAbsolutelyLogic(dest,dsize,src);
00230
00231
00232
00233
00234
00235
00236
00237
00238
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 }
00325
00326
00327 DKC_PATHSTRING * WINAPI dkcAllocPathString(const char *path)
00328 {
00329 DKC_PATHSTRING *p;
00330 size_t len;
00331
00332
00333
00334
00335 p = (DKC_PATHSTRING *)dkcAllocate(sizeof(DKC_PATHSTRING));
00336 if(NULL==p) return NULL;
00337
00338 p->mString = dkcAllocString(dkcdMAXPATH_BUFFER + 1);
00339 if(NULL==p->mString) goto Error;
00340
00341
00342 if(path){
00343 len = strlen(path);
00344
00345 if(FALSE==dkcIsNativePathString(path,len)){
00346 goto Error;
00347 }
00348 if(DKUTIL_FAILED(dkcPathStringCopy(p,path,len))){
00349 goto Error;
00350 }
00351 }
00352 p->mIterateCount = 0;
00353
00354 return p;
00355 Error:
00356 if(p){
00357 dkcFreeString(&p->mString);
00358 }
00359 dkcFree((void **)&p);
00360 return NULL;
00361 }
00362
00363 int WINAPI dkcFreePathString(DKC_PATHSTRING **ptr)
00364 {
00365 if(NULL==ptr || NULL==*ptr){
00366 return edk_ArgumentException;
00367 }
00368 dkcFreeString(&((*ptr)->mString));
00369 return dkcFree((void **)ptr);
00370 }
00371
00372
00373 size_t WINAPI dkcPathStringSize(const DKC_PATHSTRING *p)
00374 {
00375 return dkcStringSize(p->mString);
00376 }
00377
00378 const char *WINAPI dkcPathStringPointer(const DKC_PATHSTRING *p)
00379 {
00380 return dkcStringPointer(p->mString);
00381 }
00382
00383
00384
00385
00386 int WINAPI dkcPathStringDevideBegin(DKC_PATHSTRING *ptr,char *buff,size_t size)
00387 {
00388 return dkcPathStringDevideBegin_Logic(ptr,&ptr->mIterateCount,buff,size);
00389 }
00390
00391 int WINAPI dkcPathStringDevideNext(DKC_PATHSTRING *ptr,char *buff,size_t size)
00392 {
00393 return dkcPathStringDevideNext_Logic(ptr,&ptr->mIterateCount,buff,size);
00394
00395 }
00396
00397 void WINAPI dkcPathStringDevideEnd(DKC_PATHSTRING *ptr){
00398 dkcPathStringDevideEnd_Logic(&ptr->mIterateCount);
00399 }
00400
00401
00402 int WINAPI dkcPathStringDevideBegin_Logic(DKC_PATHSTRING *ptr,size_t *count,char *buff,size_t size)
00403 {
00404 int i,point;
00405 const char *p;
00406 dkcmNOT_ASSERT(NULL==ptr || NULL==buff || 0==size);
00407
00408
00409 p = dkcPathStringPointer(ptr);
00410
00411 point = dkcSJIS_StrChrSearch(p,'\\');
00412 if(-1==point){return edk_EndProcess;}
00413
00414 for(i=0;i<point;i++){
00415 if(':'==p[i]){
00416 if(DKUTIL_FAILED(dkc_strcpy(
00417 buff,size,p,(size_t)i
00418 )))
00419 {
00420 return edk_BufferOverFlow;
00421 }
00422 point = dkcSJIS_StrChrSearch(&p[i],'\\');
00423
00424 *count = (size_t)i + point + 1;
00425 return edk_SUCCEEDED;
00426 }
00427 }
00428 if(DKUTIL_FAILED(dkc_strcpy(
00429 buff,size,p,(size_t)point-1
00430 )))
00431 {
00432 return edk_FAILED;
00433 }
00434 *count = (size_t)point + 1;
00435 return edk_SUCCEEDED;
00436 }
00437
00438 int WINAPI dkcPathStringDevideNext_Logic(DKC_PATHSTRING *ptr,size_t *count,char *buff,size_t size)
00439 {
00440 int point;
00441 const char *p;
00442 size_t len;
00443
00444 p = dkcPathStringPointer(ptr);
00445 len = dkcStringSize(ptr->mString);
00446 if(len <= *count)
00447 {
00448 return edk_EndProcess;
00449 }
00450 point = dkcSJIS_StrChrSearch(&p[*count],'\\');
00451 if(-1==point)
00452 {
00453
00454
00455 len -= *count;
00456 if(DKUTIL_FAILED(dkc_strcpy(
00457 buff,size,&p[*count],len
00458 )))
00459 {
00460 return edk_FAILED;
00461 }
00462 *count += len;
00463 return edk_SUCCEEDED;
00464 }
00465 if(DKUTIL_FAILED(dkc_strcpy(
00466 buff,size,&p[*count],(size_t)point
00467 )))
00468 {
00469 return edk_FAILED;
00470 }
00471 *count += (size_t)point + 1;
00472 return edk_SUCCEEDED;
00473 }
00474
00475 void WINAPI dkcPathStringDevideEnd_Logic(size_t *count){
00476 *count = 0;
00477 }
00478
00479 int WINAPI dkcPathStringElementInsert_Logic(DKC_PATHSTRING *ptr,size_t count,
00480 const char *src,size_t len)
00481 {
00482 int r;
00483 size_t size = len + 5;
00484
00485 char *p;
00486 if(len==0 || FALSE==dkcIsNativePathString(src,len))
00487 {
00488 return edk_FAILED;
00489 }
00490 if(FALSE==dkcIsTailPathSep(src,len))
00491 {
00492 p = (char *)malloc(size);
00493
00494 if(!p) return edk_OutOfMemory;
00495 strcpy(p,src);
00496 dkcPushBackPathSep(p,len,size);
00497
00498 r = dkcStringInsert(ptr->mString,count,p,strlen(p));
00499 free(p);
00500 }else{
00501 r = dkcStringInsert(ptr->mString,count,src,len);
00502 }
00503 return r;
00504 }
00505
00506 int WINAPI dkcPathStringElementErase_Logic(
00507 DKC_PATHSTRING *ptr,size_t count)
00508 {
00509 const char *p = dkcPathStringPointer(ptr);
00510 int endlen = dkcSJIS_SearchPathSep(&p[count]);
00511
00512 if(-1==endlen){
00513 endlen = dkcPathStringSize(ptr);
00514 endlen = endlen - count;
00515 }else{
00516
00517 }
00518 return dkcStringErase(ptr->mString,count - 1,(size_t)endlen + 1);
00519 }
00520
00521
00522
00523 int WINAPI dkcPathStringElementReplace_Logic(DKC_PATHSTRING *ptr,size_t count,
00524 const char *src,size_t len)
00525 {
00526 const char *p = dkcPathStringPointer(ptr);
00527 int endlen;
00528 if(len==0 || FALSE==dkcIsNativePathString(src,len))
00529 {
00530 return edk_FAILED;
00531 }
00532 endlen = dkcSJIS_SearchPathSep(&p[count]);
00533 if(-1==endlen){
00534 endlen = dkcPathStringSize(ptr);
00535 endlen = endlen - count;
00536 }else{
00537 if(0 != endlen)
00538 endlen--;
00539 }
00540 return dkcStringReplace(ptr->mString,count,count + endlen,src,len);
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566 }
00567
00568
00569
00570
00571
00572
00573 static int dkcPathStringNormalizeCopyLogic(DKC_PATHSTRING *ptr,const char *buff,size_t size,
00574 int (WINAPI *function__)(DKC_STRING *,const char *,size_t))
00575 {
00576
00577 size_t len;
00578 int result;
00579
00580
00581 char pb[dkcdMAXPATH_BUFFER];
00582 size_t bsize = sizeof(pb);
00583
00584 result = dkcToAbsolutelyPath(pb,bsize,buff,size);
00585
00586
00587 len = strlen(pb);
00588
00589 # ifdef DEBUG //ありえないよ〜エラーチェック
00590 dkcmNOT_ASSERT(DKUTIL_FAILED(result));
00591 dkcmNOT_ASSERT(len >= bsize);
00592 dkcmNOT_ASSERT(NULL==function__);
00593 # endif
00594 if(DKUTIL_FAILED(result)){
00595 goto Error;
00596 }
00597 result = function__(ptr->mString,pb,len);
00598
00599 Error:
00600
00601 return result;
00602 }
00603
00604 int WINAPI dkcPathStringCopy(DKC_PATHSTRING *ptr,const char *buff,size_t size)
00605 {
00606
00607 dkcmNOT_ASSERT(ptr->mString->mByteSize + size >= dkcdMAXPATH);
00608 if(ptr->mString->mByteSize + size >= dkcdMAXPATH)
00609 {
00610 return edk_FAILED;
00611 }
00612 return dkcPathStringNormalizeCopyLogic(ptr,buff,size,dkcStringCopy);
00613 }
00614
00615 int WINAPI dkcPathStringNormalizeConcatenateLogic(
00616 DKC_PATHSTRING *ptr,const char *buff,size_t size)
00617 {
00618 char dest[dkcdMAXPATH_BUFFER];
00619
00620
00621 if(FALSE==dkcIsTailPathSep(dkcPathStringPointer(ptr),dkcPathStringSize(ptr)))
00622 {
00623 dkcStringConcatenate(ptr->mString,dkcdPATH_SEP_STR,1);
00624 }
00625 dkcStringConcatenate(ptr->mString,buff,size);
00626
00627 size = dkcPathStringSize(ptr) + 1;
00628
00629
00630
00631
00632
00633 if(DKUTIL_FAILED(
00634 ToAbsolutelyLogic(dest,sizeof(dest),dkcPathStringPointer(ptr))
00635 )){
00636 return edk_FAILED;
00637 }
00638
00639 return dkcPathStringCopy(ptr,dest,strlen(dest));
00640 }
00641
00642
00643 int WINAPI dkcPathStringConcatenate(DKC_PATHSTRING *ptr,const char *buff,size_t size)
00644 {
00645 int result;
00646
00647 dkcmNOT_ASSERT(ptr->mString->mByteSize + size >= dkcdMAXPATH);
00648 if(ptr->mString->mByteSize + size >= dkcdMAXPATH)
00649 {
00650 return edk_FAILED;
00651 }
00652
00653 if(ptr->mString->mByteSize)
00654 {
00655 result = dkcPathStringNormalizeConcatenateLogic(ptr,buff,size);
00656 }
00657 else
00658 {
00659 result = dkcPathStringCopy(ptr,buff,size);
00660 }
00661
00662 return result;
00663 }
00664
00665
00666
00667 int WINAPI dkcPathStringGetDrive(DKC_PATHSTRING *ptr,char *buff,size_t size){
00668 const char *p = dkcStringPointer(ptr->mString);
00669 int point = dkcSJIS_StrChrSearch(p,':');
00670 if(-1 == point) return edk_Not_Found;
00671
00672 return dkc_strcpy(buff,size,p,(size_t)1);
00673 }
00674
00675 int WINAPI dkcPathStringGetFileExtension(DKC_PATHSTRING *ptr,char *buff,size_t size)
00676 {
00677 int point2;
00678 size_t len;
00679 const char *p = dkcStringPointer(ptr->mString);
00680 int point = dkcSJIS_StrChrSearchLast(p,'.');
00681
00682 if(point < 0) return edk_Not_Found;
00683
00684 point2 = dkcSJIS_SearchPathSep(&p[point]);
00685 if(point < point2){
00686 return edk_Not_Found;
00687 }
00688 len = dkcStringSize(ptr->mString);
00689
00690
00691 if((size_t)(point + 1) > len) return edk_FAILED;
00692 return dkc_strcpy(buff,size,&p[point + 1],(size_t)len - (size_t)point );
00693
00694 }
00695
00696 int WINAPI dkcPathStringGetFileName(DKC_PATHSTRING *ptr,char *buff,size_t size)
00697 {
00698 const char *p = dkcStringPointer(ptr->mString);
00699
00700 int point = dkcSJIS_SearchPathSepLast(p);
00701 size_t len = dkcStringSize(ptr->mString);
00702
00703 #if 0
00704 if(point < 0) return edk_Not_Found;
00705 if((size_t)(point + 1) > len) return edk_FAILED;
00706 if((size_t)point == len) return edk_FAILED;
00707
00708 #else
00709 printf("%d",point);
00710
00711 dkcmFORCE_NOT_ASSERT(NULL==p);
00712 dkcmFORCE_NOT_ASSERT(point < 0);
00713 dkcmFORCE_NOT_ASSERT((size_t)(point + 1) > len);
00714 dkcmFORCE_NOT_ASSERT((size_t)point == len);
00715
00716 #endif
00717 return dkc_strcpy(buff,size,&p[point + 1],(size_t)len - (size_t)point );
00718 }
00719
00720 int WINAPI dkcPathStringGetDirectory(DKC_PATHSTRING *ptr,char *buff,size_t size)
00721 {
00722 const char *p = dkcStringPointer(ptr->mString);
00723 int point = dkcSJIS_StrChrSearchTail(p,strlen(p),dkcdPATH_SEP);
00724 size_t len = dkcStringSize(ptr->mString);
00725
00726 if(point < 0) return edk_FAILED;
00727 if((size_t)(point + 1) > len) return edk_FAILED;
00728
00729 return dkc_strcpy(buff,size,p,point);
00730
00731 }
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742 static DKC_INLINE file_exist_base(const char *filename,BOOL with_file,BOOL with_folder){
00743 struct stat s;
00744 if(!filename) return FALSE;
00745 if(!(stat(filename,&s)==0)) return FALSE;
00746
00747 if(s.st_mode & S_IFDIR){
00748 return with_folder;
00749 }
00750 return with_file;
00751 }
00752
00753 BOOL WINAPI dkcFileExist(const char *filename)
00754 {
00755 return file_exist_base(filename,TRUE,FALSE);
00756 }
00757
00758 BOOL WINAPI dkcFileAndFolderExist(const char *filename)
00759 {
00760 return file_exist_base(filename,TRUE,TRUE);
00761 }
00762
00763 BOOL WINAPI dkcFolderExist(const char *filename)
00764 {
00765 return file_exist_base(filename,FALSE,TRUE);
00766 }
00767
00768
00769
00770 ULONG WINAPI dkcFileSize(const char *filename){
00771 struct stat s;
00772 if(!filename) return 0;
00773 return (stat(filename,&s)==0) ? (ULONG)s.st_size : 0;
00774 }
00775 BOOL WINAPI dkcFileSize64(const char *str,DWORD *high,DWORD *low){
00776 #ifdef WIN32
00777 WIN32_FIND_DATA findData;
00778 HANDLE hFind=NULL;
00779
00780 if((hFind = FindFirstFile(str,&findData)) == INVALID_HANDLE_VALUE){
00781 return FALSE;
00782 }
00783
00784
00785 *high = findData.nFileSizeHigh;
00786 *low = findData.nFileSizeLow;
00787 FindClose(hFind);
00788 return TRUE;
00789 #else
00790
00791 #endif
00792 }
00793
00794
00795 BOOL WINAPI dkcSetCurrentDirectory(const char *filename){
00796 #ifdef DEBUG
00797 size_t len = strlen(filename);
00798 dkcmNOT_ASSERT(0==len || FALSE==dkcIsEffectivePath(filename,len));
00799 #endif
00800 # ifdef WIN32
00801 return(0 != SetCurrentDirectory(filename));
00802
00803 # else
00804 return (chdir(filename)==0);
00805 # endif
00806 }
00807
00808
00809 BOOL WINAPI dkcGetCurrentDirectory(char *buff,size_t size){
00810 # ifdef WIN32
00811 if(0==GetCurrentDirectory(size,buff)){
00812 return FALSE;
00813 }
00814
00815
00816
00817 #else
00818 if(NULL==getcwd(buff,size))
00819 return FALSE;
00820 #endif
00821 return TRUE;
00822
00823 #if 0
00824
00825 char path[dkcdMAXPATH_BUFFER + 1];
00826 size_t len;
00827 # ifdef WIN32
00828 if(0==GetCurrentDirectory(size,path)){
00829 return FALSE;
00830 }
00831
00832
00833
00834 #else
00835 if(NULL==getcwd(path,dkcdMAXPATH_BUFFER))
00836 return FALSE;
00837 #endif
00838 len = strlen(path);
00839 return DKUTIL_SUCCEEDED(dkc_strcpy(buff,size,path,len));
00840 #endif //end of if 0
00841 }
00842
00843 static BOOL WINAPI dkcCreateDirectoryLogic(const char *dir,const void *ptr){
00844 #ifdef WIN32
00845
00846 return (0 != CreateDirectory(dir,(SECURITY_ATTRIBUTES *)ptr));
00847 #else
00848 return (0 == mkdir( dir ));
00849 #endif
00850 }
00851
00852
00853
00854 int WINAPI dkcCreateDirectory(const char *pPath)
00855 {
00856 BOOL result;
00857 char work[dkcdMAXPATH_BUFFER];
00858 unsigned long n = 0;
00859 unsigned long len = strlen(pPath);
00860
00861 #ifdef WIN32//sjis support
00862 SECURITY_ATTRIBUTES attr;
00863
00864 DKUTIL_STRUCTURE_INIT(attr);
00865 NULL_CHAR_ARRAY(work);
00866
00867
00868 if(dkcdMAXPATH_LEN < len){
00869 dkcmNOT_ASSERT_MESSAGE("pathが長すぎる。",pPath);
00870 return edk_FAILED;
00871 }
00872 if(0==len ){
00873 return edk_ArgumentException;
00874 }
00875
00876
00877 if ( dkcmIsSJIS1(pPath[n]) || ! dkcmIS_INVALID_FOLDERNAME_CHAR(pPath[n]) )
00878 {
00879 work[n] = pPath[n];
00880 if(1==len){
00881
00882 attr.nLength = sizeof(SECURITY_ATTRIBUTES);
00883 attr.lpSecurityDescriptor = NULL;
00884 attr.bInheritHandle = FALSE;
00885
00886 result = dkcCreateDirectoryLogic( work, &attr );
00887
00888 dkcmNOT_ASSERT(FALSE==result && "directoryを作れなかった" );
00889 return edk_SUCCEEDED;
00890 }
00891 }
00892 n++;
00893
00894 while ( n < len )
00895 {
00896
00897 while ( n < len )
00898 {
00899
00900 if(! dkcmIsSJIS1(pPath[n - 1]) && ! dkcmIsSJIS2(pPath[n]) )
00901 {
00902 if ( ( dkcmIS_PATH_SEP(pPath[n]) ) )
00903 {
00904 if ( work[n-1] != ':' )
00905 {
00906 break;
00907 }
00908 }
00909 else if(dkcmIS_INVALID_FOLDERNAME_CHAR(pPath[n]))
00910 {
00911 return edk_FAILED;
00912 }
00913 }
00914
00915 work[n] = pPath[n];
00916 n++;
00917 }
00918 work[n] = '\0';
00919
00920
00921 attr.nLength = sizeof(SECURITY_ATTRIBUTES);
00922 attr.lpSecurityDescriptor = NULL;
00923 attr.bInheritHandle = FALSE;
00924
00925
00926
00927 result = dkcCreateDirectoryLogic( work, &attr );
00928
00929
00930 if(FALSE==result){
00931 return edk_FAILED;
00932 }
00933 work[n++] = dkcdPATH_SEP;
00934 }
00935 #else //no support sjis
00936 NULL_CHAR_ARRAY(work);
00937
00938
00939 if(dkcdMAXPATH_LEN < len){
00940 dkcmNOT_ASSERT_MESSAGE("pathが長すぎる。",pPath);
00941 return edk_FAILED;
00942 }
00943 if(0==len ){
00944 return edk_ArgumentException;
00945 }
00946
00947 while ( n < len )
00948 {
00949
00950 while ( n < len )
00951 {
00952 if ( ( dkcmIS_PATH_SEP(pPath[n]) ) && (n != '\0') )
00953 {
00954 if ( work[n-1] != ':' )
00955 {
00956 break;
00957 }
00958 }
00959 work[n] = pPath[n];
00960 n++;
00961 }
00962 work[n] = '\0';
00963
00964 result = dkcCreateDirectoryLogic( work,NULL );
00965
00966
00967 if(FALSE==result){
00968 return edk_FAILED;
00969 }
00970 work[n++] = dkcdPATH_SEP;
00971 }
00972
00973 #endif
00974
00975 return edk_SUCCEEDED;
00976 }
00977
00978 BOOL WINAPI dkcFileCopy(const char *dest,const char *src,BOOL bReWrite){
00979 return dkcFileCopyEx(dest,src,1024 * 64,bReWrite,FALSE,NULL,NULL);
00980 }
00981
00982 BOOL WINAPI dkcFileCopyEx(const char *dest,const char *src,
00983 size_t inner_buffer_size,BOOL bReWrite,
00984 BOOL bThreadLock,
00985 DKC_STD_CALLBACK pfcallback,void *parg)
00986 {
00987 void *buff;
00988 FILE *srcf,*destf;
00989 size_t filesize;
00990 size_t readed;
00991 size_t count;
00992 size_t i;
00993 size_t rest;
00994 int result = FALSE;
00995 DKC_THREAD_LOCK *lock = NULL;
00996 DKC_FILECOPY_CALLBACK_STRUCT cbdata;
00997
00998
00999 if(NULL==dest || NULL==src){
01000 return FALSE;
01001 }
01002 if(inner_buffer_size <= 1024){
01003 inner_buffer_size = 1024;
01004 }
01005
01006
01007 buff = malloc(inner_buffer_size);
01008 if(NULL==buff){
01009 inner_buffer_size = 1024 * 256;
01010 buff = malloc(inner_buffer_size);
01011 if(NULL==buff)
01012 return FALSE;
01013 }
01014
01015 if(bThreadLock){
01016 lock = dkcAllocThreadLock();
01017 if(NULL==lock){
01018 goto Error;
01019 }
01020 dkcThreadLock_Lock(lock);
01021 }
01022
01023 filesize = dkcFileSize(src);
01024
01025 if(FALSE==bReWrite && TRUE==dkcFileExist(dest)){
01026 goto Error;
01027 }
01028 cbdata.filesize = filesize;
01029 cbdata.count = 0;
01030 for(;;)
01031 {
01032 if(0 == filesize)
01033 {
01034 if(TRUE==dkcCreateEmptyFile(dest))
01035 {
01036 result = TRUE;
01037 }
01038 break;
01039 }
01040 if(filesize < inner_buffer_size)
01041 {
01042 if(DKUTIL_FAILED(dkcLoadBinary(buff,filesize,src,&readed)))
01043 {
01044 goto Error;
01045 }
01046 # ifdef DEBUG
01047 if(readed != filesize){
01048 ODS("readed != filesize why?\n");
01049 }
01050 # endif
01051 if(DKUTIL_SUCCEEDED(dkcSaveBinary(buff,filesize,dest)))
01052 {
01053 result = TRUE;
01054 }
01055 break;
01056 }
01057
01058
01059 srcf = dkcFOpen(src,"rb");
01060 if(NULL==srcf) goto Error;
01061 destf = dkcFOpen(dest,"wb");
01062 if(NULL==destf) goto Close;
01063
01064
01065 count = filesize / inner_buffer_size;
01066
01067 for(i=0;i<count;i++){
01068 dkcmFORCE_NOT_ASSERT(1 != fread(buff,inner_buffer_size,1,srcf));
01069 dkcmFORCE_NOT_ASSERT(1 != fwrite(buff,inner_buffer_size,1,destf));
01070
01071 cbdata.count += inner_buffer_size;
01072 if(pfcallback){
01073 if(FALSE==pfcallback(&cbdata,parg)){
01074 goto Close;
01075 }
01076 }
01077 }
01078
01079 rest = filesize - (count * inner_buffer_size);
01080
01081
01082 dkcmFORCE_NOT_ASSERT(rest != fread(buff,1,rest,srcf));
01083 dkcmFORCE_NOT_ASSERT(rest != fwrite(buff,1,rest,destf));
01084
01085
01086 cbdata.count += rest;
01087 dkcmNOT_ASSERT(cbdata.count != cbdata.filesize);
01088 if(pfcallback){
01089 if(FALSE==pfcallback(&cbdata,parg)){
01090 goto Close;
01091 }
01092 }
01093 result = TRUE;
01094 Close:
01095
01096 dkcFClose(&srcf);
01097 dkcFClose(&destf);
01098
01099 break;
01100 }
01101
01102
01103
01104
01105 Error:
01106 if(bThreadLock){
01107 if(lock){
01108 dkcThreadLock_Unlock(lock);
01109 dkcFreeThreadLock(&lock);
01110 }
01111 }
01112 if(buff){
01113 free(buff);buff=NULL;
01114 }
01115 return result;
01116 }
01117
01118 BOOL WINAPI dkcFileRemove(const char *filename)
01119 {
01120 #ifdef WIN32
01121 return (0 != DeleteFile(filename));
01122
01123 #else
01124 return (0==remove(filename));
01125 #endif
01126 }
01127
01128 BOOL WINAPI dkcFileRename(const char *oldname,const char *newname){
01129 #ifdef WIN32
01130 return (0==rename(oldname,newname));
01131 #else
01132 return (0==rename(oldname,newname));
01133 #endif
01134 }
01135
01136 BOOL WINAPI dkcCreateZeroByteFile(const char *filename,BOOL rewrite)
01137 {
01138 FILE *fp;
01139 int r = FALSE;
01140 if(FALSE==dkcFileExist(filename) || TRUE==rewrite){
01141 fp = fopen(filename,"wb");
01142 if(!fp){
01143 return r;
01144 }
01145 fclose(fp);
01146 r = TRUE;
01147 }
01148 return r;
01149 }
01150
01151
01152 int WINAPI dkcFileBinaryCompare(const char *filename1,const char *filename2)
01153 {
01154 BOOL r = FALSE;
01155
01156
01157 r = dkcFileExist(filename1);
01158 if(r==FALSE){
01159 return edk_ArgumentException;
01160 }
01161 r = dkcFileExist(filename2);
01162 if(r==FALSE){
01163 return edk_ArgumentException;
01164 }
01165
01166
01167 {
01168 DWORD high = 0,low = 0,high2 = 0,low2 = 0;
01169 r = dkcFileSize64(filename1,&high,&low);
01170 if(r==FALSE){
01171 return edk_ArgumentException;
01172 }
01173 r = dkcFileSize64(filename2,&high2,&low2);
01174 if(r==FALSE){
01175 return edk_ArgumentException;
01176 }
01177 r = (high==high2 && low==low2);
01178 if(FALSE==r){
01179 return edk_FAILED;
01180 }
01181
01182 }
01183
01184
01185 {
01186 DKC_STREAM *s1 = NULL,*s2=NULL;
01187 BYTE *buffer1 = NULL,*buffer2 = NULL;
01188 size_t buffsize = 1024 * 52;
01189
01190
01191
01192
01193
01194 r = edk_LogicError;
01195
01196 s1 = dkcAllocStreamFileType(
01197 edkcStreamDefaultEndian | edkcStreamProcessAsOrdered,
01198 filename1,"rb");
01199 if(NULL==s1){
01200 return edk_LogicError;
01201 }
01202
01203 s2 = dkcAllocStreamFileType(
01204 edkcStreamDefaultEndian | edkcStreamProcessAsOrdered,
01205 filename2,"rb");
01206
01207 if(NULL==s2){
01208 goto Error;
01209 }
01210
01211
01212 r = edk_OutOfMemory;
01213
01214 buffer1 = malloc(buffsize);
01215 if(NULL==buffer1){
01216
01217 goto Error;
01218 }
01219 buffer2 = malloc(buffsize);
01220 if(NULL==buffer2){
01221 goto Error;
01222 }
01223
01224
01225
01226 for(;;){
01227 size_t readsize1,readsize2;
01228 BOOL re1,re2;
01229
01230 dkcStreamRead(s1,buffer1,buffsize,&readsize1);
01231 dkcStreamRead(s2,buffer2,buffsize,&readsize2);
01232
01233 re1 = dkcStreamError(s1);
01234 re2 = dkcStreamError(s2);
01235 if(re1 || re2){
01236 r = edk_LogicError;
01237 goto Error;
01238 }
01239
01240
01241 dkcmNOT_ASSERT(readsize1 != readsize2);
01242
01243
01244 r = dkc_memcmp(buffer1,buffsize,buffer2,buffsize);
01245 if(DKUTIL_FAILED(r)){
01246 r = edk_FAILED;
01247 break;
01248 }
01249 re1 = dkcStreamEOF(s1);
01250 re2 = dkcStreamEOF(s2);
01251 if(re1 && re2){
01252 r = edk_SUCCEEDED;
01253 break;
01254 }
01255 else if(FALSE==re1 && FALSE==re2)
01256 {
01257 continue;
01258 }
01259 else
01260 {
01261 r = edk_LogicError;
01262 goto Error;
01263 }
01264 }
01265 Error:
01266 if(buffer2){
01267 free(buffer2);
01268 }
01269 if(buffer1){
01270 free(buffer1);
01271 }
01272 dkcFreeStream(&s2);
01273 dkcFreeStream(&s1);
01274 }
01275
01276 return r;
01277 }
01278
01279 int WINAPI dkcMemoryToFile(const char *filename,const void *buff,size_t size,UINT flag)
01280 {
01281 BOOL r;
01282 DKC_STREAM *p;
01283 int re = edk_FAILED;
01284 if(!(edkcFileRewrite & flag))
01285 {
01286
01287
01288 r = dkcFileExist(filename);
01289 if(r==TRUE){
01290 return edk_ArgumentException;
01291 }
01292 }
01293 p = dkcAllocStreamFileType(
01294 edkcStreamDefaultEndian | edkcStreamProcessAsOrdered,
01295 filename,"wb");
01296
01297 if(NULL==p){
01298 return edk_FAILED;
01299 }
01300 if(DKUTIL_FAILED(dkcStreamWrite(p,buff,size))){
01301 goto End;
01302 }
01303 re = edk_SUCCEEDED;
01304 End:
01305 dkcFreeStream(&p);
01306
01307 return re;
01308
01309 }
01310
01311
01312 int WINAPI dkcFileToMemory(const char *filename,void *buff,size_t size)
01313 {
01314 DWORD h = 0,l = 0;
01315 DKC_STREAM *p;
01316 int r = edk_FAILED;
01317 if(FALSE==dkcFileSize64(filename,&h,&l)){
01318 return edk_FileNotFound;
01319 }
01320
01321 if(h != 0 || ( (size_t)(l) > size ))
01322 {
01323 return edk_BufferOverFlow;
01324 }
01325
01326 p = dkcAllocStreamFileType(
01327 edkcStreamDefaultEndian | edkcStreamProcessAsOrdered,
01328 filename,"rb");
01329
01330 if(NULL==p){
01331 return edk_FAILED;
01332 }
01333 if(DKUTIL_FAILED(dkcStreamRead(p,buff,l,(size_t *)&h))){
01334 goto End;
01335 }
01336 if(h != l){
01337 goto End;
01338 }
01339 r = edk_SUCCEEDED;
01340 End:
01341 dkcFreeStream(&p);
01342 return r;
01343 }
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369 #if 0
01370
01371 DKC_FILE_FINDER * WINAPI dkcAllocFileFinder(
01372 const char *target,const char *dir,BOOL bSubDir
01373 ){
01374
01375 DKC_FILE_FINDER *p;
01376 p = dkcAllocate(sizeof(DKC_FILE_FINDER));
01377 if(NULL==p) return NULL;
01378
01379
01380 p->mDir = dkcAllocPathString(dir);
01381 if(NULL==p->mDir){
01382 goto Error;
01383 }
01384
01385 p->mStack = dkcAllocStack(10,sizeof(DKC_PATHSTRING *));
01386 if(NULL==p->mStack){
01387 goto Error;
01388 }
01389
01390 p->mTarget = dkcAllocString(128);
01391 if(NULL==p->mTarget){
01392 goto Error;
01393 }
01394
01395 dkcStringCopy(p->mTarget,target,strlen(target));
01396
01397 p->mState = edkcFileFinderEmpty;
01398 p->mbSubDir = bSubDir;
01399
01400 return p;
01401 Error:
01402 if(p){
01403 dkcFreeString(&(p->mTarget));
01404 dkcFreeStack(&(p->mStack));
01405 dkcFreePathString(&(p->mDir));
01406 }
01407 dkcFree((void **)&p);
01408 return NULL;
01409 }
01410
01411
01412 int WINAPI dkcFreeFileFinder(DKC_FILE_FINDER **p){
01413 if(NULL==p || NULL==(*p)){
01414 return edk_ArgumentException;
01415 }
01416 dkcFreeString(&(*p)->mTarget);
01417 dkcFreeStack(&(*p)->mStack);
01418 dkcFreePathString(&(*p)->mDir);
01419 return dkcFree((void **)p);
01420 }
01421
01422 static BOOL isDot(DKC_FILE_FINDER *ptr){
01423
01424 #ifdef WIN32
01425 return (
01426 strcmp(ptr->mFindData.cFileName,"..") == 0 ||
01427 strcmp(ptr->mFindData.cFileName,".") == 0
01428 );
01429 #else
01430
01431
01432 #endif
01433
01434 }
01435 static BOOL isFolder(DKC_FILE_FINDER *ptr){
01436 #ifdef WIN32
01437 return (
01438 ptr->mFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
01439 && strcmp(ptr->mFindData.cFileName,"..")!=0
01440 && strcmp(ptr->mFindData.cFileName,".")!=0
01441 );
01442 #else
01443
01444
01445 #endif
01446 }
01447 static int FFPushStack(DKC_FILE_FINDER *p){
01448 DKC_PATHSTRING *tp;
01449 int r;
01450 char buff[dkcdMAXPATH_BUFFER];
01451
01452 #ifdef WIN32
01453
01454 dkcCurrentDirectoryConcatenate(buff,sizeof(buff),p->mFindData.cFileName);
01455 #else
01456 dkcCurrentDirectoryConcatenate(buff,sizeof(buff),ptr->mDirent.d_name);
01457 #endif
01458
01459 tp = dkcAllocPathString(buff);
01460 if(NULL==tp){
01461 return edk_FAILED;
01462 }
01463 r = dkcStackDynamicPush(p->mStack,tp);
01464 return r;
01465 }
01466
01467 static BOOL FFIsStackEmpty(DKC_FILE_FINDER *p){
01468 return dkcStackIsEmpty(p->mStack);
01469 }
01470
01471
01472 static int FFPopStack(DKC_FILE_FINDER *p){
01473 int r;
01474 DKC_PATHSTRING *tp;
01475 r = dkcStackTop(p->mStack,&tp);
01476 if(DKUTIL_FAILED(r)){return r;}
01477
01478 dkcFreePathString( &(p->mDir));
01479 dkcStackPop(p->mStack);
01480
01481 p->mDir = tp;
01482 return r;
01483 }
01484
01485 static void FFReSearch(DKC_FILE_FINDER *p){
01486 dkcFindClose(p);
01487 FFPopStack(p);
01488 p->mState = edkcFileFinderEmpty;
01489 DKUTIL_STRUCTURE_INIT(p->mFindData);
01490 }
01491
01492
01493
01494
01495
01496
01498 static int ReflexiveSearch(DKC_FILE_FINDER *p,DKC_PATHSTRING *path,BOOL *bCopySucceeded)
01499 {
01500
01501 }
01502
01503
01504 #define FFCHECKING(p,path,bCopySucceeded)\
01505 if(TRUE==isDot(p))\
01506 {\
01507 p->mState = edkcFileFinderSearching;\
01508 return WithFolderSearch(p,path,bCopySucceeded);\
01509 }\
01510 if(TRUE==isFolder(p))\
01511 {\
01512 dkcFileFinderReferenceFileName(p,path);\
01513 p->mState = edkcFileFinderSearching;\
01514 FFPushStack(p);\
01515 return WithFolderSearch(p,path,bCopySucceeded);\
01516 }
01517
01518
01519 static int WithFolderSearch(DKC_FILE_FINDER *p,DKC_PATHSTRING *path,BOOL *bCopySucceeded)
01520 {
01521 int r;
01522
01523
01524
01525 r = 0;
01526 *bCopySucceeded = FALSE;
01527
01528 if(edkcFileFinderEmpty == p->mState)
01529 {
01530 r = dkcFindFirstFile(p);
01531 if(DKUTIL_FAILED(r)) return edk_FAILED;
01532 # ifdef WIN32 //windowsの場合は内部に格納している・・・。
01533 if(TRUE==isDot(p))
01534 {
01535 p->mState = edkcFileFinderSearching;
01536 return WithFolderSearch(p,path,bCopySucceeded);
01537 }
01538 if(TRUE==isFolder(p))
01539 {
01540 dkcFileFinderReferenceFileName(p,path);
01541 p->mState = edkcFileFinderSearching;
01542 FFPushStack(p);
01543 return WithFolderSearch(p,path,bCopySucceeded);
01544 }
01545
01546
01547 r = dkcFileFinderReferenceFileName(p,path);
01548
01549 if(DKUTIL_SUCCEEDED(r)){
01550 *bCopySucceeded = TRUE;
01551 }
01552 # endif
01553 p->mState = edkcFileFinderSearching;
01554 return r;
01555 }else if(edkcFileFinderSearching == p->mState)
01556 {
01557 r = dkcFindNextFile(p);
01558 if(edk_SUCCEEDED == r)
01559 {
01560 if(TRUE==isDot(p))
01561 {
01562 p->mState = edkcFileFinderSearching;
01563 return WithFolderSearch(p,path,bCopySucceeded);
01564 }
01565 if(TRUE==isFolder(p))
01566 {
01567 dkcFileFinderReferenceFileName(p,path);
01568 p->mState = edkcFileFinderSearching;
01569 FFPushStack(p);
01570 return WithFolderSearch(p,path,bCopySucceeded);
01571 }
01572
01573 r = dkcFileFinderReferenceFileName(p,path);
01574
01575 if(DKUTIL_SUCCEEDED(r)){
01576 *bCopySucceeded = TRUE;
01577 }
01578
01579
01580 }else if(edk_EndProcess == r)
01581 {
01582 if(FALSE==FFIsStackEmpty(p))
01583 {
01584 FFReSearch(p);
01585 return WithFolderSearch(p,path,bCopySucceeded);
01586 }else{
01587 dkcFindClose(p);
01588 }
01589 }else{
01590 dkcmNOT_ASSERT("そんなばかな〜");
01591 }
01592 return r;
01593 }else if(edkcFileFinderFinish == p->mState)
01594 {
01595 return edk_EndProcess;
01596 }
01597 dkcmNOT_ASSERT("dkcFileFinderNextのプログラムがおかしい。チートされているかも!?");
01598 return edk_FAILED;
01599
01600 }
01601
01602
01603 static int NormalSearch(DKC_FILE_FINDER *p,DKC_PATHSTRING *path,BOOL *bCopySucceeded)
01604 {
01605 int r;
01606 r = 0;
01607 *bCopySucceeded = FALSE;
01608
01609 if(edkcFileFinderEmpty == p->mState)
01610 {
01611 r = dkcFindFirstFile(p);
01612 if(DKUTIL_FAILED(r)) return edk_FAILED;
01613 # ifdef WIN32 //windowsの場合は内部に格納している・・・。
01614 if(TRUE==isDot(p) || TRUE==isFolder(p))
01615 {
01616 p->mState = edkcFileFinderSearching;
01617 return NormalSearch(p,path,bCopySucceeded);
01618 }
01619 r = dkcFileFinderReferenceFileName(p,path);
01620
01621 if(DKUTIL_SUCCEEDED(r)){
01622 *bCopySucceeded = TRUE;
01623 }
01624 # endif
01625 p->mState = edkcFileFinderSearching;
01626 return r;
01627 }else if(edkcFileFinderSearching == p->mState)
01628 {
01629 r = dkcFindNextFile(p);
01630 if(edk_SUCCEEDED == r)
01631 {
01632 if(TRUE==isDot(p) || TRUE==isFolder(p))
01633 {
01634
01635
01636
01637
01638 return NormalSearch(p,path,bCopySucceeded);
01639 }
01640 r = dkcFileFinderReferenceFileName(p,path);
01641
01642 if(DKUTIL_SUCCEEDED(r)){
01643 *bCopySucceeded = TRUE;
01644 }
01645
01646
01647 }else if(edk_EndProcess == r)
01648 {
01649 dkcFindClose(p);
01650 }else{
01651 dkcmNOT_ASSERT("そんなばかな〜");
01652 }
01653 return r;
01654 }else if(edkcFileFinderFinish == p->mState)
01655 {
01656 return edk_EndProcess;
01657 }
01658
01659
01660 dkcmNOT_ASSERT("dkcFileFinderNextのプログラムがおかしい。チートされているかも!?");
01661 return edk_FAILED;
01662
01663 }
01664
01670 int WINAPI dkcFileFinderNext(DKC_FILE_FINDER *p,DKC_PATHSTRING *path,BOOL *bCopySucceeded)
01671 {
01672 if(FALSE==p->mbSubDir){
01673 return NormalSearch(p,path,bCopySucceeded);
01674 }else{
01675 return WithFolderSearch(p,path,bCopySucceeded);
01676 }
01677
01678 }
01679 #endif
01680
01681 DKC_FINDFILE *WINAPI dkcAllocFindFile()
01682 {
01683 DKC_FINDFILE *p;
01684 p = (DKC_FINDFILE *)dkcAllocate(sizeof(DKC_FINDFILE));
01685 return p;
01686 }
01687 int WINAPI dkcFreeFindFile(DKC_FINDFILE **ptr){
01688 if(NULL==ptr ){
01689 return edk_FAILED;
01690 }
01691 return dkcFree((void **)ptr);
01692 }
01693
01694
01695 int WINAPI dkcFindFirstFile(DKC_FINDFILE *ptr,const char *target){
01696 #ifdef WIN32
01697 ptr->mHandle =
01698 FindFirstFileA( target, &(ptr->mFindData) );
01699 if(ptr->mHandle == INVALID_HANDLE_VALUE){
01700 return edk_FAILED;
01701 }
01702 #else
01703 ptr->mHandle = opendir( target );
01704 if(NULL==ptr->mHandle){
01705 return edk_FAILED;
01706 }
01707
01708 #endif
01709 return edk_SUCCEEDED;
01710 }
01711
01712 int WINAPI dkcFindNextFile(DKC_FINDFILE *ptr){
01713 # ifdef WIN32
01714 if ( 0 == FindNextFileA( ptr->mHandle, &(ptr->mFindData) ))
01715 {
01716 if ( GetLastError() == ERROR_NO_MORE_FILES )
01717 {
01718 return edk_EndProcess;
01719 }
01720 else
01721 {
01722 return edk_FAILED;
01723 }
01724 }
01725 # else
01726 errno = 0;
01727 ptr->mDirent = readdir( ptr->mHandle );
01728 if ( ptr->mDirent == 0 )
01729 {
01730 if ( errno == 0 )
01731 {
01732 return edk_EndProcess;
01733 }
01734 else
01735 {
01736 return edk_FAILED;
01737 }
01738 }
01739 # endif
01740 return edk_SUCCEEDED;
01741 }
01742
01743 int WINAPI dkcFindClose(DKC_FINDFILE *ptr)
01744 {
01745 #ifdef WIN32
01746 if(INVALID_HANDLE_VALUE == ptr->mHandle){
01747 return edk_FAILED;
01748 }
01749 FindClose(ptr->mHandle);
01750 ptr->mHandle = INVALID_HANDLE_VALUE;
01751 #else
01752 if(0 == ptr->mHandle){
01753 return edk_FAILED;
01754 }
01755 closedir(ptr->mHandle);
01756 ptr->mHandle = 0;
01757 ptr->mDirent = NULL;
01758 #endif
01759
01760 return edk_SUCCEEDED;
01761
01762 }
01763
01764 int WINAPI dkcFindFileGetFileName(DKC_FINDFILE *ptr,char *buff,size_t buffsize)
01765 {
01766 int r;
01767 size_t len;
01768 #ifdef WIN32
01769 len = strlen(ptr->mFindData.cFileName);
01770 if(0 == len) return edk_FAILED;
01771 r = dkc_strcpy(buff,buffsize,ptr->mFindData.cFileName,len);
01772 #else
01773 if(NULL==ptr->mDirent)
01774 {
01775 return edk_LogicError;
01776 }
01777 len = strlen(ptr->mDirent.d_name);
01778 if(0 == len) return edk_FAILED;
01779 r = dkc_strcpy(buff,buffsize,ptr->mDirent.d_name,len);
01780 #endif
01781 return r;
01782 }
01783
01784 BOOL WINAPI dkcFindFileIsFolder(DKC_FINDFILE *ptr){
01785
01786
01787 #ifdef WIN32
01788 return (
01789 ptr->mFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
01790 && strcmp(ptr->mFindData.cFileName,"..")!=0
01791 && strcmp(ptr->mFindData.cFileName,".")!=0
01792 );
01793 #else
01794
01795
01796 #endif
01797 }
01798
01799
01800 BOOL WINAPI dkcFindFileIsDot(DKC_FINDFILE *ptr){
01801 #ifdef WIN32
01802 return (
01803 strcmp(ptr->mFindData.cFileName,"..") == 0 ||
01804 strcmp(ptr->mFindData.cFileName,".") == 0
01805 );
01806 #else
01807
01808
01809 #endif
01810 }
01811
01812 BOOL WINAPI dkcFindFileIsNormalFile(DKC_FINDFILE *ptr)
01813 {
01814 #ifdef WIN32
01815 return (ptr->mFindData.dwFileAttributes & FILE_ATTRIBUTE_NORMAL);
01816 #else
01817
01818 #endif
01819 }
01820
01821 BOOL WINAPI dkcFindFileIsReadOnly(DKC_FINDFILE *ptr){
01822 #ifdef WIN32
01823 return (ptr->mFindData.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
01824 #else
01825
01826 #endif
01827 }
01828 void WINAPI dkcFindFileSize(DKC_FINDFILE *ptr,ULONG *High,ULONG *Low){
01829 #ifdef WIN32
01830 *High = ptr->mFindData.nFileSizeHigh;
01831 *Low = ptr->mFindData.nFileSizeLow;
01832 #else
01833
01834 #endif
01835
01836 }
01837
01838
01839
01840
01841
01842
01843
01844
01845
01846
01847
01848
01849
01850
01851
01852
01853
01854
01855
01856
01857
01858
01859
01860
01861
01862
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882
01883
01884
01885
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917
01918
01919
01920
01921
01922
01923
01924
01925
01926
01927
01928
01929
01930
01931
01932