LCOV - code coverage report
Current view: top level - frmts/pcidsk/sdk/core - metadataset_p.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 51 55 92.7 %
Date: 2024-11-21 22:18:42 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Purpose:  Implementation of the MetadataSet class.  This is a container
       4             :  *           for a set of metadata, and used by the file, channel and segment
       5             :  *           classes to manage metadata for themselves.  It is not public
       6             :  *           to SDK users.
       7             :  *
       8             :  ******************************************************************************
       9             :  * Copyright (c) 2009
      10             :  * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada.
      11             :  *
      12             :  * SPDX-License-Identifier: MIT
      13             :  ****************************************************************************/
      14             : 
      15             : #include "pcidsk_exception.h"
      16             : #include "core/metadataset.h"
      17             : 
      18             : #include "segment/metadatasegment.h"
      19             : 
      20             : #include <string>
      21             : 
      22             : using namespace PCIDSK;
      23             : 
      24             : /************************************************************************/
      25             : /*                            MetadataSet()                             */
      26             : /************************************************************************/
      27             : 
      28        1969 : MetadataSet::MetadataSet()
      29             : 
      30             : 
      31             : {
      32        1969 :     this->file = nullptr;
      33        1969 :     id = -1;
      34        1969 :     loaded = false;
      35        1969 : }
      36             : 
      37             : /************************************************************************/
      38             : /*                            ~MetadataSet()                            */
      39             : /************************************************************************/
      40             : 
      41        1969 : MetadataSet::~MetadataSet()
      42             : 
      43             : {
      44        1969 : }
      45             : 
      46             : /************************************************************************/
      47             : /*                             Initialize()                             */
      48             : /************************************************************************/
      49             : 
      50        1958 : void MetadataSet::Initialize( PCIDSKFile *fileIn, const std::string& groupIn, int idIn )
      51             : 
      52             : {
      53        1958 :     this->file = fileIn;
      54        1958 :     this->group = groupIn;
      55        1958 :     this->id = idIn;
      56        1958 : }
      57             : 
      58             : /************************************************************************/
      59             : /*                                Load()                                */
      60             : /************************************************************************/
      61             : 
      62        1507 : void MetadataSet::Load()
      63             : 
      64             : {
      65        1507 :     if( loaded )
      66           0 :         return;
      67             : 
      68             :     // This legitimately occurs in some situations, such for overview channel
      69             :     // objects.
      70        1507 :     if( file == nullptr )
      71             :     {
      72           0 :         loaded = true;
      73           0 :         return;
      74             :     }
      75             : 
      76        1507 :     PCIDSKSegment *seg = file->GetSegment( SEG_SYS , "METADATA");
      77             : 
      78        1507 :     if( seg == nullptr )
      79             :     {
      80        1368 :         loaded = true;
      81        1368 :         return;
      82             :     }
      83             : 
      84         139 :     MetadataSegment *md_seg = dynamic_cast<MetadataSegment *>( seg );
      85         139 :     if( md_seg )
      86         139 :         md_seg->FetchGroupMetadata( group.c_str(), id, md_set );
      87         139 :     loaded = true;
      88             : }
      89             : 
      90             : /************************************************************************/
      91             : /*                          GetMetadataValue()                          */
      92             : /************************************************************************/
      93             : 
      94        1344 : std::string MetadataSet::GetMetadataValue( const std::string& key )
      95             : 
      96             : {
      97        1344 :     if( !loaded )
      98        1205 :         Load();
      99             : 
     100        1344 :     if( md_set.count(key) == 0 )
     101        1194 :         return "";
     102             :     else
     103         150 :         return md_set[key];
     104             : }
     105             : 
     106             : /************************************************************************/
     107             : /*                          SetMetadataValue()                          */
     108             : /************************************************************************/
     109             : 
     110          71 : void MetadataSet::SetMetadataValue( const std::string& key, const std::string& value )
     111             : {
     112          71 :     if( !loaded )
     113          49 :         Load();
     114             : 
     115          71 :     if( file == nullptr )
     116             :     {
     117           0 :         return ThrowPCIDSKException( "Attempt to set metadata on an unassociated MetadataSet, likely an overview channel." );
     118             :     }
     119             : 
     120          71 :     md_set[key] = value;
     121             : 
     122          71 :     PCIDSKSegment *seg = file->GetSegment( SEG_SYS , "METADATA");
     123             : 
     124          71 :     if( seg == nullptr )
     125             :     {
     126          32 :         file->CreateSegment("METADATA",
     127             :                             "Please do not modify this metadata segment.",
     128          32 :                             SEG_SYS, 64); // Create a 32k segment by default.
     129          32 :         seg = file->GetSegment( SEG_SYS , "METADATA");
     130             :     }
     131             : 
     132          71 :     MetadataSegment *md_seg = dynamic_cast<MetadataSegment *>( seg );
     133          71 :     if( md_seg )
     134          71 :         md_seg->SetGroupMetadataValue( group.c_str(), id, key, value );
     135             : }
     136             : 
     137             : /************************************************************************/
     138             : /*                          GetMetadataKeys()                           */
     139             : /************************************************************************/
     140             : 
     141         277 : std::vector<std::string> MetadataSet::GetMetadataKeys()
     142             : {
     143         277 :     if( !loaded )
     144         253 :         Load();
     145             : 
     146         277 :     std::vector<std::string> keys;
     147         277 :     std::map<std::string,std::string>::iterator it;
     148             : 
     149         327 :     for( it = md_set.begin(); it != md_set.end(); ++it )
     150             :     {
     151             :         // If the value is empty the key value pair will be deleted in the file
     152             :         // This will ensure that the returned list key are in sync with the file
     153          50 :         if(!it->second.empty())
     154          50 :             keys.push_back(it->first);
     155             :     }
     156             : 
     157         554 :     return keys;
     158             : }

Generated by: LCOV version 1.14