home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip52.zip / api.c next >
C/C++ Source or Header  |  1996-02-17  |  5KB  |  221 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   unzipapi.c
  4.  
  5.   This module supplies an UnZip engine for use directly from C/C++
  6.   programs.  The functions are:
  7.  
  8.     UzpVer *UzpVersion(void);
  9.     int UzpMain(int argc, char *argv[]);
  10.     int UzpAltMain(int argc, char *argv[], UzpInit *init);
  11.     int UzpUnzipToMemory(char *zip, char *file, UzpBuffer *retstr);
  12.  
  13.   OS/2 only (for now):
  14.  
  15.     int UzpFileTree(char *name, cbList(callBack), char *cpInclude[],
  16.           char *cpExclude[]);
  17.  
  18.   You must define `DLL' in order to include the API extensions.
  19.  
  20.   ---------------------------------------------------------------------------*/
  21.  
  22.  
  23. #ifdef OS2
  24. #  define  INCL_DOSMEMMGR
  25. #  include <os2.h>
  26. #endif
  27.  
  28. #define UNZIP_INTERNAL
  29. #include "unzip.h"
  30. #include "version.h"
  31. #ifdef USE_ZLIB
  32. #  include "zlib.h"
  33. #endif
  34.  
  35.  
  36.  
  37. /*---------------------------------------------------------------------------
  38.     Documented API entry points
  39.   ---------------------------------------------------------------------------*/
  40.  
  41.  
  42. UzpVer *UzpVersion()   /* should be pointer to const struct */
  43. {
  44.     static UzpVer version;   /* doesn't change between calls */
  45.  
  46.  
  47.     version.structlen = UZPVER_LEN;
  48.  
  49. #ifdef BETA
  50.     version.flag = 1;
  51. #else
  52.     version.flag = 0;
  53. #endif
  54.     version.betalevel = BETALEVEL;
  55.     version.date = VERSION_DATE;
  56.  
  57. #ifdef ZLIB_VERSION
  58.     version.zlib_version = ZLIB_VERSION;
  59.     version.flag |= 2;
  60. #else
  61.     version.zlib_version = NULL;
  62. #endif
  63.  
  64.     /* someday each of these may have a separate patchlevel: */
  65.     version.unzip.major = UZ_MAJORVER;
  66.     version.unzip.minor = UZ_MINORVER;
  67.     version.unzip.patchlevel = PATCHLEVEL;
  68.  
  69.     version.zipinfo.major = ZI_MAJORVER;
  70.     version.zipinfo.minor = ZI_MINORVER;
  71.     version.zipinfo.patchlevel = PATCHLEVEL;
  72.  
  73.     version.os2dll.major = D2_MAJORVER;
  74.     version.os2dll.minor = D2_MINORVER;
  75.     version.os2dll.patchlevel = PATCHLEVEL;
  76.  
  77.     version.windll.major = DW_MAJORVER;
  78.     version.windll.minor = DW_MINORVER;
  79.     version.windll.patchlevel = PATCHLEVEL;
  80.  
  81.     return &version;
  82. }
  83.  
  84.  
  85.  
  86. int UzpAltMain(int argc, char *argv[], UzpInit *init)
  87. {
  88.     int r, (*dummyfn)();
  89.  
  90.  
  91.     CONSTRUCTGLOBALS();
  92.  
  93.     if (init->structlen >= (sizeof(ulg) + sizeof(dummyfn)) && init->msgfn)
  94.         G.message = init->msgfn;
  95.  
  96.     if (init->structlen >= (sizeof(ulg) + 2*sizeof(dummyfn)) && init->inputfn)
  97.         G.input = init->inputfn;
  98.  
  99.     if (init->structlen >= (sizeof(ulg) + 3*sizeof(dummyfn)) && init->pausefn)
  100.         G.mpause = init->pausefn;
  101.  
  102.     if (init->structlen >= (sizeof(ulg) + 4*sizeof(dummyfn)) && init->userfn)
  103.         (*init->userfn)();    /* allow void* arg? */
  104.  
  105.     r = unzip(__G__ argc, argv);
  106.     DESTROYGLOBALS()
  107.     RETURN(r);
  108. }
  109.  
  110.  
  111.  
  112. int UzpUnzipToMemory(char *zip,char *file,UzpBuffer *retstr)
  113. {
  114.     int r;
  115.  
  116.     CONSTRUCTGLOBALS();
  117.     G.redirect_data = 1;
  118.     r = unzipToMemory(__G__ zip,file,retstr)==0;
  119.     DESTROYGLOBALS()
  120.     return r;
  121. }
  122.  
  123.  
  124.  
  125. #ifdef OS2API
  126.  
  127. int UzpFileTree(char *name, cbList(callBack), char *cpInclude[],
  128.                 char *cpExclude[])
  129. {
  130.     int r;
  131.  
  132.     CONSTRUCTGLOBALS();
  133.     G.qflag = 2;
  134.     G.vflag = 1;
  135.     G.C_flag = 1;
  136.     G.wildzipfn = name;
  137.     G.process_all_files = TRUE;
  138.     if (cpInclude)
  139.         G.pfnames = cpInclude, G.process_all_files = FALSE;
  140.     if (cpExclude)
  141.         G.pxnames = cpExclude, G.process_all_files = FALSE;
  142.   
  143.     G.processExternally = callBack;
  144.     r = process_zipfiles(__G)==0;
  145.     DESTROYGLOBALS()
  146.     return r;
  147. }
  148.  
  149. #endif /* OS2API */
  150.  
  151.  
  152.  
  153. /*---------------------------------------------------------------------------
  154.     Helper functions
  155.   ---------------------------------------------------------------------------*/
  156.  
  157.  
  158. void setFileNotFound(__G)
  159.     __GDEF
  160. {
  161.     G.filenotfound++;
  162. }
  163.  
  164.  
  165.  
  166. int unzipToMemory(__GPRO__ char *zip, char *file, UzpBuffer *retstr)
  167. {
  168.     int r;
  169.     char *incname[2];
  170.  
  171.     G.process_all_files = FALSE;
  172.     G.extract_flag = TRUE;
  173.     G.qflag = 2;
  174.     G.C_flag = 1;
  175.     G.wildzipfn = zip;
  176.  
  177.     G.pfnames = incname;
  178.     incname[0] = file;
  179.     incname[1] = NULL;
  180.     G.filespecs = 1;
  181.  
  182.     r = process_zipfiles(__G);
  183.     if (retstr) {
  184.         retstr->strptr = G.redirect_buffer;
  185.         retstr->strlength = G.redirect_size;
  186.     }
  187.     r |= G.filenotfound;
  188.     if (r)
  189.         return r;   /* GRR:  these two lines don't make much sense... */
  190.     return r;
  191. }
  192.  
  193.  
  194.  
  195. int redirect_outfile(__G)
  196.      __GDEF
  197. {
  198.     G.redirect_size = G.lrec.ucsize;
  199. #ifdef OS2
  200.     DosAllocMem((void **)&G.redirect_buffer, G.redirect_size+1,
  201.       PAG_READ|PAG_WRITE|PAG_COMMIT);
  202.     G.redirect_pointer = G.redirect_buffer;
  203. #else
  204.     G.redirect_pointer = G.redirect_buffer = malloc(G.redirect_size+1);
  205. #endif
  206.     if (!G.redirect_buffer)
  207.         return FALSE;
  208.     G.redirect_pointer[G.redirect_size] = 0;
  209.     return TRUE;
  210. }
  211.  
  212.  
  213.  
  214. int writeToMemory(__GPRO__ uch *rawbuf, ulg size)
  215. {
  216.     if (rawbuf != G.redirect_pointer)
  217.         memcpy(G.redirect_pointer,rawbuf,size);
  218.     G.redirect_pointer += size;
  219.     return 0;
  220. }
  221.