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

  1. /*---------------------------------------------------------------------------
  2.  
  3.   api.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 * UZ_EXP 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.     /* these are retained for backward compatibility only: */
  74.     version.os2dll.major = UZ_MAJORVER;
  75.     version.os2dll.minor = UZ_MINORVER;
  76.     version.os2dll.patchlevel = PATCHLEVEL;
  77.  
  78.     version.windll.major = UZ_MAJORVER;
  79.     version.windll.minor = UZ_MINORVER;
  80.     version.windll.patchlevel = PATCHLEVEL;
  81.  
  82.     return &version;
  83. }
  84.  
  85.  
  86.  
  87. #ifndef WINDLL
  88.  
  89. int UZ_EXP UzpAltMain(int argc, char *argv[], UzpInit *init)
  90. {
  91.     int r, (*dummyfn)();
  92.  
  93.  
  94.     CONSTRUCTGLOBALS();
  95.  
  96.     if (init->structlen >= (sizeof(ulg) + sizeof(dummyfn)) && init->msgfn)
  97.         G.message = init->msgfn;
  98.  
  99.     if (init->structlen >= (sizeof(ulg) + 2*sizeof(dummyfn)) && init->inputfn)
  100.         G.input = init->inputfn;
  101.  
  102.     if (init->structlen >= (sizeof(ulg) + 3*sizeof(dummyfn)) && init->pausefn)
  103.         G.mpause = init->pausefn;
  104.  
  105.     if (init->structlen >= (sizeof(ulg) + 4*sizeof(dummyfn)) && init->userfn)
  106.         (*init->userfn)();    /* allow void* arg? */
  107.  
  108.     r = unzip(__G__ argc, argv);
  109.     DESTROYGLOBALS()
  110.     RETURN(r);
  111. }
  112.  
  113. #endif
  114.  
  115.  
  116. int UZ_EXP UzpUnzipToMemory(char *zip,char *file,UzpBuffer *retstr)
  117. {
  118.     int r;
  119.  
  120.     CONSTRUCTGLOBALS();
  121.     G.redirect_data = 1;
  122.     r = unzipToMemory(__G__ zip,file,retstr)==0;
  123.     DESTROYGLOBALS()
  124.     return r;
  125. }
  126.  
  127.  
  128.  
  129. #ifdef OS2DLL
  130.  
  131. int UZ_EXP UzpFileTree(char *name, cbList(callBack), char *cpInclude[],
  132.                 char *cpExclude[])
  133. {
  134.     int r;
  135.  
  136.     CONSTRUCTGLOBALS();
  137.     G.qflag = 2;
  138.     G.vflag = 1;
  139.     G.C_flag = 1;
  140.     G.wildzipfn = name;
  141.     G.process_all_files = TRUE;
  142.     if (cpInclude)
  143.         G.pfnames = cpInclude, G.process_all_files = FALSE;
  144.     if (cpExclude)
  145.         G.pxnames = cpExclude, G.process_all_files = FALSE;
  146.   
  147.     G.processExternally = callBack;
  148.     r = process_zipfiles(__G)==0;
  149.     DESTROYGLOBALS()
  150.     return r;
  151. }
  152.  
  153. #endif /* OS2DLL */
  154.  
  155.  
  156.  
  157. /*---------------------------------------------------------------------------
  158.     Helper functions
  159.   ---------------------------------------------------------------------------*/
  160.  
  161.  
  162. void setFileNotFound(__G)
  163.     __GDEF
  164. {
  165.     G.filenotfound++;
  166. }
  167.  
  168.  
  169.  
  170. int unzipToMemory(__GPRO__ char *zip, char *file, UzpBuffer *retstr)
  171. {
  172.     int r;
  173.     char *incname[2];
  174.  
  175.     G.process_all_files = FALSE;
  176.     G.extract_flag = TRUE;
  177.     G.qflag = 2;
  178.     G.C_flag = 1;
  179.     G.wildzipfn = zip;
  180.  
  181.     G.pfnames = incname;
  182.     incname[0] = file;
  183.     incname[1] = NULL;
  184.     G.filespecs = 1;
  185.  
  186.     r = process_zipfiles(__G);
  187.     if (retstr) {
  188.         retstr->strptr = (char *)G.redirect_buffer;
  189.         retstr->strlength = G.redirect_size;
  190.     }
  191.     r |= G.filenotfound;
  192.     if (r)
  193.         return r;   /* GRR:  these two lines don't make much sense... */
  194.     return r;
  195. }
  196.  
  197.  
  198.  
  199. int redirect_outfile(__G)
  200.      __GDEF
  201. {
  202.     G.redirect_size = G.lrec.ucsize;
  203. #ifdef OS2
  204.     DosAllocMem((void **)&G.redirect_buffer, G.redirect_size+1,
  205.       PAG_READ|PAG_WRITE|PAG_COMMIT);
  206.     G.redirect_pointer = G.redirect_buffer;
  207. #else
  208.     G.redirect_pointer = G.redirect_buffer = malloc(G.redirect_size+1);
  209. #endif
  210.     if (!G.redirect_buffer)
  211.         return FALSE;
  212.     G.redirect_pointer[G.redirect_size] = 0;
  213.     return TRUE;
  214. }
  215.  
  216.  
  217.  
  218. int writeToMemory(__GPRO__ uch *rawbuf, ulg size)
  219. {
  220.     if (rawbuf != G.redirect_pointer)
  221.         memcpy(G.redirect_pointer,rawbuf,size);
  222.     G.redirect_pointer += size;
  223.     return 0;
  224. }
  225.