home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yeah09.zip / samples / namelist / namelist.cpp < prev   
Text File  |  1996-05-25  |  2KB  |  79 lines

  1. //------------------------------------------------------------
  2. //
  3. // Name:     namelist.cpp
  4. // Version:  0.9
  5. // Author:   Björn Fahller.
  6. //
  7. // Copyright (C) Björn Fahller, 1996.
  8. //
  9. // Purpose:  Show how to write a custom error handler for EA,
  10. //           and how to get the set of EA names available
  11. //           for a file.
  12. //
  13. // History:
  14. //          Ver.  Date         What
  15. //          0.9   1996-05-26   First official release.
  16. //
  17. //------------------------------------------------------------
  18.  
  19. #include <yea.h>
  20. #include <iostream.h>
  21.  
  22. // Store original error handler in this, for the
  23. // custom one to call.
  24.  
  25. EA::ErrorHandler oldError = 0;
  26.  
  27.  
  28.  
  29. // Custom error handler that doesn't throw exceptions
  30. // on read errors, but rather tells what was wrong.
  31. // All other errors are handled as before.
  32.  
  33. void myhandler(EA::Error error, unsigned long code)
  34. {
  35.   if (error == EA::ReadError)
  36.   {
  37.     cout << "Read failed with code " << code << endl;
  38.     return;
  39.   }
  40.   oldError(error,code);
  41. }
  42.  
  43. int main(int argc, char* argv[])
  44. {
  45.   if (argc != 2)
  46.   {
  47.     cerr << "Usage: " << argv[0] << ": filename" << endl;
  48.     return -1;
  49.   }
  50.  
  51.   oldError = EA::errorHandler; // Store away old error handler,
  52.   EA::errorHandler = myhandler; // and set the new one.
  53.  
  54.  
  55.   // Get the set of extended attributes defined for the file.
  56.   EA::NameSet names = EA::namesIn(argv[1]);
  57.  
  58.  
  59.   if (names.numberOfElements() == 0)
  60.   {
  61.     cout << "No extended attributes on file \"" << argv[1] << "\"" << endl;
  62.     return 0; // No reason to go on, if there were no attributes to list.
  63.   }
  64.  
  65.   // Now, let's print the set of names found.
  66.  
  67.   cout << endl << "Extended attributes for \"" << argv[1] << "\"" << endl;
  68.   cout << "Flag\t\tName" << endl;
  69.   cout << "====\t\t====" << endl;
  70.   EA::NameSet::Cursor c(names);
  71.   forCursor(c)
  72.   {
  73.     cout << (names.elementAt(c).flags == EA::mandatory ? "mandatory" : "optional")
  74.          << '\t' << names.elementAt(c).name << endl;
  75.   }
  76.   return 0;
  77. }
  78.  
  79.