Skip to content

Commit

Permalink
Make all tests follow consistent format. Remove unused functions from…
Browse files Browse the repository at this point in the history
… list.[ch].
  • Loading branch information
mikelward committed May 9, 2012
1 parent 1d8f29a commit 8b1d7c0
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 283 deletions.
21 changes: 21 additions & 0 deletions display.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ void printdown(StringList *list, int stringwidth, int screenwidth)
}
}

void printspaces(int n)
{
for (int i = 0; i < n; i++) {
putchar(' ');
}
}

/**
* Try to set up color output.
*
Expand Down Expand Up @@ -176,4 +183,18 @@ void freecolors(Colors *colors)
free(colors);
}

/*
* divide num by mult
* and return the nearest integer >= the result
*/
int ceildiv(int num, int mult)
{
if (mult == 0) {
errorf("division by zero (%d, %d)\n", num, mult);
assert(mult != 0);
}
int res = (num + mult - 1) / mult;
return res;
}

/* vim: set ts=4 sw=4 tw=0 et:*/
2 changes: 2 additions & 0 deletions display.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ typedef List StringList;

void printacross(StringList *list, int stringwidth, int screenwidth);
void printdown(StringList *list, int stringwidth, int screenwidth);
void printspaces(int n);

int ceildiv(int num, int mult);
int setupcolors(Colors *colors);
void freecolors(Colors *colors);

Expand Down
2 changes: 2 additions & 0 deletions filetest.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ int test_fileperms();

int main(int argc, char **argv)
{
myname = "filetest";

test_bare_file();
test_absolute_file();
test_relative_file();
Expand Down
30 changes: 30 additions & 0 deletions freetest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

struct foo {
char *buf;
};

struct foo *newfoo()
{
struct foo *foo = malloc(sizeof(*foo));
foo->buf = malloc(1024);
return foo;
}

void nullfunc(struct foo *foo)
{
}

int main(int argc, const char *argv[])
{
struct foo *foo = newfoo();

nullfunc(foo);

free(foo->buf);

return 0;
}
/* vim: set ts=4 sw=4 tw=0 et:*/
133 changes: 0 additions & 133 deletions list.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,6 @@ void walklist(List *list, walker_func func)
}
}

void printlist(List *list, printer_func printer, void *pvoptions)
{
/* is casting function returning int to function returning void valid? */
walklistcontext(list, (walker_context_func)printer, pvoptions);
}

struct getmaxwidth_context {
int maxsofar;
int (*getwidth)(void *elem, void *pvoptions);
Expand All @@ -194,131 +188,4 @@ int getmaxwidth(List *list, int (*getwidth)(void *elem, void *pvoptions), void *
return context.maxsofar;
}

/**
* Print a list in rows across the page, e.g.
* 0 1 2
* 3 4 5
*
* How many columns to use is calculated by this function based on the screenwidth,
* and the width of each element, as determined by the "getwidth" function.
*
* Each element is then printed by calling "printer" on elements in the correct
* order to produce columns.
*
* printer must return the number of characters printed (excluding characters
* that don't move the cursor, e.g. color escape sequences) so that this function
* knows how many spaces to print to produce properly aligned columns.
*/
void printlistacross(List *list,
int screenwidth, int (*getwidth)(void *elem, void *pvoptions),
int (*printer)(void *elem, void *pvoptions), void *pvoptions)
{
int maxelemwidth = getmaxwidth(list, getwidth, pvoptions);

/* XXX I prefer using two so that -x and -xF have the same column layout,
* this also makes the -F and -O options easier to handle
* but this doesn't match BSD ls, and probably shouldn't be hard-coded,
* so think about how to make this better */
int colwidth = maxelemwidth + 2; /* 2 space margin between columns */
int len = length(list);
int cols = screenwidth / colwidth;
cols = (cols) ? cols : 1;
int col = 0;
int i = 0;
while (i < len) {
int nchars = printer(getitem(list, i), pvoptions);
i++;
col++;
if (col == cols) {
printf("\n");
col = 0;
}
else {
printspaces(colwidth - nchars);
}
}
if (col != 0) {
printf("\n");
}
}

/*
* divide num by mult
* and return the nearest integer >= the result
*/
int ceildiv(int num, int mult)
{
if (mult == 0) {
errorf("division by zero (%d, %d)\n", num, mult);
assert(mult != 0);
}
int res = (num + mult - 1) / mult;
return res;
}

void printspaces(int n)
{
for (int i = 0; i < n; i++) {
putchar(' ');
}
}

/**
* Print a list in columns down the page, e.g.
* 0 2 4
* 1 3 5
*
* How many columns to use is calculated by this function based on the screenwidth,
* and the width of each element, as determined by the "getwidth" function.
*
* Each element is then printed by calling "printer" on elements in the correct
* order to produce columns.
*
* printer must return the number of characters printed (excluding characters
* that don't move the cursor, e.g. color escape sequences) so that this function
* knows how many spaces to print to produce properly aligned columns.
*/
void printlistdown(List *list,
int screenwidth, int (*getwidth)(void *elem, void *pvoptions),
int (*printer)(void *elem, void *pvoptions), void *pvoptions)
{
if (list == NULL) {
errorf("list is NULL\n");
return;
}

int maxelemwidth = getmaxwidth(list, getwidth, pvoptions);
/* XXX I prefer using two so that -x and -xF have the same column layout,
* this also makes the -F and -O options easier to handle
* but this doesn't match BSD ls, and probably shouldn't be hard-coded,
* so think about how to make this better */
int colwidth = maxelemwidth + 2; /* 2 space margin between columns */

int len = length(list);
if (len <= 0) return; /* avoid div by zero */
int maxcols = screenwidth / colwidth;
maxcols = (maxcols) ? maxcols : 1;
int rows = ceildiv(len, maxcols);
int cols = ceildiv(len, rows);

int col = 0;
for (int row = 0; row < rows; row++) {
for (col = 0; col < cols; col++) {
int idx = col*rows + row;
if (idx >= len) {
break;
}
void *elem = getitem(list, idx);
int nchars = printer(elem, pvoptions);
if (col == cols-1) {
break;
}
else {
printspaces(colwidth-nchars);
}
}
printf("\n");
}
}

/* vim: set ts=4 sw=4 tw=0 et:*/
10 changes: 0 additions & 10 deletions list.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ void sortlist(List *list, list_compare_function compare);
void reverselist(List *list);
void walklist(List *list, walker_func func);
void walklistcontext(List *list, walker_context_func func, void *context);
void printlist(List *list, printer_func printer, void *pvoptions);
void printlistacross(List *list,
int screenwidth, width_func getwidth,
printer_func printer, void *pvoptions);
void printlistdown(List *list,
int screenwidth, width_func getwidth,
printer_func printer, void *pvoptions);
/* XXX move to display.[ch] */
void printspaces(int n);
int ceildiv(int num, int mult);

#endif
/* vim: set ts=4 sw=4 tw=0 et:*/
Loading

0 comments on commit 8b1d7c0

Please sign in to comment.