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

# dkcStream.c

説明を見る。

    00001
    00007 #define DKUTIL_C_STREAM_C
    00008 #include "dkcOSIndependent.h"
    00009 #include "dkcStream.h"
    00010 #include "dkcStdio.h"
    00011 #include "dkcMemoryStream.h"
    00012 #include "dkcGenericFileSystem.h"
    00013 #include <limits.h>
    00014
    00015
    00016 static DKC_INLINE int SetStreamInfo(DKC_STREAM *p,UINT flag){
    00017     UINT stream_mode = 0;
    00018     BYTE endian_mode = 0;
    00019     UINT proc_mode = 0;
    00020
    00021     BOOL isLittle = dkcIsLittleEndian();
    00022
    00023
    00025     if(flag & edkcStreamInitMemory){
    00026         stream_mode = edkcStreamInitMemory;
    00027     }else if(flag & edkcStreamInitFile){
    00028         stream_mode = edkcStreamInitFile;
    00029     }else if(flag & edkcStreamInitMemoryAdapter){
    00030         stream_mode = edkcStreamInitMemoryAdapter;
    00031     }else if(flag & edkcStreamInitFile64){
    00032         stream_mode = edkcStreamInitFile64;
    00033     }else{
    00034         goto Error;
    00035     }
    00036
    00037
    00038     endian_mode = FALSE;
    00040     if(flag & edkcStreamBigEndian){
    00041         //endianmode = edkcStreamBigEndian;
    00042         if(isLittle){
    00043             endian_mode = TRUE;
    00044         }
    00045     }else if(flag & edkcStreamLittleEndian){
    00046         //endianmode = edkcStreamLittleEndian;
    00047         if(!isLittle){
    00048             endian_mode = TRUE;
    00049         }
    00050     }else /*if(flag & edkcStreamInitDefaultEndian)*/{
    00051         if(isLittle){
    00052             //endianmode = edkcStreamLittleEndian;
    00053         }else{
    00054             //endianmode = edkcStreamBigEndian;
    00055         }
    00056     }
    00057
    00058     //プロセスの詳細 設定
    00059     proc_mode |= (flag & edkcStreamProcessDefault);
    00060     if(0==proc_mode){
    00061         proc_mode |= (flag &    edkcStreamProcessAsOrdered);
    00062
    00063         if(0==proc_mode){
    00064             proc_mode |=    edkcStreamProcessAsOrdered;
    00065         }
    00066     }
    00067     proc_mode |= (flag &    edkcStreamWriteErrorWhenEndianChange );
    00068
    00069     //update
    00070     p->mMode = stream_mode;
    00071
    00072     dkcmNOT_ASSERT(proc_mode > UCHAR_MAX);
    00073     p->mProcessMode = (BYTE)proc_mode;
    00074
    00075     p->mChangeEndian = endian_mode;
    00076
    00077     return edk_SUCCEEDED;
    00078 Error:
    00079     return edk_FAILED;
    00080
    00081 }
    00082
    00083
    00084
    00085
    00086 DKC_STREAM *WINAPI dkcAllocStreamMemoryType(UINT flag,const void *default_data,size_t size){
    00087     DKUTIL_FLAG_UP(flag,edkcStreamInitMemory);
    00088     DKUTIL_FLAG_DOWN(flag,edkcStreamInitFile);
    00089     DKUTIL_FLAG_DOWN(flag,edkcStreamInitMemoryAdapter);
    00090     DKUTIL_FLAG_DOWN(flag,edkcStreamInitFile64);
    00091     return dkcAllocStream(flag,default_data,size,NULL,NULL);
    00092
    00093 }
    00094
    00095 DKC_STREAM *WINAPI dkcAllocStreamFileType(UINT flag,const char *filename,const char *mode){
    00096     DKUTIL_FLAG_DOWN(flag,edkcStreamInitMemory);
    00097     DKUTIL_FLAG_UP(flag,edkcStreamInitFile);
    00098     DKUTIL_FLAG_DOWN(flag,edkcStreamInitMemoryAdapter);
    00099     DKUTIL_FLAG_DOWN(flag,edkcStreamInitFile64);
    00100     return dkcAllocStream(flag,NULL,0,filename,mode);
    00101 }
    00102
    00103 DKC_STREAM *WINAPI dkcAllocStreamMemoryAdapterType(UINT flag,void *adapt_buffer,size_t size){
    00104     DKUTIL_FLAG_DOWN(flag,edkcStreamInitMemory);
    00105     DKUTIL_FLAG_DOWN(flag,edkcStreamInitFile);
    00106     DKUTIL_FLAG_UP(flag,edkcStreamInitMemoryAdapter);
    00107     DKUTIL_FLAG_DOWN(flag,edkcStreamInitFile64);
    00108     return dkcAllocStream(flag,adapt_buffer,size,NULL,NULL);
    00109 }
    00110
    00111 DKC_STREAM *WINAPI dkcAllocStreamFile64Type(UINT flag,const char *filename,uint32 mode){
    00112     DKUTIL_FLAG_DOWN(flag,edkcStreamInitMemory);
    00113     DKUTIL_FLAG_DOWN(flag,edkcStreamInitFile);
    00114     DKUTIL_FLAG_DOWN(flag,edkcStreamInitMemoryAdapter);
    00115     DKUTIL_FLAG_UP(flag,edkcStreamInitFile64);
    00116     return dkcAllocStream(flag,NULL,0,filename,(const char *)&mode);
    00117
    00118 }
    00119
    00120 DKC_STREAM *WINAPI dkcAllocStreamEncouragementFileType(const char *filename,const char *mode,BOOL OrderFlag)
    00121 {
    00122     UINT flag = 0;
    00123     DKUTIL_FLAG_UP(flag,edkcStreamInitFile);
    00124     DKUTIL_FLAG_UP(flag,edkcStreamLittleEndian);
    00125     DKUTIL_FLAG_DOWN(flag,edkcStreamBigEndian);
    00126     if(OrderFlag){
    00127         DKUTIL_FLAG_UP(flag,edkcStreamProcessAsOrdered );
    00128     }else{
    00129         DKUTIL_FLAG_UP(flag,edkcStreamProcessDefault);
    00130     }
    00131     return dkcAllocStream(flag,NULL,0,filename,mode);
    00132 }
    00133
    00134 /*
    00135 static DKC_INLINE int getFile64OpenMode(const char*mode,uint32 *get_mode){
    00136     size_t len = strlen(mode),i;
    00137     uint32 tm = 0;
    00138     for(i=0;i<len;i++){
    00139         switch(mode[i]){
    00140         case 'r':
    00141             tm |= edkcReadMode;
    00142             break;
    00143         case 'w':
    00144             tm |= edkcWriteMode:
    00145             break;
    00146         case 'b':
    00147             tm |= edkcBinaryMode;
    00148             break;
    00149         case 't':
    00150             tm |= edkcTextMode;
    00151             break;
    00152         case 'a':
    00153             tm |= edkcPostScriptMode;
    00154             break;
    00155         default:
    00156
    00157             break;
    00158         }
    00159     }
    00160
    00161 }*/
    00162
    00163 DKC_STREAM * WINAPI dkcAllocStream(UINT flag,
    00164                                                                      const void *default_data,size_t size,
    00165                                                                      const char *filename,const char *mode)
    00166 {
    00167
    00168     DKC_STREAM *p;
    00169     void *psig;
    00170     const uint32 *use_flag = (const uint32 *)mode;
    00171
    00172     p = (DKC_STREAM *)dkcAllocate(sizeof(DKC_STREAM));
    00173     if(NULL==p){
    00174         return NULL;
    00175     }
    00176
    00177     if(DKUTIL_FAILED(SetStreamInfo(p,flag))){
    00178         goto Error;
    00179     }
    00180
    00181     switch(p->mMode){
    00182     case edkcStreamInitMemory:
    00183         psig = (void *)dkcAllocMemoryStream(size);
    00184         if(NULL==psig){
    00185             goto Error;
    00186         }
    00187         //update
    00188         p->mSig = psig;
    00189         //write default data
    00190         dkcStreamWrite(p,default_data,size);
    00191         dkcStreamSeek(p,0,edkcStreamSeekSet);
    00192         break;
    00193     case edkcStreamInitFile:
    00194         psig = (void *)dkcFOpen(filename,mode);
    00195         if(NULL==psig){
    00196             goto Error;
    00197         }
    00198         //update
    00199         p->mSig = psig;
    00200
    00201         break;
    00202     case edkcStreamInitMemoryAdapter:
    00203         psig = dkcAllocMemoryStreamAdapter((void *)default_data,size);
    00204         if(NULL==psig){
    00205             goto Error;
    00206         }
    00207         p->mSig = psig;
    00208     case edkcStreamInitFile64:
    00209         psig = dkcAllocFile64(*use_flag,filename);
    00210         if(NULL==psig){
    00211             goto Error;
    00212         }
    00213         p->mSig = psig;
    00214         break;
    00215     default:
    00216         goto Error;
    00217     }
    00218
    00219
    00220     //p->mEndianMode = endianmode
    00221
    00222     return p;
    00223 Error:
    00224     dkcFree((void **)&p);
    00225     return NULL;
    00226 }
    00227
    00228
    00229 int WINAPI dkcFreeStream(DKC_STREAM **p){
    00230     DKC_STREAM *t;
    00231     if(NULL==p){
    00232         return edk_FAILED;
    00233     }
    00234     t = *p;
    00235     if(NULL==t){
    00236         return edk_FAILED;
    00237     }
    00238
    00239     switch(t->mMode){
    00240     case edkcStreamInitMemory:
    00241         dkcFreeMemoryStream((DKC_MEMORYSTREAM **)&(t->mSig));
    00242         break;
    00243     case edkcStreamInitFile:
    00244         dkcFClose((FILE **)&(t->mSig));
    00245         break;
    00246     case edkcStreamInitMemoryAdapter:
    00247         dkcFreeMemoryStreamAdapter((DKC_MEMORYSTREAM_ADAPTER **)&(t->mSig));
    00248         break;
    00249     case edkcStreamInitFile64:
    00250         dkcFreeFile64((DKC_FILE64 **)&(t->mSig));
    00251         break;
    00252 #ifdef DEBUG
    00253         default:
    00254             dkcmNOT_ASSERT("dkcFreeStream FAILED");
    00255 #endif
    00256     }
    00257     return dkcFree((void **)p);
    00258 }
    00259
    00260
    00261 //**********************************************************
    00262
    00263 int WINAPI dkcStreamSeek(DKC_STREAM *ptr,int offset,int origin){
    00264     int r = edk_FAILED;
    00265     switch(ptr->mMode){
    00266     case edkcStreamInitMemory:
    00267         r = dkcMemoryStreamSeek((DKC_MEMORYSTREAM *)ptr->mSig,offset,origin);
    00268         break;
    00269     case edkcStreamInitFile:
    00270         //成功したら０ == edk_SUCCEEDED;
    00271         r = fseek((FILE *)ptr->mSig,offset,origin);
    00272         break;
    00273     case edkcStreamInitMemoryAdapter:
    00274         r = dkcMemroyStreamAdapterSeek((DKC_MEMORYSTREAM_ADAPTER *)ptr->mSig,offset,origin);
    00275         break;
    00276     case edkcStreamInitFile64:
    00277         r = dkcFile64Seek((DKC_FILE64 *)ptr->mSig,offset,origin);
    00278         break;
    00279     }
    00280     return r;
    00281
    00282 }
    00283
    00284
    00285 int WINAPI dkcStreamTell64(DKC_STREAM *ptr,uint64 *pv){
    00286     int r = edk_FAILED;
    00287     switch(ptr->mMode){
    00288     case edkcStreamInitMemory:
    00289         r =  dkcMemoryStreamTell((DKC_MEMORYSTREAM *)ptr->mSig);
    00290         *pv = r;
    00291         break;
    00292     case edkcStreamInitFile:
    00293         r = ftell((FILE *)ptr->mSig);
    00294         if(r==-1){
    00295             return edk_FAILED;
    00296         }
    00297         *pv = r;
    00298         break;
    00299     case edkcStreamInitFile64:
    00300         r = dkcFile64Tell((DKC_FILE64 *)ptr->mSig,pv);
    00301         if(DKUTIL_FAILED(r)) return r;
    00302         break;
    00303     case edkcStreamInitMemoryAdapter:
    00304         r = dkcMemoryStreamAdapterTell((DKC_MEMORYSTREAM_ADAPTER *)ptr->mSig);
    00305         *pv = r;
    00306         break;
    00307     }
    00308
    00309     return edk_SUCCEEDED;
    00310 }
    00312 long WINAPI dkcStreamTell(DKC_STREAM *ptr){
    00313     int r = -1;
    00314     uint64 t;
    00315     switch(ptr->mMode){
    00316     case edkcStreamInitMemory:
    00317         r =  dkcMemoryStreamTell((DKC_MEMORYSTREAM *)ptr->mSig);
    00318         break;
    00319     case edkcStreamInitFile:
    00320         r = ftell((FILE *)ptr->mSig);
    00321         if(r==-1){
    00322             return edk_FAILED;
    00323         }
    00324         break;
    00325     case edkcStreamInitFile64:
    00326         dkcFile64Tell((DKC_FILE64 *)ptr->mSig,&t);
    00327         if(t > LONG_MAX){
    00328             return -1;
    00329         }
    00330         r = (long)t;
    00331         break;
    00332     case edkcStreamInitMemoryAdapter:
    00333         r = dkcMemoryStreamAdapterTell((DKC_MEMORYSTREAM_ADAPTER *)ptr->mSig);
    00334         break;
    00335     }
    00336     return r;
    00337     //dkcmNOT_ASSERT("dkcStreamTell failed");
    00338 }
    00339
    00340 int WINAPI dkcStreamFlush(DKC_STREAM *ptr){
    00341     int r = edk_SUCCEEDED;
    00342     //filter
    00343     if(edkcStreamInitFile64==ptr->mMode){
    00344         _flushall();
    00345         return edk_SUCCEEDED;
    00346     }
    00347     if(edkcStreamInitFile!=ptr->mMode){
    00348         return edk_FAILED;
    00349     }
    00350
    00351     r = fflush((FILE *)ptr->mSig);
    00352
    00353     if(r==EOF){
    00354         return edk_FAILED;
    00355     }
    00356     return edk_SUCCEEDED;
    00357 }
    00358
    00359 BOOL WINAPI dkcStreamEOF(DKC_STREAM *ptr){
    00360
    00361     //filter
    00362     if(edkcStreamInitFile64==ptr->mMode){
    00363         return dkcFile64EOF((DKC_FILE64 *)ptr->mSig);
    00364     }
    00365     if(edkcStreamInitFile != ptr->mMode){
    00366         goto Exit;
    00367     }
    00368     if(0 != feof((FILE *)ptr->mSig)){
    00369         return TRUE;
    00370     }
    00371 Exit:
    00372     return FALSE;
    00373 }
    00374
    00375 BOOL WINAPI dkcStreamError(DKC_STREAM *ptr){
    00376     //filter
    00377     if(edkcStreamInitFile != ptr->mMode){
    00378         goto Exit;
    00379     }
    00380     if(0 != ferror((FILE *)ptr->mSig)){
    00381         return TRUE;
    00382     }
    00383 Exit:
    00384     return FALSE;
    00385 }
    00386 //**********************************************************
    00387
    00388 static DKC_INLINE int dkcStreamReadObayAnOrder(
    00389     DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize)
    00390 {
    00391     int r = edk_FAILED;
    00392     size_t readsize_ = 0;
    00393     size_t count;
    00394     size_t tc,tsize;
    00395     BYTE *tbuffer;
    00396     FILE *fp = (FILE *)ptr->mSig;
    00397
    00398     if(NULL==readsize){
    00399         readsize = &readsize_;
    00400     }
    00401
    00402     //1,sizeの引数を入れ替えるとどうか？
    00403     //1 = fread(buffer,size,1,ptr->mfp);
    00404     //read size
    00405     tsize = size;
    00406     //temp variable
    00407     tc = 0;
    00408     //offset counter
    00409     count = 0;
    00410     //byte type pointer
    00411     tbuffer = (BYTE *)buffer;
    00412     for(;;)
    00413     {
    00414         //error check
    00415         if(ferror(fp) ){
    00416             //*readsize = count;
    00417             r = edk_FAILED;
    00418             break;
    00419         }
    00420         if(feof(fp)){
    00421             //*readsize = count;
    00422             r = edk_BufferOverFlow;
    00423             break;
    00424         }
    00425         //read
    00426         tc = fread(&tbuffer[count],1,tsize,fp);
    00427         //update
    00428         tsize -= tc;
    00429         count += tc;
    00430
    00431         if(count == size){
    00432             r = edk_SUCCEEDED;
    00433             break;
    00434         }
    00435 #   ifdef DEBUG
    00436         //ありえないエラーチェック
    00437         dkcmNOT_ASSERT(count > size);
    00438 #   else
    00439         if(count > size){
    00440             break;
    00441         }
    00442 #   endif
    00443     }
    00444     *readsize = count;
    00445     return r;
    00446
    00447 }
    00448
    00449 DKC_INLINE int WINAPI dkcStreamRead(DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize){
    00450     int r = edk_FAILED;
    00451     FILE *fp = (FILE *)ptr->mSig;
    00452     size_t redsize = 0;
    00453
    00454     //readsize filter
    00455     if(NULL==readsize){
    00456         readsize = &redsize;
    00457     }
    00458
    00459     //エンディアンチェンジ指定されているよ〜〜。
    00460     /*if(flag & edkcStreamReadErrorWhenEndianChange){
    00461         if(ptr->mChangeEndian){
    00462             return edk_Not_Satisfactory;
    00463         }
    00464     }*/
    00465
    00466     switch(ptr->mMode){
    00467     case edkcStreamInitMemory:
    00468         r = dkcMemoryStreamRead((DKC_MEMORYSTREAM *)ptr->mSig,
    00469             buffer,size,readsize);
    00470         break;
    00471     case edkcStreamInitFile:
    00472
    00473         if(ptr->mProcessMode & edkcStreamProcessDefault){
    00474
    00475             *readsize = fread(buffer,1,size,fp);
    00476
    00477             //set result
    00478             if(feof(fp)){
    00479                 r = edk_BufferOverFlow;
    00480             }else if(!ferror(fp)){
    00481                 r = edk_SUCCEEDED;
    00482             }
    00483
    00484         }else{
    00485             r = dkcStreamReadObayAnOrder(ptr,buffer,size,readsize);
    00486         }
    00487
    00488
    00489         break;
    00490     case edkcStreamInitMemoryAdapter:
    00491         r = dkcMemoryStreamAdapterRead((DKC_MEMORYSTREAM_ADAPTER *)
    00492             ptr->mSig,buffer,size,readsize);
    00493         break;
    00494     case edkcStreamInitFile64:
    00495         r = dkcFile64Read((DKC_FILE64*)ptr->mSig,buffer,size,readsize);
    00496
    00497         break;
    00498     }
    00499     return r;
    00500 }
    00501
    00502 static int WINAPI StreamRead(DKC_STREAM *ptr, void *buffer,size_t size,void *hoge){
    00503     return dkcStreamRead(ptr,buffer,size,(size_t *)hoge);
    00504 }
    00505
    00507 int WINAPI dkcStreamRead8(DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize){
    00508     return dkcStreamRead(ptr,buffer,size,readsize);
    00509 }
    00510
    00511 int WINAPI dkcStreamRead16(DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize)
    00512 {
    00513     return dkcStreamProcess16(ptr,buffer,size,StreamRead,(void *)readsize);
    00514     /*
    00515     size_t sentinel = size / 2;
    00516     size_t i = 0;
    00517     size_t t_rs = 0;
    00518
    00519     USHORT *pus;
    00520
    00521     size_t real_readsize = 0;
    00522     int result;
    00523
    00524     //filter
    00525     if(size_t % 2 != 0){
    00526         return edk_ArgumentException;
    00527     }
    00528
    00529     pus = (USHORT *)buffer;
    00530
    00531     for(;i<sentinel;i++){
    00532         //process
    00533         result = dkcStreamRead(ptr,&pus[i],2,&t_rs);
    00534
    00535
    00536         //update data
    00537         real_readsize += t_rs;
    00538
    00539
    00540         //process state update check
    00541         switch(result){
    00542         case edkSUCCEEDED:
    00543             continue;
    00544         case edk_FAILED:
    00545         case edk_BufferOverFlow:
    00546             goto End;
    00547         default:
    00548             dkcmNOT_ASSERT("dkcStreamSwapRead16 Error");
    00549             goto End;
    00550         }
    00551     }//end of for
    00552 End:
    00553     if(readsize){//set readsize
    00554         *readsize = real_readsize;
    00555     }
    00556     //process result
    00557     return result;
    00558     */
    00559 }
    00560
    00561 int WINAPI dkcStreamRead32(DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize){
    00562     return dkcStreamProcess32(ptr,buffer,size,StreamRead,(void *)readsize);
    00563 }
    00564
    00565 int WINAPI dkcStreamRead64(DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize){
    00566     return dkcStreamProcess64(ptr,buffer,size,StreamRead,(void *)readsize);
    00567 }
    00568
    00569
    00570 int WINAPI dkcStreamRef(DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize)
    00571 {
    00572     //位置を記憶
    00573     long t = dkcStreamTell(ptr);
    00574     //読みこむ（内部で位置がsize分（readsize分）ずらされる）
    00575     int r = dkcStreamRead(ptr,buffer,size,readsize);
    00576
    00577
    00578     /*
    00579     if(readsize > INT_MAX){
    00580         dkcmNOT_ASSEERT("INT_MAX byteも一気に読みこむな！ヽ(`Д´)ノムキィ");
    00581         return edk_FAILED;
    00582     }
    00583     t = (*readsize) - t;
    00584
    00585     //この条件はありえない。
    00586     dkcmNOT_ASSERT(t < 0);*/
    00587
    00588
    00590     dkcStreamSeek(ptr,t,edkcStreamSeekSet);
    00591     return r;
    00592 }
    00593
    00594 int WINAPI dkcStreamRef16(DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize){
    00595     long t = dkcStreamTell(ptr);
    00596     int r = dkcStreamRead16(ptr,buffer,size,readsize);
    00597     dkcStreamSeek(ptr,t,edkcStreamSeekSet);
    00598     return r;
    00599 }
    00605 int WINAPI dkcStreamRef32(DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize){
    00606     long t = dkcStreamTell(ptr);
    00607     int r = dkcStreamRead32(ptr,buffer,size,readsize);
    00608     dkcStreamSeek(ptr,t,edkcStreamSeekSet);
    00609     return r;
    00610 }
    00615 int WINAPI dkcStreamRef64(DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize){
    00616     long t = dkcStreamTell(ptr);
    00617     int r = dkcStreamRead64(ptr,buffer,size,readsize);
    00618     dkcStreamSeek(ptr,t,edkcStreamSeekSet);
    00619     return r;
    00620
    00621 }
    00622
    00623
    00624
    00625
    00626
    00627
    00628
    00629
    00630
    00631
    00632
    00633
    00634
    00635
    00636 static DKC_INLINE int dkcStreamWriteObayAnOrder(DKC_STREAM *ptr,const void *buffer,size_t size){
    00637     int r = edk_FAILED;
    00638     size_t tc,tsize;
    00639     size_t count;
    00640     const BYTE *tbuffer = (const BYTE *)buffer;
    00641     FILE *fp;
    00642
    00643     tc = 0;
    00644     count = 0;
    00645     tsize = size;
    00646     fp =  (FILE *)ptr->mSig;
    00647
    00648     for(;;){
    00649         //error check
    00650         if(ferror(fp)){
    00651             return edk_FAILED;
    00652         }
    00653         if(feof(fp)){
    00654             return edk_BufferOverFlow;
    00655         }
    00656         //write
    00657         tc = fwrite(&tbuffer[count],1,tsize,fp);
    00658         //update
    00659         tsize -= tc;
    00660         count += tc;
    00661
    00662         if(count == size){
    00663             r = edk_SUCCEEDED;
    00664             break;
    00665         }
    00666 #   ifdef DEBUG
    00667         //ありえないエラーチェック
    00668         dkcmNOT_ASSERT(count > size);
    00669 #   else
    00670         if(count > size){
    00671
    00672             break;
    00673         }
    00674 #   endif
    00675
    00676     }
    00677
    00678
    00679     return r;
    00680 }
    00681
    00682
    00683 int WINAPI dkcStreamWriteWithWriteSize(DKC_STREAM *ptr,const void *buffer,size_t size,size_t *ws)
    00684 {
    00685     int r = edk_FAILED;
    00686     UINT proc_flag = ptr->mProcessMode;
    00688     if(proc_flag & edkcStreamWriteErrorWhenEndianChange){
    00689         if(ptr->mChangeEndian){
    00690             return edk_Not_Satisfactory;
    00691         }
    00692     }
    00693
    00694     switch(ptr->mMode){
    00695     case edkcStreamInitMemory:
    00696         r =  dkcMemoryStreamWrite((DKC_MEMORYSTREAM *)ptr->mSig,buffer,size);
    00697         break;
    00698     case edkcStreamInitFile:
    00699         r = dkcStreamWriteObayAnOrder(ptr,buffer,size);
    00700         break;
    00701     case edkcStreamInitMemoryAdapter:
    00702         r = dkcMemoryStreamAdapterWrite((DKC_MEMORYSTREAM_ADAPTER *)ptr->mSig,buffer,size);
    00703         break;
    00704     case edkcStreamInitFile64:
    00705         r = dkcFile64Write((DKC_FILE64*)ptr->mSig,buffer,size,ws);
    00706
    00707         break;
    00708     }
    00709
    00710     return r;
    00711
    00712 }
    00713
    00714 static int WINAPI StreamWrite(DKC_STREAM *ptr, void *buffer,size_t size,void *hoge){
    00715     return dkcStreamWrite(ptr,buffer,size);
    00716 }
    00717 /*int WINAPI dkcStreamRef8(DKC_STREAM *ptr,void *buffer,size_t size,size_t *readsize){
    00718     //dkcStream
    00719
    00720 }*/
    00721
    00722 /*static DKC_INLINE dkcStreamWriteChangeEndian(DKC_STREAM *ptr,const void *buffer,size_t size){
    00723     USHORT *pb;
    00724     size_t elem_num;
    00725     size_t i;
    00726
    00727     pb = (USHORT)malloc(size);
    00728     if(NULL==pb){
    00729         return edk_FAILED;
    00730     }
    00731
    00732     memcpy(pb,buffer,size);
    00733     elem_num = size / 16;
    00734
    00735     //エンディアンをチェンジさせる。
    00736     for(i=0;i<elem_num;i++){
    00737         pb[i] = dkcReverseEndian16(pb[i]);
    00738     }
    00739     r = dkcStreamWrite(ptr,pb,size);
    00740
    00741     free(pb);
    00742 }*/
    00743
    00744
    00745 int WINAPI dkcStreamWrite8(DKC_STREAM *ptr,void *buffer,size_t size){
    00746     return dkcStreamWrite(ptr,buffer,size);
    00747 }
    00748 int WINAPI dkcStreamWrite16(DKC_STREAM *ptr, const void *buffer,size_t size){
    00749     return dkcStreamProcess16(ptr,(void *)buffer,size,StreamWrite,NULL);
    00750 }
    00751
    00752 int WINAPI dkcStreamWrite32(DKC_STREAM *ptr, const void *buffer,size_t size){
    00753     return dkcStreamProcess32(ptr,(void *)buffer,size,StreamWrite,NULL);
    00754
    00755 }
    00756
    00757
    00758
    00759 int WINAPI dkcStreamWrite64(DKC_STREAM *ptr, const void *buffer,size_t size){
    00760     return dkcStreamProcess64(ptr,(void *)buffer,size,StreamWrite,NULL);
    00761 }
    00762
    00763
    00764
    00766 int WINAPI dkcStreamProcess(DKC_STREAM *ptr,void *buffer,size_t size,
    00767                                                         DKC_STREAM_PROCESS_TYPE write_t,void *data)
    00768 {
    00769     return write_t(ptr,buffer,size,data);
    00770 }
    00771
    00772
    00773
    00774 int WINAPI dkcStreamProcess16(DKC_STREAM *ptr,void *buffer,size_t size,
    00775                                                                              DKC_STREAM_PROCESS_TYPE write_t,void *data){
    00776     int r;
    00777     USHORT *pb;
    00778     size_t elem_num,pause = 16;
    00779     size_t i;
    00780
    00781     if(size % pause != 0){
    00782         return edk_ArgumentException;
    00783     }
    00784     if(ptr->mChangeEndian)
    00785     {//エンディアンチェンジが必要
    00786         pb = (USHORT *)malloc(size);
    00787         if(NULL==pb){
    00788             //return edk_FAILED;
    00789             return edk_OutOfMemory;
    00790         }
    00791
    00792         //メモリコピー
    00793         memcpy(pb,buffer,size);
    00794         elem_num = size / pause;
    00795
    00796         //エンディアンをチェンジさせる。
    00797         for(i=0;i<elem_num;i++){
    00798             pb[i] = dkcReverseEndian16(pb[i]);
    00799         }
    00800         //書き込む
    00801         r = write_t(ptr,pb,size,data);
    00802
    00803         free(pb);
    00804     }else{
    00805         r = write_t(ptr,buffer,size,data);
    00806     }
    00807     return r;
    00808 }
    00809
    00810
    00811 int WINAPI dkcStreamProcess32(DKC_STREAM *ptr,void *buffer,size_t size,
    00812                                                                              DKC_STREAM_PROCESS_TYPE write_t,void *data){
    00813     int r;
    00814     ULONG *pb;
    00815     size_t elem_num,pause = 32;
    00816     size_t i;
    00817
    00818     if(size % pause != 0){
    00819         return edk_ArgumentException;
    00820     }
    00821     if(ptr->mChangeEndian)
    00822     {//エンディアンチェンジが必要
    00823         pb = (ULONG *)malloc(size);
    00824         if(NULL==pb){
    00825             return edk_OutOfMemory;
    00826         }
    00827
    00828         //メモリコピー
    00829         memcpy(pb,buffer,size);
    00830         elem_num = size / pause;
    00831
    00832         //エンディアンをチェンジさせる。
    00833         for(i=0;i<elem_num;i++){
    00834             pb[i] = dkcReverseEndian32(pb[i]);
    00835         }
    00836         //書き込む
    00837         r = write_t(ptr,pb,size,data);
    00838
    00839         free(pb);
    00840     }else{
    00841         r = write_t(ptr,buffer,size,data);
    00842     }
    00843     return r;
    00844
    00845 }
    00846
    00847
    00848
    00849
    00850
    00851
    00852
    00853
    00854
    00855
    00856 int WINAPI dkcStreamProcess64(DKC_STREAM *ptr,void *buffer,size_t size,
    00857                                                                              DKC_STREAM_PROCESS_TYPE write_t,void *data){
    00858     int r;
    00859     ULONGLONG *pb;
    00860     size_t elem_num,pause = 64;
    00861     size_t i;
    00862
    00863     if(size % pause != 0){
    00864         return edk_ArgumentException;
    00865     }
    00866     if(ptr->mChangeEndian)
    00867     {//エンディアンチェンジが必要
    00868         pb = (ULONGLONG *)malloc(size);
    00869         if(NULL==pb){
    00870             return edk_OutOfMemory;
    00871         }
    00872
    00873         //メモリコピー
    00874         memcpy(pb,buffer,size);
    00875         elem_num = size / pause;
    00876
    00877         //エンディアンをチェンジさせる。
    00878         for(i=0;i<elem_num;i++){
    00879             pb[i] = dkcReverseEndian64(pb[i]);
    00880         }
    00881         //書き込む
    00882         r = write_t(ptr,pb,size,data);
    00883
    00884         free(pb);
    00885     }else{
    00886         r = write_t(ptr,buffer,size,data);
    00887     }
    00888     return r;
    00889 }
    00890
    00891
    00892
    00893 //stream内をmemset(0)する。(seek位置も最初に戻される。
    00894 int WINAPI dkcStreamClear(DKC_STREAM *ptr){
    00895     //file size
    00896     size_t fsize;
    00897     //temp size
    00898     size_t tsize;
    00899     size_t write_size;
    00900     FILE *fp;
    00901     int r = edk_FAILED;
    00902     char null_array[1024];
    00903
    00904
    00905     switch(ptr->mMode){
    00906     case edkcStreamInitMemory:
    00907         r = dkcMemoryStreamClear( (DKC_MEMORYSTREAM *)ptr->mSig );
    00908         break;
    00909     case edkcStreamInitFile:
    00910
    00911         memset(null_array,0,sizeof(null_array));
    00912         fp =(FILE *) ptr->mSig;
    00913
    00914
    00915         fseek( fp, 0, SEEK_END ) ;
    00916         fsize = ftell( fp ) ;
    00917         fseek( fp, 0, SEEK_SET ) ;
    00918
    00919         if(fsize > sizeof(null_array))
    00920         {//ファイルのサイズがNULL配列より大きい場合
    00921
    00922             tsize = 0;
    00923             write_size = sizeof(null_array);
    00924             for(;;){
    00925
    00926                 r = dkcStreamWrite(ptr,null_array,write_size);
    00927
    00928                 if(DKUTIL_FAILED(r)){
    00929                     return r;
    00930                 }
    00931                 if(tsize >= fsize){
    00932                     r = edk_SUCCEEDED;
    00933                     break;
    00934                 }
    00935
    00936                 if(tsize + write_size > fsize){
    00937                     write_size = fsize - tsize;
    00938                 }else{
    00939                     tsize += write_size;
    00940
    00941                     //こいつはいらないけど一応･･･
    00942                     write_size = sizeof(null_array);
    00943                 }
    00944             }
    00945         }else{//ちっこいなら上書き上書き･･･
    00946             r = dkcStreamWrite(ptr,null_array,fsize);
    00947         }
    00948
    00949         break;
    00950     case edkcStreamInitMemoryAdapter:
    00951         r = dkcMemoryStreamAdapterClear((DKC_MEMORYSTREAM_ADAPTER *)ptr->mSig);
    00952         break;
    00953
    00954
    00955     }
    00956     return r;
    00957 }
    00958