1 /*
2  * Copyright (C) 2018 Red Hat, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License Version 2.1
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "Table.hpp"
22 
Table()23 Table::Table()
24     : table(scols_new_table())
25 {
26     if (!table) {
27         throw std::runtime_error("Could not create table");
28     }
29 }
30 
setColumnSeparator(const std::string & separator)31 void Table::setColumnSeparator(const std::string &separator)
32 {
33     scols_table_set_column_separator(table, separator.c_str());
34 }
35 
getColumn(size_t n) const36 std::shared_ptr<Column> Table::getColumn(size_t n) const
37 {
38     if (columns.size() < n) {
39         std::string message = "Out of bounds. Index: " + n;
40         message += " Size: " + columns.size();
41         throw std::out_of_range(message);
42     }
43     return columns[n];
44 }
45 
getLine(size_t n) const46 std::shared_ptr<Line> Table::getLine(size_t n) const
47 {
48     if (lines.size() < n) {
49         std::string message = "Out of bounds. Index: " + n;
50         message += " Size: " + lines.size();
51         throw std::out_of_range(message);
52     }
53     return lines[n];
54 }
55 
moveColumn(const std::shared_ptr<Column> & before,const std::shared_ptr<Column> & toMove)56 void Table::moveColumn(const std::shared_ptr<Column> &before, const std::shared_ptr<Column> &toMove)
57 {
58     scols_table_move_column(table, before->column, toMove->column);
59 }
60 
newColumn(const std::string & name,double widthHint,int flags)61 std::shared_ptr<Column> Table::newColumn(const std::string &name, double widthHint, int flags)
62 {
63     auto smartColsColumn = scols_table_new_column(table, name.c_str(), widthHint, flags);
64     auto column = std::make_shared<Column>(smartColsColumn);
65 
66     columns.push_back(column);
67     return column;
68 }
69 
newLine()70 std::shared_ptr<Line> Table::newLine()
71 {
72     auto smartColsLine = scols_table_new_line(table, nullptr);
73     auto line = std::make_shared<Line>(smartColsLine);
74 
75     lines.push_back(line);
76     return line;
77 }
78 
79 
newLine(const std::shared_ptr<Line> & parent)80 std::shared_ptr<Line> Table::newLine(const std::shared_ptr<Line> &parent)
81 {
82     auto smartColsLine = scols_table_new_line(table, parent->line);
83     auto line = std::make_shared<Line>(smartColsLine);
84 
85     scols_unref_line(smartColsLine);
86     lines.push_back(line);
87     return line;
88 }
89 
addColumn(const std::shared_ptr<Column> & column)90 void Table::addColumn(const std::shared_ptr<Column> &column)
91 {
92     scols_table_add_column(table, column->column);
93     columns.push_back(column);
94 }
95 
addLine(const std::shared_ptr<Line> & line)96 void Table::addLine(const std::shared_ptr<Line> &line)
97 {
98     scols_table_add_line(table, line->line);
99     lines.push_back(line);
100 }
101 
removeColumn(const std::shared_ptr<Column> & column)102 void Table::removeColumn(const std::shared_ptr<Column> &column)
103 {
104     std::remove(std::begin(columns), std::end(columns), column);
105     scols_table_remove_column(table, column->column);
106 }
107 
removeColumns()108 void Table::removeColumns()
109 {
110     columns.clear();
111     scols_table_remove_columns(table);
112 }
113 
removeLine(const std::shared_ptr<Line> & line)114 void Table::removeLine(const std::shared_ptr<Line> &line)
115 {
116     std::remove(std::begin(lines), std::end(lines), line);
117     scols_table_remove_line(table, line->line);
118 }
119 
removeLines()120 void Table::removeLines()
121 {
122     lines.clear();
123     scols_table_remove_lines(table);
124 }
125 
toString()126 std::string Table::toString()
127 {
128     char *data;
129     scols_print_table_to_string(table, &data);
130     return std::string(data);
131 }
132 
toString(const std::shared_ptr<Line> & start,const std::shared_ptr<Line> & end)133 std::string Table::toString(const std::shared_ptr<Line> &start, const std::shared_ptr<Line> &end)
134 {
135     char *data;
136     if (start != nullptr && end != nullptr) {
137         scols_table_print_range_to_string(table, start->line, end->line, &data);
138         auto lines = std::string(data);
139         free(data);
140         return lines;
141     } else {
142         return std::string();
143     }
144 }
145 
setSymbols(struct libscols_symbols * symbols)146 void Table::setSymbols(struct libscols_symbols *symbols)
147 {
148     if (scols_table_set_symbols(table, symbols) == -EINVAL)
149         std::runtime_error("Cannot set stream");
150 }
151 
setDefaultSymbols()152 void Table::setDefaultSymbols()
153 {
154     if (scols_table_set_default_symbols(table) == -EINVAL)
155         std::runtime_error("Cannot set default symbols");
156 }
157 
setStream(FILE * stream)158 void Table::setStream(FILE *stream)
159 {
160     if (scols_table_set_stream(table, stream) == -EINVAL)
161         std::runtime_error("Cannot set stream");
162 }
163 
setTermforce(TermForce force)164 void Table::setTermforce(TermForce force)
165 {
166     if (scols_table_set_termforce(table, static_cast<int>(force)) == -EINVAL)
167         std::runtime_error("Cannot set default symbols");
168 }
169