home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / profile / advprof / enhprof.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  4.3 KB  |  190 lines

  1. //************************************************************
  2. // GUI Profile Viewer Example
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //************************************************************
  8. #include <iostream.h>
  9. #include <fstream.h>
  10. #include <ctype.h>
  11. #include "enhprof.hpp"
  12.  
  13. EnhancedProfile :: EnhancedProfile ( const IString &name,
  14.                                      Boolean createIfNotFound )
  15.   : IProfile( validate( name, createIfNotFound ) )
  16.   {
  17.   }
  18.  
  19. EnhancedProfile :: EnhancedProfile ( const IProfile &profile )
  20.   : IProfile( profile )
  21.   {
  22.   }
  23.  
  24. void EnhancedProfile :: displayOn ( ostream &aStream,
  25.                                     const IString &apps,
  26.                                     const IString &keys ) const
  27.   {
  28.   aStream << "Profile: " << this->name() << endl;
  29.   IProfile
  30.     self( *this );
  31.   IProfile::Cursor
  32.     cursor( self );
  33.   for ( cursor.setToFirst(); 
  34.         cursor.isValid(); 
  35.         cursor.setToNext() )
  36.     {
  37.     IString
  38.       app = self.applicationOrKeyAt( cursor );
  39.  
  40.     if ( app.isLike( apps ) )
  41.       {
  42.       ProfileApplication
  43.         nextApp( self, app );
  44.       nextApp.displayOn( aStream, keys );
  45.       }
  46.     }
  47.   }
  48.  
  49. IString EnhancedProfile :: validate ( const IString &name,
  50.                                       Boolean createIfNotFound )
  51.   {
  52.   int
  53.     option = createIfNotFound ? 0 : ios::nocreate;
  54.   IString
  55.     outName( name );
  56.   ifstream
  57.     test( outName, option );
  58.   if ( !test )
  59.     {
  60.     outName += ".ini";
  61.     ifstream
  62.       test( outName, option );
  63.     if ( !test )
  64.       {
  65.       IString
  66.         msg( "Unable to open profile " );
  67.       msg += outName;
  68.       throw msg;
  69.       }
  70.     }
  71.   return outName;
  72.   }
  73.  
  74. ProfileApplication :: ProfileApplication ( const IProfile &profile,
  75.                                            const IString  &name )
  76.   : prof( profile ),
  77.     nm( name )
  78.   {
  79.   }
  80.  
  81. ProfileApplication :: ~ProfileApplication ( )
  82.   {
  83.   }
  84.  
  85. IString ProfileApplication :: name ( ) const
  86.   {
  87.   return this->nm;
  88.   }
  89.  
  90. IProfile ProfileApplication :: profile ( ) const
  91.   {
  92.   return this->prof;
  93.   }
  94.  
  95. void ProfileApplication :: displayOn ( ostream &aStream, 
  96.                                        const IString &keys ) const
  97.   {
  98.   aStream << "Application: " << this->name() << endl;
  99.   IProfile
  100.     profile( this->profile() );
  101.   IProfile::Cursor
  102.     cursor( profile, this->name() );
  103.   for ( cursor.setToFirst(); 
  104.         cursor.isValid(); 
  105.         cursor.setToNext() )
  106.     {
  107.     IString
  108.       key = profile.applicationOrKeyAt( cursor );
  109.  
  110.     if ( key.isLike( keys ) )
  111.       {
  112.       ProfileKey
  113.         nextKey( *this, key );
  114.       nextKey.displayOn( aStream );
  115.       }
  116.     }
  117.   }
  118.  
  119. ProfileKey :: ProfileKey ( const ProfileApplication &application,
  120.                            const IString  &name )
  121.   : app( application ),
  122.     nm( name )
  123.   {
  124.   }
  125.  
  126. ProfileKey :: ~ProfileKey ( )
  127.   {
  128.   }
  129.  
  130. IString ProfileKey :: name ( ) const
  131.   {
  132.   return this->nm;
  133.   }
  134.  
  135. IString ProfileKey :: value ( ) const
  136.   {
  137.   IProfile
  138.     profile( this->application().profile() );
  139.   IString
  140.     data( profile.elementWithKey( this->name(), 
  141.                                   this->application().name() ) );
  142.   return data;
  143.   }
  144.  
  145. IString ProfileKey :: formatted ( unsigned tabs ) const
  146.   {
  147.   IString
  148.     data = this->value(),
  149.     tab  = IString( '\t' ).copy( tabs ),
  150.     result;
  151.   unsigned
  152.     pos = 1;
  153.   while ( pos <= data.length() )
  154.     {
  155.     unsigned
  156.       bytes = data.length() - pos + 1;
  157.     if ( bytes > 16 )
  158.       bytes = 16;
  159.     result += tab;
  160.     for ( int i = 0; i < bytes; i++ )
  161.       {
  162.       if ( isprint( data[ pos + i ] ) )
  163.         result += data[ pos + i ];
  164.       else
  165.         result += '.';
  166.       }
  167.     result += IString( 0, 17 - bytes );
  168.     IString
  169.       hex( data.subString( pos, bytes ) );
  170.     hex.c2x().leftJustify( 32, ' ' );
  171.     hex.insert( ' ', 8 );
  172.     hex.insert( ' ', 17 );
  173.     hex.insert( ' ', 26 );
  174.     cout << hex << endl;
  175.     result += hex + IString( '\n' );
  176.     pos += bytes;
  177.     }
  178.   return result;
  179.   }
  180.  
  181. ProfileApplication ProfileKey :: application ( ) const
  182.   {
  183.   return this->app;
  184.   }
  185.  
  186. void ProfileKey :: displayOn ( ostream &aStream ) const
  187.   {
  188.   aStream << "\tKey: " << this->name() << endl << this->formatted(2);
  189.   }
  190.