home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / data / stparse / stparse.cpp < prev   
Text File  |  1996-10-29  |  5KB  |  149 lines

  1. //************************************************************
  2. // Data Types - Using IStringParser
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //
  8. // DESCRIPTION:
  9. //   This program searches the current directory for the oldest and newest
  10. //   files and reports findings to stdout.
  11. //
  12. //   Usage:   stparse2
  13. //
  14. //************************************************************
  15. #include <idate.hpp>
  16. #include <itime.hpp>
  17. #include <istring.hpp>
  18. #include <istparse.hpp>
  19. #include <iostream.h>
  20. #include <stdlib.h>
  21. #include <fstream.h>
  22.  
  23. int main ()
  24. {
  25.    // List a directory and redirect it to a file.
  26. #ifdef IC_PM
  27.    int rc = system ("dir /N > dir.out");
  28. #endif
  29. #ifdef IC_WIN
  30.    int rc = system ("dir > dir.out");
  31. #endif
  32.    if (!rc)
  33.    {
  34.      // Create an input stream.
  35.      ifstream input( "dir.out" );
  36.      if ( !input )
  37.      {
  38.         cerr << "Error opening file.\a" << endl;
  39.         return 0;
  40.      }
  41.  
  42.      // Create variables to hold parsing tokens.
  43.      IString line,
  44.              pattern1 = IString( 0, 1,
  45.                                 "bytes", IString("bytes").length(),
  46.                                  0, 1,
  47.                                  '*' ),
  48.              pattern2 = IString( 0, 1,
  49.                                 "<DIR>", IString("<DIR>").length(),
  50.                                  0, 1,
  51.                                  '*' ),
  52.              month, day, year,
  53.              hour, minutes, AMPM,
  54.              date,
  55.              time,
  56.              size,
  57.              sizeEAs,
  58.              filename,
  59.              oldestFilename,
  60.              newestFilename;
  61.      IDate oldestDate, newestDate(1,1), theDate;
  62.      ITime oldestTime, newestTime(0,0), theTime;
  63.  
  64.      // Skip the first five lines of the output that contain nonpertinent
  65.      // information.
  66.      for (int i=0; i<5; i++)
  67.         IString::lineFrom (input);
  68.  
  69.      // Read lines and look for the oldest and newest files.
  70.      while ( input )
  71.      {
  72.        // Get a line.
  73.        line = IString::lineFrom( input );
  74.  
  75.        // If the line is the summary information at the bottom,
  76.        // quit the loop.
  77.        if (line.isLike (pattern1))
  78.           break;
  79.  
  80.        // Only process the line if it contains information for a file,
  81.        // not a directory.
  82.        if (!line.isLike (pattern2))
  83.        {
  84.           // Parse the line into its tokens.
  85.           // OS/2 has an EA size field; Windows does not.
  86. #ifdef IC_PM
  87.           line >> date >> time >> size >> sizeEAs >> filename;
  88. #endif
  89. #ifdef IC_WIN
  90.           line >> date >> time >> size >> filename;
  91. #endif
  92.           // Strip leading or trailing white space from the file name.
  93.           filename.strip();
  94.           // Our output file will always be the newest, so exclude it.
  95.           if (filename != "DIR.OUT")
  96.           {
  97.              // Parse the date and create an IDate object.  Hardcode the
  98.              // default separators.  A more robust solution would query
  99.              // them from the system.
  100. #ifdef IC_PM
  101.              date >> month >> '-' >> day >> '-' >> year;
  102. #endif
  103. #ifdef IC_WIN
  104.              date >> month >> '/' >> day >> '/' >> year;
  105. #endif
  106.              IDate theDate (IDate::Month(month.asInt()), day.asInt(),
  107.                 year.asInt()+1900);
  108.    
  109.              // Parse the time and create an ITime object.
  110.              time >> hour >> ":" >> 1 >> minutes >> 2 >> AMPM;
  111.              ITime theTime (((AMPM == "a") ||
  112.                              (AMPM == "A")) ?
  113.                              hour.asInt() : hour.asInt() + 12,
  114.                              minutes.asInt());
  115.    
  116.              // Check to see if the date of the current file is older
  117.              // than the oldest or newer than the newest.  If so, reset
  118.              // the variables.
  119.              if ((theDate < oldestDate) ||
  120.                 ((theDate == oldestDate) && (theTime < oldestTime)))
  121.              {
  122.                 oldestDate = theDate;
  123.                 oldestFilename = filename;
  124.                 oldestTime = theTime;
  125.              }
  126.              else if ((theDate > newestDate) ||
  127.                 ((theDate == newestDate) && (theTime > newestTime)))
  128.              {
  129.                 newestDate = theDate;
  130.                 newestFilename = filename;
  131.                 newestTime = theTime;
  132.              }
  133.           }
  134.        }
  135.      }
  136.  
  137.      // Report our findings.
  138.      cout << "Oldest file is " << oldestFilename << " with date " <<
  139.           oldestDate.asString() << " and time " << oldestTime.asString() <<
  140.           endl;
  141.      cout << "Newest file is " << newestFilename << " with date " <<
  142.           newestDate.asString() << " and time " << newestTime.asString() <<
  143.           endl;
  144.    }
  145.    else
  146.       cout << "Dir command could not be completed." << endl;
  147.    return 0;
  148. }
  149.