home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac / altd201a.zip / EXAMPLES.ARJ / EXAMPLES / EX25CON.CPP < prev    next >
C/C++ Source or Header  |  1996-04-19  |  4KB  |  133 lines

  1. //
  2. // EX25CON.CPP
  3. //
  4. //  C++/DOS Example program for ArchiveLib 2.0
  5. //
  6. //  Copyright (c) Greenleaf Software, Inc. 1996
  7. //  All Rights Reserved
  8. //
  9. // MEMBERS/FUNCTIONS DEMONSTRATED
  10. //
  11. //   ALCreate()
  12. //   ALReadDir()
  13. //   ALFreeDir()
  14. //   ALAppend()
  15. //
  16. // DESCRIPTION
  17. //
  18. //  This example program demonstrates several functions in the
  19. //  ArchiveLib simplified interface.  It first adds a set of files
  20. //  to an archive using ALCreate().  It then reads the directory
  21. //  for the archive and prints its contents.  It then appends
  22. //  additional files to the archive.
  23. //
  24. //  This program also demonstrates the use of the callback function
  25. //  in the simplified interface.
  26. //
  27. // REVISION HISTORY
  28. //
  29. //  February 1, 1996  2.0A  : Second release
  30. //
  31. //  April 5, 1996     2.01A : Added a little bit of info to the UI.  Note
  32. //                            that I have to flush the output stream before
  33. //                            doing getch(), since IBM Visual Age doesn't
  34. //                            do it for me at an end of line.
  35. //
  36.  
  37. #include <conio.h>
  38. #include <iostream.h>
  39. #include <iomanip.h>
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include "alsimple.h"
  44.  
  45. //
  46. // The callback function for the simplified interface gets
  47. // called at at two different times.  First, when an object
  48. // is being added to the archive, the callback function
  49. // is called with a valid filename in parameter name,
  50. // and -1 in the two numeric paramters.  Second, at various
  51. // times during the compression cycle, it is called with
  52. // a 0 (null pointer) for the parameter name, and valid ratios
  53. // in the two numeric parameters.
  54. //
  55. // This simple callback function prints file names, and prints
  56. // both ratios during progress.
  57. //
  58.  
  59. void my_callback( const char AL_DLL_FAR *name,
  60.                   int object_ratio,
  61.                   int job_ratio )
  62. {
  63.     if ( name )
  64.         cout << "\n" << name << "  ";
  65.     if ( object_ratio >= 0 ) {
  66.         char buf[ 24 ];
  67.         sprintf( buf, "%d%%  %d%%", object_ratio, job_ratio );
  68.         cout << "          \b\b\b\b\b\b\b\b\b\b" << buf;
  69.         for ( int i = 0 ; i < (int) strlen( buf ) ; i++ )
  70.             cout << '\b';
  71.     }
  72.     cout.flush();
  73. }
  74.  
  75. //
  76. // A simple function that is used when I want to display a ratio
  77. //
  78. int ratio( struct ALZipDir *z )
  79. {
  80.     return int( 100L - ( ( 100L * z->compressed_size ) / (long) z->size ) );
  81. }
  82.  
  83. main()
  84. {
  85.     cout << "Archive Library 2.0\nEX25CON.CPP\n\n";
  86.     cout << "This example program creates CON00.ZIP, initializing it\n";
  87.     cout << "with compressed copies of BUILD*.* and *.BAK.  After doing\n";
  88.     cout << "this it reads the directory and prints the results.  Finally\n";
  89.     cout << "it performs an append, adding C:\\COMMAND.* to the archive.\n";
  90.     cout << "\nHit ESCAPE to abort, any other key to proceed..." << flush;
  91.     int c = getch();
  92.     cout << endl;
  93.     if ( c == 0 || c == 0x1b || c == 3 )
  94.         exit( 1 );
  95.     int i;
  96.     int count;
  97.     int error;
  98.     struct ALZipDir *z;
  99.     char *levels[] = { "Stored ",
  100.                        "DeflatN",
  101.                        "DeflatX",
  102.                        "DeflatF",
  103.                        "DeflatS" };
  104.     printf( "\nCreating CON00.ZIP with *.BAK and BUILD*.*\n\n" );
  105.     i = ALCreate( "con00.zip", "*.bak, build*.*", 0, my_callback );
  106.     printf( "\nALCreate() returned %d\n\n", i );
  107.     z = ALReadDir( "con00.zip", &count, &error );
  108.     if ( z != 0 && error == AL_SUCCESS )
  109.         printf( "Contents of CON00.ZIP:\n\n" );
  110.         for ( i = 0 ; i < count ; i++ )
  111.             printf( "%7ld %s %7ld %3d%% %02d-%02d-%04d %02d:%02d %s %s\n",
  112.                     z[ i ].size,
  113.                     levels[ z[ i ].level ],
  114.                     z[ i ].compressed_size,
  115.                     ratio( z + i ),
  116.                     z[ i ].month,
  117.                     z[ i ].date,
  118.                     z[ i ].year,
  119.                     z[ i ].hour,
  120.                     z[ i ].minute,
  121.                     z[ i ].name,
  122.                     z[ i ].comment );
  123.     ALFreeDir( z );
  124.     printf( "\nAppending c:\\command.* to CON00.ZIP\n" );
  125.     i = ALAppend( "con00.zip", "*.tr? c:\\command.*", 1, my_callback );
  126.     printf( "\n\n" );
  127.     printf( "ALAppend() returned %d\n\n", i );
  128.     cout << "\nHit any key to exit..." << flush;
  129.     getch();
  130.     return i;
  131. }
  132.  
  133.