Use namespace, fix some warnings.

This commit is contained in:
tastytea 2020-10-27 12:05:50 +01:00
parent fa225ff8e7
commit c0eb7494e1
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
17 changed files with 178 additions and 124 deletions

View File

@ -16,4 +16,9 @@
#include "gitea2rss.hpp"
namespace gitea2rss
{
bool cgi = false;
} // namespace gitea2rss

View File

@ -22,6 +22,9 @@
#include <ctime>
#include <string>
namespace gitea2rss
{
using std::string;
using std::uint8_t;
using std::chrono::system_clock;
@ -35,10 +38,10 @@ string get_http(const string &url);
size_t writer_body(char *data, size_t size, size_t nmemb);
//! Convert time_point to RFC 822 compliant time string.
const string strtime(const system_clock::time_point &timepoint);
string strtime(const system_clock::time_point &timepoint);
//! Convert ISO 8601 time string to RFC 822 time string.
const string strtime(const string &time);
string strtime(const string &time);
//! Write line of XML.
void write_line(uint8_t spaces, const string &tag, const string &value);
@ -53,21 +56,23 @@ uint8_t write_releases(const string &url);
uint8_t write_tags(const string &url);
//! @brief Get the base URL, without trailing slash.
const string get_baseurl(const string &url);
string get_baseurl(const string &url);
//! Get the domain name.
const string get_domain(const string &url);
string get_domain(const string &url);
//! Get the full name of the repo (user/project).
const string get_repo(const string &url);
string get_repo(const string &url);
//! Get the project name.
const string get_project(const string &url);
string get_project(const string &url);
//! Escape some characters to named HTML entities.
const string escape_some_html(string html);
string escape_some_html(string html);
//! Return environment variable or "" if it is not set.
string get_env_var(const string &variable);
} // namespace gitea2rss
#endif // GITEA2RSS_HPP

View File

@ -22,6 +22,9 @@
#include <stdexcept>
#include <string>
namespace gitea2rss
{
using std::cerr;
using std::cout;
using std::endl;
@ -49,6 +52,7 @@ string get_http(const string &url)
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
curl_easy_setopt(connection, CURLOPT_WRITEFUNCTION, writer_body);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
CURLcode code{curl_easy_setopt(connection, CURLOPT_FOLLOWLOCATION, 1L)};
if (code != CURLE_OK)
{
@ -57,9 +61,9 @@ string get_http(const string &url)
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
curl_easy_setopt(connection, CURLOPT_MAXREDIRS, 5L);
code =
curl_easy_setopt(connection, CURLOPT_USERAGENT,
(string("gitea2rss/") += global::version).c_str());
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
code = curl_easy_setopt(connection, CURLOPT_USERAGENT,
(string("gitea2rss/") += version).c_str());
if (code != CURLE_OK)
{
throw runtime_error{"Failed to set User-Agent."};
@ -68,6 +72,7 @@ string get_http(const string &url)
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
curl_easy_setopt(connection, CURLOPT_HTTPGET, 1L);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
code = curl_easy_setopt(connection, CURLOPT_URL, url.c_str());
if (code != CURLE_OK)
{
@ -116,3 +121,5 @@ size_t writer_body(char *data, size_t size, size_t nmemb)
return size * nmemb;
}
} // namespace gitea2rss

View File

@ -21,9 +21,8 @@
using std::cerr;
using std::cout;
using std::endl;
using std::string;
using std::chrono::system_clock;
using namespace gitea2rss;
int main(int argc, char *argv[])
{
@ -66,15 +65,18 @@ int main(int argc, char *argv[])
}
else if (argc < 2)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
cerr << "usage: " << argv[0]
<< " URL of Gitea project [releases|tags]\n";
return 1;
}
else
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
url = argv[1];
if (argc > 2)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
type = argv[2];
}
}

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,22 +14,25 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <sstream>
#include <json/json.h>
#include "gitea2rss.hpp"
#include <iostream>
#include <json/json.h>
#include <sstream>
using std::cout;
using std::cerr;
using std::cout;
using std::endl;
using std::stringstream;
namespace gitea2rss
{
uint8_t write_releases(const string &url)
{
const string baseurl = get_baseurl(url);
const string repo = get_repo(url);
stringstream data(get_http(baseurl + "/api/v1/repos/"
+ repo + "/releases"));
stringstream data(
get_http(baseurl + "/api/v1/repos/" + repo + "/releases"));
if (cgi)
{
@ -53,19 +56,25 @@ uint8_t write_releases(const string &url)
const string body = escape_some_html(release["body"].asString());
cout << " <item>\n";
write_line(6, "title", get_project(url) + ": "
+ release["name"].asString());
write_line(6, "link", baseurl + "/" + repo + "/releases");
write_line(6, "title",
get_project(url) + ": " + release["name"].asString());
write_line(6, "link", (baseurl + "/" += repo) += "/releases");
write_line(6, "guid isPermaLink=\"false\"",
get_domain(url) + " release " + release["id"].asString());
write_line(6, "pubDate", strtime(release["published_at"].asString()));
write_line(6, "description",
"\n <![CDATA[<p><strong>" + type + "</strong></p>\n"
"<pre>" + body + "</pre>\n"
" <p><a href=\"" + release["tarball_url"].asString()
+ "\">Download tarball</a></p>]]>\n ");
"\n <![CDATA[<p><strong>" + type
+ "</strong></p>\n"
"<pre>"
+ body
+ "</pre>\n"
" <p><a href=\""
+ release["tarball_url"].asString()
+ "\">Download tarball</a></p>]]>\n ");
cout << " </item>\n";
}
return 0;
}
} // namespace gitea2rss

View File

@ -19,38 +19,41 @@
#include <map>
#include <regex>
namespace gitea2rss
{
using std::getenv;
const string get_baseurl(const string &url)
string get_baseurl(const string &url)
{
const size_t pos = url.find('/', 8);
return url.substr(0, pos);
}
const string get_domain(const string &url)
string get_domain(const string &url)
{
const string baseurl = get_baseurl(url);
return baseurl.substr(baseurl.rfind('/') + 1);
}
const string get_repo(const string &url)
string get_repo(const string &url)
{
const size_t pos = url.find('/', 8) + 1;
return url.substr(pos);
}
const string get_project(const string &url)
string get_project(const string &url)
{
const string repo = get_repo(url);
return repo.substr(repo.find('/') + 1);
}
const string escape_some_html(string html)
string escape_some_html(string html)
{
const std::map<const char, const string> names = {{'<', "&lt;"},
{'>', "&gt;"}};
for (auto &pair : names)
for (const auto &pair : names)
{
const std::regex re(string(1, pair.first));
html = std::regex_replace(html, re, pair.second);
@ -68,3 +71,5 @@ string get_env_var(const string &variable)
}
return "";
}
} // namespace gitea2rss

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,13 +14,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <sstream>
#include <json/json.h>
#include "gitea2rss.hpp"
#include <iostream>
#include <json/json.h>
#include <sstream>
namespace gitea2rss
{
using std::cout;
using std::cerr;
using std::cout;
using std::endl;
using std::stringstream;
@ -28,8 +31,8 @@ uint8_t write_tags(const string &url)
{
const string baseurl = get_baseurl(url);
const string repo = get_repo(url);
stringstream data(get_http(baseurl + "/api/v1/repos/"
+ repo + "/git/refs"));
stringstream data(
get_http(baseurl + "/api/v1/repos/" + repo + "/git/refs"));
if (cgi)
{
@ -59,18 +62,23 @@ uint8_t write_tags(const string &url)
cout << " <item>\n";
write_line(6, "title", get_project(url) + ": " + name);
write_line(6, "link", baseurl + "/" + repo + "/src/tag/" + name);
write_line(6, "link", baseurl + "/" += repo + "/src/tag/" += name);
write_line(6, "guid isPermaLink=\"false\"",
get_domain(url) + " tag " + sha);
write_line(6, "description",
"\n <![CDATA[<p><strong>" + name + "</strong> "
"on commit " + sha + "</p>\n"
" <p><a href=\""
+ baseurl + "/" + repo + "/archive/" + name + ".tar.gz"
+ "\">Download tarball</a></p>]]>\n ");
"\n <![CDATA[<p><strong>" + name
+ "</strong> "
"on commit "
+ sha
+ "</p>\n"
" <p><a href=\""
+ baseurl + "/" + repo + "/archive/" + name + ".tar.gz"
+ "\">Download tarball</a></p>]]>\n ");
cout << " </item>\n";
}
return 0;
}
} // namespace gitea2rss

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,25 +14,29 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iomanip>
#include "gitea2rss.hpp"
#include <iomanip>
const string strtime(const system_clock::time_point &timepoint)
namespace gitea2rss
{
string strtime(const system_clock::time_point &timepoint)
{
constexpr uint16_t bufsize = 1024;
std::time_t time = system_clock::to_time_t(timepoint);
std::tm *tm;
tm = std::gmtime(&time);
std::tm *tm{std::gmtime(&time)};
char buffer[bufsize];
std::strftime(buffer, bufsize, "%a, %d %b %Y %T %z", tm);
return static_cast<const string>(buffer);
}
const string strtime(const string &time)
string strtime(const string &time)
{
std::tm tm = {};
tm.tm_isdst = -1; // Detect daylight saving time.
tm.tm_isdst = -1; // Detect daylight saving time.
std::stringstream ss(time);
ss >> std::get_time(&tm, "%Y-%m-%dT%T"); // Assume time is UTC.
return strtime(system_clock::from_time_t(timegm(&tm)));
}
} // namespace gitea2rss

View File

@ -1,9 +1,11 @@
#ifndef VERSION_HPP
#define VERSION_HPP
namespace global
namespace gitea2rss
{
static constexpr char version[] = "@PROJECT_VERSION@";
}
static constexpr char version[] = "@PROJECT_VERSION@";
} // namespace gitea2rss
#endif // VERSION_HPP

View File

@ -20,8 +20,10 @@
#include <iostream>
#include <regex>
namespace gitea2rss
{
using std::cout;
using std::getenv;
void write_line(const uint8_t spaces, const string &tag, const string &value)
{
@ -74,6 +76,8 @@ void write_preamble(const string &url, const string &type)
write_line(4, "title", get_project(url) + " " + type);
write_line(4, "link", url);
write_line(4, "description", "List of " + type + " of " + get_repo(url));
write_line(4, "generator", string("gitea2rss ") + global::version);
write_line(4, "generator", string("gitea2rss ") + version);
write_line(4, "lastBuildDate", strtime(system_clock::now()));
}
} // namespace gitea2rss

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,23 +14,23 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <catch.hpp>
#include "gitea2rss.hpp"
#include <catch.hpp>
#include <string>
using std::string;
using namespace gitea2rss;
SCENARIO ("escape_some_html() works as expected", "[strings]")
SCENARIO("escape_some_html() works as expected", "[strings]")
{
WHEN ("String with escape-able characters")
WHEN("String with escape-able characters")
{
const string escaped
= escape_some_html("<p><small>.</small></p>");
const string escaped = escape_some_html("<p><small>.</small></p>");
THEN ("The HTML is escaped")
THEN("The HTML is escaped")
{
REQUIRE(escaped ==
"&lt;p&gt;&lt;small&gt;.&lt;/small&gt;&lt;/p&gt;");
REQUIRE(escaped
== "&lt;p&gt;&lt;small&gt;.&lt;/small&gt;&lt;/p&gt;");
}
}
}

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,31 +14,32 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <catch.hpp>
#include "gitea2rss.hpp"
#include <catch.hpp>
#include <string>
using std::string;
using namespace gitea2rss;
SCENARIO ("get_baseurl() works as expected", "[strings]")
SCENARIO("get_baseurl() works as expected", "[strings]")
{
WHEN ("HTTPS URL")
WHEN("HTTPS URL")
{
const string baseurl
= get_baseurl("https://git.example.com/user/project");
const string baseurl =
get_baseurl("https://git.example.com/user/project");
THEN ("The base URL is correctly returned")
THEN("The base URL is correctly returned")
{
REQUIRE(baseurl == "https://git.example.com");
}
}
WHEN ("HTTP URL")
WHEN("HTTP URL")
{
const string baseurl
= get_baseurl("http://git.example.com/user/project");
const string baseurl =
get_baseurl("http://git.example.com/user/project");
THEN ("The base URL is correctly returned")
THEN("The base URL is correctly returned")
{
REQUIRE(baseurl == "http://git.example.com");
}

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,20 +14,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <catch.hpp>
#include "gitea2rss.hpp"
#include <catch.hpp>
#include <string>
using std::string;
using namespace gitea2rss;
SCENARIO ("get_domain() works as expected", "[strings]")
SCENARIO("get_domain() works as expected", "[strings]")
{
WHEN ("HTTPS URL is valid")
WHEN("HTTPS URL is valid")
{
const string domain
= get_domain("https://git.example.com/user/project");
const string domain =
get_domain("https://git.example.com/user/project");
THEN ("The domain is correctly returned")
THEN("The domain is correctly returned")
{
REQUIRE(domain == "git.example.com");
}

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,29 +14,30 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <catch.hpp>
#include "gitea2rss.hpp"
#include <catch.hpp>
#include <string>
using std::string;
using namespace gitea2rss;
SCENARIO ("get_http() works as expected", "[http]")
SCENARIO("get_http() works as expected", "[http]")
{
WHEN ("URL is valid")
WHEN("URL is valid")
{
const string answer = get_http("https://ip.tastytea.de/");
THEN ("Answer is not empty")
THEN("Answer is not empty")
{
REQUIRE(answer != "");
}
}
WHEN ("URL is invalid")
WHEN("URL is invalid")
{
const string answer = get_http("https://url.invalid/");
THEN ("Answer is empty")
THEN("Answer is empty")
{
REQUIRE(answer == "");
}

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,20 +14,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <catch.hpp>
#include "gitea2rss.hpp"
#include <catch.hpp>
#include <string>
using std::string;
using namespace gitea2rss;
SCENARIO ("get_project() works as expected", "[strings]")
SCENARIO("get_project() works as expected", "[strings]")
{
WHEN ("HTTPS URL is valid")
WHEN("HTTPS URL is valid")
{
const string project
= get_project("https://git.example.com/user/project");
const string project =
get_project("https://git.example.com/user/project");
THEN ("The project is correctly returned")
THEN("The project is correctly returned")
{
REQUIRE(project == "project");
}

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,20 +14,20 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <catch.hpp>
#include "gitea2rss.hpp"
#include <catch.hpp>
#include <string>
using std::string;
using namespace gitea2rss;
SCENARIO ("get_repo() works as expected", "[strings]")
SCENARIO("get_repo() works as expected", "[strings]")
{
WHEN ("HTTPS URL is valid")
WHEN("HTTPS URL is valid")
{
const string repo
= get_repo("https://git.example.com/user/project");
const string repo = get_repo("https://git.example.com/user/project");
THEN ("The repo is correctly returned")
THEN("The repo is correctly returned")
{
REQUIRE(repo == "user/project");
}

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019 tastytea <tastytea@tastytea.de>
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -14,33 +14,32 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <chrono>
#include <catch.hpp>
#include "gitea2rss.hpp"
#include <catch.hpp>
#include <chrono>
#include <string>
using std::string;
using namespace gitea2rss;
SCENARIO ("strtime() works as expected", "[time]")
SCENARIO("strtime() works as expected", "[time]")
{
GIVEN ("Time as time_point")
GIVEN("Time as time_point")
{
std::chrono::system_clock::time_point tp;
const string time
= strtime(tp);
const string time = strtime(tp);
THEN ("The time is returned in the correct format")
THEN("The time is returned in the correct format")
{
REQUIRE(time == "Thu, 01 Jan 1970 00:00:00 +0000");
}
}
GIVEN ("Time as ISO 8601 string")
GIVEN("Time as ISO 8601 string")
{
const string time
= strtime("1970-01-01T00:00:00+0000");
const string time = strtime("1970-01-01T00:00:00+0000");
THEN ("The time is returned in the correct format")
THEN("The time is returned in the correct format")
{
REQUIRE(time == "Thu, 01 Jan 1970 00:00:00 +0000");
}