eccodes/html/keys__iterator_8c-example.html

116 lines
8.5 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>grib_api: keys_iterator.c</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.3 -->
<div class="tabs">
<ul>
<li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
</ul>
</div>
<h1>keys_iterator.c</h1>keys_iterator.c How to get the names of all the keys defined in a message and how to iterate through them.<p>
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001
<a name="l00010"></a>00010 <span class="comment">/*</span>
<a name="l00011"></a>00011 <span class="comment"> * C Implementation: keys_iterator</span>
<a name="l00012"></a>00012 <span class="comment"> *</span>
<a name="l00013"></a>00013 <span class="comment"> * Description:</span>
<a name="l00014"></a>00014 <span class="comment"> * Example on how to use keys_iterator functions and the</span>
<a name="l00015"></a>00015 <span class="comment"> * grib_keys_iterator structure to get all the available</span>
<a name="l00016"></a>00016 <span class="comment"> * keys in a message.</span>
<a name="l00017"></a>00017 <span class="comment"> *</span>
<a name="l00018"></a>00018 <span class="comment"> * Author: Enrico Fucile</span>
<a name="l00019"></a>00019 <span class="comment"> *</span>
<a name="l00020"></a>00020 <span class="comment"> *</span>
<a name="l00021"></a>00021 <span class="comment"> */</span>
<a name="l00022"></a>00022
<a name="l00023"></a>00023 <span class="preprocessor">#include &lt;assert.h&gt;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00025"></a>00025 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00026"></a>00026 <span class="preprocessor">#include &lt;unistd.h&gt;</span>
<a name="l00027"></a>00027
<a name="l00028"></a>00028 <span class="preprocessor">#include "<a class="code" href="grib__api_8h.html" title="Copyright 2005-2016 ECMWF.">grib_api.h</a>"</span>
<a name="l00029"></a>00029
<a name="l00030"></a>00030 <span class="preprocessor">#define MAX_KEY_LEN 255</span>
<a name="l00031"></a>00031 <span class="preprocessor"></span><span class="preprocessor">#define MAX_VAL_LEN 1024</span>
<a name="l00032"></a>00032 <span class="preprocessor"></span>
<a name="l00033"></a>00033 <span class="keyword">static</span> <span class="keywordtype">void</span> usage(<span class="keywordtype">char</span>* progname);
<a name="l00034"></a>00034
<a name="l00035"></a>00035 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])
<a name="l00036"></a>00036 {
<a name="l00037"></a>00037 <span class="comment">/* To skip read only and not coded keys</span>
<a name="l00038"></a>00038 <span class="comment"> unsigned long key_iterator_filter_flags=GRIB_KEYS_ITERATOR_SKIP_READ_ONLY ||</span>
<a name="l00039"></a>00039 <span class="comment"> GRIB_KEYS_ITERATOR_SKIP_COMPUTED;</span>
<a name="l00040"></a>00040 <span class="comment"> */</span>
<a name="l00041"></a>00041 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> key_iterator_filter_flags=<a name="a0"></a><a class="code" href="group__keys__iterator.html#g0e4d2d943ddd61d0b877060e6fbea405">GRIB_KEYS_ITERATOR_ALL_KEYS</a>;
<a name="l00042"></a>00042
<a name="l00043"></a>00043 <span class="comment">/* valid name_spaces are ls and mars */</span>
<a name="l00044"></a>00044 <span class="keywordtype">char</span>* name_space=<span class="stringliteral">"ls"</span>;
<a name="l00045"></a>00045
<a name="l00046"></a>00046 <span class="comment">/* name_space=NULL to get all the keys */</span>
<a name="l00047"></a>00047 <span class="comment">/* char* name_space=0; */</span>
<a name="l00048"></a>00048
<a name="l00049"></a>00049 FILE* f;
<a name="l00050"></a>00050 <a name="a1"></a><a class="code" href="group__grib__handle.html#g309a5ee24f4c730646d3f80ad0ef5f1b">grib_handle</a>* h=NULL;
<a name="l00051"></a>00051 <a name="a2"></a><a class="code" href="group__keys__iterator.html#gfa22412541f9d2df9680a82371036dbb">grib_keys_iterator</a>* kiter=NULL;
<a name="l00052"></a>00052 <span class="keywordtype">int</span> err=0;
<a name="l00053"></a>00053 <span class="keywordtype">int</span> grib_count=0;
<a name="l00054"></a>00054
<a name="l00055"></a>00055 <span class="keywordtype">char</span> value[MAX_VAL_LEN];
<a name="l00056"></a>00056 <span class="keywordtype">size_t</span> vlen=MAX_VAL_LEN;
<a name="l00057"></a>00057
<a name="l00058"></a>00058 <span class="keywordflow">if</span> (argc != 2) usage(argv[0]);
<a name="l00059"></a>00059
<a name="l00060"></a>00060 f = fopen(argv[1],<span class="stringliteral">"r"</span>);
<a name="l00061"></a>00061 <span class="keywordflow">if</span>(!f) {
<a name="l00062"></a>00062 perror(argv[1]);
<a name="l00063"></a>00063 exit(1);
<a name="l00064"></a>00064 }
<a name="l00065"></a>00065
<a name="l00066"></a>00066 <span class="keywordflow">while</span>((h = <a name="a3"></a><a class="code" href="group__grib__handle.html#g5e24f8499aa7e4178ccc25a5de3145c5" title="Create a handle from a file resource.">grib_handle_new_from_file</a>(0,f,&amp;err)) != NULL) {
<a name="l00067"></a>00067
<a name="l00068"></a>00068 grib_count++;
<a name="l00069"></a>00069 printf(<span class="stringliteral">"-- GRIB N. %d --\n"</span>,grib_count);
<a name="l00070"></a>00070 <span class="keywordflow">if</span>(!h) {
<a name="l00071"></a>00071 printf(<span class="stringliteral">"ERROR: Unable to create grib handle\n"</span>);
<a name="l00072"></a>00072 exit(1);
<a name="l00073"></a>00073 }
<a name="l00074"></a>00074
<a name="l00075"></a>00075 kiter=<a name="a4"></a><a class="code" href="group__keys__iterator.html#g66a11d157fc81e1c27fc2acad0a5b1f4">grib_keys_iterator_new</a>(h,key_iterator_filter_flags,name_space);
<a name="l00076"></a>00076 <span class="keywordflow">if</span> (!kiter) {
<a name="l00077"></a>00077 printf(<span class="stringliteral">"ERROR: Unable to create keys iterator\n"</span>);
<a name="l00078"></a>00078 exit(1);
<a name="l00079"></a>00079 }
<a name="l00080"></a>00080
<a name="l00081"></a>00081 <span class="keywordflow">while</span>(<a name="a5"></a><a class="code" href="group__keys__iterator.html#g28f6ac808ecdc324fa3a47bc7b3f6dec">grib_keys_iterator_next</a>(kiter))
<a name="l00082"></a>00082 {
<a name="l00083"></a>00083 <span class="keyword">const</span> <span class="keywordtype">char</span>* name = <a name="a6"></a><a class="code" href="group__keys__iterator.html#gec3348471f770965c07964e286249e12">grib_keys_iterator_get_name</a>(kiter);
<a name="l00084"></a>00084 vlen=MAX_VAL_LEN;
<a name="l00085"></a>00085 GRIB_CHECK(<a name="a7"></a><a class="code" href="group__get__set.html#g8aeaf9f930eea1cc0f15e92f18a25053" title="Get a string value from a key, if several keys of the same name are present, the...">grib_get_string</a>(h,name,value,&amp;vlen),name);
<a name="l00086"></a>00086 printf(<span class="stringliteral">"%s = %s\n"</span>,name,value);
<a name="l00087"></a>00087 }
<a name="l00088"></a>00088
<a name="l00089"></a>00089 <a name="a8"></a><a class="code" href="group__keys__iterator.html#g79716d2b701c3dbb3d3a8e633f35a40a">grib_keys_iterator_delete</a>(kiter);
<a name="l00090"></a>00090
<a name="l00091"></a>00091 }
<a name="l00092"></a>00092
<a name="l00093"></a>00093 <span class="keywordflow">return</span> 0;
<a name="l00094"></a>00094
<a name="l00095"></a>00095 }
<a name="l00096"></a>00096
<a name="l00097"></a>00097 <span class="keyword">static</span> <span class="keywordtype">void</span> usage(<span class="keywordtype">char</span>* progname) {
<a name="l00098"></a>00098 printf(<span class="stringliteral">"\nUsage: %s grib_file\n"</span>,progname);
<a name="l00099"></a>00099 exit(1);
<a name="l00100"></a>00100 }
<a name="l00101"></a>00101
</pre></div> <hr size="1"><address style="text-align: right;"><small>Generated on Tue Sep 22 15:18:21 2009 for grib_api by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3 </small></address>
</body>
</html>