PageRenderTime 19ms CodeModel.GetById 2ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/bzip2/dlltest.c

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