[debt] include cleanup

This commit is contained in:
Timothy Stack 2020-10-28 21:23:46 -07:00
parent 0dff822ade
commit 5e42b4cb8a
43 changed files with 258 additions and 277 deletions

View File

@ -29,8 +29,8 @@
* @file ansi_scrubber.hh
*/
#ifndef _ansi_scrubber_hh
#define _ansi_scrubber_hh
#ifndef lnav_ansi_scrubber_hh
#define lnav_ansi_scrubber_hh
#include <map>
#include <string>

View File

@ -1,5 +1,3 @@
#include <utility>
/**
* Copyright (c) 2017, Timothy Stack
*

View File

@ -29,6 +29,7 @@
#include "config.h"
#include "lnav_log.hh"
#include "is_utf8.hh"
#include "string_util.hh"
@ -49,3 +50,42 @@ void scrub_to_utf8(char *buffer, size_t length)
}
}
}
size_t unquote(char *dst, const char *str, size_t len)
{
if (str[0] == 'r' || str[0] == 'u') {
str += 1;
len -= 1;
}
char quote_char = str[0];
size_t index = 0;
require(str[0] == '\'' || str[0] == '"');
for (size_t lpc = 1; lpc < (len - 1); lpc++, index++) {
dst[index] = str[lpc];
if (str[lpc] == quote_char) {
lpc += 1;
}
else if (str[lpc] == '\\' && (lpc + 1) < len) {
switch (str[lpc + 1]) {
case 'n':
dst[index] = '\n';
break;
case 'r':
dst[index] = '\r';
break;
case 't':
dst[index] = '\t';
break;
default:
dst[index] = str[lpc + 1];
break;
}
lpc += 1;
}
}
dst[index] = '\0';
return index;
}

View File

@ -30,10 +30,46 @@
#ifndef lnav_string_util_hh
#define lnav_string_util_hh
#include <string.h>
#include <string>
void scrub_to_utf8(char *buffer, size_t length);
inline bool is_line_ending(char ch) {
return ch == '\r' || ch == '\n';
}
size_t unquote(char *dst, const char *str, size_t len);
inline bool startswith(const char *str, const char *prefix)
{
return strncmp(str, prefix, strlen(prefix)) == 0;
}
inline bool startswith(const std::string &str, const char *prefix)
{
return startswith(str.c_str(), prefix);
}
inline bool endswith(const char *str, const char *suffix)
{
size_t len = strlen(str), suffix_len = strlen(suffix);
if (suffix_len > len) {
return false;
}
return strcmp(&str[len - suffix_len], suffix) == 0;
}
template<int N>
inline bool endswith(const std::string& str, const char (&suffix) [N])
{
if (N > str.length()) {
return false;
}
return strcmp(&str[str.size() - N], suffix) == 0;
}
#endif

View File

@ -1,5 +1,3 @@
#include <utility>
/**
* Copyright (c) 2007-2012, Timothy Stack
*
@ -176,8 +174,8 @@ public:
};
static bookmark_type_t *find_type(const std::string &name) {
type_iterator iter = find_if(type_begin(), type_end(), mark_eq(name));
bookmark_type_t *retval = NULL;
auto iter = find_if(type_begin(), type_end(), mark_eq(name));
bookmark_type_t *retval = nullptr;
if (iter != type_end()) {
retval = (*iter);

View File

@ -27,8 +27,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _bottom_status_source_hh
#define _bottom_status_source_hh
#ifndef lnav_bottom_status_source_hh
#define lnav_bottom_status_source_hh
#include <string>
@ -58,7 +58,7 @@ public:
bottom_status_source();
virtual ~bottom_status_source() { };
virtual ~bottom_status_source() = default;
lv_functor_t line_number_wire;
lv_functor_t percent_wire;

View File

@ -31,6 +31,11 @@
#include "config.h"
#include <algorithm>
#include "sql_util.hh"
#include "lnav_util.hh"
#include "base/lnav_log.hh"
#include "column_namer.hh"
@ -42,15 +47,15 @@ bool column_namer::existing_name(const std::string &in_name) const
return true;
}
if (find(this->cn_builtin_names.begin(),
this->cn_builtin_names.end(),
in_name) != this->cn_builtin_names.end()) {
if (std::find(this->cn_builtin_names.begin(),
this->cn_builtin_names.end(),
in_name) != this->cn_builtin_names.end()) {
return true;
}
if (find(this->cn_names.begin(),
this->cn_names.end(),
in_name) != this->cn_names.end()) {
if (std::find(this->cn_names.begin(),
this->cn_names.end(),
in_name) != this->cn_names.end()) {
return true;
}

View File

@ -32,21 +32,13 @@
#ifndef _column_namer_hh
#define _column_namer_hh
#include <map>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include "sql_util.hh"
#include "lnav_util.hh"
class column_namer {
public:
column_namer()
{
this->cn_builtin_names.emplace_back("col");
};
column_namer() : cn_builtin_names({"col"}) {}
bool existing_name(const std::string &in_name) const;

View File

@ -39,7 +39,6 @@
#include "optional.hpp"
#include "auto_fd.hh"
#include "attr_line.hh"
#include "textview_curses.hh"
#include "shlex.hh"
struct exec_context;

View File

@ -1,5 +1,3 @@
#include <utility>
/**
* Copyright (c) 2007-2012, Timothy Stack
*

View File

@ -31,25 +31,23 @@
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "auto_mem.hh"
#include "base/lnav_log.hh"
#include "sql_util.hh"
#include "environ_vtab.hh"
using namespace std;
extern char **environ;
const char *ENVIRON_CREATE_STMT = "\
-- Access lnav's environment variables through this table.\n\
CREATE TABLE environ (\n\
name text PRIMARY KEY,\n\
value text\n\
);\n\
";
const char *ENVIRON_CREATE_STMT = R"(
-- Access lnav's environment variables through this table.
CREATE TABLE environ (
name text PRIMARY KEY,
value text
);
)";
struct vtab {
sqlite3_vtab base;

View File

@ -29,14 +29,11 @@
#include "config.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "lnav.hh"
#include "auto_mem.hh"
#include "base/lnav_log.hh"
#include "sql_util.hh"
#include "file_vtab.hh"
#include "session_data.hh"
#include "vtab_module.hh"

View File

@ -30,14 +30,11 @@
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <glob.h>
#include <grp.h>
#include <pwd.h>
#include "auto_mem.hh"
#include "base/lnav_log.hh"
#include "sql_util.hh"
#include "vtab_module.hh"

View File

@ -29,13 +29,11 @@
#include "config.h"
#include <numeric>
#include <algorithm>
#include <pcrecpp.h>
#include "fmt/format.h"
#include "lnav_util.hh"
#include "ansi_scrubber.hh"
#include "help_text_formatter.hh"
#include "readline_highlighters.hh"

View File

@ -54,7 +54,7 @@
#include "lnav_util.hh"
template<typename A>
static void to_key_seq(A &dst, char *src)
static void to_key_seq(A &dst, const char *src)
{
dst[0] = '\0';
for (size_t lpc = 0; src[lpc]; lpc++) {

View File

@ -31,10 +31,7 @@
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdint.h>
#include <string>

View File

@ -83,6 +83,7 @@
#include "help-txt.h"
#include "init-sql.h"
#include "logfile.hh"
#include "base/string_util.hh"
#include "base/lnav_log.hh"
#include "log_accel.hh"
#include "lnav_util.hh"

View File

@ -41,6 +41,7 @@
#include <pcrecpp.h>
#include <yajl/api/yajl_tree.h>
#include "base/string_util.hh"
#include "lnav.hh"
#include "lnav_config.hh"
#include "lnav_util.hh"

View File

@ -48,6 +48,7 @@
#include "pcrecpp.h"
#include "auto_fd.hh"
#include "base/string_util.hh"
#include "base/lnav_log.hh"
#include "lnav_util.hh"
#include "auto_mem.hh"
@ -55,7 +56,6 @@
#include "lnav_config.hh"
#include "yajlpp/yajlpp.hh"
#include "yajlpp/yajlpp_def.hh"
#include "shlex.hh"
#include "styling.hh"
#include "bin2c.h"
#include "default-config.h"

View File

@ -29,8 +29,8 @@
* @file lnav_config.hh
*/
#ifndef _lnav_config_hh
#define _lnav_config_hh
#ifndef lnav_config_hh
#define lnav_config_hh
#include <map>
#include <string>

View File

@ -99,45 +99,6 @@ std::string hash_bytes(const char *str1, size_t s1len, ...)
return hash.to_string();
}
size_t unquote(char *dst, const char *str, size_t len)
{
if (str[0] == 'r' || str[0] == 'u') {
str += 1;
len -= 1;
}
char quote_char = str[0];
size_t index = 0;
require(str[0] == '\'' || str[0] == '"');
for (size_t lpc = 1; lpc < (len - 1); lpc++, index++) {
dst[index] = str[lpc];
if (str[lpc] == quote_char) {
lpc += 1;
}
else if (str[lpc] == '\\' && (lpc + 1) < len) {
switch (str[lpc + 1]) {
case 'n':
dst[index] = '\n';
break;
case 'r':
dst[index] = '\r';
break;
case 't':
dst[index] = '\t';
break;
default:
dst[index] = str[lpc + 1];
break;
}
lpc += 1;
}
}
dst[index] = '\0';
return index;
}
std::string time_ago(time_t last_time, bool convert_local)
{
time_t delta, current_time = time(NULL);

View File

@ -99,8 +99,6 @@ inline std::string toupper(const std::string &str)
return toupper(str.c_str());
}
size_t unquote(char *dst, const char *str, size_t len);
#undef rounddown
/**
@ -256,27 +254,6 @@ inline bool is_glob(const char *fn)
bool is_url(const char *fn);
inline bool startswith(const char *str, const char *prefix)
{
return strncmp(str, prefix, strlen(prefix)) == 0;
}
inline bool startswith(const std::string &str, const char *prefix)
{
return startswith(str.c_str(), prefix);
}
inline bool endswith(const char *str, const char *suffix)
{
size_t len = strlen(str), suffix_len = strlen(suffix);
if (suffix_len > len) {
return false;
}
return strcmp(&str[len - suffix_len], suffix) == 0;
}
std::string build_path(const std::vector<ghc::filesystem::path> &paths);
bool read_file(const ghc::filesystem::path &path, std::string &out);

View File

@ -29,8 +29,8 @@
* @file log_data_table.hh
*/
#ifndef _log_data_table_hh
#define _log_data_table_hh
#ifndef lnav_log_data_table_hh
#define lnav_log_data_table_hh
#include <string>
#include <vector>

View File

@ -32,8 +32,8 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <strings.h>
#include "base/string_util.hh"
#include "fmt/format.h"
#include "yajlpp/yajlpp.hh"
#include "yajlpp/yajlpp_def.hh"
@ -98,6 +98,131 @@ struct line_range logline_value::origin_in_full_msg(const char *msg, ssize_t len
return retval;
}
logline_value::logline_value(const intern_string_t name,
logline_value::kind_t kind, shared_buffer_ref &sbr,
bool ident, const scaling_factor *scaling, int col,
int start, int end, bool from_module,
const log_format *format)
: lv_name(name), lv_kind(kind),
lv_identifier(ident), lv_column(col), lv_hidden(false), lv_sub_offset(0),
lv_origin(start, end),
lv_from_module(from_module),
lv_format(format)
{
if (sbr.get_data() == nullptr) {
this->lv_kind = kind = VALUE_NULL;
}
switch (kind) {
case VALUE_JSON:
case VALUE_STRUCT:
case VALUE_TEXT:
case VALUE_QUOTED:
case VALUE_TIMESTAMP:
this->lv_sbr.subset(sbr, start, end - start);
break;
case VALUE_NULL:
break;
case VALUE_INTEGER:
strtonum(this->lv_value.i, sbr.get_data_at(start), end - start);
if (scaling != NULL) {
scaling->scale(this->lv_value.i);
}
break;
case VALUE_FLOAT: {
ssize_t len = end - start;
char scan_value[len + 1];
memcpy(scan_value, sbr.get_data_at(start), len);
scan_value[len] = '\0';
this->lv_value.d = strtod(scan_value, NULL);
if (scaling != NULL) {
scaling->scale(this->lv_value.d);
}
break;
}
case VALUE_BOOLEAN:
if (strncmp(sbr.get_data_at(start), "true", end - start) == 0 ||
strncmp(sbr.get_data_at(start), "yes", end - start) == 0) {
this->lv_value.i = 1;
}
else {
this->lv_value.i = 0;
}
break;
case VALUE_UNKNOWN:
case VALUE__MAX:
ensure(0);
break;
}
}
std::string logline_value::to_string() const
{
char buffer[128];
switch (this->lv_kind) {
case VALUE_NULL:
return "null";
case VALUE_JSON:
case VALUE_STRUCT:
case VALUE_TEXT:
case VALUE_TIMESTAMP:
if (this->lv_sbr.empty()) {
return this->lv_intern_string.to_string();
}
return std::string(this->lv_sbr.get_data(), this->lv_sbr.length());
case VALUE_QUOTED:
if (this->lv_sbr.length() == 0) {
return "";
} else {
switch (this->lv_sbr.get_data()[0]) {
case '\'':
case '"': {
char unquoted_str[this->lv_sbr.length()];
size_t unquoted_len;
unquoted_len = unquote(unquoted_str, this->lv_sbr.get_data(),
this->lv_sbr.length());
return std::string(unquoted_str, unquoted_len);
}
default:
return std::string(this->lv_sbr.get_data(), this->lv_sbr.length());
}
}
case VALUE_INTEGER:
snprintf(buffer, sizeof(buffer), "%" PRId64, this->lv_value.i);
break;
case VALUE_FLOAT:
snprintf(buffer, sizeof(buffer), "%lf", this->lv_value.d);
break;
case VALUE_BOOLEAN:
if (this->lv_value.i) {
return "true";
}
else {
return "false";
}
break;
case VALUE_UNKNOWN:
case VALUE__MAX:
ensure(0);
break;
}
return std::string(buffer);
}
vector<log_format *> log_format::lf_root_formats;
vector<log_format *> &log_format::get_root_formats()

View File

@ -379,126 +379,9 @@ public:
logline_value(const intern_string_t name, kind_t kind, shared_buffer_ref &sbr,
bool ident, const scaling_factor *scaling,
int col, int start, int end, bool from_module=false,
const log_format *format=NULL)
: lv_name(name), lv_kind(kind),
lv_identifier(ident), lv_column(col), lv_hidden(false), lv_sub_offset(0),
lv_origin(start, end),
lv_from_module(from_module),
lv_format(format)
{
if (sbr.get_data() == nullptr) {
this->lv_kind = kind = VALUE_NULL;
}
const log_format *format=NULL);
switch (kind) {
case VALUE_JSON:
case VALUE_STRUCT:
case VALUE_TEXT:
case VALUE_QUOTED:
case VALUE_TIMESTAMP:
this->lv_sbr.subset(sbr, start, end - start);
break;
case VALUE_NULL:
break;
case VALUE_INTEGER:
strtonum(this->lv_value.i, sbr.get_data_at(start), end - start);
if (scaling != NULL) {
scaling->scale(this->lv_value.i);
}
break;
case VALUE_FLOAT: {
ssize_t len = end - start;
char scan_value[len + 1];
memcpy(scan_value, sbr.get_data_at(start), len);
scan_value[len] = '\0';
this->lv_value.d = strtod(scan_value, NULL);
if (scaling != NULL) {
scaling->scale(this->lv_value.d);
}
break;
}
case VALUE_BOOLEAN:
if (strncmp(sbr.get_data_at(start), "true", end - start) == 0 ||
strncmp(sbr.get_data_at(start), "yes", end - start) == 0) {
this->lv_value.i = 1;
}
else {
this->lv_value.i = 0;
}
break;
case VALUE_UNKNOWN:
case VALUE__MAX:
ensure(0);
break;
}
};
const std::string to_string() const
{
char buffer[128];
switch (this->lv_kind) {
case VALUE_NULL:
return "null";
case VALUE_JSON:
case VALUE_STRUCT:
case VALUE_TEXT:
case VALUE_TIMESTAMP:
if (this->lv_sbr.empty()) {
return this->lv_intern_string.to_string();
}
return std::string(this->lv_sbr.get_data(), this->lv_sbr.length());
case VALUE_QUOTED:
if (this->lv_sbr.length() == 0) {
return "";
} else {
switch (this->lv_sbr.get_data()[0]) {
case '\'':
case '"': {
char unquoted_str[this->lv_sbr.length()];
size_t unquoted_len;
unquoted_len = unquote(unquoted_str, this->lv_sbr.get_data(),
this->lv_sbr.length());
return std::string(unquoted_str, unquoted_len);
}
default:
return std::string(this->lv_sbr.get_data(), this->lv_sbr.length());
}
}
case VALUE_INTEGER:
snprintf(buffer, sizeof(buffer), "%" PRId64, this->lv_value.i);
break;
case VALUE_FLOAT:
snprintf(buffer, sizeof(buffer), "%lf", this->lv_value.d);
break;
case VALUE_BOOLEAN:
if (this->lv_value.i) {
return "true";
}
else {
return "false";
}
break;
case VALUE_UNKNOWN:
case VALUE__MAX:
ensure(0);
break;
}
return std::string(buffer);
};
std::string to_string() const;
const char *text_value() const {
if (this->lv_sbr.empty()) {
@ -510,7 +393,7 @@ public:
return this->lv_sbr.get_data();
};
const size_t text_length() const {
size_t text_length() const {
if (this->lv_sbr.empty()) {
return this->lv_intern_string.size();
}

View File

@ -39,10 +39,10 @@
#include <map>
#include <string>
#include <fstream>
#include "fmt/format.h"
#include "base/string_util.hh"
#include "yajlpp/yajlpp.hh"
#include "yajlpp/yajlpp_def.hh"
#include "lnav_config.hh"

View File

@ -29,8 +29,8 @@
* @file log_search_table.hh
*/
#ifndef _log_search_table_hh
#define _log_search_table_hh
#ifndef lnav_log_search_table_hh
#define lnav_log_search_table_hh
#include <string>
#include <vector>

View File

@ -30,6 +30,7 @@
#include "config.h"
#include "base/lnav_log.hh"
#include "base/string_util.hh"
#include "sql_util.hh"
#include "log_vtab_impl.hh"
#include "yajlpp/yajlpp_def.hh"

View File

@ -33,6 +33,7 @@
#include <algorithm>
#include <sqlite3.h>
#include "base/string_util.hh"
#include "k_merge_tree.h"
#include "lnav_util.hh"
#include "log_accel.hh"
@ -87,7 +88,7 @@ static future<string> pretty_pipe_callback(exec_context &ec,
string retval = ss.str();
if (endswith(retval.c_str(), "\n")) {
if (endswith(retval, "\n")) {
retval.resize(retval.length() - 1);
}

View File

@ -29,7 +29,7 @@
#include "config.h"
#include "view_curses.hh"
#include "base/string_util.hh"
#include "pretty_printer.hh"
void pretty_printer::append_to(attr_line_t &al)
@ -168,7 +168,7 @@ void pretty_printer::write_element(const pretty_printer::element &el)
this->pp_stream
<< std::endl
<< result.get_string();
if (!endswith(result.get_string().c_str(), "\n")) {
if (result.empty() || result.get_string().back() != '\n') {
this->pp_stream << std::endl;
}
this->pp_stream

View File

@ -30,19 +30,15 @@
#ifndef pretty_printer_hh
#define pretty_printer_hh
#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>
#include <stack>
#include <deque>
#include <sstream>
#include <iomanip>
#include <utility>
#include "attr_line.hh"
#include "data_scanner.hh"
#include "lnav_util.hh"
class pretty_printer {

View File

@ -55,6 +55,7 @@
#include <string>
#include "base/string_util.hh"
#include "lnav_config.hh"
#include "pcrepp/pcrepp.hh"
#include "shlex.hh"
@ -244,7 +245,7 @@ char *readline_context::completion_generator(const char *text, int state)
cmpfunc(text, &poss_str[1], len) == 0)) {
auto poss_slash_count = std::count(poss.begin(), poss.end(), '/');
if (endswith(poss.c_str(), "/")) {
if (endswith(poss, "/")) {
poss_slash_count -= 1;
}
if (std::count(&text[0], &text[len], '/') == poss_slash_count) {
@ -273,7 +274,7 @@ char *readline_context::completion_generator(const char *text, int state)
}
auto poss_slash_count = std::count(poss_str.begin(), poss_str.end(), '/');
if (endswith(poss.c_str(), "/")) {
if (endswith(poss, "/")) {
poss_slash_count -= 1;
}
if (std::count(&text[0], &text[len], '/') == poss_slash_count) {

View File

@ -31,6 +31,7 @@
#include "config.h"
#include "base/string_util.hh"
#include "pcrepp/pcrepp.hh"
#include "sql_util.hh"
#include "shlex.hh"

View File

@ -29,15 +29,9 @@
#include "config.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "logfile.hh"
#include "auto_mem.hh"
#include "base/lnav_log.hh"
#include "pcrepp/pcrepp.hh"
#include "sql_util.hh"
#include "file_vtab.hh"
#include "vtab_module.hh"
using namespace std;
@ -55,8 +49,6 @@ enum {
};
struct regexp_capture {
using iterator = vector<logfile *>::iterator;
static constexpr const char *CREATE_STMT = R"(
-- The regexp_capture() table-valued function allows you to execute a regular-
-- expression over a given string and get the captured data as rows in a table.

View File

@ -35,7 +35,6 @@
#include <glob.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
#include "spookyhash/SpookyV2.h"

View File

@ -29,8 +29,8 @@
* @file session_data.hh
*/
#ifndef _session_data_hh
#define _session_data_hh
#ifndef lnav_session_data_hh
#define lnav_session_data_hh
#include <map>
#include <string>

View File

@ -19,6 +19,7 @@
#include "pcrepp/pcrepp.hh"
#include "base/string_util.hh"
#include "yajlpp/yajlpp.hh"
#include "column_namer.hh"
#include "yajl/api/yajl_gen.h"
@ -340,7 +341,9 @@ int string_extension_functions(struct FuncDef **basic_funcs,
})
),
sqlite_func_adapter<decltype(&endswith), endswith>::builder(
sqlite_func_adapter<decltype(
static_cast<bool (*)(const char *, const char *)>(&endswith)),
endswith>::builder(
help_text("endswith",
"Test if a string ends with the given suffix")
.sql_function()

View File

@ -32,8 +32,6 @@
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include "strnatcmp.h"

View File

@ -29,8 +29,6 @@
#include "config.h"
#include <stdlib.h>
#include <string>
#include "yajlpp/yajlpp.hh"

View File

@ -36,7 +36,6 @@
#include "shlex.hh"
#include "fmt/format.h"
#include "pcrepp/pcrepp.hh"
#include "lnav_util.hh"
#include "data_parser.hh"
#include "ansi_scrubber.hh"

View File

@ -31,7 +31,6 @@
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdint.h>

View File

@ -39,8 +39,6 @@
#include "view_curses.hh"
#include "ansi_scrubber.hh"
#include "lnav_config.hh"
#include "yajlpp/yajlpp.hh"
#include "yajlpp/yajlpp_def.hh"
#include "attr_line.hh"
#include "shlex.hh"

View File

@ -30,14 +30,8 @@
#include "config.h"
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <utility>
#include "lnav.hh"
#include "base/lnav_log.hh"