home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / pslib / pslib.c < prev    next >
Text File  |  1998-06-08  |  14KB  |  443 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: f:/miner/source/pslib/rcs/pslib.c $
  15.  * $Revision: 1.18 $
  16.  * $Author: john $
  17.  * $Date: 1994/02/15 12:53:20 $
  18.  *
  19.  * Main program for pslib.exe
  20.  *
  21.  * $Log: pslib.c $
  22.  * Revision 1.18  1994/02/15  12:53:20  john
  23.  * Made numfiles stored in library be a short instead of char.
  24.  * 
  25.  * Revision 1.17  1994/02/14  20:11:27  john
  26.  * First version working with new cfile stuff.
  27.  * 
  28.  * Revision 1.16  1993/12/17  16:17:49  yuan
  29.  * Fixed file not found message if library file doesn't exist.
  30.  * 
  31.  * Revision 1.15  1993/12/08  16:08:17  yuan
  32.  * Fixed MAX_FILES bug.
  33.  * 
  34.  * Revision 1.14  1993/10/28  14:39:54  yuan
  35.  * old cfread changed to cfreadfile
  36.  * 
  37.  * 
  38.  * Revision 1.13  1993/10/27  18:30:27  yuan
  39.  * Added free for lib_name when error occurs.
  40.  * Added file sizes and overall compression ratios
  41.  * during listing.
  42.  * 
  43.  * Revision 1.12  1993/10/22  18:00:25  yuan
  44.  * fixed 0 file size problem
  45.  * 
  46.  * Revision 1.11  1993/10/22  17:51:29  yuan
  47.  * Fixed some network generated flaky problems
  48.  * 
  49.  * Revision 1.10  1993/10/19  14:10:53  yuan
  50.  * Fixed a minor free(lib_name) problem.
  51.  * 
  52.  * Revision 1.9  1993/10/18  17:59:36  yuan
  53.  * Fixed memory alloc errors
  54.  * 
  55.  * Revision 1.8  1993/09/29  17:50:06  yuan
  56.  * No major changes were made to pslib.c
  57.  * However, all printf error messages and exits
  58.  * were removed from the library.  Now error
  59.  * codes are returned back to the caller.  If
  60.  * a buffer has an error, NULL is returned.
  61.  * 
  62.  * revision 1.7  1993/09/27  17:13:58  yuan
  63.  * lib_read_test function added.  All test functions moved
  64.  * from library.c over to pslib.c
  65.  * 
  66.  * Revision 1.6  1993/09/21  17:31:51  yuan
  67.  * Minor formatting in listing function fixed.
  68.  * 
  69.  * Revision 1.5  1993/09/21  17:22:31  yuan
  70.  * *** empty log message ***
  71.  * 
  72.  * Revision 1.4  1993/09/21  17:16:04  yua
  73.  * clganing up
  74.  * 
  75.  * Revision 1.3  1993/09/14  13:13:30  yuan
  76.  * additional test functions -cfr & -cfw added to test cfread and cfwrite.
  77.  * 
  78.  * Revision 1.2  1993/09/09  17:46:08  yuan
  79.  * malloc problem with long paths removed.
  80.  * make sure that 'libname' is not a flag.
  81.  * 
  82.  * Revision 1.1  1993/09/08  16:15:42  yuan
  83.  * Initial revision
  84.  * 
  85.  *
  86.  */
  87.  
  88. #pragma off (unreferenced)
  89. static char rcsid[] = "$Id: pslib.c 1.18 1994/02/15 12:53:20 john Exp $";
  90. #pragma on (unreferenced)
  91.  
  92. #include <time.h>
  93. #include <stdio.h>
  94. #include <stdlib.h>
  95. #include <string.h>
  96. #include <ctype.h>
  97. #include <stdarg.h>
  98. #include <conio.h>
  99. #include <dos.h>
  100. #include <fcntl.h>
  101.  
  102. #include "library.h"
  103. #include "parsarg.h"
  104. #include "mem.h"
  105.  
  106. void print_usage ( void ) {
  107.  
  108.      printf("    Usage: PSLIB <libfile> [<options>] <files>\n");
  109.      printf("       options :\n");
  110.      printf("         -a : add <files> to <libfile>\n");
  111.      printf("         -c : turn compression flag on\n");
  112.      printf("         -l : list all files in <libfile>\n");
  113.      printf("         -x : test extraction function\n");
  114.      printf("        -lr : test library read function\n");
  115.      printf("       -cfr : test cfreadfile function\n");
  116.      printf("       -cfw : test cfwrite function\n");
  117.      printf("\n       (for -cfr & -cfw, no <libfile> should be specified)\n");
  118.  
  119.      exit(0);
  120. }
  121.  
  122.  
  123. void datetime( char *filename, ushort *date_ptr, ushort *time_ptr ) {
  124.     int handle;
  125.     ushort date, time;
  126.  
  127.     if( _dos_open( filename, O_RDONLY, &handle ) != 0 )
  128.         printf( "    Unable to`open '%s' for date & time check\n", filename );
  129.     else {
  130.         _dos_getftime( handle, &date, &time );
  131.         _dos_close( handle );
  132.         *date_ptr = date;
  133.         *time_ptr = time;
  134.     }
  135. }
  136.  
  137.  
  138. void list_files( void ) {
  139.  
  140.     char header_buf[5];
  141.     short numfiles;
  142.     static char *methods[] = { "Stored", " LZW  " };
  143.     int i;
  144.     int total_length, total_size, total_ratio;
  145.  
  146.     printf("    Listing files in %s.\n\n", lib_name);
  147.     InputLibFile = fopen( lib_name, "rb" );
  148.  
  149.      if (InputLibFile == NULL) 
  150.         {
  151.         fprintf( stderr, "    PSLIB : %s file not found\n", lib_name );
  152.         return;
  153.         }
  154.  
  155.     for (i=0;i<5;i++) header_buf[i]=0;
  156.     for (i=0;i<4;i++)
  157.          header_buf[i] = (char) getc( InputLibFile );
  158.  
  159.     if (strcmp(header_buf, "PSLB")) {
  160.         fprintf( stderr, "    PSLIB : %s is not a PSLB file.\n", lib_name );
  161.         fclose( InputLibFile );
  162.     }
  163.     else {
  164.         //numfiles = (short) getc( InputLibFile );
  165.           fread( &numfiles, sizeof(short),1, InputLibFile );
  166.         total_length = total_size = 0;
  167.         printf( "                     Original\n");
  168.         printf( "Filename       Length  Size  Ratio  Method         Date & Time\n");
  169.         printf( "---------------------------------------------------------------------------\n");
  170.         for (i=0;i<numfiles;i++) {
  171.            read_data( InputLibFile, &Header );
  172.            total_length += Header.length;
  173.            total_size += Header.original_size;
  174.            printf( "%-13s %6lu %6lu %4d%%   %-s      ",
  175.                     Header.name,
  176.                     Header.length,
  177.                     Header.original_size,
  178.                     Header.ratio,
  179.                     methods[ Header.compression ] );
  180.             printf( "%-d/%d/%d %.2d:%.2d:%.2d\n",
  181.                     MONTH(Header.date), DAY(Header.date), YEAR(Header.date),
  182.                     HOUR(Header.time), MINUTE(Header.time), SECOND(Header.time) );
  183.         }
  184.         fclose( InputLibFile );
  185.         printf( "---------------------------------------------------------------------------\n");
  186.         total_ratio = 100 - (int) ( total_length * 100L / total_size );
  187.         printf( "              %6lu %6lu %4d%%", total_length, total_size, total_ratio);
  188.         printf("               Number of Files: %d\n", numfiles );
  189.  
  190.         }
  191.     }
  192.  
  193.  
  194. void check_list( char *argv ) {
  195.     if (!strcmp( argv, "-l"))
  196.         l_flag = LISTING;
  197. }
  198.  
  199. void header_count( char *argv ) {
  200.     if (!(*argv == '-')) headers++;
  201. }
  202.  
  203. void cfr_test( char *input, char *output ) {
  204.     ubyte *outputbuf;
  205.     int size;
  206.  
  207.     //outputbuf = cfreadfile( input, &size );
  208.     //if (WriteFile ( output, outputbuf, size ))
  209.     //    printf("WriteFile Error\n");
  210.  
  211. }
  212.  
  213.  
  214. void cfw_test( char *input, char *output ) {
  215.     ubyte *inputbuf;
  216.     int length;
  217.     int success;
  218.  
  219.     //inputbuf = ( ubyte * ) ReadFileRaw( input, &length );
  220.     //if (success = cfwrite( output, inputbuf, length ) )
  221.     //    printf("    CFWRITE '%s' -> '%s' successful!\n", input, output);
  222.  
  223. }
  224.  
  225.  
  226. void extract_test( char *extractname, char *extractout ) {
  227.     ubyte *buffer;
  228.  
  229.     printf("Extracting %s from %s\n", extractname, lib_name);
  230.     buffer = extract ( lib_name, extractname );
  231.     if (buffer != NULL) {
  232.         if (WriteFile( extractout, buffer, Header.original_size ))
  233.             printf("WriteFile Error\n");
  234.         free ( buffer );
  235.     } else
  236.         printf("    %s not found in library or local directory\n", extractname );
  237.     return;
  238. }
  239.  
  240.  
  241. void lib_read_test( char *extractname, char *extractout ) {
  242.     ubyte *buffer;
  243.     int length;
  244.  
  245.     lib_init( lib_name);
  246.     buffer = ReadFile ( extractname, &length );
  247.     if (buffer == NULL)
  248.         printf("    %s not found in library or local directory\n", extractname );
  249.         else {
  250.             printf("   %s found! and extracted.\n", extractname);
  251.             if (WriteFile(extractout, buffer, length ))
  252.                 printf("WriteFile Error\n");
  253.             free ( buffer );
  254.         }
  255.     lib_close();
  256.     return;
  257. }
  258.  
  259.  
  260. void process_arg( char *argv ) {
  261.     char filename[13], ext[_MAX_EXT];
  262.     unsigned char *input;
  263.     unsigned char *output;
  264.     file_header Header;
  265.     int length, i;
  266.  
  267.     _splitpath ( argv, NULL, NULL, &filename, &ext );
  268.     strcat( filename, ext );
  269.  
  270.     if ( strcmp( argv, lib_name ) )    // Don't want to write library twice.
  271.   {
  272.     if (!strcmp( argv, "-c")) {
  273.         printf("*** Compression Flag enabled.\n");
  274.         c_flag = LF_LZW;
  275.         return;
  276.     }
  277.     if (!strcmp( argv, "-a")) {
  278.         printf("*** Building Library.\n");
  279.         init_library ( lib_name, headers );
  280.         b_flag = BUILDING;
  281.         return;
  282.     }
  283.     else if ( *argv == '-' ) {
  284.         fprintf(stderr, "    PSLIB : Invalid Flag %s\n", argv );
  285.         return;
  286.     }
  287.  
  288.     for (i=0;i<file_count;i++) {
  289.         if (!strcmp(FileList[i], filename)) {
  290.             printf("    ERROR processing %s : Duplicate file name.\n", filename );
  291.             return;
  292.         }
  293.     }
  294.     if ( b_flag == BUILDING ) {
  295.         input = ( ubyte * ) ReadFileRaw( argv, &length );
  296.         if (length == 0) {
  297.             free(input);
  298.             printf("     File: %s has no size.  Skipping...\n", argv);
  299.         }
  300.         if (length>0) {
  301.         strcpy ( Header.name, filename );
  302.         MALLOC(FileList[file_count], char, 13);
  303.         strcpy ( FileList[file_count++], filename );
  304.           if (file_count >= MAX_FILES-1) {
  305.                 printf("\n    ERROR: MAX_FILES exceeded by file_count\n", file_count);
  306.                 for (i=0;i<file_count;i++)
  307.                     free( FileList[i] );
  308.                 free( lib_name );
  309.             if ( input != NULL ) free( input );
  310.                 exit(1);
  311.                 } 
  312.         Header.compression = c_flag;
  313.         datetime( argv, &Header.date, &Header.time );
  314.         if ( c_flag == LF_LZW ) {
  315.             printf("*** LZW Compressing and Adding %s to Library %s : ", filename, lib_name );
  316.             Header.offset = file_size ( lib_name );
  317.             Header.original_size = length;
  318.             output = lzw_compress( input, NULL, length, &Header.length );
  319.             Header.ratio = 100 - (int) ( Header.length * 100L / Header.original_size );
  320.             if (Header.length >= 0) {
  321.                 printf( "%d%%\n", Header.ratio );
  322.                 if (AppendFile( lib_name, output, Header.length))
  323.                     printf("AppendFile Error\n");
  324.             }
  325.             else {
  326.                 Header.length = length;
  327.                 Header.ratio = 0;
  328.                 printf("\n     Compression ratio sucked!  Storing uncompressed...\n");
  329.                 Header.compression = 0;
  330.                 if (AppendFile( lib_name, input, length ))
  331.                     printf("AppendFile Error\n");
  332.             }
  333.             write_file_header( lib_name, Header );
  334.             if ( input != NULL ) free( input );
  335.             if ( output != NULL ) free( output );
  336.         }
  337.         else {
  338.             printf("*** Storing %s (uncompressed) in Library %s\n", filename, lib_name );
  339.             Header.offset = file_size ( lib_name );
  340.             Header.length = length;
  341.             Header.original_size = length;
  342.             Header.ratio = 0;
  343.             write_file_header( lib_name, Header );
  344.             if (AppendFile( lib_name, input, length ))
  345.                 printf("AppendFile Error\n");
  346.             if ( input != NULL ) free( input );
  347.             //if ( output != NULL ) free( output );
  348.         }
  349.       }
  350.     }
  351.   }
  352. }                                                               
  353.  
  354.  
  355.  
  356. void main( int argc, char *argv[] ) {
  357.     int i;
  358.  
  359.     printf( "\nPSLIB 1.0 - " );
  360.     printf( "Parallax Software Library Generation Tool.\n\n");
  361.  
  362.     if ( !strcmp( argv[1], "-cfr" ) ) {
  363.         if (argc < 4) {
  364.             printf("    Usage: PSLIB -cfr <cf-file> <outputfile>\n");
  365.             exit(1);
  366.         } else
  367.     cfr_test( argv[2], argv[3] );
  368.     exit(0);
  369.     }
  370.  
  371.     if ( !strcmp( argv[1], "-cfw" ) ) {
  372.         if (argc < 4) {
  373.             printf("    Usage: PSLIB -cfw <inputfile>`<cf/outputfile>\n");
  374.             exit(1);
  375.         } else
  376.     cfw_test( argv[2], argv[3] );
  377.     exit(0);
  378.     }
  379.  
  380.     if ( argc < 3 ) {
  381.         print_usage();
  382.     }
  383.  
  384.     file_count = 0;
  385.     i=1;
  386.  
  387.     MALLOC(lib_name, char, 128);
  388.     while (*argv[i] == '-')
  389.         i++;
  390.     strcpy( lib_name, argv[i] );
  391.     strupr ( lib_name );
  392.  
  393.     if ( !strcmp( argv[2], "-lr" ) ) {
  394.         if (argc < 5) {
  395.             printf("    Usage: PSLIB <libfile> -lr <extractname> <outputfile>\n");
  396.             free(lib_name);
  397.             exit(1);
  398.         } else
  399.     lib_read_test( argv[3], argv[4] );
  400.     } else
  401.     if ( !strcmp( argv[2], "-x" ) ) {
  402.         if (argc < 5) {
  403.             printf("    Usage: PSLIB <libfile> -x <extractname> <outputfile>\n");
  404.             free(lib_name);
  405.             exit(1);
  406.         } else
  407.     extract_test( argv[3], argv[4] );
  408.     }
  409.  
  410.     else {
  411.         headers = 0;
  412.         parse_args( argc-1, argv+1, check_list, 0 );
  413.         if (l_flag == LISTING)
  414.             list_files();
  415.         else {
  416.         parse_args( argc-1, argv+1, header_count, PA_EXPAND );
  417.         if (headers<2) {
  418.             free(lib_name);
  419.             print_usage();
  420.         }
  421.         parse_args( argc-1, argv+1, process_arg, PA_EXPAND );
  422.         }
  423.         if (b_flag == BUILDING) {
  424.             OutputLibFile = fopen( lib_name, "r+b" );
  425.             if (!fseek( OutputLibFile, 4L, SEEK_SET ) ) {
  426.                     short temp;
  427.                     temp = file_count;
  428.                 //fputc( file_count, OutputLibFile );
  429.                     fwrite( &temp, sizeof(short), 1, OutputLibFile );
  430.                 printf( "\nFile Count : %d\n", file_count);
  431.                 for (i=0;i<file_count;i++) free(FileList[i]);
  432.                 }
  433.             fclose( OutputLibFile );
  434.             }
  435.         else if (b_flag + l_flag == 0) {
  436.             free(lib_name);
  437.             print_usage();
  438.         }
  439.     free(lib_name);
  440.     }
  441. }
  442. 
  443.