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