00001
00007 #ifdef WIN32
00008 #include <SYS/STAT.H>
00009 #endif
00010 #include "dkcOSIndependent.h"
00011 #include "dkcSJISFileSystem.h"
00012 #include "dkcStdio.h"
00013 #include "dkcString.h"
00014 #include "dkcThreadLock.h"
00015 #include "dkcSingleList.h"
00016
00017
00018
00019 static DKC_INLINE BOOL jms1(int c){
00020 return (((((unsigned char)(c))>=0x81)&&(((unsigned char)(c))<=0x9F))||((((unsigned char)(c))>=0xE0)&&(((unsigned char)(c))<=0xFC)));
00021 }
00022 static DKC_INLINE BOOL jms2(int c){
00023 return ((((unsigned char)(c))!=0x7F)&&(((unsigned char)(c))>=0x40)&&(((unsigned char)(c))<=0xFC));
00024 }
00025
00036 DKC_EXTERN int WINAPI dkcIsShiftJIS( const char *str, int nPos )
00037 {
00038 int i;
00039 int state;
00040
00041 state = 0;
00042 for( i = 0; str[i] != '\0'; i++ )
00043 {
00044 if ( ( state == 0 ) && ( jms1( str[i] ) ) ) state = 1;
00045 else if ( ( state == 1 ) && ( jms2( str[i] ) ) ) state = 2;
00046 else if ( ( state == 2 ) && ( jms1( str[i] ) ) ) state = 1;
00047 else state = 0;
00048
00049
00050 if ( i == nPos ) return state;
00051 }
00052 return 0;
00053 }
00054 static DKC_INLINE int isJMS(const char *str, int nPos ){
00055 return dkcIsShiftJIS(str,nPos);
00056 }
00058 static DKC_INLINE char *strtail( const char *stringg )
00059 {
00060 return strchr( stringg, '\0' );
00061 }
00062
00067 char * WINAPI dkcGetFileExtension( const char *PathName )
00068 {
00069
00070 char *p;
00071 char *get_tail;
00072
00073 get_tail = strtail( PathName );
00074 for( p = get_tail; p >= PathName; p-- )
00075 {
00076 if ( ('\\'==*p) && !isJMS(PathName,p-PathName) )
00077 return get_tail;
00078
00079 if ( '.' == *p )
00080 return p+1;
00081 }
00082 return get_tail;
00083 }
00084
00085
00086 BOOL WINAPI dkcIsEffectivePath(const char *path,size_t size){
00087 char dest[dkcdMAXPATH_BUFFER];
00088 const size_t dsize = dkcdMAXPATH_BUFFER;
00089
00090
00091 if(FALSE==dkcFileExist(path)){
00092 return FALSE;
00093 }
00094
00095 if(DKUTIL_FAILED(dkcToAbsolutelyPath(dest,dsize,path,size))){
00096 return FALSE;
00097 }
00098
00099 if(FALSE==dkcIsNativePathString(dest,strlen(dest))){
00100 return FALSE;
00101 }
00102
00103 return TRUE;
00104 }
00105 BOOL WINAPI dkcIsRelativityPath(const char *path)
00106 {
00107 int point;
00108 dkcmNOT_ASSERT(NULL==path);
00109 point = dkcSJIS_StrChrSearch(path,':');
00110 if(point == -1) return TRUE;
00111 return FALSE;
00112 }
00113 BOOL WINAPI dkcIsAbsolutelyPath(const char *path)
00114 {
00115 return !dkcIsRelativityPath(path);
00116 }
00117
00119 BOOL WINAPI dkcIsTailPathSep(const char *src,size_t dlen){
00120 int point;
00121 point = dkcSJIS_SearchPathSepLast(src);
00122
00123 if((size_t)point == dlen - 1)
00124 {
00125 return TRUE;
00126 }
00127
00128 return FALSE;
00129 }
00131 int WINAPI dkcPushBackPathSep(char *dest,size_t dlen,size_t size){
00132 if(FALSE==dkcIsTailPathSep(dest,dlen)){
00133 if(size < dlen + 2){
00134 return edk_OutputBufferWasLost;
00135 }
00136 dest[dlen ] = dkcdPATH_SEP;
00137 dest[dlen + 1] = '\0';
00138 return edk_SUCCEEDED;
00139 }
00140 return edk_EndProcess;
00141 }
00142
00143 int WINAPI dkcDirectoryConcatenate(char *dest,size_t dlen,size_t dsize,const char *src){
00144
00145
00146 dkcmNOT_ASSERT(dlen + 2 > dsize);
00147 if(dlen + 2 > dsize){
00148 return edk_FAILED;
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 dkcPushBackPathSep(dest,dlen,dsize);
00162 return dkc_strcat_amap(dest,dsize,dlen,src,strlen(src));
00163 }
00164
00165 int WINAPI dkcCurrentDirectoryConcatenate(char *dest,size_t dsize,const char *src)
00166 {
00167
00168
00169
00170 dkcmNOT_ASSERT(dsize <= dkcdMAXPATH_LEN);
00171
00172 dkcGetCurrentDirectory(dest,dsize);
00173
00174
00175 return dkcDirectoryConcatenate(dest,strlen(dest),dsize,src);
00176 }
00177 #define MAX_PATH_CHECK(dsize) \
00178 {\
00179 dkcmNOT_ASSERT(dsize < dkcdMAXPATH_BUFFER);\
00180 if(dsize < dkcdMAXPATH_BUFFER){\
00181 return edk_BufferOverFlow;\
00182 }\
00183 }
00184
00185 static int ToAbsolutelyLogic(char *dest,size_t dsize,const char *src)
00186 {
00187
00188
00189
00190 MAX_PATH_CHECK(dsize);
00191 # ifdef WIN32
00192
00193 if(NULL==_fullpath(dest,src,dsize)){
00194 return edk_FAILED;
00195 }
00196 # else//unix or linux ??
00197 if(NULL==__realpath(src,dest)){
00198 return edk_FAILED;
00199 }
00200 # endif
00201 return edk_SUCCEEDED;
00202 }
00203
00204 int WINAPI dkcToAbsolutelyPath(char *dest,size_t dsize,const char *src,size_t ssize)
00205 {
00206 char *tp = NULL;
00207 int r;
00208 MAX_PATH_CHECK(dsize);
00209
00210
00211 if(NULL==dest) return edk_FAILED;
00212
00213 if(dkcIsRelativityPath(src)==TRUE)
00214 {
00215
00216 tp = malloc(dkcdMAXPATH_BUFFER);
00217 if(NULL==tp) return edk_OutOfMemory;
00218
00219 dkcCurrentDirectoryConcatenate(tp,dkcdMAXPATH_BUFFER,src);
00220 r = ToAbsolutelyLogic(dest,dsize,tp);
00221 free(tp);
00222 return r;
00223
00224 }
00225 return ToAbsolutelyLogic(dest,dsize,src);
00226
00227
00228
00229
00230
00231
00232
00233
00234
00236
00237
00238
00239
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 DKC_PATHSTRING * WINAPI dkcAllocPathString(const char *path)
00324 {
00325 DKC_PATHSTRING *p;
00326 size_t len;
00327
00328
00329
00330
00331 p = dkcAllocate(sizeof(DKC_PATHSTRING));
00332 if(NULL==p) return NULL;
00333
00334 p->mString = dkcAllocString(dkcdMAXPATH_BUFFER + 1);
00335 if(NULL==p->mString) goto Error;
00336
00337
00338 if(path){
00339 len = strlen(path);
00340
00341 if(FALSE==dkcIsNativePathString(path,len)){
00342 goto Error;
00343 }
00344 dkcPathStringCopy(p,path,len);
00345 }
00346 p->mIterateCount = 0;
00347
00348 return p;
00349 Error:
00350 if(p){
00351 dkcFreeString(&p->mString);
00352 }
00353 dkcFree(&p);
00354 return NULL;
00355 }
00356
00357 int WINAPI dkcFreePathString(DKC_PATHSTRING **ptr)
00358 {
00359 if(NULL==ptr || NULL==*ptr){
00360 return edk_ArgumentException;
00361 }
00362 dkcFreeString(&((*ptr)->mString));
00363 return dkcFree(ptr);
00364 }
00365
00366
00367 size_t WINAPI dkcPathStringSize(const DKC_PATHSTRING *p)
00368 {
00369 return dkcStringSize(p->mString);
00370 }
00371
00372 const char *WINAPI dkcPathStringPointer(const DKC_PATHSTRING *p)
00373 {
00374 return dkcStringPointer(p->mString);
00375 }
00376
00377
00378
00379
00380 int WINAPI dkcPathStringDevideBegin(DKC_PATHSTRING *ptr,char *buff,size_t size)
00381 {
00382 return dkcPathStringDevideBegin_Logic(ptr,&ptr->mIterateCount,buff,size);
00383 }
00384
00385 int WINAPI dkcPathStringDevideNext(DKC_PATHSTRING *ptr,char *buff,size_t size)
00386 {
00387 return dkcPathStringDevideNext_Logic(ptr,&ptr->mIterateCount,buff,size);
00388
00389 }
00390
00391 void WINAPI dkcPathStringDevideEnd(DKC_PATHSTRING *ptr){
00392 dkcPathStringDevideEnd_Logic(&ptr->mIterateCount);
00393 }
00394
00395
00396 int WINAPI dkcPathStringDevideBegin_Logic(DKC_PATHSTRING *ptr,size_t *count,char *buff,size_t size)
00397 {
00398 int i,point;
00399 const char *p;
00400 dkcmNOT_ASSERT(NULL==ptr || NULL==buff || 0==size);
00401
00402
00403 p = dkcPathStringPointer(ptr);
00404
00405 point = dkcSJIS_StrChrSearch(p,'\\');
00406 if(-1==point){return edk_EndProcess;}
00407
00408 for(i=0;i<point;i++){
00409 if(':'==p[i]){
00410 if(DKUTIL_FAILED(dkc_strcpy(
00411 buff,size,p,(size_t)i
00412 )))
00413 {
00414 return edk_BufferOverFlow;
00415 }
00416 point = dkcSJIS_StrChrSearch(&p[i],'\\');
00417
00418 *count = (size_t)i + point + 1;
00419 return edk_SUCCEEDED;
00420 }
00421 }
00422 if(DKUTIL_FAILED(dkc_strcpy(
00423 buff,size,p,(size_t)point-1
00424 )))
00425 {
00426 return edk_FAILED;
00427 }
00428 *count = (size_t)point + 1;
00429 return edk_SUCCEEDED;
00430 }
00431
00432 int WINAPI dkcPathStringDevideNext_Logic(DKC_PATHSTRING *ptr,size_t *count,char *buff,size_t size)
00433 {
00434 int point;
00435 const char *p;
00436 size_t len;
00437
00438 p = dkcPathStringPointer(ptr);
00439 len = dkcStringSize(ptr->mString);
00440 if(len <= *count)
00441 {
00442 return edk_EndProcess;
00443 }
00444 point = dkcSJIS_StrChrSearch(&p[*count],'\\');
00445 if(-1==point)
00446 {
00447
00448
00449 len -= *count;
00450 if(DKUTIL_FAILED(dkc_strcpy(
00451 buff,size,&p[*count],len
00452 )))
00453 {
00454 return edk_FAILED;
00455 }
00456 *count += len;
00457 return edk_SUCCEEDED;
00458 }
00459 if(DKUTIL_FAILED(dkc_strcpy(
00460 buff,size,&p[*count],(size_t)point
00461 )))
00462 {
00463 return edk_FAILED;
00464 }
00465 *count += (size_t)point + 1;
00466 return edk_SUCCEEDED;
00467 }
00468
00469 void WINAPI dkcPathStringDevideEnd_Logic(size_t *count){
00470 *count = 0;
00471 }
00472
00473 int WINAPI dkcPathStringElementInsert_Logic(DKC_PATHSTRING *ptr,size_t count,
00474 const char *src,size_t len)
00475 {
00476 int r;
00477 size_t size = len + 5;
00478
00479 char *p;
00480 if(len==0 || FALSE==dkcIsNativePathString(src,len))
00481 {
00482 return edk_FAILED;
00483 }
00484 if(FALSE==dkcIsTailPathSep(src,len))
00485 {
00486 p = malloc(size);
00487
00488 if(!p) return edk_OutOfMemory;
00489 strcpy(p,src);
00490 dkcPushBackPathSep(p,len,size);
00491
00492 r = dkcStringInsert(ptr->mString,count,p,strlen(p));
00493 free(p);
00494 }else{
00495 r = dkcStringInsert(ptr->mString,count,src,len);
00496 }
00497 return r;
00498 }
00499
00500 int WINAPI dkcPathStringElementErase_Logic(
00501 DKC_PATHSTRING *ptr,size_t count)
00502 {
00503 const char *p = dkcPathStringPointer(ptr);
00504 int endlen = dkcSJIS_SearchPathSep(&p[count]);
00505
00506 if(-1==endlen){
00507 endlen = dkcPathStringSize(ptr);
00508 endlen = endlen - count;
00509 }else{
00510
00511 }
00512 return dkcStringErase(ptr->mString,count - 1,(size_t)endlen + 1);
00513 }
00514
00515
00516
00517 int WINAPI dkcPathStringElementReplace_Logic(DKC_PATHSTRING *ptr,size_t count,
00518 const char *src,size_t len)
00519 {
00520 const char *p = dkcPathStringPointer(ptr);
00521 int endlen;
00522 if(len==0 || FALSE==dkcIsNativePathString(src,len))
00523 {
00524 return edk_FAILED;
00525 }
00526 endlen = dkcSJIS_SearchPathSep(&p[count]);
00527 if(-1==endlen){
00528 endlen = dkcPathStringSize(ptr);
00529 endlen = endlen - count;
00530 }else{
00531 if(0 != endlen)
00532 endlen--;
00533 }
00534 return dkcStringReplace(ptr->mString,count,count + endlen,src,len);
00535
00536
00537
00538
00539
00540
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 static int dkcPathStringNormalizeCopyLogic(DKC_PATHSTRING *ptr,const char *buff,size_t size,
00568 int (WINAPI *function__)(DKC_STRING *,const char *,size_t))
00569 {
00570
00571 size_t len;
00572 int result;
00573
00574
00575 char pb[dkcdMAXPATH_BUFFER];
00576 size_t bsize = sizeof(pb);
00577
00578 result = dkcToAbsolutelyPath(pb,bsize,buff,size);
00579
00580
00581 len = strlen(pb);
00582
00583 # ifdef DEBUG //ありえないよ~エラーチェック
00584 dkcmNOT_ASSERT(DKUTIL_FAILED(result));
00585 dkcmNOT_ASSERT(len >= bsize);
00586 dkcmNOT_ASSERT(NULL==function__);
00587 # endif
00588 if(DKUTIL_FAILED(result)){
00589 goto Error;
00590 }
00591 result = function__(ptr->mString,pb,len);
00592
00593 Error:
00594
00595 return result;
00596 }
00597
00598 int WINAPI dkcPathStringCopy(DKC_PATHSTRING *ptr,const char *buff,size_t size)
00599 {
00600
00601 dkcmNOT_ASSERT(ptr->mString->mByteSize + size >= dkcdMAXPATH);
00602 if(ptr->mString->mByteSize + size >= dkcdMAXPATH)
00603 {
00604 return edk_FAILED;
00605 }
00606 return dkcPathStringNormalizeCopyLogic(ptr,buff,size,dkcStringCopy);
00607 }
00608
00609 int WINAPI dkcPathStringNormalizeConcatenateLogic(
00610 DKC_PATHSTRING *ptr,const char *buff,size_t size)
00611 {
00612 char dest[dkcdMAXPATH_BUFFER];
00613
00614
00615 if(FALSE==dkcIsTailPathSep(dkcPathStringPointer(ptr),dkcPathStringSize(ptr)))
00616 {
00617 dkcStringConcatenate(ptr->mString,dkcdPATH_SEP_STR,1);
00618 }
00619 dkcStringConcatenate(ptr->mString,buff,size);
00620
00621 size = dkcPathStringSize(ptr) + 1;
00622
00623
00624
00625
00626
00627 if(DKUTIL_FAILED(
00628 ToAbsolutelyLogic(dest,sizeof(dest),dkcPathStringPointer(ptr))
00629 )){
00630 return edk_FAILED;
00631 }
00632
00633 return dkcPathStringCopy(ptr,dest,strlen(dest));
00634 }
00635
00636
00637 int WINAPI dkcPathStringConcatenate(DKC_PATHSTRING *ptr,const char *buff,size_t size)
00638 {
00639 int result;
00640
00641 dkcmNOT_ASSERT(ptr->mString->mByteSize + size >= dkcdMAXPATH);
00642 if(ptr->mString->mByteSize + size >= dkcdMAXPATH)
00643 {
00644 return edk_FAILED;
00645 }
00646
00647 if(ptr->mString->mByteSize)
00648 {
00649 result = dkcPathStringNormalizeConcatenateLogic(ptr,buff,size);
00650 }
00651 else
00652 {
00653 result = dkcPathStringCopy(ptr,buff,size);
00654 }
00655
00656 return result;
00657 }
00658
00659
00660
00661 int WINAPI dkcPathStringGetDrive(DKC_PATHSTRING *ptr,char *buff,size_t size){
00662 const char *p = dkcStringPointer(ptr->mString);
00663 int point = dkcSJIS_StrChrSearch(p,':');
00664 if(-1 == point) return edk_Not_Found;
00665
00666 return dkc_strcpy(buff,size,p,(size_t)1);
00667 }
00668
00669 int WINAPI dkcPathStringGetFileExtension(DKC_PATHSTRING *ptr,char *buff,size_t size)
00670 {
00671 int point2;
00672 size_t len;
00673 const char *p = dkcStringPointer(ptr->mString);
00674 int point = dkcSJIS_StrChrSearchLast(p,'.');
00675
00676 if(point < 0) return edk_Not_Found;
00677
00678 point2 = dkcSJIS_SearchPathSep(&p[point]);
00679 if(point < point2){
00680 return edk_Not_Found;
00681 }
00682 len = dkcStringSize(ptr->mString);
00683
00684
00685 if((size_t)(point + 1) > len) return edk_FAILED;
00686 return dkc_strcpy(buff,size,&p[point + 1],(size_t)len - (size_t)point );
00687
00688 }
00689
00690 int WINAPI dkcPathStringGetFileName(DKC_PATHSTRING *ptr,char *buff,size_t size)
00691 {
00692 const char *p = dkcStringPointer(ptr->mString);
00693 int point = dkcSJIS_StrChrSearchLast(p,dkcdPATH_SEP);
00694 size_t len = dkcStringSize(ptr->mString);
00695
00696 if(point < 0) return edk_FAILED;
00697 if((size_t)(point + 1) > len) return edk_FAILED;
00698 if((size_t)point == len) return edk_FAILED;
00699 return dkc_strcpy(buff,size,&p[point + 1],(size_t)len - (size_t)point );
00700 }
00701
00702 int WINAPI dkcPathStringGetDirectory(DKC_PATHSTRING *ptr,char *buff,size_t size)
00703 {
00704 const char *p = dkcStringPointer(ptr->mString);
00705 int point = dkcSJIS_StrChrSearchTail(p,strlen(p),dkcdPATH_SEP);
00706 size_t len = dkcStringSize(ptr->mString);
00707
00708 if(point < 0) return edk_FAILED;
00709 if((size_t)(point + 1) > len) return edk_FAILED;
00710
00711 return dkc_strcpy(buff,size,p,point);
00712
00713 }
00714
00715
00716
00717
00718
00719 BOOL WINAPI dkcFileExist(const char *filename){
00720 struct stat s;
00721 if(!filename) return FALSE;
00722 return (stat(filename,&s)==0) ? TRUE : FALSE;
00723 }
00724
00725 UINT WINAPI dkcFileSize(const char *filename){
00726 struct stat s;
00727 if(!filename) return 0;
00728 return (stat(filename,&s)==0) ? (UINT)s.st_size : 0;
00729 }
00730
00731
00732 BOOL WINAPI dkcSetCurrentDirectory(const char *filename){
00733 #ifdef DEBUG
00734 size_t len = strlen(filename);
00735 dkcmNOT_ASSERT(0==len || FALSE==dkcIsEffectivePath(filename,len));
00736 #endif
00737 # ifdef WIN32
00738 return(0 != SetCurrentDirectory(filename));
00739
00740 # else
00741 return (chdir(filename)==0);
00742 # endif
00743 }
00744
00745
00746 BOOL WINAPI dkcGetCurrentDirectory(char *buff,size_t size){
00747 # ifdef WIN32
00748 if(0==GetCurrentDirectory(size,buff)){
00749 return FALSE;
00750 }
00751
00752
00753
00754 #else
00755 if(NULL==getcwd(buff,size))
00756 return FALSE;
00757 #endif
00758 return TRUE;
00759
00760 #if 0
00761
00762 char path[dkcdMAXPATH_BUFFER + 1];
00763 size_t len;
00764 # ifdef WIN32
00765 if(0==GetCurrentDirectory(size,path)){
00766 return FALSE;
00767 }
00768
00769
00770
00771 #else
00772 if(NULL==getcwd(path,dkcdMAXPATH_BUFFER))
00773 return FALSE;
00774 #endif
00775 len = strlen(path);
00776 return DKUTIL_SUCCEEDED(dkc_strcpy(buff,size,path,len));
00777 #endif //end of if 0
00778 }
00779
00780 static BOOL WINAPI dkcCreateDirectoryLogic(const char *dir,const void *ptr){
00781 #ifdef WIN32
00782
00783 return (0 != CreateDirectory(dir,(SECURITY_ATTRIBUTES *)ptr));
00784 #else
00785 return (0 == mkdir( dir ));
00786 #endif
00787 }
00788
00789
00790
00791 int WINAPI dkcCreateDirectory(const char *pPath)
00792 {
00793 BOOL result;
00794 char work[dkcdMAXPATH_BUFFER];
00795 unsigned long n = 0;
00796 unsigned long len = strlen(pPath);
00797
00798 #ifdef WIN32//sjis support
00799 SECURITY_ATTRIBUTES attr;
00800
00801 DKUTIL_STRUCTURE_INIT(attr);
00802 NULL_CHAR_ARRAY(work);
00803
00804
00805 if(dkcdMAXPATH_LEN < len){
00806 dkcmNOT_ASSERT_MESSAGE("pathが長すぎる。",pPath);
00807 return edk_FAILED;
00808 }
00809 if(0==len ){
00810 return edk_ArgumentException;
00811 }
00812
00813
00814 if ( dkcmIsSJIS1(pPath[n]) || ! dkcmIS_INVALID_FOLDERNAME_CHAR(pPath[n]) )
00815 {
00816 work[n] = pPath[n];
00817 if(1==len){
00818
00819 attr.nLength = sizeof(SECURITY_ATTRIBUTES);
00820 attr.lpSecurityDescriptor = NULL;
00821 attr.bInheritHandle = FALSE;
00822
00823 result = dkcCreateDirectoryLogic( work, &attr );
00824
00825 dkcmNOT_ASSERT("directoryを作れなかった" && FALSE==result);
00826 return edk_SUCCEEDED;
00827 }
00828 }
00829 n++;
00830
00831 while ( n < len )
00832 {
00833
00834 while ( n < len )
00835 {
00836
00837 if(! dkcmIsSJIS1(pPath[n - 1]) && ! dkcmIsSJIS2(pPath[n]) )
00838 {
00839 if ( ( dkcmIS_PATH_SEP(pPath[n]) ) )
00840 {
00841 if ( work[n-1] != ':' )
00842 {
00843 break;
00844 }
00845 }
00846 else if(dkcmIS_INVALID_FOLDERNAME_CHAR(pPath[n]))
00847 {
00848 return edk_FAILED;
00849 }
00850 }
00851
00852 work[n] = pPath[n];
00853 n++;
00854 }
00855 work[n] = '\0';
00856
00857
00858 attr.nLength = sizeof(SECURITY_ATTRIBUTES);
00859 attr.lpSecurityDescriptor = NULL;
00860 attr.bInheritHandle = FALSE;
00861
00862 result = dkcCreateDirectoryLogic( work, &attr );
00863
00864
00865 if(FALSE==result){
00866 return edk_FAILED;
00867 }
00868 work[n++] = dkcdPATH_SEP;
00869 }
00870 #else //no support sjis
00871 NULL_CHAR_ARRAY(work);
00872
00873
00874 if(dkcdMAXPATH_LEN < len){
00875 dkcmNOT_ASSERT_MESSAGE("pathが長すぎる。",pPath);
00876 return edk_FAILED;
00877 }
00878 if(0==len ){
00879 return edk_ArgumentException;
00880 }
00881
00882 while ( n < len )
00883 {
00884
00885 while ( n < len )
00886 {
00887 if ( ( dkcmIS_PATH_SEP(pPath[n]) ) && (n != '\0') )
00888 {
00889 if ( work[n-1] != ':' )
00890 {
00891 break;
00892 }
00893 }
00894 work[n] = pPath[n];
00895 n++;
00896 }
00897 work[n] = '\0';
00898
00899 result = dkcCreateDirectoryLogic( work,NULL );
00900
00901
00902 if(FALSE==result){
00903 return edk_FAILED;
00904 }
00905 work[n++] = dkcdPATH_SEP;
00906 }
00907
00908 #endif
00909
00910 return edk_SUCCEEDED;
00911 }
00912
00913 BOOL WINAPI dkcFileCopy(const char *dest,const char *src){
00914 return dkcFileCopyEx(dest,src,1024 * 64,FALSE);
00915 }
00916
00917 BOOL WINAPI dkcFileCopyEx(const char *dest,const char *src,
00918 size_t inner_buffer_size,BOOL bThreadLock)
00919 {
00920 void *buff;
00921 FILE *srcf,*destf;
00922 size_t filesize;
00923 size_t readed;
00924 size_t count;
00925 size_t i;
00926 size_t rest;
00927 int result = FALSE;
00928
00929 if(NULL==dest || NULL==src) return FALSE;
00930 if(inner_buffer_size <= 1024){
00931 inner_buffer_size = 1024;
00932 }
00933
00934
00935 buff = malloc(inner_buffer_size);
00936 if(NULL==buff){
00937 inner_buffer_size = 1024 * 256;
00938 buff = malloc(inner_buffer_size);
00939 if(NULL==buff)
00940 return FALSE;
00941 }
00942
00943 if(bThreadLock){
00944 dkcThreadLock_Lock();
00945 }
00946
00947 filesize = dkcFileSize(src);
00948 for(;;)
00949 {
00950 if(0 == filesize)
00951 {
00952 dkcCreateEmptyFile(dest);
00953 break;
00954 }
00955 if(filesize < inner_buffer_size)
00956 {
00957 if(DKUTIL_FAILED(dkcLoadBinary(buff,filesize,src,&readed)))
00958 {
00959 goto Error;
00960 }
00961 # ifdef DEBUG
00962 if(readed != filesize){
00963 ODS("readed != filesize why?\n");
00964 }
00965 # endif
00966 dkcSaveBinary(buff,filesize,dest);
00967 break;
00968 }
00969
00970
00971 srcf = dkcFOpen(src,"rb");
00972 if(NULL==srcf) goto Error;
00973 destf = dkcFOpen(dest,"wb");
00974 if(NULL==destf) goto Error;
00975
00976
00977 count = filesize / inner_buffer_size;
00978
00979 for(i=0;i<count;i++){
00980 dkcmNOT_ASSERT(1 != fread(buff,inner_buffer_size,1,srcf));
00981 dkcmNOT_ASSERT(1 != fwrite(buff,inner_buffer_size,1,destf));
00982 }
00983
00984 rest = filesize - (count * inner_buffer_size);
00985
00986
00987 dkcmNOT_ASSERT(rest != fread(buff,1,rest,srcf));
00988 dkcmNOT_ASSERT(rest != fwrite(buff,1,rest,destf));
00989
00990
00991 dkcFClose(&srcf);
00992 dkcFClose(&destf);
00993
00994 break;
00995 }
00996
00997
00998 result = TRUE;
00999 Error:
01000 if(bThreadLock){
01001 dkcThreadLock_Unlock();
01002 }
01003 if(buff){
01004 free(buff);buff=NULL;
01005 }
01006 return result;
01007 }
01008
01009 BOOL WINAPI dkcFileRemove(const char *filename)
01010 {
01011 #ifdef WIN32
01012 return (0 != DeleteFile(filename));
01013
01014 #else
01015 return (0==remove(filename));
01016 #endif
01017 }
01018
01019 BOOL WINAPI dkcFileRename(const char *oldname,const char *newname){
01020 #ifdef WIN32
01021 return (0==rename(oldname,newname));
01022 #else
01023 return (0==rename(oldname,newname));
01024 #endif
01025 }
01026
01027 BOOL WINAPI dkcCreateZeroByteFile(const char *filename,BOOL rewrite)
01028 {
01029 FILE *fp;
01030 int r = FALSE;
01031 if(FALSE==dkcFileExist(filename) || TRUE==rewrite){
01032 fp = fopen(filename,"wb");
01033 if(!fp){
01034 return r;
01035 }
01036 fclose(fp);
01037 r = TRUE;
01038 }
01039 return r;
01040 }
01041
01042
01043
01044
01045
01046
01047 #if 0
01048
01049 DKC_FILE_FINDER * WINAPI dkcAllocFileFinder(
01050 const char *target,const char *dir,BOOL bSubDir
01051 ){
01052
01053 DKC_FILE_FINDER *p;
01054 p = dkcAllocate(sizeof(DKC_FILE_FINDER));
01055 if(NULL==p) return NULL;
01056
01057
01058 p->mDir = dkcAllocPathString(dir);
01059 if(NULL==p->mDir){
01060 goto Error;
01061 }
01062
01063 p->mStack = dkcAllocStack(10,sizeof(DKC_PATHSTRING *));
01064 if(NULL==p->mStack){
01065 goto Error;
01066 }
01067
01068 p->mTarget = dkcAllocString(128);
01069 if(NULL==p->mTarget){
01070 goto Error;
01071 }
01072
01073 dkcStringCopy(p->mTarget,target,strlen(target));
01074
01075 p->mState = edkcFileFinderEmpty;
01076 p->mbSubDir = bSubDir;
01077
01078 return p;
01079 Error:
01080 if(p){
01081 dkcFreeString(&(p->mTarget));
01082 dkcFreeStack(&(p->mStack));
01083 dkcFreePathString(&(p->mDir));
01084 }
01085 dkcFree(&p);
01086 return NULL;
01087 }
01088
01089
01090 int WINAPI dkcFreeFileFinder(DKC_FILE_FINDER **p){
01091 if(NULL==p || NULL==(*p)){
01092 return edk_ArgumentException;
01093 }
01094 dkcFreeString(&(*p)->mTarget);
01095 dkcFreeStack(&(*p)->mStack);
01096 dkcFreePathString(&(*p)->mDir);
01097 return dkcFree(p);
01098 }
01099
01100 static BOOL isDot(DKC_FILE_FINDER *ptr){
01101
01102 #ifdef WIN32
01103 return (
01104 strcmp(ptr->mFindData.cFileName,"..") == 0 ||
01105 strcmp(ptr->mFindData.cFileName,".") == 0
01106 );
01107 #else
01108
01109
01110 #endif
01111
01112 }
01113 static BOOL isFolder(DKC_FILE_FINDER *ptr){
01114 #ifdef WIN32
01115 return (
01116 ptr->mFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
01117 && strcmp(ptr->mFindData.cFileName,"..")!=0
01118 && strcmp(ptr->mFindData.cFileName,".")!=0
01119 );
01120 #else
01121
01122
01123 #endif
01124 }
01125 static int FFPushStack(DKC_FILE_FINDER *p){
01126 DKC_PATHSTRING *tp;
01127 int r;
01128 char buff[dkcdMAXPATH_BUFFER];
01129
01130 #ifdef WIN32
01131
01132 dkcCurrentDirectoryConcatenate(buff,sizeof(buff),p->mFindData.cFileName);
01133 #else
01134 dkcCurrentDirectoryConcatenate(buff,sizeof(buff),ptr->mDirent.d_name);
01135 #endif
01136
01137 tp = dkcAllocPathString(buff);
01138 if(NULL==tp){
01139 return edk_FAILED;
01140 }
01141 r = dkcStackDynamicPush(p->mStack,tp);
01142 return r;
01143 }
01144
01145 static BOOL FFIsStackEmpty(DKC_FILE_FINDER *p){
01146 return dkcStackIsEmpty(p->mStack);
01147 }
01148
01149
01150 static int FFPopStack(DKC_FILE_FINDER *p){
01151 int r;
01152 DKC_PATHSTRING *tp;
01153 r = dkcStackTop(p->mStack,&tp);
01154 if(DKUTIL_FAILED(r)){return r;}
01155
01156 dkcFreePathString( &(p->mDir));
01157 dkcStackPop(p->mStack);
01158
01159 p->mDir = tp;
01160 return r;
01161 }
01162
01163 static void FFReSearch(DKC_FILE_FINDER *p){
01164 dkcFindClose(p);
01165 FFPopStack(p);
01166 p->mState = edkcFileFinderEmpty;
01167 DKUTIL_STRUCTURE_INIT(p->mFindData);
01168 }
01169
01170
01171
01172
01173
01174
01176 static int ReflexiveSearch(DKC_FILE_FINDER *p,DKC_PATHSTRING *path,BOOL *bCopySucceeded)
01177 {
01178
01179 }
01180
01181
01182 #define FFCHECKING(p,path,bCopySucceeded)\
01183 if(TRUE==isDot(p))\
01184 {\
01185 p->mState = edkcFileFinderSearching;\
01186 return WithFolderSearch(p,path,bCopySucceeded);\
01187 }\
01188 if(TRUE==isFolder(p))\
01189 {\
01190 dkcFileFinderReferenceFileName(p,path);\
01191 p->mState = edkcFileFinderSearching;\
01192 FFPushStack(p);\
01193 return WithFolderSearch(p,path,bCopySucceeded);\
01194 }
01195
01196
01197 static int WithFolderSearch(DKC_FILE_FINDER *p,DKC_PATHSTRING *path,BOOL *bCopySucceeded)
01198 {
01199 int r;
01200
01201
01202
01203 r = 0;
01204 *bCopySucceeded = FALSE;
01205
01206 if(edkcFileFinderEmpty == p->mState)
01207 {
01208 r = dkcFindFirstFile(p);
01209 if(DKUTIL_FAILED(r)) return edk_FAILED;
01210 # ifdef WIN32 //windowsの場合は内部に格納している・・・。
01211 if(TRUE==isDot(p))
01212 {
01213 p->mState = edkcFileFinderSearching;
01214 return WithFolderSearch(p,path,bCopySucceeded);
01215 }
01216 if(TRUE==isFolder(p))
01217 {
01218 dkcFileFinderReferenceFileName(p,path);
01219 p->mState = edkcFileFinderSearching;
01220 FFPushStack(p);
01221 return WithFolderSearch(p,path,bCopySucceeded);
01222 }
01223
01224
01225 r = dkcFileFinderReferenceFileName(p,path);
01226
01227 if(DKUTIL_SUCCEEDED(r)){
01228 *bCopySucceeded = TRUE;
01229 }
01230 # endif
01231 p->mState = edkcFileFinderSearching;
01232 return r;
01233 }else if(edkcFileFinderSearching == p->mState)
01234 {
01235 r = dkcFindNextFile(p);
01236 if(edk_SUCCEEDED == r)
01237 {
01238 if(TRUE==isDot(p))
01239 {
01240 p->mState = edkcFileFinderSearching;
01241 return WithFolderSearch(p,path,bCopySucceeded);
01242 }
01243 if(TRUE==isFolder(p))
01244 {
01245 dkcFileFinderReferenceFileName(p,path);
01246 p->mState = edkcFileFinderSearching;
01247 FFPushStack(p);
01248 return WithFolderSearch(p,path,bCopySucceeded);
01249 }
01250
01251 r = dkcFileFinderReferenceFileName(p,path);
01252
01253 if(DKUTIL_SUCCEEDED(r)){
01254 *bCopySucceeded = TRUE;
01255 }
01256
01257
01258 }else if(edk_EndProcess == r)
01259 {
01260 if(FALSE==FFIsStackEmpty(p))
01261 {
01262 FFReSearch(p);
01263 return WithFolderSearch(p,path,bCopySucceeded);
01264 }else{
01265 dkcFindClose(p);
01266 }
01267 }else{
01268 dkcmNOT_ASSERT("そんなばかな~");
01269 }
01270 return r;
01271 }else if(edkcFileFinderFinish == p->mState)
01272 {
01273 return edk_EndProcess;
01274 }
01275 dkcmNOT_ASSERT("dkcFileFinderNextのプログラムがおかしい。チートされているかも!?");
01276 return edk_FAILED;
01277
01278 }
01279
01280
01281 static int NormalSearch(DKC_FILE_FINDER *p,DKC_PATHSTRING *path,BOOL *bCopySucceeded)
01282 {
01283 int r;
01284 r = 0;
01285 *bCopySucceeded = FALSE;
01286
01287 if(edkcFileFinderEmpty == p->mState)
01288 {
01289 r = dkcFindFirstFile(p);
01290 if(DKUTIL_FAILED(r)) return edk_FAILED;
01291 # ifdef WIN32 //windowsの場合は内部に格納している・・・。
01292 if(TRUE==isDot(p) || TRUE==isFolder(p))
01293 {
01294 p->mState = edkcFileFinderSearching;
01295 return NormalSearch(p,path,bCopySucceeded);
01296 }
01297 r = dkcFileFinderReferenceFileName(p,path);
01298
01299 if(DKUTIL_SUCCEEDED(r)){
01300 *bCopySucceeded = TRUE;
01301 }
01302 # endif
01303 p->mState = edkcFileFinderSearching;
01304 return r;
01305 }else if(edkcFileFinderSearching == p->mState)
01306 {
01307 r = dkcFindNextFile(p);
01308 if(edk_SUCCEEDED == r)
01309 {
01310 if(TRUE==isDot(p) || TRUE==isFolder(p))
01311 {
01312
01313
01314
01315
01316 return NormalSearch(p,path,bCopySucceeded);
01317 }
01318 r = dkcFileFinderReferenceFileName(p,path);
01319
01320 if(DKUTIL_SUCCEEDED(r)){
01321 *bCopySucceeded = TRUE;
01322 }
01323
01324
01325 }else if(edk_EndProcess == r)
01326 {
01327 dkcFindClose(p);
01328 }else{
01329 dkcmNOT_ASSERT("そんなばかな~");
01330 }
01331 return r;
01332 }else if(edkcFileFinderFinish == p->mState)
01333 {
01334 return edk_EndProcess;
01335 }
01336
01337
01338 dkcmNOT_ASSERT("dkcFileFinderNextのプログラムがおかしい。チートされているかも!?");
01339 return edk_FAILED;
01340
01341 }
01342
01348 int WINAPI dkcFileFinderNext(DKC_FILE_FINDER *p,DKC_PATHSTRING *path,BOOL *bCopySucceeded)
01349 {
01350 if(FALSE==p->mbSubDir){
01351 return NormalSearch(p,path,bCopySucceeded);
01352 }else{
01353 return WithFolderSearch(p,path,bCopySucceeded);
01354 }
01355
01356 }
01357 #endif
01358
01359 DKC_FINDFILE *WINAPI dkcAllocFindFile()
01360 {
01361 DKC_FINDFILE *p;
01362 p = dkcAllocate(sizeof(DKC_FINDFILE));
01363 return p;
01364 }
01365 int WINAPI dkcFreeFindFile(DKC_FINDFILE **ptr){
01366 if(NULL==ptr ){
01367 return edk_FAILED;
01368 }
01369 return dkcFree(ptr);
01370 }
01371
01372
01373 int WINAPI dkcFindFirstFile(DKC_FINDFILE *ptr,const char *target){
01374 #ifdef WIN32
01375 ptr->mHandle =
01376 FindFirstFileA( target, &(ptr->mFindData) );
01377 if(ptr->mHandle == INVALID_HANDLE_VALUE){
01378 return edk_FAILED;
01379 }
01380 #else
01381 ptr->mHandle = opendir( target );
01382 if(NULL==ptr->mHandle){
01383 return edk_FAILED;
01384 }
01385
01386 #endif
01387 return edk_SUCCEEDED;
01388 }
01389
01390 int WINAPI dkcFindNextFile(DKC_FINDFILE *ptr){
01391 # ifdef WIN32
01392 if ( 0 == FindNextFileA( ptr->mHandle, &(ptr->mFindData) ))
01393 {
01394 if ( GetLastError() == ERROR_NO_MORE_FILES )
01395 {
01396 return edk_EndProcess;
01397 }
01398 else
01399 {
01400 return edk_FAILED;
01401 }
01402 }
01403 # else
01404 errno = 0;
01405 ptr->mDirent = readdir( ptr->mHandle );
01406 if ( ptr->mDirent == 0 )
01407 {
01408 if ( errno == 0 )
01409 {
01410 return edk_EndProcess;
01411 }
01412 else
01413 {
01414 return edk_FAILED;
01415 }
01416 }
01417 # endif
01418 return edk_SUCCEEDED;
01419 }
01420
01421 int WINAPI dkcFindClose(DKC_FINDFILE *ptr)
01422 {
01423 #ifdef WIN32
01424 if(INVALID_HANDLE_VALUE == ptr->mHandle){
01425 return edk_FAILED;
01426 }
01427 FindClose(ptr->mHandle);
01428 ptr->mHandle = INVALID_HANDLE_VALUE;
01429 #else
01430 if(0 == ptr->mHandle){
01431 return edk_FAILED;
01432 }
01433 closedir(ptr->mHandle);
01434 ptr->mHandle = 0;
01435 ptr->mDirent = NULL;
01436 #endif
01437
01438 return edk_SUCCEEDED;
01439
01440 }
01441
01442 int WINAPI dkcFindFileGetFileName(DKC_FINDFILE *ptr,char *buff,size_t buffsize)
01443 {
01444 int r;
01445 size_t len;
01446 #ifdef WIN32
01447 len = strlen(ptr->mFindData.cFileName);
01448 if(0 == len) return edk_FAILED;
01449 r = dkc_strcpy(buff,buffsize,ptr->mFindData.cFileName,len);
01450 #else
01451 if(NULL==ptr->mDirent)
01452 {
01453 return edk_LogicError;
01454 }
01455 len = strlen(ptr->mDirent.d_name);
01456 if(0 == len) return edk_FAILED;
01457 r = dkc_strcpy(buff,buffsize,ptr->mDirent.d_name,len);
01458 #endif
01459 return r;
01460 }
01461
01462 BOOL WINAPI dkcFindFileIsFolder(DKC_FINDFILE *ptr){
01463
01464
01465 #ifdef WIN32
01466 return (
01467 ptr->mFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
01468 && strcmp(ptr->mFindData.cFileName,"..")!=0
01469 && strcmp(ptr->mFindData.cFileName,".")!=0
01470 );
01471 #else
01472
01473
01474 #endif
01475 }
01476
01477
01478 BOOL WINAPI dkcFindFileIsDot(DKC_FINDFILE *ptr){
01479 #ifdef WIN32
01480 return (
01481 strcmp(ptr->mFindData.cFileName,"..") == 0 ||
01482 strcmp(ptr->mFindData.cFileName,".") == 0
01483 );
01484 #else
01485
01486
01487 #endif
01488 }
01489
01490 BOOL WINAPI dkcFindFileIsNormalFile(DKC_FINDFILE *ptr)
01491 {
01492 #ifdef WIN32
01493 return (ptr->mFindData.dwFileAttributes & FILE_ATTRIBUTE_NORMAL);
01494 #else
01495
01496 #endif
01497 }
01498
01499 BOOL WINAPI dkcFindFileIsReadOnly(DKC_FINDFILE *ptr){
01500 #ifdef WIN32
01501 return (ptr->mFindData.dwFileAttributes & FILE_ATTRIBUTE_READONLY);
01502 #else
01503
01504 #endif
01505 }
01506 void WINAPI dkcFindFileSize(DKC_FINDFILE *ptr,ULONG *High,ULONG *Low){
01507 #ifdef WIN32
01508 *High = ptr->mFindData.nFileSizeHigh;
01509 *Low = ptr->mFindData.nFileSizeLow;
01510 #else
01511
01512 #endif
01513
01514 }
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550
01551
01552
01553
01554
01555
01556
01557
01558
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609
01610