BRL-CAD
columns.c
Go to the documentation of this file.
1 /* C O L U M N S . C
2  * BRL-CAD
3  *
4  * Copyright (c) 1985-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * 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 file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libged/columns.c
21  *
22  * A set of routines for printing columns of data.
23  *
24  */
25 
26 /* TODO: It should be lowered to libbu. */
27 
28 #include "common.h"
29 
30 #include <stdlib.h>
31 #include <string.h>
32 
33 
34 #include "vmath.h"
35 #include "raytrace.h"
36 #include "db.h"
37 
38 
39 static int col_count; /* names listed on current line */
40 static int col_len; /* length of previous name */
41 #define TERMINAL_WIDTH 80 /* XXX */
42 #define COLUMNS ((TERMINAL_WIDTH + NAMESIZE - 1) / NAMESIZE)
43 
44 void
46  struct bu_vls *str,
47  const char *cp)
48 {
49  /* Output newline if last column printed. */
50  if (col_count >= COLUMNS || (col_len+NAMESIZE-1) >= TERMINAL_WIDTH) {
51  /* line now full */
52  bu_vls_putc(str, '\n');
53  col_count = 0;
54  } else if (col_count != 0) {
55  /* Space over before starting new column */
56  do {
57  bu_vls_putc(str, ' ');
58  col_len++;
59  } while ((col_len % NAMESIZE) != 0);
60  }
61  /* Output string and save length for next tab. */
62  col_len = 0;
63  while (*cp != '\0') {
64  bu_vls_putc(str, *cp);
65  ++cp;
66  ++col_len;
67  }
68  col_count++;
69 }
70 
71 
72 void
73 vls_col_eol(struct bu_vls *str)
74 {
75  if (col_count != 0) /* partial line */
76  bu_vls_putc(str, '\n');
77  col_count = 0;
78  col_len = 0;
79 }
80 
81 
82 /*
83  * Given two pointers to pointers to directory entries, do a string compare
84  * on the respective names and return that value.
85  */
86 int
87 cmpdirname(const void *a, const void *b)
88 {
89  struct directory **dp1, **dp2;
90 
91  dp1 = (struct directory **)a;
92  dp2 = (struct directory **)b;
93  return bu_strcmp((*dp1)->d_namep, (*dp2)->d_namep);
94 }
95 
96 
97 /*
98  * Local Variables:
99  * mode: C
100  * tab-width: 8
101  * indent-tabs-mode: t
102  * c-file-style: "stroustrup"
103  * End:
104  * ex: shiftwidth=4 tabstop=8
105  */
Header file for the BRL-CAD common definitions.
void vls_col_eol(struct bu_vls *str)
Definition: columns.c:73
#define COLUMNS
Definition: columns.c:42
#define TERMINAL_WIDTH
Definition: columns.c:41
int bu_strcmp(const char *string1, const char *string2)
Definition: str.c:171
int cmpdirname(const void *a, const void *b)
Definition: columns.c:87
void vls_col_item(struct bu_vls *str, const char *cp)
Definition: columns.c:45
Definition: vls.h:56
void bu_vls_putc(struct bu_vls *vp, int c)
Definition: vls.c:666