add initial bulk run from pre-commit over all files
This commit is contained in:
258
src/util.cpp
258
src/util.cpp
@@ -34,48 +34,46 @@
|
||||
#ifdef FREEBSD
|
||||
#define _WITH_GETLINE
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <Lexer.h>
|
||||
#include <main.h>
|
||||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/select.h>
|
||||
#include <Lexer.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unicode.h>
|
||||
#include <unistd.h>
|
||||
#include <utf8.h>
|
||||
#include <util.h>
|
||||
#include <main.h>
|
||||
|
||||
#define STRING_UTIL_CONFIRM_YES "yes"
|
||||
#define STRING_UTIL_CONFIRM_YES_U "Yes"
|
||||
#define STRING_UTIL_CONFIRM_NO "no"
|
||||
#define STRING_UTIL_CONFIRM_ALL "all"
|
||||
#define STRING_UTIL_CONFIRM_ALL_U "All"
|
||||
#define STRING_UTIL_CONFIRM_QUIT "quit"
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#define STRING_UTIL_CONFIRM_YES "yes"
|
||||
#define STRING_UTIL_CONFIRM_YES_U "Yes"
|
||||
#define STRING_UTIL_CONFIRM_NO "no"
|
||||
#define STRING_UTIL_CONFIRM_ALL "all"
|
||||
#define STRING_UTIL_CONFIRM_ALL_U "All"
|
||||
#define STRING_UTIL_CONFIRM_QUIT "quit"
|
||||
|
||||
static const char* newline = "\n";
|
||||
static const char* noline = "";
|
||||
static const char* noline = "";
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
static void signal_handler (int s)
|
||||
{
|
||||
if (s == SIGINT)
|
||||
{
|
||||
static void signal_handler(int s) {
|
||||
if (s == SIGINT) {
|
||||
std::cout << "\n\nInterrupted: No changes made.\n";
|
||||
exit (1);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,45 +82,38 @@ static void signal_handler (int s)
|
||||
// 1 = yes
|
||||
// 2 = all
|
||||
// 3 = quit
|
||||
int confirm4 (const std::string& question)
|
||||
{
|
||||
std::vector <std::string> options {STRING_UTIL_CONFIRM_YES_U,
|
||||
STRING_UTIL_CONFIRM_YES,
|
||||
STRING_UTIL_CONFIRM_NO,
|
||||
STRING_UTIL_CONFIRM_ALL_U,
|
||||
STRING_UTIL_CONFIRM_ALL,
|
||||
STRING_UTIL_CONFIRM_QUIT};
|
||||
std::vector <std::string> matches;
|
||||
int confirm4(const std::string& question) {
|
||||
std::vector<std::string> options{STRING_UTIL_CONFIRM_YES_U, STRING_UTIL_CONFIRM_YES,
|
||||
STRING_UTIL_CONFIRM_NO, STRING_UTIL_CONFIRM_ALL_U,
|
||||
STRING_UTIL_CONFIRM_ALL, STRING_UTIL_CONFIRM_QUIT};
|
||||
std::vector<std::string> matches;
|
||||
|
||||
signal (SIGINT, signal_handler);
|
||||
signal(SIGINT, signal_handler);
|
||||
|
||||
do
|
||||
{
|
||||
std::cout << question
|
||||
<< " ("
|
||||
<< options[1] << '/'
|
||||
<< options[2] << '/'
|
||||
<< options[4] << '/'
|
||||
<< options[5]
|
||||
<< ") ";
|
||||
do {
|
||||
std::cout << question << " (" << options[1] << '/' << options[2] << '/' << options[4] << '/'
|
||||
<< options[5] << ") ";
|
||||
|
||||
std::string answer {""};
|
||||
std::getline (std::cin, answer);
|
||||
Context::getContext ().debug ("STDIN '" + answer + '\'');
|
||||
answer = std::cin.eof () ? STRING_UTIL_CONFIRM_QUIT : Lexer::lowerCase (Lexer::trim (answer));
|
||||
autoComplete (answer, options, matches, 1); // Hard-coded 1.
|
||||
}
|
||||
while (! std::cin.eof () && matches.size () != 1);
|
||||
std::string answer{""};
|
||||
std::getline(std::cin, answer);
|
||||
Context::getContext().debug("STDIN '" + answer + '\'');
|
||||
answer = std::cin.eof() ? STRING_UTIL_CONFIRM_QUIT : Lexer::lowerCase(Lexer::trim(answer));
|
||||
autoComplete(answer, options, matches, 1); // Hard-coded 1.
|
||||
} while (!std::cin.eof() && matches.size() != 1);
|
||||
|
||||
signal (SIGINT, SIG_DFL);
|
||||
signal(SIGINT, SIG_DFL);
|
||||
|
||||
if (matches.size () == 1)
|
||||
{
|
||||
if (matches[0] == STRING_UTIL_CONFIRM_YES_U) return 1;
|
||||
else if (matches[0] == STRING_UTIL_CONFIRM_YES) return 1;
|
||||
else if (matches[0] == STRING_UTIL_CONFIRM_ALL_U) return 2;
|
||||
else if (matches[0] == STRING_UTIL_CONFIRM_ALL) return 2;
|
||||
else if (matches[0] == STRING_UTIL_CONFIRM_QUIT) return 3;
|
||||
if (matches.size() == 1) {
|
||||
if (matches[0] == STRING_UTIL_CONFIRM_YES_U)
|
||||
return 1;
|
||||
else if (matches[0] == STRING_UTIL_CONFIRM_YES)
|
||||
return 1;
|
||||
else if (matches[0] == STRING_UTIL_CONFIRM_ALL_U)
|
||||
return 2;
|
||||
else if (matches[0] == STRING_UTIL_CONFIRM_ALL)
|
||||
return 2;
|
||||
else if (matches[0] == STRING_UTIL_CONFIRM_QUIT)
|
||||
return 3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -135,16 +126,15 @@ int confirm4 (const std::string& question)
|
||||
// For the implementation details, refer to
|
||||
// https://svnweb.freebsd.org/base/head/sys/kern/kern_uuid.c
|
||||
#if defined(FREEBSD) || defined(OPENBSD)
|
||||
const std::string uuid ()
|
||||
{
|
||||
const std::string uuid() {
|
||||
uuid_t id;
|
||||
uint32_t status;
|
||||
char *buffer (0);
|
||||
uuid_create (&id, &status);
|
||||
uuid_to_string (&id, &buffer, &status);
|
||||
char* buffer(0);
|
||||
uuid_create(&id, &status);
|
||||
uuid_to_string(&id, &buffer, &status);
|
||||
|
||||
std::string res (buffer);
|
||||
free (buffer);
|
||||
std::string res(buffer);
|
||||
free(buffer);
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -153,26 +143,24 @@ const std::string uuid ()
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef HAVE_UUID_UNPARSE_LOWER
|
||||
// Older versions of libuuid don't have uuid_unparse_lower(), only uuid_unparse()
|
||||
void uuid_unparse_lower (uuid_t uu, char *out)
|
||||
{
|
||||
uuid_unparse (uu, out);
|
||||
// Characters in out are either 0-9, a-z, '-', or A-Z. A-Z is mapped to
|
||||
// a-z by bitwise or with 0x20, and the others already have this bit set
|
||||
for (size_t i = 0; i < 36; ++i) out[i] |= 0x20;
|
||||
void uuid_unparse_lower(uuid_t uu, char* out) {
|
||||
uuid_unparse(uu, out);
|
||||
// Characters in out are either 0-9, a-z, '-', or A-Z. A-Z is mapped to
|
||||
// a-z by bitwise or with 0x20, and the others already have this bit set
|
||||
for (size_t i = 0; i < 36; ++i) out[i] |= 0x20;
|
||||
}
|
||||
#endif
|
||||
|
||||
const std::string uuid ()
|
||||
{
|
||||
const std::string uuid() {
|
||||
uuid_t id;
|
||||
uuid_generate (id);
|
||||
char buffer[100] {};
|
||||
uuid_unparse_lower (id, buffer);
|
||||
uuid_generate(id);
|
||||
char buffer[100]{};
|
||||
uuid_unparse_lower(id, buffer);
|
||||
|
||||
// Bug found by Steven de Brouwer.
|
||||
buffer[36] = '\0';
|
||||
|
||||
return std::string (buffer);
|
||||
return std::string(buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -201,19 +189,15 @@ const std::string uuid ()
|
||||
// - delimiter is the character used to split up projects into subprojects.
|
||||
// - defaults to the period, '.'
|
||||
//
|
||||
const std::string indentProject (
|
||||
const std::string& project,
|
||||
const std::string& whitespace /* = " " */,
|
||||
char delimiter /* = '.' */)
|
||||
{
|
||||
const std::string indentProject(const std::string& project,
|
||||
const std::string& whitespace /* = " " */,
|
||||
char delimiter /* = '.' */) {
|
||||
// Count the delimiters in *i.
|
||||
std::string prefix = "";
|
||||
std::string::size_type pos = 0;
|
||||
std::string::size_type lastpos = 0;
|
||||
while ((pos = project.find (delimiter, pos + 1)) != std::string::npos)
|
||||
{
|
||||
if (pos != project.size () - 1)
|
||||
{
|
||||
while ((pos = project.find(delimiter, pos + 1)) != std::string::npos) {
|
||||
if (pos != project.size() - 1) {
|
||||
prefix += whitespace;
|
||||
lastpos = pos;
|
||||
}
|
||||
@@ -223,23 +207,19 @@ const std::string indentProject (
|
||||
if (lastpos == 0)
|
||||
child = project;
|
||||
else
|
||||
child = project.substr (lastpos + 1);
|
||||
child = project.substr(lastpos + 1);
|
||||
|
||||
return prefix + child;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
const std::vector <std::string> extractParents (
|
||||
const std::string& project,
|
||||
const char& delimiter /* = '.' */)
|
||||
{
|
||||
std::vector <std::string> vec;
|
||||
const std::vector<std::string> extractParents(const std::string& project,
|
||||
const char& delimiter /* = '.' */) {
|
||||
std::vector<std::string> vec;
|
||||
std::string::size_type pos = 0;
|
||||
std::string::size_type copyUntil = 0;
|
||||
while ((copyUntil = project.find (delimiter, pos + 1)) != std::string::npos)
|
||||
{
|
||||
if (copyUntil != project.size () - 1)
|
||||
vec.push_back (project.substr (0, copyUntil));
|
||||
while ((copyUntil = project.find(delimiter, pos + 1)) != std::string::npos) {
|
||||
if (copyUntil != project.size() - 1) vec.push_back(project.substr(0, copyUntil));
|
||||
pos = copyUntil;
|
||||
}
|
||||
return vec;
|
||||
@@ -247,75 +227,63 @@ const std::vector <std::string> extractParents (
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef HAVE_TIMEGM
|
||||
time_t timegm (struct tm *tm)
|
||||
{
|
||||
time_t timegm(struct tm* tm) {
|
||||
time_t ret;
|
||||
char *tz;
|
||||
tz = getenv ("TZ");
|
||||
setenv ("TZ", "UTC", 1);
|
||||
tzset ();
|
||||
ret = mktime (tm);
|
||||
char* tz;
|
||||
tz = getenv("TZ");
|
||||
setenv("TZ", "UTC", 1);
|
||||
tzset();
|
||||
ret = mktime(tm);
|
||||
if (tz)
|
||||
setenv ("TZ", tz, 1);
|
||||
setenv("TZ", tz, 1);
|
||||
else
|
||||
unsetenv ("TZ");
|
||||
tzset ();
|
||||
unsetenv("TZ");
|
||||
tzset();
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool nontrivial (const std::string& input)
|
||||
{
|
||||
bool nontrivial(const std::string& input) {
|
||||
std::string::size_type i = 0;
|
||||
int character;
|
||||
while ((character = utf8_next_char (input, i)))
|
||||
if (! unicodeWhitespace (character))
|
||||
return true;
|
||||
while ((character = utf8_next_char(input, i)))
|
||||
if (!unicodeWhitespace(character)) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
const char* optionalBlankLine ()
|
||||
{
|
||||
return Context::getContext ().verbose ("blank") ? newline : noline;
|
||||
const char* optionalBlankLine() {
|
||||
return Context::getContext().verbose("blank") ? newline : noline;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void setHeaderUnderline (Table& table)
|
||||
{
|
||||
void setHeaderUnderline(Table& table) {
|
||||
// If an alternating row color is specified, notify the table.
|
||||
if (Context::getContext ().color ())
|
||||
{
|
||||
Color alternate (Context::getContext ().config.get ("color.alternate"));
|
||||
table.colorOdd (alternate);
|
||||
table.intraColorOdd (alternate);
|
||||
if (Context::getContext().color()) {
|
||||
Color alternate(Context::getContext().config.get("color.alternate"));
|
||||
table.colorOdd(alternate);
|
||||
table.intraColorOdd(alternate);
|
||||
|
||||
if (Context::getContext ().config.getBoolean ("fontunderline"))
|
||||
{
|
||||
table.colorHeader (Color ("underline " + Context::getContext ().config.get ("color.label")));
|
||||
if (Context::getContext().config.getBoolean("fontunderline")) {
|
||||
table.colorHeader(Color("underline " + Context::getContext().config.get("color.label")));
|
||||
} else {
|
||||
table.colorHeader(Color(Context::getContext().config.get("color.label")));
|
||||
table.underlineHeaders();
|
||||
}
|
||||
} else {
|
||||
if (Context::getContext().config.getBoolean("fontunderline"))
|
||||
table.colorHeader(Color("underline"));
|
||||
else
|
||||
{
|
||||
table.colorHeader (Color (Context::getContext ().config.get ("color.label")));
|
||||
table.underlineHeaders ();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Context::getContext ().config.getBoolean ("fontunderline"))
|
||||
table.colorHeader (Color ("underline"));
|
||||
else
|
||||
table.underlineHeaders ();
|
||||
table.underlineHeaders();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Perform strtol on a string and check if the extracted value matches.
|
||||
//
|
||||
bool extractLongInteger (const std::string& input, long& output)
|
||||
{
|
||||
output = strtol (input.c_str (), nullptr, 10);
|
||||
return (format ("{1}", output) == input);
|
||||
bool extractLongInteger(const std::string& input, long& output) {
|
||||
output = strtol(input.c_str(), nullptr, 10);
|
||||
return (format("{1}", output) == input);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user