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

# dkcOSIndependent.c

説明を見る。

    00001
    00007 #define DKUTIL_C_OS_INDEPENDENT_C
    00008
    00009 #include "dkcOSIndependent.h"
    00010 #include "dkcDefined.h"
    00011
    00012 #include "dkcQueue.h"
    00013
    00014
    00015 #include <assert.h>
    00016 #include <math.h>
    00017
    00018
    00019 #ifdef WIN32
    00020
    00021 #include <shellapi.h>
    00022
    00023 #pragma warning(disable:4127)
    00024
    00025 int dkcDynamicCheckEnvironment(BOOL isForceExit){
    00026     int r = edk_FAILED;
    00027     if((sizeof(int) != sizeof(long))) goto Error;
    00028     if(sizeof(int) != 4) goto Error;
    00029
    00030     if((sizeof(UINT) != sizeof(ULONG))) goto Error;
    00031     if(sizeof(UINT) != 4) goto Error;
    00032
    00033     if((sizeof(BYTE) != sizeof(char))) goto Error;
    00034     if((sizeof(BYTE) != 1)) goto Error;
    00035     if(sizeof(double) != 8) goto Error;
    00036     if(sizeof(void *) != sizeof(unsigned)) goto Error;
    00037     if(sizeof(void *) != sizeof(int)) goto Error;
    00038     if(sizeof(short) != 2) goto Error;
    00039     r = edk_SUCCEEDED;
    00040 Error:
    00041     if(DKUTIL_FAILED(r) && isForceExit){
    00042         exit(-1);
    00043     }
    00044     return r;
    00045 }
    00046
    00047 #pragma warning(default:4127)
    00048 #else
    00049 int dkcDynamicCheckEnvironment(BOOL isForceExit){
    00050
    00051 }
    00052
    00053 #endif
    00054
    00055
    00056 int WINAPI dkcIsNativePathString(const char *s,size_t size){
    00057
    00058     size_t i=0;
    00059     if(s==NULL) return edk_FAILED;
    00060     for(;i<size;i++){
    00061         if(dkcmIS_INVALID_PATH_CHAR(s[i]))
    00062         {
    00063             return FALSE;
    00064         }
    00065     }
    00066     return TRUE;
    00067 }
    00068
    00069 DKC_INLINE int WINAPI dkcOutputDebugString(const char *str,...){
    00070     char s[1024 * 2];
    00071
    00072
    00073     int va_list_result;
    00074     va_list Valist;
    00075
    00076
    00077
    00078     SET_VA_LIST_C(Valist,va_list_result,s,sizeof(s),str);
    00079     if(0 > va_list_result){
    00080         return edk_FAILED;
    00081     }
    00082     OutputDebugString(s);
    00083     return edk_SUCCEEDED;
    00084     //fprintf(stderr,s);
    00085 }
    00086
    00087 int WINAPI dkcErrorMessage(const char *expression,
    00088                                          const char *filename,size_t line,const char *message,...)
    00089 {
    00090     char s[1024];
    00091     char ss[1024 * 2];
    00092
    00093     int va_list_result;
    00094     va_list Valist;
    00095
    00096     NULL_CHAR_ARRAY(s);
    00097     NULL_CHAR_ARRAY(ss);
    00098
    00099     if(message){
    00100         SET_VA_LIST_C(Valist,va_list_result,s,sizeof(s),message);
    00101     }else{
    00102         //SET_VA_LIST(s,sizeof(s),"No message");
    00103         _snprintf(s,sizeof(s),"No message\n");
    00104     }
    00105
    00106     _snprintf(ss,sizeof(ss),
    00107         "DKUTIL_ASSERT(%s):\nfile:%s / \n line:%d / \n message: %s",
    00108         expression,filename,line,s);
    00109
    00110
    00111
    00112 #ifdef NDEBUG//release
    00113 #   ifdef WIN32
    00114         ODS(ss);
    00115     //::MessageBox(NULL,ss,"ERROR ASSERTION !!",MB_OK);
    00116     {
    00117         LPCTSTR name="ERROR_TEMP.txt";
    00118         {
    00119             //ファイルをオープン
    00120             FILE *fp;
    00121             if(NULL != (fp = fopen( name , "at" ))){
    00122
    00123                 // エラーログファイルに書き出す
    00124                 fputs( ss , fp ) ;
    00125                 fclose( fp ) ;
    00126                 Sleep(800);
    00127             }
    00128         }
    00129         ShellExecute(NULL, "open",name, NULL, NULL, SW_SHOWNORMAL);
    00130         //Sleep(1000);
    00131         //DeleteFile(name);
    00132     }
    00133     MB("このテキストをこのソフトの開発者に渡せば、バグ発見の近道なります。\n \
    00134         開発者に知らせるときはこのテキストを転記してあげましょう。");
    00135     if(IDYES==MessageBox(NULL,
    00136         "このままこのソフトを起動しつづけると、更なるエラーが発生する可能性があります。\n終了しますか？",
    00137         "あってはいけない エラー!!!",
    00138         MB_YESNO))
    00139     {
    00140         exit(edk_FAILED);
    00141         //terminate();
    00142     }else{
    00143     }
    00144 #   else    //else of win32
    00145
    00146     fprintf(stderr,ss);
    00147     exit(edk_FAILED);
    00148
    00149 #   endif //end of win32
    00150
    00151 #else //debug
    00152
    00153 #   ifdef WIN32
    00154         ODS(ss);
    00155     fprintf(stderr,ss);
    00156     //_CrtDebugBreak();
    00157     DebugBreak();
    00158 #   else //else of win32
    00159
    00160     fprintf(stderr,ss);
    00161     Debugger();
    00162
    00163 #   endif //end of win32
    00164 #endif//end of NDEBUG
    00165     //assert(ss);
    00166
    00167     return TRUE;
    00168 }
    00169
    00170 void *WINAPIV dkcMallocAdapter(size_t s){
    00171     return malloc(s);
    00172 }
    00173
    00174 void WINAPIV dkcFreeAdapter(void *p){
    00175     if(p){
    00176         free(p);
    00177     }
    00178 }
    00179
    00180 DKC_INLINE void *dkcAllocateFill(size_t size,BYTE fill){
    00181     void *p = dkcAllocateFast(size);
    00182     if(NULL==p) return NULL;
    00183     memset(p,fill,size);
    00184     return p;
    00185 }
    00186 DKC_INLINE void *dkcAllocateFast(size_t size){
    00187     return malloc(size);
    00188 }
    00189 DKC_INLINE void* dkcAllocate(size_t size){
    00190     return dkcAllocateFill(size,0);
    00191 /*void *ptr;
    00192     ptr = dkcAllocateFast(size);
    00193     if(NULL==ptr) return NULL;
    00194     memset(ptr,0,size);
    00195     return ptr;
    00196     */
    00197 }
    00198
    00199 DKC_INLINE int  dkcFree(void **p){
    00200     if(!*p) return edk_FAILED;
    00201     free(*p);
    00202     *p = NULL;
    00203     return edk_SUCCEEDED;
    00204 }
    00205 BOOL dkcIs_foepn_mode(const char *s){
    00206     /* wrtb+ */
    00207     const char *ms= "wrtb+";
    00208     size_t slen = strlen(s);
    00209     size_t mslen = strlen(ms);
    00210     size_t i,j;
    00211     int flag = FALSE;
    00212     for(i=0;i<slen;i++){
    00213         for(j=0;j<mslen;j++){
    00214             if(ms[j]==s[i]){
    00215                 flag = TRUE;
    00216                 break;
    00217             }
    00218         }
    00219         if(TRUE!=flag){
    00220             return FALSE;
    00221         }else{
    00222             flag = FALSE;
    00223         }
    00224     }
    00225     return TRUE;
    00226 }
    00227 /*
    00228 struct dkcDoublePoint{
    00229     double x,y;
    00230 }DKC_DOUBLE_POINT;
    00231
    00232 int calc(DKC_DOUBLE_POINT *pa,size_t nums){
    00233     pa->x
    00234
    00235 }*/
    00236 /*
    00237 double calc(double StartX,double StartY,double EndX,double EndY)
    00238 {
    00239     return  StartY - ((EndY - StartY) / (EndX - StartX)) * StartX;
    00240 }
    00241 */
    00242 size_t dkcReallocateSizeFunction(size_t OldSize,size_t ExpandSize){
    00243     //y=-25.43035 * pow(x,2) + 116.7214
    00244     //y = -0.00000018 * pow(x,2) + 0.00019
    00245     //y = -99.9999 * pow(OldSize,2) + 104857600;
    00246     //y = 0.105263 * x * x + 10.52631
    00247     //double x;
    00248 //  size_t size;
    00249     if(OldSize <= 1024 * 5){//5KB以内なら2倍
    00250         return OldSize * 2;
    00251     }else if(OldSize >= 1024 * 1024 * 10 || 0==OldSize)
    00252     {//10MB以上なら + ExpandSize
    00253         return OldSize + ExpandSize;
    00254     }
    00255     //それ以外ならExpandSize * 3
    00256     return OldSize + ExpandSize * 3;
    00257     //return OldSize * 2;//ひでぇ関数だ。
    00258 }
    00259
    00260 int WINAPI dkcReallocate(void **NewPtr,size_t NewSize,
    00261     void **OldPtr)
    00262 {
    00263     return dkcReallocateEx(realloc,NewPtr,NewSize,OldPtr);
    00264 }
    00265
    00266 int WINAPI dkcReallocateEx(
    00267     DKC_REALLOC_F_TYPE your_realloc,void **NewPtr,size_t NewSize,
    00268     void **OldPtr
    00269 )
    00270 {
    00271     void *ptr = NULL;
    00272
    00273     if(NULL== your_realloc  /*|| NULL == *OldPtr ||
    00274         0==OldSize*/
    00275     ){
    00276         return edk_ArgumentException;//アホ引数入れるな！！
    00277     }
    00278
    00279     ptr = your_realloc(*OldPtr,NewSize);
    00280     if(NULL==ptr) return edk_FAILED;
    00281
    00282     //更新
    00283     *OldPtr = NULL;
    00284     *NewPtr = ptr;
    00285
    00286     return edk_SUCCEEDED;
    00287 }
    00288
    00289
    00290
    00291 int WINAPI dkcReallocateAutoExpand(
    00292     DKC_REALLOC_F_TYPE your_realloc,void **NewPtr,
    00293     void **OldPtr,size_t OldSize,size_t *reallocated_size
    00294     )
    00295 {
    00296     void *ptr = NULL;
    00297     size_t want_size = OldSize / 8;
    00298     size_t ra_size = dkcReallocateSizeFunction(OldSize,
    00299          (want_size <= 20) ? 20 : want_size
    00300     );
    00301     size_t div = 8;
    00302     size_t tmp_ra = ra_size / div;
    00303     size_t i;
    00304     if(NULL== your_realloc /*|| NULL==*NewPtr*/ || NULL == *OldPtr ||
    00305         0==OldSize  || NULL==reallocated_size
    00306     ){
    00307         return edk_ArgumentException;//アホ引数入れるな！！
    00308     }
    00309
    00310     for(i=0;i<div;i++){
    00311         ptr = your_realloc(*OldPtr,ra_size);
    00312         if(NULL==ptr){
    00313             if(OldSize > (ra_size -= tmp_ra)){//とりあえずtmp_ra分を引く。
    00314                 break;//こら望みなしだわ
    00315             }
    00316             continue;
    00317         }else{
    00318             break;
    00319         }
    00320     }
    00321     if(NULL==ptr) return edk_FAILED;
    00322
    00323     //更新
    00324     *NewPtr = ptr;
    00325     *OldPtr = NULL;
    00326
    00327     *reallocated_size = ra_size;
    00328
    00329     return edk_SUCCEEDED;
    00330 }
    00331
    00332 static int WINAPI dkcLoadFile(void *data,size_t size,const char *fname,const char *mode,size_t *readsize){//="rb"
    00333     FILE *fp ;
    00334     if(NULL==readsize || NULL==data || NULL==fname || NULL==mode){
    00335         return edk_FAILED;
    00336     }
    00337     fp = fopen( fname , mode ) ;//rb
    00338     if(fp==NULL)return edk_FAILED;
    00339     *readsize = dkcFReadAll(data,size,fp);
    00340     //*readsize = fread( data , 1 , size , fp ) ;
    00341     //fread( data , sizeof(size)  , 1 , fp ) ;
    00342     fclose( fp ) ;
    00343
    00344     return edk_SUCCEEDED;
    00345
    00346 }
    00347 static int WINAPI dkcSaveFile(const void *data,size_t size,const char *fname,const char *mode)
    00348 {
    00349     FILE *fp;
    00350     if(NULL==fname || NULL==mode){
    00351         return edk_FAILED;
    00352     }
    00353     fp = fopen( fname , mode ) ;//wb
    00354     if(fp==NULL) return edk_FAILED;
    00355     dkcFWriteAll(data,size,fp);
    00356     //fwrite( data , size  , 1 , fp ) ;
    00357     //fwrite( data , sizeof(size)  , 1 , fp ) ;
    00358     fclose( fp ) ;
    00359     return edk_SUCCEEDED;
    00360 }
    00361
    00362
    00363 DKC_EXTERN FILE * WINAPI dkcFOpen(const char *filename,const char *mode){
    00364     return fopen(filename,mode);
    00365 }
    00366
    00367 DKC_EXTERN int WINAPI dkcFClose(FILE **ptr){
    00368     if(NULL==ptr || NULL==*ptr) return edk_ArgumentException;
    00369     fclose(*ptr);
    00370     *ptr = NULL;
    00371     return edk_SUCCEEDED;
    00372 }
    00373 DKC_EXTERN size_t WINAPI dkcFReadAll(void *buffer,size_t size,FILE *fp)
    00374 {
    00375     size_t count;
    00376     size_t tc,tsize;
    00377     BYTE *tbuffer;
    00378
    00379     //read size
    00380     tsize = size;
    00381     //temp variable
    00382     tc = 0;
    00383     //offset counter
    00384     count = 0;
    00385     //byte type pointer
    00386     tbuffer = (BYTE *)buffer;
    00387     for(;;)
    00388     {
    00389         //error check
    00390         if(ferror(fp) ){
    00391             break;
    00392         }
    00393         if(feof(fp)){
    00394             break;
    00395         }
    00396         //read
    00397         tc = fread(&tbuffer[count],1,tsize,fp);
    00398         //update
    00399         tsize -= tc;
    00400         count += tc;
    00401
    00402         if(count == size){
    00403             break;
    00404         }
    00405 #   ifdef DEBUG
    00406         //ありえないエラーチェック
    00407         dkcmNOT_ASSERT(count > size);
    00408 #   else
    00409         if(count > size){
    00410             break;
    00411         }
    00412 #   endif
    00413     }
    00414     return count;
    00415 }
    00416
    00417 DKC_EXTERN size_t WINAPI dkcFWriteAll(const void *buffer,size_t size,FILE *fp){
    00418     size_t count;
    00419     size_t tc,tsize;
    00420     BYTE *tbuffer;
    00421
    00422     //read size
    00423     tsize = size;
    00424     //temp variable
    00425     tc = 0;
    00426     //offset counter
    00427     count = 0;
    00428     //byte type pointer
    00429     tbuffer = (BYTE *)buffer;
    00430     for(;;)
    00431     {
    00432         //error check
    00433         if(ferror(fp) ){
    00434             break;
    00435         }
    00436         if(feof(fp)){
    00437             break;
    00438         }
    00439         //read
    00440         tc = fwrite(&tbuffer[count],1,tsize,fp);
    00441         //update
    00442         tsize -= tc;
    00443         count += tc;
    00444
    00445         if(count == size){
    00446             break;
    00447         }
    00448 #   ifdef DEBUG
    00449         //ありえないエラーチェック
    00450         dkcmNOT_ASSERT(count > size);
    00451 #   else
    00452         if(count > size){
    00453             break;
    00454         }
    00455 #   endif
    00456     }
    00457     return count;
    00458
    00459 }
    00460
    00461
    00462 DKC_INLINE size_t WINAPI dkcFSize(FILE *fp)
    00463 {
    00464     long t;
    00465     size_t len;
    00466     //temporary
    00467     t = ftell(fp);
    00468     fseek(fp,0,SEEK_END);
    00469     len = ftell(fp);
    00470     fseek(fp,t,SEEK_SET);
    00471     return len;
    00472 }
    00473
    00474 //fopenのファイル関数を使ってバイナリセーブを行う。
    00475 int WINAPI dkcSaveBinary(const void *data,size_t size,const char *fname){//="wb"
    00476     return dkcSaveFile(data,size,fname,"wb");
    00477 }
    00478
    00479 DKC_INLINE BOOL WINAPI dkcCreateEmptyFile(const char *filename){
    00480     FILE *fp;
    00481     fp = fopen( filename , "wb" ) ;//wb
    00482     if(fp==NULL) return FALSE;
    00483     fclose( fp ) ;
    00484     return TRUE;
    00485 }
    00486
    00487
    00488 DKC_INLINE int WINAPI dkcLoadBinary(void *data,size_t size,const char *fname,size_t *readsize){//="rb"
    00489     return dkcLoadFile(data,size,fname,"rb",readsize);
    00490 }
    00491
    00492 DKC_INLINE int WINAPI dkcSaveText(const char *text,size_t length,const char *fname){
    00493     return dkcSaveFile(text,length,fname,"wt");
    00494 }
    00495 DKC_INLINE int WINAPI dkcLoadText(char *text,size_t length,const char *fname,size_t *readsize){
    00496     return dkcLoadFile(text,length,fname,"rt",readsize);
    00497 }
    00498
    00499
    00500 DKC_INLINE int WINAPI dkcInitFileFast( const char *filename )
    00501 {
    00502     FILE *fp;
    00503     // エラーログファイルを開く(初期化)
    00504     if(NULL==(fp = fopen( filename , "wt" ))){
    00505         return edk_FAILED;
    00506     }
    00507     fclose( fp ) ;
    00508
    00509     return edk_SUCCEEDED;
    00510 }
    00511
    00512 DKC_INLINE  int WINAPI dkcAddLogFile( const char *filename,const char *str , ... ){
    00513     FILE *fp=NULL;
    00514     char s[2048]="";
    00515     va_list VaList;
    00516     int r;
    00517     //SET_VA_LIST_INSERT_YEN_N(s,2048,str);
    00518     SET_VA_LIST_C(VaList,r,s,sizeof(s),str);
    00519
    00520     //ファイルをオープン
    00521     if(NULL == (fp = fopen( filename , "at" )))
    00522         return edk_FAILED;
    00523     // エラーログファイルに書き出す
    00524     fputs( s , fp ) ;
    00525     fclose( fp ) ;
    00526     return edk_SUCCEEDED;
    00527 }
    00528
    00529
    00530 BOOL WINAPI dkcSwapFast(void *p1,void *p2,size_t size)
    00531 {
    00532     void *p;
    00533 #if 0//stack overflow 例外が発生するのも嫌だから挙動は共通で。
    00534     int state = 0;
    00535     p = _alloca( size );
    00536     if(NULL==p){
    00537         state = 1;
    00538         p = malloc(size);
    00539         if(NULL==p)
    00540             return FALSE;
    00541     }
    00542 #else
    00543     p = malloc(size);
    00544     if(NULL==p)
    00545         return FALSE;
    00546 #endif
    00547     memcpy(p,p1,size);
    00548     memcpy(p1,p2,size);
    00549     memcpy(p2,p,size);
    00550 #if 0
    00551     if(1==state){
    00552         free(p);
    00553     }
    00554
    00555 #else
    00556     free(p);
    00557 #endif
    00558     return TRUE;
    00559 }
    00560 /*
    00561 template<typename TYPE_T>
    00562 inline void dkcSwapT(TYPE_T *p1,TYPE_T *p2,size_t size){
    00563     size_t i;
    00564     char buf;
    00565     size_t mod_ = size % sizeof(TYPE_T);
    00566     size_t cnt = size / sizeof(TYPE_T);
    00567     for(i=0;i<cnt;i++){
    00568         SWAP_NUM(p1[i],p2[i]);
    00569     }
    00570     for(i=0;i<mod_;i++){
    00571         buf = (((BYTE *)p1)[size - mod_ + i]);
    00572         (((BYTE *)p1)[size - mod_ + i]) =
    00573             (((BYTE *)p2)[size - mod_ + i]) ;
    00574         (((BYTE *)p2)[size - mod_ + i]) = buf;
    00575     }
    00576
    00577 }
    00578
    00579
    00580 void WINAPI dkcSwapFast(int *p1,int *p2,size_t size)
    00581 {
    00582     size_t i;
    00583     char buf;
    00584     size_t mod_ = size % sizeof(int);
    00585     size_t cnt = size / sizeof(int);
    00586     for(i=0;i<cnt;i++){
    00587         SWAP_NUM(p1[i],p2[i]);
    00588     }
    00589     for(i=0;i<mod_;i++){
    00590         buf = (((BYTE *)p1)[size - mod_ + i]);
    00591         (((BYTE *)p1)[size - mod_ + i]) =
    00592             (((BYTE *)p2)[size - mod_ + i]) ;
    00593         (((BYTE *)p2)[size - mod_ + i]) = buf;
    00594     }
    00595 }*/
    00596
    00597 #ifdef _MSC_VER
    00598 #   pragma warning(disable:4244)
    00599 #endif
    00600
    00601 void WINAPI dkcSwap64(ULONGLONG *p1,ULONGLONG *p2,size_t size){
    00602     size_t i;
    00603     BYTE buf;
    00604     size_t mod_ = size % sizeof(ULONGLONG);
    00605     size_t cnt = size / sizeof(ULONGLONG);
    00606     //char *tp1 = (char *)p1,*tp2 = (char *)p2;
    00607     for(i=0;i<cnt;i++){
    00608         SWAP_NUM(p1[i],p2[i]);
    00609     }
    00610     for(i=0;i<mod_;i++){
    00611
    00612         //SWAP_NUM((char)tp1[size - mod_ + 1],(char)tp2[size - mod_ + i]);
    00613         /*a = b - a ;\
    00614     b -= a ;\
    00615     a += b
    00616         */
    00617         /*( ( ((char *) p2)[size - mod_ + i]))
    00618             = (char)( ( ((char *) p2)[size - mod_ + i])) -
    00619             (char)( ( ((char *) p1)[size - mod_ + i]));
    00620
    00621         ( ( ((char *) p2)[size - mod_ + i])) -= ( ( ((char *) p1)[size - mod_ + i]));
    00622
    00623         ( ( ((char *) p1)[size - mod_ + i])) += ( ( ((char *) p2)[size - mod_ + i]));
    00624             */
    00625
    00626
    00627         buf = (((BYTE *)p1)[size - mod_ + i]);
    00628         (((BYTE *)p1)[size - mod_ + i]) =
    00629             (((BYTE *)p2)[size - mod_ + i]) ;
    00630         (((BYTE *)p2)[size - mod_ + i]) = buf;
    00631
    00632     }
    00633
    00634 }
    00635
    00636 void WINAPI dkcSwap(void *p1,void *p2,size_t size)
    00637 {
    00638     dkcSwap64((ULONGLONG *)p1,(ULONGLONG *)p2,size);
    00639 }
    00640
    00641
    00642
    00643 DKC_INLINE void WINAPI dkcTwoDWORDToULONGLONG(ULONGLONG *dest,DWORD high,DWORD low){
    00644     //*dest = (ULONGLONG)((high << 32) + low);
    00645     *dest = (ULONGLONG)high << 32;
    00646     *dest += low;
    00647         //( high * MAXDWORD ) + low;
    00648 }
    00649
    00650 DKC_INLINE void WINAPI dkcULONGLONGToTwoDWORD(DWORD *dhigh,DWORD *dlow,ULONGLONG src){
    00651     DWORD *o = (DWORD *)&src;
    00652     dkcmNOT_ASSERT(sizeof(DWORD) * 2 != sizeof(ULONGLONG));
    00653     //エンディアン問題をどうにかしよう！
    00654     *dhigh = o[0];
    00655     *dlow = o[1];
    00656 }
    00657
    00658 DKC_INLINE void WINAPI dkcLONGLONGToTwoLONG(LONG *high,LONG *low,LONGLONG src){
    00659     LONG *o = (LONG *)&src;
    00660     dkcmNOT_ASSERT(sizeof(LONG) * 2 != sizeof(LONGLONG));
    00661     *high = o[0];
    00662     *low = o[1];
    00663 }
    00664
    00665 DKC_INLINE void WINAPI dkcTwoLONGToLONGLONG(LONGLONG *dest,LONG high,LONG low){
    00666     //*dest = (ULONGLONG)((high << 32) + low);
    00667     *dest = (LONGLONG)high << 32;
    00668     *dest += low;
    00669
    00670 }
    00671
    00672 /*
    00673 //まずい、これは難しい。
    00674 DKC_INLINE int WINAPI dkcDataReplace(BYTE *dest,size_t destsize,
    00675                                                                          const BYTE *src,size_t srcsize,
    00676                                                                          const BYTE *target_data,size_t target_data_size,
    00677                                                                          const BYTE *replace_data,size_t replace_data_size)
    00678 {
    00679
    00680     size_t i;
    00681
    00682     //とりあえず、20個　で　offsetはsize_t分
    00683     DKC_QUEUE *pq = dkcAllocQueue(20,sizeof(size_t));
    00684     if(NULL==pq){
    00685         return edk_OutOfMemory;
    00686     }
    00687
    00688     //まずは置換場所を探す
    00689     for(i=0;i<srcsize;i++){
    00690         memcmp(&src[i],
    00691
    00692
    00693
    00694
    00695
    00696 }*/
    00697
    00698
    00699
    00700 #ifdef _MSC_VER
    00701 #   pragma warning(default:4244)
    00702
    00703
    00704
    00705 void WINAPI dkcCheckMemoryLeak(BOOL flag){
    00706     if(flag){
    00707         _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    00708     }else{
    00709         _CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF);
    00710     }
    00711 }
    00712
    00713 #else
    00714
    00715 void WINAPI dkcCheckMemoryLeak(BOOL flag){
    00716
    00717 }
    00718 #endif
    00719
    00720 const char *WINAPI dkcGetPathSep(){
    00721 #ifdef WIN32
    00722     static char target[3]={dkcdPATH_SEP,'/','\0'};
    00723 #else
    00724     static char target[3]={dkcdPATH_SEP,'\0'};
    00725 #endif
    00726     return target;
    00727 }
    00728
    00729
    00730 #ifdef _MSC_VER
    00731 BOOL WINAPI dkcIsMMX(void)
    00732 {
    00733     BOOL flag = FALSE;
    00734
    00735     __asm{
    00736         push    edx
    00737         push    ecx
    00738         pushfd
    00739         pop     eax
    00740         xor     eax, 00200000h
    00741         push    eax
    00742         popfd
    00743         pushfd
    00744         pop     ebx
    00745         cmp     eax, ebx
    00746         jnz     non_mmx
    00747         mov     eax, 0
    00748         cpuid
    00749         cmp     eax, 0
    00750         jz      non_mmx
    00751         mov     eax, 1
    00752         cpuid
    00753         and     edx, 00800000h
    00754         jz      non_mmx
    00755         mov     flag, TRUE
    00756     non_mmx:
    00757         pop     ecx
    00758         pop     edx
    00759     }
    00760     return flag;
    00761 }
    00762 #else
    00763 BOOL WINAPI dkcIsMMX(void){
    00764   return FALSE;
    00765 }
    00766 #endif
    00767
    00768
    00769 DKC_INLINE USHORT dkcReverseEndian16(USHORT x){
    00770     return (USHORT)((x >> 8) | (x << 8));
    00771 }
    00772
    00773 DKC_INLINE DWORD dkcReverseEndian32(DWORD x){
    00774     return (x << 24) | ((x & 0x0000ff00) << 8) | ((x & 0x00ff0000) >> 8) | (x >> 24);
    00775 }
    00776
    00777 DKC_INLINE ULONGLONG dkcReverseEndian64(ULONGLONG x) {
    00778
    00779     return (
    00780         (ULONGLONG)dkcReverseEndian32((DWORD)(x & 0x00000000ffffffff)) << 32)
    00781         |
    00782         dkcReverseEndian32((DWORD)(x >> 32)
    00783     );
    00784
    00785 }
    00786 DKC_INLINE  BOOL dkcIsLittleEndian(){
    00787     int x=1;
    00788     return (BOOL)((*(char*)&x));
    00789 }
    00790
    00791
    00792 DKC_INLINE BOOL dkcCheckOverflowULONG(ULONG a1,ULONG a2){
    00793     ULONG maxv = ULONG_MAX;
    00794   ULONG halfv = maxv / 2 ;
    00795   ULONG x = a1,y = a2;
    00796   if(x < y){
    00797     SWAP_NUM(x,y);
    00798   }
    00799   if(x > halfv + 1){
    00800     if(y >= halfv){
    00801       return TRUE;
    00802     }
    00803   }
    00804   return FALSE;
    00805 }
    00806
    00807
    00808
    00809
    00810 static void *get_offset(void *p,size_t offset){
    00811     BYTE *oo = (BYTE *)p;
    00812
    00813     return (void *)&(oo[offset]);
    00814 }
    00815
    00821 int WINAPI dkcSelect(void *dest,const void *a_src,size_t n,int k,size_t width ,DKC_COMPARE_TYPE less)
    00822 {
    00823
    00824     int i,j,left, right;
    00825
    00826     void *x = malloc(width);
    00827     void *a = malloc(n * width);
    00828     if(NULL==x || NULL==a){
    00829         return edk_ArgumentException;
    00830     }
    00831     if(n > INT_MAX){
    00832         return edk_FAILED;
    00833     }
    00834
    00835
    00836     memcpy(a,a_src,width * n);
    00837
    00838
    00839     left = 0;  right = n - 1;
    00840
    00841     while (left < right)
    00842     {
    00843
    00844         memcpy(x,   (const void *)get_offset(a,k * width),width);
    00845         i = left;
    00846         j = right;
    00847
    00848         for ( ; ; ) {
    00849
    00850             while( less(get_offset(a,i * width),x) > 0){
    00851             //while( *(int *)get_offset(a,i * width) < *(int *)x){
    00852                 i++;
    00853             }
    00854
    00855             while( less(x,get_offset(a,j * width)) > 0){
    00856             //while( *(int *)x < *(int *)get_offset(a,j * width) ){
    00857                 j--;
    00858             }
    00859
    00860             if (i > j){
    00861                 break;
    00862             }
    00863
    00864
    00865             dkcSwapFast(
    00866                 get_offset(a,i * width),
    00867                 get_offset(a,j * width),
    00868                 width
    00869                 );
    00870
    00871             i++;
    00872             j--;
    00873         }
    00874         if (j < k){
    00875             left  = i;
    00876         }
    00877         if (k < i){
    00878             right = j;
    00879         }
    00880     }
    00881
    00882     //ゲットする
    00883     {
    00884         //printf("%d",*(int *)get_offset(a,k * width));
    00885
    00886         memcpy(dest,(const void *)get_offset(a,k * width),width);
    00887
    00888     }
    00889     free(x);
    00890     free(a);
    00891
    00892     return edk_SUCCEEDED;
    00893
    00894 }
    00895
    00896
    00897 DKC_INLINE int WINAPI dkcSelectMax(void *a,const void *a_src, size_t n,size_t width ,DKC_COMPARE_TYPE less){
    00898     if(n-1 > INT_MAX){
    00899         return edk_FAILED;
    00900     }
    00901     return dkcSelect(a,a_src,n,(int)n-1,width,less);
    00902 }
    00903 DKC_INLINE int WINAPI dkcSelectMin(void *a,const void *a_src, size_t n,size_t width ,DKC_COMPARE_TYPE less){
    00904     return dkcSelect(a,a_src,n,(int)0,width,less);
    00905 }
    00906 #if 0
    00907 int WINAPI dkcSelect(void *dest,const void *a_src,size_t n,size_t k,size_t width ,DKC_COMPARE_TYPE less)
    00908 {
    00909     //int i, j, left, right;
    00910     size_t i,j,left, right;
    00911     //keytype x, t;
    00912     //size_t x,t;
    00913     void *x = malloc(width);
    00914     void *a = malloc(n * width);
    00915     if(NULL==x || NULL==a){
    00916         return edk_ArgumentException;
    00917     }
    00918
    00919     //void *t = malloc(width);
    00920     memcpy(a,a_src,width * n);
    00921
    00922
    00923     left = 0;  right = n - 1;
    00924     /*
    00925     x = a[k];  i = left;  j = right;
    00926         for ( ; ; ) {
    00927             while (a[i] < x) i++;
    00928             while (x < a[j]) j--;
    00929             if (i > j) break;
    00930             t = a[i];  a[i] = a[j];  a[j] = t;
    00931             i++;  j--;
    00932         }
    00933         if (j < k) left  = i;
    00934         if (k < i) right = j;
    00935     */
    00936     while (left < right)
    00937     {
    00938         //x = a[k];  i = left;  j = right;
    00939         //offset_temp = (BYTE *)a;
    00940
    00941         //memcpy(x, (const void *)&offset_temp[k * width],width);
    00942         memcpy(x,   (const void *)get_offset(a,k * width),width);
    00943         i = left;
    00944         j = right;
    00945
    00946         for ( ; ; ) {
    00947             //while (a[i] < x) i++;
    00948             //while(less(a[i],x)) i++;
    00949 //#error less じゃなくて greater でも、qsortと同じ仕様にするため･･･。スマン
    00950             //while( less(get_offset(a,i * width),x) < 0){
    00951             while( less(get_offset(a,i * width),x) ){
    00952                 i++;
    00953             }
    00954
    00955             /*void *p = get_offset(a,i * width);
    00956             BYTE *ma = (BYTE *)get_offset(a,n * width);
    00957             dkcmNOT_ASSERT(p < a);
    00958             dkcmNOT_ASSERT(p > ((BYTE *)a) + n * width);
    00959             dkcmNOT_ASSERT((ma - (BYTE *)p) % width != 0);
    00960             while(less(p,x) > 0){
    00961                 i++;
    00962                 p = get_offset(a,i * width);
    00963                 ma = (BYTE *)get_offset(a,n * width);
    00964                 dkcmNOT_ASSERT(p < a);
    00965                 dkcmNOT_ASSERT(p > ma - width);
    00966                 dkcmNOT_ASSERT((ma - p) % width != 0);
    00967             }*/
    00968
    00969             //while (x < a[j]) j--;
    00970             //while(less(x,a[j]) j--;
    00971             //while( less(x,get_offset(a,j * width)) < 0){
    00972             while( less(x,get_offset(a,j * width)) ){
    00973                 j--;
    00974             }
    00975
    00976             if (i > j){
    00977                 break;
    00978             }
    00979
    00980             //t = a[i];  a[i] = a[j];  a[j] = t;
    00981             //dkcSwap(&a[i],&a[j],width);
    00982             dkcSwap(
    00983                 get_offset(a,i * width),
    00984                 get_offset(a,j * width),
    00985                 width
    00986                 );
    00987
    00988             i++;
    00989             j--;
    00990         }
    00991         if (j < k){
    00992             left  = i;
    00993         }
    00994         if (k < i){
    00995             right = j;
    00996         }
    00997     }
    00998
    00999     //ゲットする
    01000     {
    01001         printf("%d",*(int *)get_offset(a,k * width));
    01002         //memcpy(,  (const void *)get_offset(a,k * width),width);
    01003         //memcpy(dest,(const void *)get_offset(a,k * width),width);
    01004         memcpy(dest,x,width);
    01005     }
    01006     free(x);
    01007     free(a);
    01008     //free(t);
    01009     return edk_SUCCEEDED;
    01010     //return a[k];
    01011 }
    01012 #endif
    01013
    01014 DKC_INLINE int dkcRotateShiftRightMemoryLogic(void *d,size_t size,size_t n,void *workbuff,size_t worksize)
    01015 {
    01016     BYTE *dest = (BYTE *)d;
    01017     size_t temp;
    01018
    01019     if(n > worksize){
    01020         return edk_FAILED;
    01021     }
    01022     for(;;){
    01023         if(n < size){
    01024             break;
    01025         }
    01026         n -= size;
    01027     }
    01028     temp = size - n;
    01029     memcpy(workbuff,dest + temp,n);
    01030     memmove(dest + n,dest,temp);
    01031     memcpy(dest,workbuff,n);
    01032     return edk_SUCCEEDED;
    01033
    01034 }
    01035
    01036 int WINAPI dkcRotateShiftRightMemory(void *d,size_t size,size_t num){
    01037     //size_t i = 0;
    01038     void *pd = NULL;
    01039     int r = edk_FAILED;
    01040
    01041     pd = malloc(num);
    01042     if(NULL==pd){
    01043         return edk_OutOfMemory;
    01044     }
    01045     if(DKUTIL_SUCCEEDED(dkcRotateShiftRightMemoryLogic(d,size,num,pd,num))){
    01046         r = edk_SUCCEEDED;
    01047     }
    01048
    01049     free(pd);
    01050
    01051     return r;
    01052
    01053 }