dlltest.c
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:5k
源码类别:

生物技术

开发平台:

C/C++

  1. /*  * ===========================================================================  * PRODUCTION $Log: dlltest.c,v $  * PRODUCTION Revision 1000.0  2003/10/29 15:44:09  gouriano  * PRODUCTION PRODUCTION: IMPORTED [ORIGINAL] Dev-tree R1.1  * PRODUCTION  * ===========================================================================  */ /*
  2.    minibz2
  3.       libbz2.dll test program.
  4.       by Yoshioka Tsuneo(QWF00133@nifty.ne.jp/tsuneo-y@is.aist-nara.ac.jp)
  5.       This file is Public Domain.
  6.       welcome any email to me.
  7.    usage: minibz2 [-d] [-{1,2,..9}] [[srcfilename] destfilename]
  8. */
  9. #define BZ_IMPORT
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "bzlib.h"
  13. #ifdef _WIN32
  14. #include <io.h>
  15. #endif
  16. #ifdef _WIN32
  17. #define BZ2_LIBNAME "libbz2-1.0.2.DLL" 
  18. #include <windows.h>
  19. static int BZ2DLLLoaded = 0;
  20. static HINSTANCE BZ2DLLhLib;
  21. int BZ2DLLLoadLibrary(void)
  22. {
  23.    HINSTANCE hLib;
  24.    if(BZ2DLLLoaded==1){return 0;}
  25.    hLib=LoadLibrary(BZ2_LIBNAME);
  26.    if(hLib == NULL){
  27.       fprintf(stderr,"Can't load %sn",BZ2_LIBNAME);
  28.       return -1;
  29.    }
  30.    BZ2_bzlibVersion=GetProcAddress(hLib,"BZ2_bzlibVersion");
  31.    BZ2_bzopen=GetProcAddress(hLib,"BZ2_bzopen");
  32.    BZ2_bzdopen=GetProcAddress(hLib,"BZ2_bzdopen");
  33.    BZ2_bzread=GetProcAddress(hLib,"BZ2_bzread");
  34.    BZ2_bzwrite=GetProcAddress(hLib,"BZ2_bzwrite");
  35.    BZ2_bzflush=GetProcAddress(hLib,"BZ2_bzflush");
  36.    BZ2_bzclose=GetProcAddress(hLib,"BZ2_bzclose");
  37.    BZ2_bzerror=GetProcAddress(hLib,"BZ2_bzerror");
  38.    if (!BZ2_bzlibVersion || !BZ2_bzopen || !BZ2_bzdopen
  39.        || !BZ2_bzread || !BZ2_bzwrite || !BZ2_bzflush
  40.        || !BZ2_bzclose || !BZ2_bzerror) {
  41.       fprintf(stderr,"GetProcAddress failed.n");
  42.       return -1;
  43.    }
  44.    BZ2DLLLoaded=1;
  45.    BZ2DLLhLib=hLib;
  46.    return 0;
  47. }
  48. int BZ2DLLFreeLibrary(void)
  49. {
  50.    if(BZ2DLLLoaded==0){return 0;}
  51.    FreeLibrary(BZ2DLLhLib);
  52.    BZ2DLLLoaded=0;
  53. }
  54. #endif /* WIN32 */
  55. void usage(void)
  56. {
  57.    puts("usage: minibz2 [-d] [-{1,2,..9}] [[srcfilename] destfilename]");
  58. }
  59. int main(int argc,char *argv[])
  60. {
  61.    int decompress = 0;
  62.    int level = 9;
  63.    char *fn_r = NULL;
  64.    char *fn_w = NULL;
  65. #ifdef _WIN32
  66.    if(BZ2DLLLoadLibrary()<0){
  67.       fprintf(stderr,"Loading of %s failed.  Giving up.n", BZ2_LIBNAME);
  68.       exit(1);
  69.    }
  70.    printf("Loading of %s succeeded.  Library version is %s.n",
  71.           BZ2_LIBNAME, BZ2_bzlibVersion() );
  72. #endif
  73.    while(++argv,--argc){
  74.       if(**argv =='-' || **argv=='/'){
  75.          char *p;
  76.          for(p=*argv+1;*p;p++){
  77.             if(*p=='d'){
  78.                decompress = 1;
  79.             }else if('1'<=*p && *p<='9'){
  80.                level = *p - '0';
  81.             }else{
  82.                usage();
  83.                exit(1);
  84.             }
  85.          }
  86.       }else{
  87.          break;
  88.       }
  89.    }
  90.    if(argc>=1){
  91.       fn_r = *argv;
  92.       argc--;argv++;
  93.    }else{
  94.       fn_r = NULL;
  95.    }
  96.    if(argc>=1){
  97.       fn_w = *argv;
  98.       argc--;argv++;
  99.    }else{
  100.       fn_w = NULL;
  101.    }
  102.    {
  103.       int len;
  104.       char buff[0x1000];
  105.       char mode[10];
  106.       if(decompress){
  107.          BZFILE *BZ2fp_r = NULL;
  108.          FILE *fp_w = NULL;
  109.          if(fn_w){
  110.             if((fp_w = fopen(fn_w,"wb"))==NULL){
  111.                printf("can't open [%s]n",fn_w);
  112.                perror("reason:");
  113.                exit(1);
  114.             }
  115.          }else{
  116.             fp_w = stdout;
  117.          }
  118.          if((fn_r == NULL && (BZ2fp_r = BZ2_bzdopen(fileno(stdin),"rb"))==NULL)
  119.             || (fn_r != NULL && (BZ2fp_r = BZ2_bzopen(fn_r,"rb"))==NULL)){
  120.             printf("can't bz2openstreamn");
  121.             exit(1);
  122.          }
  123.          while((len=BZ2_bzread(BZ2fp_r,buff,0x1000))>0){
  124.             fwrite(buff,1,len,fp_w);
  125.          }
  126.          BZ2_bzclose(BZ2fp_r);
  127.          if(fp_w != stdout) fclose(fp_w);
  128.       }else{
  129.          BZFILE *BZ2fp_w = NULL;
  130.          FILE *fp_r = NULL;
  131.          if(fn_r){
  132.             if((fp_r = fopen(fn_r,"rb"))==NULL){
  133.                printf("can't open [%s]n",fn_r);
  134.                perror("reason:");
  135.                exit(1);
  136.             }
  137.          }else{
  138.             fp_r = stdin;
  139.          }
  140.          mode[0]='w';
  141.          mode[1] = '0' + level;
  142.          mode[2] = '';
  143.          if((fn_w == NULL && (BZ2fp_w = BZ2_bzdopen(fileno(stdout),mode))==NULL)
  144.             || (fn_w !=NULL && (BZ2fp_w = BZ2_bzopen(fn_w,mode))==NULL)){
  145.             printf("can't bz2openstreamn");
  146.             exit(1);
  147.          }
  148.          while((len=fread(buff,1,0x1000,fp_r))>0){
  149.             BZ2_bzwrite(BZ2fp_w,buff,len);
  150.          }
  151.          BZ2_bzclose(BZ2fp_w);
  152.          if(fp_r!=stdin)fclose(fp_r);
  153.       }
  154.    }
  155. #ifdef _WIN32
  156.    BZ2DLLFreeLibrary();
  157. #endif
  158.    return 0;
  159. }