メインページ | アルファベット順一覧 | 構成 | ファイル一覧 | 構成メンバ | ファイルメンバ | 関連ページ

# dkcGenericFileSystem.c

説明を見る。

    00001
    00010 #include "dkcGenericFileSystem.h"
    00011
    00012 #ifdef WIN32
    00013
    00014 #ifdef _MSC_VER
    00015 //関数へのﾎﾟｲﾝﾀがﾃﾞｰﾀへのﾎﾟｲﾝﾀに変換されました。
    00016 #   pragma warning(disable:4055 4152)
    00017 #endif
    00018
    00019 enum{
    00020     edkcWin32SJIS = 0,
    00021     edkcWin32Unicode = 1,
    00022 };
    00023
    00024
    00025 typedef HANDLE (WINAPI *CreateFileA_FT)(
    00026     LPCSTR lpFileName,
    00027     DWORD dwDesiredAccess,
    00028     DWORD dwShareMode,
    00029     LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    00030     DWORD dwCreationDisposition,
    00031     DWORD dwFlagsAndAttributes,
    00032     HANDLE hTemplateFile
    00033     );
    00034
    00035
    00036 typedef HANDLE (WINAPI *CreateFileW_FT)(
    00037     LPCWSTR lpFileName,
    00038     DWORD dwDesiredAccess,
    00039     DWORD dwShareMode,
    00040     LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    00041     DWORD dwCreationDisposition,
    00042     DWORD dwFlagsAndAttributes,
    00043     HANDLE hTemplateFile
    00044     );
    00045
    00046
    00047 typedef BOOL (WINAPI *SetFileAttributesA_FT)(
    00048     LPCSTR lpFileName,
    00049     DWORD dwFileAttributes
    00050     );
    00051
    00052
    00053 typedef BOOL (WINAPI *SetFileAttributesW_FT)(
    00054     LPCWSTR lpFileName,
    00055     DWORD dwFileAttributes
    00056     );
    00057
    00058
    00059
    00060 typedef DWORD (WINAPI *GetFileAttributesA_FT)(
    00061     LPCSTR lpFileName
    00062     );
    00063
    00064 typedef DWORD (WINAPI *GetFileAttributesW_FT)(
    00065     LPCWSTR lpFileName
    00066     );
    00067
    00068
    00069 typedef BOOL (WINAPI *DeleteFileA_FT)(
    00070     LPCSTR lpFileName
    00071     );
    00072
    00073 typedef BOOL (WINAPI *DeleteFileW_FT)(
    00074     LPCWSTR lpFileName
    00075     );
    00076
    00077
    00078
    00079
    00080 DKC_INLINE void dkc_to_CreateFile_arg_data(DKC_OPEN_ARG_DATA *data,uint32 flag)
    00081 {
    00082     BOOL wm = (flag & edkcWriteMode) != 0;
    00083     BOOL rm = (flag & edkcReadMode) != 0;
    00084     BOOL pm = (flag & edkcPostScriptMode) != 0;
    00085     memset(data,0,sizeof(*data));
    00086
    00087     if(pm){ //追記モード
    00088         /*if(wm && rm){
    00089             data->CreationDisposition |= OPEN_EXISTING;
    00090         }else if(wm){
    00091             data->CreationDisposition |= OPEN_EXISTING;
    00092         }else if(rm){
    00093             data->CreationDisposition |= OPEN_EXISTING;
    00094         }*/
    00095         data->CreationDisposition |= OPEN_EXISTING;
    00096     }else{//追記じゃないモード
    00097         if(wm && rm){
    00098             //もし、存在したらオープンする指定
    00099             data->CreationDisposition |= OPEN_EXISTING;
    00100         }else if(wm){
    00101             //上書きし指定
    00102             data->CreationDisposition |= CREATE_ALWAYS;
    00103         }else if(rm){
    00104             //存在したらオープンする指定
    00105             data->CreationDisposition |= OPEN_EXISTING;
    00106         }
    00107
    00108     }
    00109
    00110
    00111     if(wm){
    00112         //書き込み中は読み込みだけ許可
    00113         data->ShareMode |= FILE_SHARE_READ;
    00114
    00115         data->DesiredAccess |= GENERIC_WRITE;
    00116     }
    00117     if(rm){
    00118         //同じく読み込み中も
    00119         data->ShareMode |= FILE_SHARE_READ;
    00120
    00121         data->DesiredAccess |= GENERIC_READ;
    00122     }
    00123
    00124
    00125
    00126     //普通ファイル指定
    00127     data->FlagsAndAttributes |= FILE_ATTRIBUTE_NORMAL;
    00128     //data->TemplateFile = NULL;
    00129
    00130 }
    00131
    00132
    00133 #define WIN32_FS_CALL(type,obj,function_address)\
    00134     (type)(function_address)
    00135
    00136
    00137 static void *win32_open(void *pobj,uint32 flags,const void *filename){
    00138     DKC_WIN32_FS *pfs = pobj;
    00139     HANDLE fp;
    00140     DKC_OPEN_ARG_DATA Arg;
    00141     CreateFileA_FT af = pfs->CreateFile;
    00142     CreateFileW_FT wf = pfs->CreateFile;
    00143
    00144
    00145     dkc_to_CreateFile_arg_data(&Arg,flags);
    00146
    00147
    00148     if(edkcWin32SJIS == pfs->mFlag){
    00149         fp = af(
    00150                 filename,
    00151                 Arg.DesiredAccess, // access (read-write) mode
    00152                 Arg.ShareMode, // share mode
    00153                 Arg.lpSecurityAttributes,// pointer to security attributes
    00154                 Arg.CreationDisposition, // how to create
    00155                 Arg.FlagsAndAttributes,// file attributes
    00156                 Arg.TemplateFile // handle to file with
    00157             //GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
    00158         );
    00159     }else{
    00160         fp = wf(
    00161                 filename,
    00162                 Arg.DesiredAccess, // access (read-write) mode
    00163                 Arg.ShareMode, // share mode
    00164                 Arg.lpSecurityAttributes,// pointer to security attributes
    00165                 Arg.CreationDisposition, // how to create
    00166                 Arg.FlagsAndAttributes,// file attributes
    00167                 Arg.TemplateFile // handle to file with
    00168             //GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
    00169         );
    00170
    00171     }
    00172
    00173     if(fp==INVALID_HANDLE_VALUE){
    00174         //visual_lasterror();
    00175         return NULL;
    00176     }
    00177     pfs->mHandle = fp;
    00178     memcpy(&(pfs->mArg),&Arg,sizeof(pfs->mArg));
    00179
    00180     return fp;
    00181 }
    00182
    00183
    00184 static int win32_read(void *pobj,void *pb,size_t size,size_t *readsize){
    00185     DKC_WIN32_FS *pfs = pobj;
    00186     HANDLE h = pfs->mHandle;
    00187     //thread lock
    00188     BOOL r = ReadFile(
    00189             h, // handle of file to read
    00190             pb, // pointer to buffer that receives data
    00191             size, // number of bytes to read
    00192             (LPDWORD)&readsize, // pointer to number of bytes read
    00193             NULL//&mOL // pointer to structure for data
    00194         );
    00195
    00196     if(0==r){
    00197         pfs->mLastError = GetLastError();
    00198
    00199         r = edk_FAILED;
    00200         goto End;
    00201     }
    00202     if (r && readsize== 0)
    00203     {// ファイルの終わりに達した。
    00204         //mIsEOF = true;
    00205     }
    00206     r = edk_SUCCEEDED;
    00207 End:
    00208     //thread unlock
    00209     return r;
    00210 }
    00211
    00212 static int win32_write(void *pobj,const void *pb,size_t size,size_t *writesize){
    00213     DKC_WIN32_FS *pfs = pobj;
    00214
    00215
    00216     //thread lock
    00217     BOOL r = WriteFile(pfs->mHandle, pb, size , (LPDWORD)&writesize, NULL);
    00218
    00219     if(r==FALSE){
    00220
    00221         pfs->mLastError = GetLastError();
    00222         r = edk_FAILED;
    00223         goto End;
    00224     }
    00225     r = edk_SUCCEEDED;
    00226 End:
    00227     //thread unlock
    00228     return r;
    00229
    00230 }
    00231 static int win32_close(void *pobj){
    00232     DKC_WIN32_FS *pfs = pobj;
    00233     int r;
    00234     if(pfs->mHandle != INVALID_HANDLE_VALUE)
    00235     {
    00236         if(FALSE==CloseHandle(pfs->mHandle)){
    00237             r = edk_FAILED;
    00238             goto End;
    00239         }
    00240     }
    00241     pfs->mHandle = INVALID_HANDLE_VALUE;
    00242     r = edk_SUCCEEDED;
    00243 End:
    00244     return r;
    00245 }
    00246
    00247 static int win32_seek(void *pobj,const DKC_GFS_SEEK_ARG *parg){
    00248     DKC_WIN32_FS *pfs = pobj;
    00249     DWORD origin = 0xFFFFFFFF;
    00250     DWORD dw;
    00251     DWORD LastError;
    00252     int r;
    00253
    00254     switch(parg->origin){
    00255     case edkcSeekSet:
    00256         origin = FILE_BEGIN;
    00257         break;
    00258     case edkcSeekCurrent:
    00259         origin = FILE_CURRENT;
    00260         break;
    00261     case edkcSeekEnd:
    00262         origin = FILE_END;
    00263         break;
    00264     }
    00265     dkcmNOT_ASSERT(0xFFFFFFFF==origin);
    00266
    00267     //hFile ハンドルが FILE_FLAG_NO_BUFFERING を持つ場合は、
    00268     //セクタサイズの整数倍の位置にしかファイルポインタを移動できません。
    00269     // 事例 2 : lpDistanceToMoveHigh != NULL の場合
    00270     dw = SetFilePointer(pfs->mHandle,
    00271                             parg->Point.u.LowPart,
    00272                             (PLONG)&(parg->Point.u.HighPart),
    00273                             origin//FILE_BEGIN
    00274         );
    00275
    00276     //multithread lock
    00277     //これではダメ
    00278     //if (dw == 0xFFFFFFFF && error())
    00279
    00280     LastError = GetLastError();
    00281     pfs->mLastError = LastError;
    00282     if(LastError != NOERROR)
    00283     {
    00284         //visual_lasterror();
    00285         //< エラー処理 >
    00286         //return false;
    00287
    00288         r = edk_FAILED;
    00289         goto End;
    00290     }
    00291     //シーク成功したらとりあえずEOF解除･･･　ヽ(`Д´)ﾉｳﾜｧﾝ
    00292     //mIsEOF = false;
    00293     r = edk_SUCCEEDED;
    00294 End:
    00295     //thread unlock
    00296     return r;
    00297 }
    00298
    00299
    00300 static int win32_tell(void *pobj,DKC_UINT64_STRUCT *dest){
    00301     DKC_WIN32_FS *pfs = pobj;
    00302     LONG high = 0, low = 0;
    00303     DKC_UINT64_STRUCT ull;
    00304     DWORD LastError;
    00305     int r;
    00306
    00307     low = SetFilePointer(pfs->mHandle,
    00308                         low,
    00309                         &high, FILE_CURRENT
    00310     );
    00311
    00312     //multithread lock
    00313
    00314     //lowと high を一緒にゲットしているので･･･　これは間違い
    00315     //if (low == 0xFFFFFFFF && error())
    00316
    00317     LastError = GetLastError();
    00318     pfs->mLastError = LastError;
    00319     if(LastError != NOERROR)
    00320     {
    00321         //visual_lasterror();
    00322         //< エラー処理 >
    00323         //return false;
    00324
    00325         r = edk_FAILED;
    00326         goto End;
    00327     }
    00328     //シーク成功したらとりあえずEOF解除･･･　ヽ(`Д´)ﾉｳﾜｧﾝ
    00329     //mIsEOF = false;
    00330     r = edk_SUCCEEDED;
    00331     ull.u.LowPart = low;
    00332     ull.u.HighPart = high;
    00333     memcpy(dest,&ull,sizeof(*dest));
    00334 End:
    00335     //thread unlock
    00336     return r;
    00337
    00338 }
    00339
    00340 static int win32_delete_file(void *pobj,const void *filename_){
    00341     DKC_WIN32_FS *pfs = pobj;
    00342     BOOL r;
    00343     DeleteFileA_FT af = pfs->DeleteFile;
    00344     DeleteFileW_FT wf = pfs->DeleteFile;
    00345     if(edkcWin32SJIS == pfs->mFlag){
    00346         r = af(filename_);
    00347     }else{
    00348         r = wf(filename_);
    00349     }
    00350     if(FALSE==r){
    00351         return edk_FAILED;
    00352     }
    00353     return edk_SUCCEEDED;
    00354 }
    00355
    00356
    00357
    00358
    00359 #define LoadFunctionGFS(funcname,dest) \
    00360     tp = dkcGetProcAddress(dllp,funcname);\
    00361     if(NULL==tp){\
    00362         return FALSE;\
    00363     }\
    00364     dest = tp
    00365
    00366 static BOOL load_function_win32_sjis(DKC_DLL *dllp,DKC_WIN32_FS *p){
    00367     void *tp;
    00368     LoadFunctionGFS("CreateFileA",p->CreateFile);
    00369     LoadFunctionGFS("SetFileAttributesA",p->SetFileAttributes);
    00370     LoadFunctionGFS("GetFileAttributesA",p->GetFileAttributes);
    00371     LoadFunctionGFS("DeleteFileA",p->DeleteFile);
    00372     p->mFlag = edkcWin32SJIS;
    00373     return TRUE;
    00374 }
    00375
    00376 static BOOL load_function_win32_unicode(DKC_DLL *dllp,DKC_WIN32_FS *p){
    00377     void *tp;
    00378     LoadFunctionGFS("CreateFileW",p->CreateFile);
    00379     LoadFunctionGFS("SetFileAttributesW",p->SetFileAttributes);
    00380     LoadFunctionGFS("GetFileAttributesW",p->GetFileAttributes);
    00381     LoadFunctionGFS("DeleteFileW",p->DeleteFile);
    00382     p->mFlag = edkcWin32Unicode;
    00383     return TRUE;
    00384 }
    00385 typedef BOOL (*load_fs_func_win32_t)(DKC_DLL *,DKC_WIN32_FS *);
    00386
    00387 static DKC_WIN32_FS *alloc_win32_fs(load_fs_func_win32_t ploadfunc){
    00388     //void *tp;
    00389     DKC_WIN32_FS *p;
    00390     DKC_DLL *dllp;
    00391
    00392     p = dkcAllocate(sizeof(DKC_WIN32_FS));
    00393     if(NULL==p){
    00394         return NULL;
    00395     }
    00396
    00397     dllp = dkcLoadLibrary("kernel32.dll");
    00398     if(NULL==dllp){
    00399         goto Error;
    00400     }
    00401
    00402     /*
    00403     tp = dkcGetProcAddress(dllp,"CreateFileA");
    00404     if(NULL==tp){
    00405         goto Error;
    00406     }
    00407     p->CreateFile = (CreateFileA_FT)tp;
    00408     */
    00409     if(FALSE==ploadfunc(dllp,p)){
    00410         goto Error;
    00411     }
    00412
    00413     p->mdllobj = dllp;
    00414     return p;
    00415
    00416 Error:
    00417     dkcFree(&p);
    00418     return NULL;
    00419 }
    00420
    00421
    00422
    00423 static int free_win32_fs(void *pa){
    00424     DKC_WIN32_FS *p = pa;
    00425
    00426     win32_close(p);
    00427
    00428     if(DKUTIL_FAILED(dkcUnloadLibrary(&(p->mdllobj))))
    00429     {
    00430         return edk_FAILED;
    00431     }
    00432     memset(p,0,sizeof(*p));
    00433     return dkcFree(&p);
    00434     //return edk_SUCCEEDED;
    00435 }
    00436
    00437 static DKC_GENERIC_FILESYSTEM *WINAPI dkcAlloc_Win32_FileSystem(BOOL isSJIS){
    00438     void *fp;
    00439
    00440     DKC_GENERIC_FILESYSTEM *pfs = dkcAllocate(sizeof(DKC_GENERIC_FILESYSTEM));
    00441     if(NULL==pfs){
    00442         return NULL;
    00443     }
    00444
    00445     if(isSJIS){
    00446         fp = load_function_win32_sjis;
    00447     }else{
    00448         fp = load_function_win32_unicode;
    00449     }
    00450
    00451     pfs->mfp = alloc_win32_fs(fp);
    00452
    00453     if(NULL==pfs->mfp){
    00454         goto Error;
    00455     }
    00456     pfs->mfClose = win32_close;
    00457     pfs->mfOpen = win32_open;
    00458     pfs->mfRead = win32_read;
    00459     pfs->mfSeek = win32_seek;
    00460     pfs->mfTell = win32_tell;
    00461     pfs->mfWrite = win32_write;
    00462     pfs->mfDeleteFile = win32_delete_file;
    00463
    00464     pfs->mfObjFree = free_win32_fs;
    00465     return pfs;
    00466
    00467 Error:
    00468     //free_sjis
    00469     dkcFree(&pfs);
    00470     return NULL;
    00471 }
    00472
    00473 /*
    00474 DKC_GENERIC_FILESYSTEM *WINAPI dkcAllocGenericFileSystem(){
    00475 #ifdef WIN32
    00476     if(dkcIsOSNT()){
    00477         return dkcAlloc_UNICODE_FileSystem();
    00478     }
    00479     return dkcAlloc_SJIS_FileSystem();
    00480 #else
    00481
    00482 #endif
    00483 }
    00484 */
    00485 DKC_GENERIC_FILESYSTEM *WINAPI dkcAlloc_SJIS_FileSystem()
    00486 {
    00487     return dkcAlloc_Win32_FileSystem(TRUE);
    00488 }
    00489
    00490
    00491
    00492 DKC_GENERIC_FILESYSTEM *WINAPI dkcAlloc_UNICODE_FileSystem()
    00493 {
    00494     return dkcAlloc_Win32_FileSystem(FALSE);
    00495 }
    00496
    00497
    00498 #endif
    00499
    00500 int WINAPI dkcFreeGenericFileSystem(DKC_GENERIC_FILESYSTEM **pp){
    00501     DKC_GENERIC_FILESYSTEM *p = *pp;
    00502     DKC_GFS_FREE_F_TYPE fff;
    00503     int r;
    00504
    00505     if(NULL==pp || NULL==p){
    00506         return edk_FAILED;
    00507     }
    00508
    00509
    00510     fff = p->mfObjFree;
    00511     r = fff(p->mfp);
    00512     if(DKUTIL_FAILED(r)){
    00513         return edk_FAILED;
    00514     }
    00515
    00516     return dkcFree(pp);
    00517 }
    00518
    00519 int WINAPI dkcGenericFileSystemOpen(DKC_GENERIC_FILESYSTEM *ptr,uint32 flags,const void *filename){
    00520     void *p = ptr->mfOpen(ptr->mfp,flags,filename);
    00521     if(NULL==p){
    00522         return edk_FAILED;
    00523     }
    00524     return edk_SUCCEEDED;
    00525 }
    00526
    00527 int WINAPI dkcGenericFileSystemClose(DKC_GENERIC_FILESYSTEM *ptr){
    00528     return ptr->mfClose(ptr->mfp);
    00529 }
    00530
    00531 int WINAPI dkcGenericFileSystemRead(DKC_GENERIC_FILESYSTEM *ptr,void *pbuff,size_t size,size_t *read_size)
    00532 {
    00533     return ptr->mfRead(ptr->mfp,pbuff,size,read_size);
    00534 }
    00535
    00536 int WINAPI dkcGenericFileSystemWrite(DKC_GENERIC_FILESYSTEM *ptr,const void *pbuff,size_t size,size_t *write_size)
    00537 {
    00538     return ptr->mfWrite(ptr->mfp,pbuff,size,write_size);
    00539 }
    00540
    00541 int WINAPI dkcGenericFileSystemSeek(DKC_GENERIC_FILESYSTEM *ptr,const DKC_GFS_SEEK_ARG *parg)
    00542 {
    00543     return ptr->mfSeek(ptr->mfp,parg);
    00544 }
    00545
    00546 int WINAPI dkcGenericFileSystemTell(DKC_GENERIC_FILESYSTEM *ptr,DKC_UINT64_STRUCT *offset)
    00547 {
    00548     return ptr->mfTell(ptr->mfp,offset);
    00549 }
    00550
    00551 #ifdef _MSC_VER
    00552 #   pragma warning(default:4055 4152)
    00553 #endif