<div class="fragment"><div class="line"><span class="comment">// ***************************************************************** -*- C++ -*-</span></div>
<div class="line"><span class="comment">// iptcprint.cpp, $Rev: 3090 $</span></div>
<div class="line"><span class="comment">// Sample program to print the IPTC metadata of an image</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;<a class="code" href="doc/exiv2_8hpp.html" title="Include all Exiv2 header files.">exiv2/exiv2.hpp</a>&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iomanip&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;cassert&gt;</span></div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* <span class="keyword">const</span> argv[])</div>
<div class="line"><span class="keyword">try</span> {</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">if</span> (argc != 2) {</div>
<div class="line">        std::cout &lt;&lt; <span class="stringliteral">&quot;Usage: &quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; file\n&quot;</span>;</div>
<div class="line">        <span class="keywordflow">return</span> 1;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <a class="code" href="doc/classExiv2_1_1Image.html#a89ad3ffe7a4e8a943d267d77843415fb" title="Image auto_ptr type.">Exiv2::Image::AutoPtr</a> image = <a name="a0"></a><a class="code" href="doc/classExiv2_1_1ImageFactory.html#aba929c4ca4a71625d12bcb97bcc28161" title="Create an Image subclass of the appropriate type by reading the specified file. Image type is derived...">Exiv2::ImageFactory::open</a>(argv[1]);</div>
<div class="line">    assert (image.get() != 0);</div>
<div class="line">    image-&gt;readMetadata();</div>
<div class="line"></div>
<div class="line">    <a name="_a1"></a><a class="code" href="doc/classExiv2_1_1IptcData.html" title="A container for IPTC data. This is a top-level class of the Exiv2 library.">Exiv2::IptcData</a> &amp;iptcData = image-&gt;iptcData();</div>
<div class="line">    <span class="keywordflow">if</span> (iptcData.<a name="a2"></a><a class="code" href="doc/classExiv2_1_1IptcData.html#afda626e27ebecd599005c68022db9c1c" title="Return true if there is no IPTC metadata.">empty</a>()) {</div>
<div class="line">        std::string error(argv[1]);</div>
<div class="line">        error += <span class="stringliteral">&quot;: No IPTC data found in the file&quot;</span>;</div>
<div class="line">        <span class="keywordflow">throw</span> <a name="a4"></a><a class="code" href="doc/namespaceExiv2.html#accd3e49cafe9db52c1e0e6f648753cae" title="Error class used for exceptions (std::string based)">Exiv2::Error</a>(1, error);</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <a class="code" href="doc/classExiv2_1_1IptcData.html#a0d53776cd2f36e63fff78c8f142a7caf" title="IptcMetadata iterator type.">Exiv2::IptcData::iterator</a> end = iptcData.<a name="a5"></a><a class="code" href="doc/classExiv2_1_1IptcData.html#a6753e8a713ab2b42a3bdc7b3d9eab401" title="End of the metadata.">end</a>();</div>
<div class="line">    <span class="keywordflow">for</span> (<a class="code" href="doc/classExiv2_1_1IptcData.html#a0d53776cd2f36e63fff78c8f142a7caf" title="IptcMetadata iterator type.">Exiv2::IptcData::iterator</a> md = iptcData.<a name="a6"></a><a class="code" href="doc/classExiv2_1_1IptcData.html#a03385c128b29d262ade837093fddc0d2" title="Begin of the metadata.">begin</a>(); md != end; ++md) {</div>
<div class="line">        std::cout &lt;&lt; std::setw(44) &lt;&lt; std::setfill(<span class="charliteral">&#39; &#39;</span>) &lt;&lt; std::left</div>
<div class="line">                  &lt;&lt; md-&gt;key() &lt;&lt; <span class="stringliteral">&quot; &quot;</span></div>
<div class="line">                  &lt;&lt; <span class="stringliteral">&quot;0x&quot;</span> &lt;&lt; std::setw(4) &lt;&lt; std::setfill(<span class="charliteral">&#39;0&#39;</span>) &lt;&lt; std::right</div>
<div class="line">                  &lt;&lt; std::hex &lt;&lt; md-&gt;tag() &lt;&lt; <span class="stringliteral">&quot; &quot;</span></div>
<div class="line">                  &lt;&lt; std::setw(9) &lt;&lt; std::setfill(<span class="charliteral">&#39; &#39;</span>) &lt;&lt; std::left</div>
<div class="line">                  &lt;&lt; md-&gt;typeName() &lt;&lt; <span class="stringliteral">&quot; &quot;</span></div>
<div class="line">                  &lt;&lt; std::dec &lt;&lt; std::setw(3)</div>
<div class="line">                  &lt;&lt; std::setfill(<span class="charliteral">&#39; &#39;</span>) &lt;&lt; std::right</div>
<div class="line">                  &lt;&lt; md-&gt;count() &lt;&lt; <span class="stringliteral">&quot;  &quot;</span></div>
<div class="line">                  &lt;&lt; std::dec &lt;&lt; md-&gt;value()</div>
<div class="line">                  &lt;&lt; std::endl;</div>
<div class="line">    }</div>
<div class="line"></div>
<div class="line">    <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="line"><span class="keywordflow">catch</span> (<a name="_a7"></a><a class="code" href="doc/classExiv2_1_1AnyError.html" title="Error class interface. Allows the definition and use of a hierarchy of error classes which can all be...">Exiv2::AnyError</a>&amp; e) {</div>
<div class="line">    std::cout &lt;&lt; <span class="stringliteral">&quot;Caught Exiv2 exception &#39;&quot;</span> &lt;&lt; e &lt;&lt; <span class="stringliteral">&quot;&#39;\n&quot;</span>;</div>
<div class="line">    <span class="keywordflow">return</span> -1;</div>
<div class="line">}</div>
</div><!-- fragment -->
