geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

colprint.c
Go to the documentation of this file.
1/*
2* Copyright (c) 2017 Masatake YAMATO
3*
4* This source code is released for free distribution under the terms of the
5* GNU General Public License version 2 or (at your option) any later version.
6*
7*/
8#include "general.h" /* must always come first */
9
10#include "colprint_p.h"
11#include "ptrarray.h"
12#include "routines.h"
13#include "strlist.h"
14#include "vstring.h"
15
16#include <stdarg.h>
17#include <stdio.h>
18#include <string.h>
19
20
22 COLPRINT_LEFT, /* L:... */
23 COLPRINT_RIGHT, /* R:... */
25};
26
30 unsigned int maxWidth;
32};
33
37};
38
39static void fillWithWhitespaces (int i, FILE *fp)
40{
41 while (i-- > 0)
42 {
43 fputc(' ', fp);
44 }
45}
46
47static struct colprintHeaderColumn * colprintHeaderColumnNew (const char* spec)
48{
49 int offset = 2;
50 struct colprintHeaderColumn *headerCol = xCalloc (1, struct colprintHeaderColumn);
51
52 if (strstr(spec, "L:") == spec)
53 headerCol->justification = COLPRINT_LEFT;
54 else if (strstr(spec, "R:") == spec)
55 headerCol->justification = COLPRINT_RIGHT;
56 else
57 {
58 headerCol->justification = COLPRINT_LEFT;
59 offset = 0;
60 }
61
62 headerCol->value = vStringNewInit(spec + offset);
63 headerCol->maxWidth = vStringLength(headerCol->value);
64 return headerCol;
65}
66
67static void colprintHeaderColumnDelete (struct colprintHeaderColumn * headerCol)
68{
69 vStringDelete (headerCol->value);
70 eFree (headerCol);
71}
72
73struct colprintTable *colprintTableNew (const char* columnHeader, ... /* NULL TERMINATED */)
74{
75 char *tmp;
76 va_list ap;
77 struct colprintTable *table;
78 struct colprintHeaderColumn *headerCol;
79
80
81 table = xCalloc (1, struct colprintTable);
84
85 headerCol = colprintHeaderColumnNew(columnHeader);
86 ptrArrayAdd (table->header, headerCol);
87
88 va_start(ap, columnHeader);
89 while (1)
90 {
91 tmp = va_arg(ap, char*);
92 if (tmp)
93 {
94 headerCol = colprintHeaderColumnNew(tmp);
95 ptrArrayAdd (table->header, headerCol);
96 }
97 else
98 break;
99 }
100 va_end(ap);
101
102 struct colprintHeaderColumn *last_col = ptrArrayLast (table->header);
103 if (last_col)
104 last_col->justification = COLPRINT_LAST;
105
106 return table;
107}
108
110{
111 ptrArrayDelete(table->header);
112 table->header = NULL;
113
114 ptrArrayDelete(table->lines);
115 table->header = NULL;
116
117 eFree (table);
118}
119
120static void colprintColumnPrintGeneric (vString *column, struct colprintHeaderColumn *spec, bool machinable, FILE *fp)
121{
122 int maxWidth = spec->maxWidth + (spec->needPrefix? 1: 0);
123
124 if ((column == spec->value) && (spec->needPrefix))
125 {
126 fputc('#', fp);
127 maxWidth--;
128 }
129
130 if (machinable)
131 {
132 fputs (vStringValue (column), fp);
133 if (spec->justification != COLPRINT_LAST)
134 fputc ('\t', fp);
135 }
136 else
137 {
138 int padLen = maxWidth - vStringLength (column);
139 if (spec->justification == COLPRINT_LEFT
140 || spec->justification == COLPRINT_LAST)
141 {
142 fputs (vStringValue (column), fp);
143 if (spec->justification != COLPRINT_LAST)
144 {
145 fillWithWhitespaces (padLen, fp);
146 fputc (' ', fp);
147 }
148 }
149 else
150 {
151 fillWithWhitespaces (padLen, fp);
152 fputs (vStringValue (column), fp);
153 fputc (' ', fp);
154 }
155 }
156}
157
158static void colprintHeaderColumnPrint (struct colprintHeaderColumn *headerCol, bool machinable, FILE* fp)
159{
160 colprintColumnPrintGeneric (headerCol->value, headerCol, machinable, fp);
161}
162
163static void colprintHeaderPrint (ptrArray *header, unsigned int startFrom, bool withHeader, bool machinable, FILE *fp)
164{
165 unsigned int i;
166
167 if (!withHeader)
168 return;
169
170 for (i = startFrom; i < ptrArrayCount(header); i++)
171 {
172 struct colprintHeaderColumn *headerCol = ptrArrayItem (header, i);
173 colprintHeaderColumnPrint (headerCol, machinable, fp);
174 }
175 fputc('\n', fp);
176}
177
178static void colprintLinePrint (stringList *line, unsigned int startFrom, ptrArray *header, bool machinable, FILE *fp)
179{
180 unsigned int i;
181
182 for (i = startFrom; i < stringListCount (line); i++)
183 {
185 struct colprintHeaderColumn *spec = ptrArrayItem (header, i);
186 colprintColumnPrintGeneric(value, spec, machinable, fp);
187 }
188}
189static void colprintLinesPrint (ptrArray *lines, unsigned int startFrom, ptrArray *header, bool machinable, FILE *fp)
190{
191 unsigned int i;
192
193 for (i = 0; i < ptrArrayCount (lines); i++)
194 {
196 colprintLinePrint (line, startFrom, header, machinable, fp);
197 fputc('\n', fp);
198 }
199}
200
201static void colprintUpdateMaxWidths (ptrArray *header, ptrArray *lines, unsigned int startFrom)
202{
203 for (unsigned int c = 0; c < ptrArrayCount(header); c++)
204 {
205 struct colprintHeaderColumn *spec = ptrArrayItem (header, c);
206
207 if (c == startFrom)
208 spec->needPrefix = true;
209 else
210 spec->needPrefix = false;
211 }
212
213 for (unsigned int c = 0; c < ptrArrayCount(header); c++)
214 {
215 struct colprintHeaderColumn *spec = ptrArrayItem (header, c);
216
217 for (unsigned int l = 0; l < ptrArrayCount(lines); l++)
218 {
219 struct colprintLine *line = ptrArrayItem(lines, l);
220 vString *column = ptrArrayItem((ptrArray *)line, c);
221 if (spec->maxWidth < vStringLength(column))
222 spec->maxWidth = vStringLength(column);
223 }
224 }
225}
226
227void colprintTablePrint (struct colprintTable *table, unsigned int startFrom, bool withHeader, bool machinable, FILE *fp)
228{
229 colprintUpdateMaxWidths (table->header, table->lines, startFrom);
230
231 colprintHeaderPrint (table->header, startFrom, withHeader, machinable, fp);
232 colprintLinesPrint (table->lines, startFrom, table->header, machinable, fp);
233}
234
235void colprintTableSort (struct colprintTable *table, int (* compareFn) (struct colprintLine *, struct colprintLine *))
236{
237 ptrArraySort (table->lines, (int (*) (const void *, const void *))compareFn);
238}
239
240struct colprintLine *colprintTableGetNewLine (struct colprintTable *table)
241{
243
244 ptrArrayAdd (table->lines, line);
245 return (struct colprintLine *)line;
246}
247
248static void colprintLineAppendColumn (struct colprintLine *line, vString *column)
249{
250 stringList *slist = (stringList *)line;
251 stringListAdd (slist, column);
252}
253
254void colprintLineAppendColumnCString (struct colprintLine *line, const char *column)
255{
256 vString* vcol = vStringNewInit (column? column: "");
258}
259
260void colprintLineAppendColumnVString (struct colprintLine *line, vString* column)
261{
263}
264
265void colprintLineAppendColumnChar (struct colprintLine *line, char column)
266{
267 vString* vcol = vStringNew ();
268 vStringPut (vcol, column);
270}
271
272void colprintLineAppendColumnInt (struct colprintLine *line, unsigned int column)
273{
274 char buf[12];
275
276 snprintf(buf, 12, "%u", column);
278}
279
280void colprintLineAppendColumnBool (struct colprintLine *line, bool column)
281{
282 colprintLineAppendColumnCString (line, column? "yes": "no");
283}
284
285const char *colprintLineGetColumn (struct colprintLine *line, unsigned int column)
286{
287 stringList *slist = (stringList *)line;
288 if (column <= stringListCount(slist))
289 {
290 vString *vstr = stringListItem (slist, column);
291 return vStringValue (vstr);
292 }
293 else
294 return NULL;
295}
static void colprintHeaderColumnPrint(struct colprintHeaderColumn *headerCol, bool machinable, FILE *fp)
Definition: colprint.c:158
void colprintLineAppendColumnCString(struct colprintLine *line, const char *column)
Definition: colprint.c:254
static void colprintLinesPrint(ptrArray *lines, unsigned int startFrom, ptrArray *header, bool machinable, FILE *fp)
Definition: colprint.c:189
void colprintTableSort(struct colprintTable *table, int(*compareFn)(struct colprintLine *, struct colprintLine *))
Definition: colprint.c:235
static void colprintColumnPrintGeneric(vString *column, struct colprintHeaderColumn *spec, bool machinable, FILE *fp)
Definition: colprint.c:120
static void colprintUpdateMaxWidths(ptrArray *header, ptrArray *lines, unsigned int startFrom)
Definition: colprint.c:201
static struct colprintHeaderColumn * colprintHeaderColumnNew(const char *spec)
Definition: colprint.c:47
static void colprintHeaderPrint(ptrArray *header, unsigned int startFrom, bool withHeader, bool machinable, FILE *fp)
Definition: colprint.c:163
static void fillWithWhitespaces(int i, FILE *fp)
Definition: colprint.c:39
void colprintLineAppendColumnBool(struct colprintLine *line, bool column)
Definition: colprint.c:280
colprintJustification
Definition: colprint.c:21
@ COLPRINT_LAST
Definition: colprint.c:24
@ COLPRINT_LEFT
Definition: colprint.c:22
@ COLPRINT_RIGHT
Definition: colprint.c:23
struct colprintLine * colprintTableGetNewLine(struct colprintTable *table)
Definition: colprint.c:240
void colprintTableDelete(struct colprintTable *table)
Definition: colprint.c:109
struct colprintTable * colprintTableNew(const char *columnHeader,...)
Definition: colprint.c:73
static void colprintHeaderColumnDelete(struct colprintHeaderColumn *headerCol)
Definition: colprint.c:67
void colprintLineAppendColumnChar(struct colprintLine *line, char column)
Definition: colprint.c:265
void colprintLineAppendColumnVString(struct colprintLine *line, vString *column)
Definition: colprint.c:260
static void colprintLineAppendColumn(struct colprintLine *line, vString *column)
Definition: colprint.c:248
void colprintLineAppendColumnInt(struct colprintLine *line, unsigned int column)
Definition: colprint.c:272
void colprintTablePrint(struct colprintTable *table, unsigned int startFrom, bool withHeader, bool machinable, FILE *fp)
Definition: colprint.c:227
static void colprintLinePrint(stringList *line, unsigned int startFrom, ptrArray *header, bool machinable, FILE *fp)
Definition: colprint.c:178
const char * colprintLineGetColumn(struct colprintLine *line, unsigned int column)
Definition: colprint.c:285
vString * line
Definition: geany_cobol.c:133
unsigned int ptrArrayCount(const ptrArray *const current)
Definition: ptrarray.c:80
void ptrArraySort(ptrArray *const current, int(*compare)(const void *, const void *))
Definition: ptrarray.c:179
void * ptrArrayItem(const ptrArray *const current, const unsigned int indx)
Definition: ptrarray.c:86
void * ptrArrayLast(const ptrArray *const current)
Definition: ptrarray.c:92
ptrArray * ptrArrayNew(ptrArrayDeleteFunc deleteFunc)
Definition: ptrarray.c:37
void ptrArrayDelete(ptrArray *const current)
Definition: ptrarray.c:111
unsigned int ptrArrayAdd(ptrArray *const current, void *ptr)
Definition: ptrarray.c:47
void(* ptrArrayDeleteFunc)(void *data)
Definition: ptrarray.h:24
#define NULL
Definition: rbtree.h:150
char * strstr(const char *str, const char *substr)
Definition: routines.c:304
void eFree(void *const ptr)
Definition: routines.c:252
#define xCalloc(n, Type)
Definition: routines.h:24
long lines
Definition: stats.c:32
stringList * stringListNew(void)
Definition: strlist.c:27
void stringListDelete(stringList *const current)
Definition: strlist.c:102
vString * stringListItem(const stringList *const current, const unsigned int indx)
Definition: strlist.c:86
unsigned int stringListCount(const stringList *const current)
Definition: strlist.c:81
void stringListAdd(stringList *const current, vString *string)
Definition: strlist.c:32
unsigned int maxWidth
Definition: colprint.c:30
vString * value
Definition: colprint.c:28
enum colprintJustification justification
Definition: colprint.c:29
ptrArray * lines
Definition: colprint.c:36
ptrArray * header
Definition: colprint.c:35
vString * vStringNew(void)
Definition: vstring.c:70
void vStringDelete(vString *const string)
Definition: vstring.c:60
vString * vStringNewInit(const char *const s)
Definition: vstring.c:90
#define vStringLength(vs)
Definition: vstring.h:31
#define vStringValue(vs)
Definition: vstring.h:28
static void vStringPut(vString *const string, const int c)
Definition: vstring.h:101