This repository has been archived on 2020-05-10. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon-cpp/docs/example14_easy_treeview_8cp...

55 lines
12 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>mastodon-cpp: example14_easy_treeview.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">mastodon-cpp
&#160;<span id="projectnumber">0.30.1</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',false,false,'search.php','Search');
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">example14_easy_treeview.cpp</div> </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><span class="comment">/* This file is part of mastodon-cpp.</span></div><div class="line"><span class="comment"> * Prints a Mastodon-conversation in a tree.</span></div><div class="line"><span class="comment"> * Argument has to be in the form: https://&lt;domain&gt;/&lt;user&gt;/&lt;status id&gt;</span></div><div class="line"><span class="comment"> * </span></div><div class="line"><span class="comment"> * This example uses a rather wasteful approach in order to use less code and</span></div><div class="line"><span class="comment"> * therefore be more clear. Please don&#39;t use it on long conversations.</span></div><div class="line"><span class="comment"> */</span></div><div class="line"></div><div class="line"><span class="comment">// Don&#39;t compile this if the Easy-interface is turned off</span></div><div class="line"><span class="preprocessor">#ifndef WITHOUT_EASY</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;string&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;thread&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;chrono&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;ctime&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;regex&gt;</span></div><div class="line"></div><div class="line"><span class="comment">// If we are compiling mastodon-cpp, use another include path</span></div><div class="line"><span class="preprocessor">#ifdef MASTODON_CPP</span></div><div class="line"><span class="preprocessor"> #include &quot;mastodon-cpp.hpp&quot;</span></div><div class="line"><span class="preprocessor"> #include &quot;easy/all.hpp&quot;</span></div><div class="line"><span class="preprocessor">#else</span></div><div class="line"><span class="preprocessor"> #include &lt;mastodon-cpp/mastodon-cpp.hpp&gt;</span></div><div class="line"> <span class="comment">// Include all headers in mastodon-cpp/easy/</span></div><div class="line"><span class="preprocessor"> #include &lt;mastodon-cpp/easy/all.hpp&gt;</span></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"></div><div class="line"><span class="keyword">using</span> <a name="_a0"></a><a class="code" href="classMastodon_1_1API.html">Mastodon::API</a>;</div><div class="line"><span class="keyword">using</span> <a name="_a1"></a><a class="code" href="classMastodon_1_1Easy.html">Mastodon::Easy</a>;</div><div class="line"><span class="keyword">using</span> std::cout;</div><div class="line"><span class="keyword">using</span> std::chrono::system_clock;</div><div class="line"></div><div class="line"><span class="comment">// Print a status to stdout, nicely formatted</span></div><div class="line"><span class="keywordtype">void</span> format_status(<span class="keyword">const</span> Easy::Status &amp;status, <span class="keyword">const</span> std::uint8_t &amp;level)</div><div class="line">{</div><div class="line"> std::string space(level, <span class="charliteral">&#39; &#39;</span>);</div><div class="line"> <span class="comment">// Remove HTML-tags</span></div><div class="line"> std::regex restrip(<span class="stringliteral">&quot;&lt;[^&gt;]*&gt;&quot;</span>);</div><div class="line"> std::string content = std::regex_replace(status.content(), restrip, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"></div><div class="line"> cout &lt;&lt; space &lt;&lt; <span class="stringliteral">&quot;+-----------------------------------------\n&quot;</span>;</div><div class="line"> cout &lt;&lt; space &lt;&lt; <span class="stringliteral">&quot;| &quot;</span> &lt;&lt; status.account().acct() &lt;&lt; <span class="stringliteral">&quot;: \n&quot;</span>;</div><div class="line"></div><div class="line"> <span class="keywordflow">while</span> (!content.empty())</div><div class="line"> {</div><div class="line"> cout &lt;&lt; space &lt;&lt; <span class="stringliteral">&quot;| &quot;</span> &lt;&lt; content.substr(0, 40) &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line"> <span class="keywordflow">if</span> (content.length() &gt; 40)</div><div class="line"> {</div><div class="line"> content = content.substr(40);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> {</div><div class="line"> content.clear();</div><div class="line"> }</div><div class="line"> }</div><div class="line"> cout &lt;&lt; space &lt;&lt; <span class="stringliteral">&quot;| &quot;</span></div><div class="line"> &lt;&lt; Easy::strtime_local(status.created_at(), <span class="stringliteral">&quot;%T&quot;</span>) &lt;&lt; <span class="charliteral">&#39;\n&#39;</span>;</div><div class="line"> cout &lt;&lt; space &lt;&lt; <span class="stringliteral">&quot;+-----------------------------------------&quot;</span> &lt;&lt; std::endl;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// Fetch status and its descendants, output them</span></div><div class="line">std::uint16_t print_status(Easy &amp;masto, <span class="keyword">const</span> std::string &amp;<span class="keywordtype">id</span>,</div><div class="line"> <span class="keyword">const</span> std::uint8_t level)</div><div class="line">{</div><div class="line"> std::uint16_t ret;</div><div class="line"> std::string answer;</div><div class="line"> API::parametermap parameters =</div><div class="line"> {</div><div class="line"> { <span class="stringliteral">&quot;id&quot;</span>, { <span class="keywordtype">id</span> }}</div><div class="line"> };</div><div class="line"></div><div class="line"> ret = masto.get(Mastodon::API::v1::statuses_id, parameters, answer);</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (ret == 0)</div><div class="line"> {</div><div class="line"> format_status(Easy::Status(answer), level);</div><div class="line"></div><div class="line"> ret = masto.get(Mastodon::API::v1::statuses_id_context,</div><div class="line"> parameters, answer);</div><div class="line"> <span class="keywordflow">if</span> (ret == 0)</div><div class="line"> {</div><div class="line"> Easy::Context context(answer);</div><div class="line"> <span class="keywordflow">for</span> (<span class="keyword">const</span> Easy::Status &amp;s : context.descendants())</div><div class="line"> {</div><div class="line"> <span class="comment">// Only print descendant if it is a direct reply</span></div><div class="line"> <span class="comment">// NOTE: This is a wasteful approach, I use it only in the</span></div><div class="line"> <span class="comment">// interest of more clarity. Don&#39;t do this at home. :-)</span></div><div class="line"> <span class="keywordflow">if</span> (s.in_reply_to_id() == id)</div><div class="line"> {</div><div class="line"> print_status(masto, s.id(), level + 4);</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> ret;</div><div class="line">}</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> *argv[])</div><div class="line">{</div><div class="line"> <span class="keywordflow">if</span> (argc &lt; 2)</div><div class="line"> {</div><div class="line"> std::cerr &lt;&lt; <span class="stringliteral">&quot;usage: &quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; &lt;url&gt;\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"> <span class="comment">// Extract domain name</span></div><div class="line"> std::string instance = std::string(argv[1]);</div><div class="line"> std::size_t pos = instance.find(<span class="charliteral">&#39;/&#39;</span>) + 2;</div><div class="line"> instance = instance.substr(pos, instance.find(<span class="charliteral">&#39;/&#39;</span>, pos) - pos);</div><div class="line"></div><div class="line"> <span class="comment">// Extract status ID</span></div><div class="line"> std::string <span class="keywordtype">id</span> = std::string(argv[1]);</div><div class="line"> <span class="keywordtype">id</span> = <span class="keywordtype">id</span>.substr(<span class="keywordtype">id</span>.rfind(<span class="charliteral">&#39;/&#39;</span>) + 1);</div><div class="line"></div><div class="line"> cout &lt;&lt; <span class="stringliteral">&quot; Instance: &quot;</span> &lt;&lt; instance &lt;&lt; <span class="stringliteral">&quot;\nStatus ID: &quot;</span> &lt;&lt; <span class="keywordtype">id</span> &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div><div class="line"></div><div class="line"> Easy masto(instance, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"> <span class="keywordflow">return</span> print_status(masto, <span class="keywordtype">id</span>, 0);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="preprocessor">#else</span></div><div class="line"><span class="preprocessor">#include &lt;cstdio&gt;</span></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line"> printf(<span class="stringliteral">&quot;mastodon-cpp was compiled without Easy support.\n&quot;</span>);</div><div class="line"> <span class="keywordflow">return</span> 255;</div><div class="line">}</div><div class="line"><span class="preprocessor">#endif // WITHOUT_EASY</span></div></div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.14
</small></address>
</body>
</html>