| 1 |
#include "smartcolsP.h" |
| 2 |
|
| 3 |
/** |
| 4 |
* scola_table_print_range: |
| 5 |
* @tb: table |
| 6 |
* @start: first printed line or NULL to print from the begin of the table |
| 7 |
* @end: last printed line or NULL to print all from start. |
| 8 |
* |
| 9 |
* If the start is the first line in the table than prints table header too. |
| 10 |
* The header is printed only once. This does not work for trees. |
| 11 |
* |
| 12 |
* Returns: 0, a negative value in case of an error. |
| 13 |
*/ |
| 14 |
int scols_table_print_range( struct libscols_table *tb, |
| 15 |
struct libscols_line *start, |
| 16 |
struct libscols_line *end) |
| 17 |
{ |
| 18 |
struct ul_buffer buf = UL_INIT_BUFFER; |
| 19 |
struct libscols_iter itr; |
| 20 |
int rc; |
| 21 |
|
| 22 |
if (scols_table_is_tree(tb)) |
| 23 |
return -EINVAL; |
| 24 |
|
| 25 |
DBG(TAB, ul_debugobj(tb, "printing range from API")); |
| 26 |
|
| 27 |
rc = __scols_initialize_printing(tb, &buf); |
| 28 |
if (rc) |
| 29 |
return rc; |
| 30 |
|
| 31 |
if (start) { |
| 32 |
itr.direction = SCOLS_ITER_FORWARD; |
| 33 |
itr.head = &tb->tb_lines; |
| 34 |
itr.p = &start->ln_lines; |
| 35 |
} else |
| 36 |
scols_reset_iter(&itr, SCOLS_ITER_FORWARD); |
| 37 |
|
| 38 |
if (!start || itr.p == tb->tb_lines.next) { |
| 39 |
rc = __scols_print_header(tb, &buf); |
| 40 |
if (rc) |
| 41 |
goto done; |
| 42 |
} |
| 43 |
|
| 44 |
rc = __scols_print_range(tb, &buf, &itr, end); |
| 45 |
done: |
| 46 |
__scols_cleanup_printing(tb, &buf); |
| 47 |
return rc; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* scols_table_print_range_to_string: |
| 52 |
* @tb: table |
| 53 |
* @start: first printed line or NULL to print from the beginning of the table |
| 54 |
* @end: last printed line or NULL to print all from start. |
| 55 |
* @data: pointer to the beginning of a memory area to print to |
| 56 |
* |
| 57 |
* The same as scols_table_print_range(), but prints to @data instead of |
| 58 |
* stream. |
| 59 |
* |
| 60 |
* Returns: 0, a negative value in case of an error. |
| 61 |
*/ |
| 62 |
#ifdef HAVE_OPEN_MEMSTREAM |
| 63 |
int scols_table_print_range_to_string( struct libscols_table *tb, |
| 64 |
struct libscols_line *start, |
| 65 |
struct libscols_line *end, |
| 66 |
char **data) |
| 67 |
{ |
| 68 |
FILE *stream, *old_stream; |
| 69 |
size_t sz; |
| 70 |
int rc; |
| 71 |
|
| 72 |
if (!tb) |
| 73 |
return -EINVAL; |
| 74 |
|
| 75 |
DBG(TAB, ul_debugobj(tb, "printing range to string")); |
| 76 |
|
| 77 |
/* create a stream for output */ |
| 78 |
stream = open_memstream(data, &sz); |
| 79 |
if (!stream) |
| 80 |
return -ENOMEM; |
| 81 |
|
| 82 |
old_stream = scols_table_get_stream(tb); |
| 83 |
scols_table_set_stream(tb, stream); |
| 84 |
rc = scols_table_print_range(tb, start, end); |
| 85 |
fclose(stream); |
| 86 |
scols_table_set_stream(tb, old_stream); |
| 87 |
|
| 88 |
return rc; |
| 89 |
} |
| 90 |
#else |
| 91 |
int scols_table_print_range_to_string( |
| 92 |
struct libscols_table *tb __attribute__((__unused__)), |
| 93 |
struct libscols_line *start __attribute__((__unused__)), |
| 94 |
struct libscols_line *end __attribute__((__unused__)), |
| 95 |
char **data __attribute__((__unused__))) |
| 96 |
{ |
| 97 |
return -ENOSYS; |
| 98 |
} |
| 99 |
#endif |
| 100 |
|
| 101 |
static int do_print_table(struct libscols_table *tb, int *is_empty) |
| 102 |
{ |
| 103 |
int rc = 0; |
| 104 |
struct ul_buffer buf = UL_INIT_BUFFER; |
| 105 |
|
| 106 |
if (!tb) |
| 107 |
return -EINVAL; |
| 108 |
|
| 109 |
DBG(TAB, ul_debugobj(tb, "printing")); |
| 110 |
if (is_empty) |
| 111 |
*is_empty = 0; |
| 112 |
|
| 113 |
if (list_empty(&tb->tb_columns)) { |
| 114 |
DBG(TAB, ul_debugobj(tb, "error -- no columns")); |
| 115 |
return -EINVAL; |
| 116 |
} |
| 117 |
if (list_empty(&tb->tb_lines)) { |
| 118 |
DBG(TAB, ul_debugobj(tb, "ignore -- no lines")); |
| 119 |
if (scols_table_is_json(tb)) { |
| 120 |
ul_jsonwrt_init(&tb->json, tb->out, 0, tb->json_format); |
| 121 |
if (tb->json_format != UL_JSON_LINE) { |
| 122 |
ul_jsonwrt_root_open(&tb->json); |
| 123 |
ul_jsonwrt_array_open(&tb->json, tb->name ? tb->name : ""); |
| 124 |
ul_jsonwrt_array_close(&tb->json); |
| 125 |
ul_jsonwrt_root_close(&tb->json); |
| 126 |
} |
| 127 |
} else if (is_empty) |
| 128 |
*is_empty = 1; |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
tb->header_printed = 0; |
| 133 |
rc = __scols_initialize_printing(tb, &buf); |
| 134 |
if (rc) |
| 135 |
return rc; |
| 136 |
|
| 137 |
if (scols_table_is_json(tb) && tb->json_format != UL_JSON_LINE) { |
| 138 |
ul_jsonwrt_root_open(&tb->json); |
| 139 |
ul_jsonwrt_array_open(&tb->json, tb->name ? tb->name : ""); |
| 140 |
} |
| 141 |
|
| 142 |
if (tb->format == SCOLS_FMT_HUMAN) |
| 143 |
__scols_print_title(tb); |
| 144 |
|
| 145 |
rc = __scols_print_header(tb, &buf); |
| 146 |
if (rc) |
| 147 |
goto done; |
| 148 |
|
| 149 |
if (scols_table_is_tree(tb)) |
| 150 |
rc = __scols_print_tree(tb, &buf); |
| 151 |
else |
| 152 |
rc = __scols_print_table(tb, &buf); |
| 153 |
|
| 154 |
if (scols_table_is_json(tb)) { |
| 155 |
if (tb->json_format != UL_JSON_LINE) { |
| 156 |
ul_jsonwrt_array_close(&tb->json); |
| 157 |
ul_jsonwrt_root_close(&tb->json); |
| 158 |
} else { |
| 159 |
fputc('\n', tb->out); /* trailing newline after last object */ |
| 160 |
} |
| 161 |
} |
| 162 |
done: |
| 163 |
__scols_cleanup_printing(tb, &buf); |
| 164 |
return rc; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* scols_print_table: |
| 169 |
* @tb: table |
| 170 |
* |
| 171 |
* Prints the table to the output stream and terminate by \n. |
| 172 |
* |
| 173 |
* Returns: 0, a negative value in case of an error. |
| 174 |
*/ |
| 175 |
int scols_print_table(struct libscols_table *tb) |
| 176 |
{ |
| 177 |
int empty = 0; |
| 178 |
int rc = do_print_table(tb, &empty); |
| 179 |
|
| 180 |
if (rc == 0 && !empty && !scols_table_is_json(tb)) |
| 181 |
fputc('\n', tb->out); |
| 182 |
return rc; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* scols_print_table_to_string: |
| 187 |
* @tb: table |
| 188 |
* @data: pointer to the beginning of a memory area to print to |
| 189 |
* |
| 190 |
* Prints the table to @data. |
| 191 |
* |
| 192 |
* Returns: 0, a negative value in case of an error. |
| 193 |
*/ |
| 194 |
#ifdef HAVE_OPEN_MEMSTREAM |
| 195 |
int scols_print_table_to_string(struct libscols_table *tb, char **data) |
| 196 |
{ |
| 197 |
FILE *stream, *old_stream; |
| 198 |
size_t sz; |
| 199 |
int rc; |
| 200 |
|
| 201 |
if (!tb) |
| 202 |
return -EINVAL; |
| 203 |
|
| 204 |
DBG(TAB, ul_debugobj(tb, "printing to string")); |
| 205 |
|
| 206 |
/* create a stream for output */ |
| 207 |
stream = open_memstream(data, &sz); |
| 208 |
if (!stream) |
| 209 |
return -ENOMEM; |
| 210 |
|
| 211 |
old_stream = scols_table_get_stream(tb); |
| 212 |
scols_table_set_stream(tb, stream); |
| 213 |
rc = do_print_table(tb, NULL); |
| 214 |
fclose(stream); |
| 215 |
scols_table_set_stream(tb, old_stream); |
| 216 |
|
| 217 |
return rc; |
| 218 |
} |
| 219 |
#else |
| 220 |
int scols_print_table_to_string( |
| 221 |
struct libscols_table *tb __attribute__((__unused__)), |
| 222 |
char **data __attribute__((__unused__))) |
| 223 |
{ |
| 224 |
return -ENOSYS; |
| 225 |
} |
| 226 |
#endif |