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

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