BinHeader.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include "librpm.h"
00013 
00014 #include <iostream>
00015 
00016 #include "zypp/base/Logger.h"
00017 
00018 #include "zypp/target/rpm/BinHeader.h"
00019 
00020 using namespace std;
00021 
00022 #undef Y2LOG
00023 #define Y2LOG "BinHeader"
00024 
00025 namespace zypp {
00026   namespace target {
00027     namespace rpm {
00028 
00030       //
00031       //        CLASS NAME : BinHeader::intList
00032       //
00034       
00035       BinHeader::intList::intList()
00036           : cnt( 0 ), val( 0 ), type( RPM_NULL_TYPE )
00037       {}
00038       
00039       unsigned BinHeader::intList::set( void * val_r, tag cnt_r, tag type_r ) {
00040         val = val_r;
00041         cnt = val ? cnt_r : 0;
00042         type = type_r;
00043         return cnt;
00044       }
00045       
00046       int BinHeader::intList::operator[]( const unsigned idx_r ) const {
00047         if ( idx_r < cnt ) {
00048           switch ( type ) {
00049           case RPM_CHAR_TYPE:
00050             return ((char*)val)[idx_r];
00051           case RPM_INT8_TYPE:
00052             return ((int_8*)val)[idx_r];
00053           case RPM_INT16_TYPE:
00054             return ((int_16*)val)[idx_r];
00055           case RPM_INT32_TYPE:
00056             return ((int_32*)val)[idx_r];
00057           }
00058         }
00059         return 0;
00060       }
00061       
00063       //
00064       //        CLASS NAME : BinHeader::stringList
00065       //
00067       
00068       void BinHeader::stringList::clear() {
00069         if ( val )
00070           free( val );
00071         val = 0;
00072         cnt = 0;
00073       }
00074       
00075       BinHeader::stringList::stringList()
00076           : cnt( 0 ), val( 0 )
00077       {}
00078       
00079       unsigned BinHeader::stringList::set( char ** val_r, tag cnt_r ) {
00080         clear();
00081         val = val_r;
00082         cnt = val ? cnt_r : 0;
00083         return cnt;
00084       }
00085       
00086       std::string BinHeader::stringList::operator[]( const unsigned idx_r ) const {
00087         return( idx_r < cnt ? val[idx_r] : "" );
00088       }
00089       
00091       //
00092       //        CLASS NAME : BinHeader
00093       //
00095       
00097       //
00098       //
00099       //        METHOD NAME : BinHeader::BinHeader
00100       //        METHOD TYPE : Constructor
00101       //
00102       BinHeader::BinHeader( Header h_r )
00103           : _h( h_r )
00104       {
00105         if ( _h ) {
00106           ::headerLink( _h );
00107         }
00108       }
00109       
00111       //
00112       //
00113       //        METHOD NAME : BinHeader::BinHeader
00114       //        METHOD TYPE : Constructor
00115       //
00116       BinHeader::BinHeader( BinHeader::Ptr & rhs )
00117       {
00118         INT << "INJECT from " << rhs;
00119         if ( ! (rhs && rhs->_h) ) {
00120           _h = 0;
00121         } else {
00122           _h = rhs->_h;  // ::headerLink already done in rhs
00123           rhs->_h = 0;
00124         }
00125         INT << ": " << *this << "   (" << rhs << ")" << endl;
00126       }
00127       
00129       //
00130       //
00131       //        METHOD NAME : BinHeader::~BinHeader
00132       //        METHOD TYPE : Destructor
00133       //
00134       BinHeader::~BinHeader()
00135       {
00136         if ( _h ) {
00137           ::headerFree( _h );
00138         }
00139       }
00140       
00142       //
00143       //
00144       //        METHOD NAME : BinHeader::assertHeader
00145       //        METHOD TYPE : void
00146       //
00147       bool BinHeader::assertHeader()
00148       {
00149         if ( !_h ) {
00150           _h = ::headerNew();
00151           if ( !_h ) {
00152             INT << "OOPS: NULL HEADER created!" << endl;
00153             return false;
00154           }
00155         }
00156         return true;
00157       }
00158       
00160       //
00161       //
00162       //        METHOD NAME : BinHeader::has_tag
00163       //        METHOD TYPE : bool
00164       //
00165       //        DESCRIPTION :
00166       //
00167       bool BinHeader::has_tag( tag tag_r ) const
00168       {
00169         return( !empty() && ::headerIsEntry( _h, tag_r ) );
00170       }
00171       
00173       //
00174       //
00175       //        METHOD NAME : BinHeader::int_list
00176       //        METHOD TYPE : unsigned
00177       //
00178       //        DESCRIPTION :
00179       //
00180       unsigned BinHeader::int_list( tag tag_r, intList & lst_r ) const
00181       {
00182         if ( !empty() ) {
00183           int_32 type = 0;
00184           int_32 cnt  = 0;
00185           void * val  = 0;
00186           ::headerGetEntry( _h, tag_r, &type, &val, &cnt );
00187       
00188           if ( val ) {
00189             switch ( type ) {
00190             case RPM_NULL_TYPE:
00191               return lst_r.set( 0, 0, type );
00192             case RPM_CHAR_TYPE:
00193             case RPM_INT8_TYPE:
00194             case RPM_INT16_TYPE:
00195             case RPM_INT32_TYPE:
00196               return lst_r.set( val, cnt, type );
00197       
00198             case RPM_STRING_ARRAY_TYPE:
00199               free( val );
00200               // fall through
00201             default:
00202               INT << "RPM_TAG MISSMATCH: RPM_INT32_TYPE " << tag_r << " got type " << type << endl;
00203             }
00204           }
00205         }
00206         return lst_r.set( 0, 0, RPM_NULL_TYPE );
00207       }
00208       
00210       //
00211       //
00212       //        METHOD NAME : BinHeader::string_list
00213       //        METHOD TYPE : unsigned
00214       //
00215       //        DESCRIPTION :
00216       //
00217       unsigned BinHeader::string_list( tag tag_r, stringList & lst_r ) const
00218       {
00219         if ( !empty() ) {
00220           int_32 type = 0;
00221           int_32 cnt  = 0;
00222           void * val  = 0;
00223           ::headerGetEntry( _h, tag_r, &type, &val, &cnt );
00224       
00225           if ( val ) {
00226             switch ( type ) {
00227             case RPM_NULL_TYPE:
00228               return lst_r.set( 0, 0 );
00229             case RPM_STRING_ARRAY_TYPE:
00230               return lst_r.set( (char**)val, cnt );
00231       
00232             default:
00233               INT << "RPM_TAG MISSMATCH: RPM_STRING_ARRAY_TYPE " << tag_r << " got type " << type << endl;
00234             }
00235           }
00236         }
00237         return lst_r.set( 0, 0 );
00238       }
00239       
00241       //
00242       //
00243       //        METHOD NAME : BinHeader::int_val
00244       //        METHOD TYPE : int
00245       //
00246       //        DESCRIPTION :
00247       //
00248       int BinHeader::int_val( tag tag_r ) const
00249       {
00250         if ( !empty() ) {
00251           int_32 type = 0;
00252           int_32 cnt  = 0;
00253           void * val  = 0;
00254           ::headerGetEntry( _h, tag_r, &type, &val, &cnt );
00255       
00256           if ( val ) {
00257             switch ( type ) {
00258             case RPM_NULL_TYPE:
00259               return 0;
00260             case RPM_CHAR_TYPE:
00261               return *((char*)val);
00262             case RPM_INT8_TYPE:
00263               return *((int_8*)val);
00264             case RPM_INT16_TYPE:
00265               return *((int_16*)val);
00266             case RPM_INT32_TYPE:
00267               return *((int_32*)val);
00268       
00269             case RPM_STRING_ARRAY_TYPE:
00270               free( val );
00271               // fall through
00272             default:
00273               INT << "RPM_TAG MISSMATCH: RPM_INT32_TYPE " << tag_r << " got type " << type << endl;
00274             }
00275           }
00276         }
00277         return 0;
00278       }
00279       
00281       //
00282       //
00283       //        METHOD NAME : BinHeader::string_val
00284       //        METHOD TYPE : std::string
00285       //
00286       //        DESCRIPTION :
00287       //
00288       std::string BinHeader::string_val( tag tag_r ) const
00289       {
00290         if ( !empty() ) {
00291           int_32 type = 0;
00292           int_32 cnt  = 0;
00293           void * val  = 0;
00294           ::headerGetEntry( _h, tag_r, &type, &val, &cnt );
00295       
00296           if ( val ) {
00297             switch ( type ) {
00298             case RPM_NULL_TYPE:
00299               return "";
00300             case RPM_STRING_TYPE:
00301               return (char*)val;
00302       
00303             case RPM_STRING_ARRAY_TYPE:
00304               free( val );
00305               // fall through
00306             default:
00307               INT << "RPM_TAG MISSMATCH: RPM_STRING_TYPE " << tag_r << " got type " << type << endl;
00308             }
00309           }
00310         }
00311         return "";
00312       }
00313       
00315       //
00316       //
00317       //        METHOD NAME : BinHeader::stringList_val
00318       //        METHOD TYPE : std::list<std::string>
00319       //
00320       //        DESCRIPTION :
00321       //
00322       std::list<std::string> BinHeader::stringList_val( tag tag_r ) const
00323       {
00324         std::list<std::string> ret;
00325       
00326         if ( !empty() ) {
00327           stringList lines;
00328           unsigned count = string_list( tag_r, lines );
00329           for ( unsigned i = 0; i < count; ++i ) {
00330             ret.push_back( lines[i] );
00331           }
00332         }
00333         return ret;
00334       }
00335       
00337       //
00338       //
00339       //      METHOD NAME : BinHeader::dumpOn
00340       //      METHOD TYPE : ostream &
00341       //
00342       //      DESCRIPTION :
00343       //
00344       ostream & BinHeader::dumpOn( ostream & str ) const
00345       {
00346         ReferenceCounted::dumpOn( str );
00347         return str << '{' << (void*)_h << '}';
00348       }
00349 
00350     } // namespace rpm
00351   } // namespace target
00352 } // namespace zypp

Generated on Thu Jul 6 00:07:26 2006 for zypp by  doxygen 1.4.6