mastodonpp  0.5.5
Classes | Typedefs | Enumerations | Functions
mastodonpp Namespace Reference

C++ wrapper for the Mastodon API. More...

Classes

struct  answer_type
 Return type for Requests. More...
 
class  API
 Holds API endpoints. More...
 
class  Connection
 Represents a connection to an instance. Used for requests. More...
 
class  CURLException
 Exception for libcurl errors. More...
 
class  CURLWrapper
 Handles the details of network connections. More...
 
struct  event_type
 A stream event. More...
 
class  Instance
 Holds the access data of an instance. More...
 

Typedefs

using endpoint_variant = variant< API::endpoint_type, string_view >
 An endpoint. Either API::endpoint_type or std::string_view. More...
 
using parametermap = map< string_view, variant< string_view, vector< string_view > >>
 std::map of parameters for API calls. More...
 
using parameterpair = pair< string_view, variant< string_view, vector< string_view > >>
 A single parameter of a parametermap. More...
 

Enumerations

enum  http_method {
  GET, POST, PATCH, PUT,
  DELETE
}
 The HTTP method. More...
 

Functions

string unescape_html (string html)
 Replaces HTML entities with UTF-8 characters. More...
 
std::ostream & operator<< (std::ostream &out, const answer_type &answer)
 

Detailed Description

C++ wrapper for the Mastodon API.

Since
0.1.0

Typedef Documentation

◆ endpoint_variant

using mastodonpp::endpoint_variant = typedef variant<API::endpoint_type,string_view>

An endpoint. Either API::endpoint_type or std::string_view.

Since
0.1.0

◆ parametermap

using mastodonpp::parametermap = typedef map<string_view, variant<string_view, vector<string_view> >>

std::map of parameters for API calls.

Note that arrays always have to be specified as vectors, even if they have only 1 element. To send a file, use “@file:” followed by the file name as value.

Example:

parametermap parameters
{
{"poll[expires_in]", "86400"},
{"poll[options]", vector<string_view>{"Yes", "No", "Maybe"}},
{"status", "How is the weather?"}
};
Since
0.1.0

◆ parameterpair

using mastodonpp::parameterpair = typedef pair<string_view, variant<string_view, vector<string_view> >>

A single parameter of a parametermap.

Since
0.1.0

Enumeration Type Documentation

◆ http_method

The HTTP method.

Since
0.1.0
41 {
42  GET, // NOLINT(readability-identifier-naming)
43  POST, // NOLINT(readability-identifier-naming)
44  PATCH, // NOLINT(readability-identifier-naming)
45  PUT, // NOLINT(readability-identifier-naming)
46  DELETE // NOLINT(readability-identifier-naming)
47 };

Function Documentation

◆ operator<<()

std::ostream& mastodonpp::operator<< ( std::ostream &  out,
const answer_type answer 
)
Since
0.1.0
40 {
41  out << answer.body;
42  return out;
43 }

◆ unescape_html()

string mastodonpp::unescape_html ( string  html)

Replaces HTML entities with UTF-8 characters.

Supports named and numbered entities, decimal and hexadecimal.

Example:

// Will output: 2€ = 2€ = 2€
std::cout << mastodonpp::unescape_html("2&euro; = 2&#8364; = 2&#x20ac;");
Parameters
htmlThe HTML to unescape.
Since
0.4.0
40 {
41  string buffer{move(html)};
42  string output;
43 
44  // Source: https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_
45  // entity_references#Character_entity_references_in_HTML
46  const map<string_view, char32_t> names
47  {
48  { "exclamation", 0x0021 }, { "quot", 0x0022 },
49  { "percent", 0x0025 }, { "amp", 0x0026 },
50  { "apos", 0x0027 }, { "add", 0x002B },
51  { "lt", 0x003C }, { "equal", 0x003D },
52  { "gt", 0x003E }, { "nbsp", 0x00A0 },
53  { "iexcl", 0x00A1 }, { "cent", 0x00A2 },
54  { "pound", 0x00A3 }, { "curren", 0x00A4 },
55  { "yen", 0x00A5 }, { "brvbar", 0x00A6 },
56  { "sect", 0x00A7 }, { "uml", 0x00A8 },
57  { "copy", 0x00A9 }, { "ordf", 0x00AA },
58  { "laquo", 0x00AB }, { "not", 0x00AC },
59  { "shy", 0x00AD }, { "reg", 0x00AE },
60  { "macr", 0x00AF }, { "deg", 0x00B0 },
61  { "plusmn", 0x00B1 }, { "sup2", 0x00B2 },
62  { "sup3", 0x00B3 }, { "acute", 0x00B4 },
63  { "micro", 0x00B5 }, { "para", 0x00B6 },
64  { "middot", 0x00B7 }, { "cedil", 0x00B8 },
65  { "sup1", 0x00B9 }, { "ordm", 0x00BA },
66  { "raquo", 0x00BB }, { "frac14", 0x00BC },
67  { "frac12", 0x00BD }, { "frac34", 0x00BE },
68  { "iquest", 0x00BF }, { "Agrave", 0x00C0 },
69  { "Aacute", 0x00C1 }, { "Acirc", 0x00C2 },
70  { "Atilde", 0x00C3 }, { "Auml", 0x00C4 },
71  { "Aring", 0x00C5 }, { "AElig", 0x00C6 },
72  { "Ccedil", 0x00C7 }, { "Egrave", 0x00C8 },
73  { "Eacute", 0x00C9 }, { "Ecirc", 0x00CA },
74  { "Euml", 0x00CB }, { "Igrave", 0x00CC },
75  { "Iacute", 0x00CD }, { "Icirc", 0x00CE },
76  { "Iuml", 0x00CF }, { "ETH", 0x00D0 },
77  { "Ntilde", 0x00D1 }, { "Ograve", 0x00D2 },
78  { "Oacute", 0x00D3 }, { "Ocirc", 0x00D4 },
79  { "Otilde", 0x00D5 }, { "Ouml", 0x00D6 },
80  { "times", 0x00D7 }, { "Oslash", 0x00D8 },
81  { "Ugrave", 0x00D9 }, { "Uacute", 0x00DA },
82  { "Ucirc", 0x00DB }, { "Uuml", 0x00DC },
83  { "Yacute", 0x00DD }, { "THORN", 0x00DE },
84  { "szlig", 0x00DF }, { "agrave", 0x00E0 },
85  { "aacute", 0x00E1 }, { "acirc", 0x00E2 },
86  { "atilde", 0x00E3 }, { "auml", 0x00E4 },
87  { "aring", 0x00E5 }, { "aelig", 0x00E6 },
88  { "ccedil", 0x00E7 }, { "egrave", 0x00E8 },
89  { "eacute", 0x00E9 }, { "ecirc", 0x00EA },
90  { "euml", 0x00EB }, { "igrave", 0x00EC },
91  { "iacute", 0x00ED }, { "icirc", 0x00EE },
92  { "iuml", 0x00EF }, { "eth", 0x00F0 },
93  { "ntilde", 0x00F1 }, { "ograve", 0x00F2 },
94  { "oacute", 0x00F3 }, { "ocirc", 0x00F4 },
95  { "otilde", 0x00F5 }, { "ouml", 0x00F6 },
96  { "divide", 0x00F7 }, { "oslash", 0x00F8 },
97  { "ugrave", 0x00F9 }, { "uacute", 0x00FA },
98  { "ucirc", 0x00FB }, { "uuml", 0x00FC },
99  { "yacute", 0x00FD }, { "thorn", 0x00FE },
100  { "yuml", 0x00FF }, { "OElig", 0x0152 },
101  { "oelig", 0x0153 }, { "Scaron", 0x0160 },
102  { "scaron", 0x0161 }, { "Yuml", 0x0178 },
103  { "fnof", 0x0192 }, { "circ", 0x02C6 },
104  { "tilde", 0x02DC }, { "Alpha", 0x0391 },
105  { "Beta", 0x0392 }, { "Gamma", 0x0393 },
106  { "Delta", 0x0394 }, { "Epsilon", 0x0395 },
107  { "Zeta", 0x0396 }, { "Eta", 0x0397 },
108  { "Theta", 0x0398 }, { "Iota", 0x0399 },
109  { "Kappa", 0x039A }, { "Lambda", 0x039B },
110  { "Mu", 0x039C }, { "Nu", 0x039D },
111  { "Xi", 0x039E }, { "Omicron", 0x039F },
112  { "Pi", 0x03A0 }, { "Rho", 0x03A1 },
113  { "Sigma", 0x03A3 }, { "Tau", 0x03A4 },
114  { "Upsilon", 0x03A5 }, { "Phi", 0x03A6 },
115  { "Chi", 0x03A7 }, { "Psi", 0x03A8 },
116  { "Omega", 0x03A9 }, { "alpha", 0x03B1 },
117  { "beta", 0x03B2 }, { "gamma", 0x03B3 },
118  { "delta", 0x03B4 }, { "epsilon", 0x03B5 },
119  { "zeta", 0x03B6 }, { "eta", 0x03B7 },
120  { "theta", 0x03B8 }, { "iota", 0x03B9 },
121  { "kappa", 0x03BA }, { "lambda", 0x03BB },
122  { "mu", 0x03BC }, { "nu", 0x03BD },
123  { "xi", 0x03BE }, { "omicron", 0x03BF },
124  { "pi", 0x03C0 }, { "rho", 0x03C1 },
125  { "sigmaf", 0x03C2 }, { "sigma", 0x03C3 },
126  { "tau", 0x03C4 }, { "upsilon", 0x03C5 },
127  { "phi", 0x03C6 }, { "chi", 0x03C7 },
128  { "psi", 0x03C8 }, { "omega", 0x03C9 },
129  { "thetasym", 0x03D1 }, { "upsih", 0x03D2 },
130  { "piv", 0x03D6 }, { "ensp", 0x2002 },
131  { "emsp", 0x2003 }, { "thinsp", 0x2009 },
132  { "zwnj", 0x200C }, { "zwj", 0x200D },
133  { "lrm", 0x200E }, { "rlm", 0x200F },
134  { "ndash", 0x2013 }, { "mdash", 0x2014 },
135  { "horbar", 0x2015 }, { "lsquo", 0x2018 },
136  { "rsquo", 0x2019 }, { "sbquo", 0x201A },
137  { "ldquo", 0x201C }, { "rdquo", 0x201D },
138  { "bdquo", 0x201E }, { "dagger", 0x2020 },
139  { "Dagger", 0x2021 }, { "bull", 0x2022 },
140  { "hellip", 0x2026 }, { "permil", 0x2030 },
141  { "prime", 0x2032 }, { "Prime", 0x2033 },
142  { "lsaquo", 0x2039 }, { "rsaquo", 0x203A },
143  { "oline", 0x203E }, { "frasl", 0x2044 },
144  { "euro", 0x20AC }, { "image", 0x2111 },
145  { "weierp", 0x2118 }, { "real", 0x211C },
146  { "trade", 0x2122 }, { "alefsym", 0x2135 },
147  { "larr", 0x2190 }, { "uarr", 0x2191 },
148  { "rarr", 0x2192 }, { "darr", 0x2193 },
149  { "harr", 0x2194 }, { "crarr", 0x21B5 },
150  { "lArr", 0x21D0 }, { "uArr", 0x21D1 },
151  { "rArr", 0x21D2 }, { "dArr", 0x21D3 },
152  { "hArr", 0x21D4 }, { "forall", 0x2200 },
153  { "part", 0x2202 }, { "exist", 0x2203 },
154  { "empty", 0x2205 }, { "nabla", 0x2207 },
155  { "isin", 0x2208 }, { "notin", 0x2209 },
156  { "ni", 0x220B }, { "prod", 0x220F },
157  { "sum", 0x2211 }, { "minus", 0x2212 },
158  { "lowast", 0x2217 }, { "radic", 0x221A },
159  { "prop", 0x221D }, { "infin", 0x221E },
160  { "ang", 0x2220 }, { "and", 0x2227 },
161  { "or", 0x2228 }, { "cap", 0x2229 },
162  { "cup", 0x222A }, { "int", 0x222B },
163  { "there4", 0x2234 }, { "sim", 0x223C },
164  { "cong", 0x2245 }, { "asymp", 0x2248 },
165  { "ne", 0x2260 }, { "equiv", 0x2261 },
166  { "le", 0x2264 }, { "ge", 0x2265 },
167  { "sub", 0x2282 }, { "sup", 0x2283 },
168  { "nsub", 0x2284 }, { "sube", 0x2286 },
169  { "supe", 0x2287 }, { "oplus", 0x2295 },
170  { "otimes", 0x2297 }, { "perp", 0x22A5 },
171  { "sdot", 0x22C5 }, { "lceil", 0x2308 },
172  { "rceil", 0x2309 }, { "lfloor", 0x230A },
173  { "rfloor", 0x230B }, { "lang", 0x2329 },
174  { "rang", 0x232A }, { "loz", 0x25CA },
175  { "spades", 0x2660 }, { "clubs", 0x2663 },
176  { "hearts", 0x2665 }, { "diams", 0x2666 }
177  };
178 
179  // Used to convert number to utf-8 char.
180  wstring_convert<codecvt_utf8<char32_t>, char32_t> u8c;
181  // Matches numbered entities between 1 and 8 digits, decimal or hexadecimal,
182  // or named entities.
183  const regex re_entity{"&(#(x)?([[:alnum:]]{1,8})"
184  "|[^;[:space:][:punct:]]+);"};
185  smatch match;
186 
187  while (regex_search(buffer, match, re_entity))
188  {
189  output += match.prefix().str();
190  try
191  {
192  const char32_t codepoint{[&match, &names]
193  {
194  // If it doesn't start with a '#' it is a named entity.
195  if (match[1].str()[0] != '#')
196  {
197  return names.at(match[1].str());
198  }
199  // 'x' after '#' means the number is hexadecimal.
200  if (match[2].length() == 1)
201  {
202  return static_cast<char32_t>(stoul(match[3].str(),
203  nullptr, 16));
204  }
205  // '#' without 'x' means the number is decimal.
206  return static_cast<char32_t>(stoul(match[3].str(),
207  nullptr, 10));
208  }()};
209  output += u8c.to_bytes(codepoint);
210  }
211  catch (const std::out_of_range &) // Named entity could not be found.
212  {
213  output += match.str();
214  }
215  buffer = match.suffix().str();
216  }
217  output += buffer;
218 
219  return output;
220 }
mastodonpp::parametermap
map< string_view, variant< string_view, vector< string_view > >> parametermap
std::map of parameters for API calls.
Definition: types.hpp:64
mastodonpp::unescape_html
string unescape_html(string html)
Replaces HTML entities with UTF-8 characters.
Definition: helpers.cpp:39