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