Rename linecmp and line-structs in join(1) and sort(1)
We will steal the names for the global functions.
This commit is contained in:
parent
d585d4b028
commit
54d3f3b3a5
32
join.c
32
join.c
|
@ -26,7 +26,7 @@ struct field {
|
||||||
size_t len;
|
size_t len;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct line {
|
struct jline {
|
||||||
char *text;
|
char *text;
|
||||||
size_t nf;
|
size_t nf;
|
||||||
size_t maxf;
|
size_t maxf;
|
||||||
|
@ -47,7 +47,7 @@ struct outlist {
|
||||||
struct span {
|
struct span {
|
||||||
size_t nl;
|
size_t nl;
|
||||||
size_t maxl;
|
size_t maxl;
|
||||||
struct line **lines;
|
struct jline **lines;
|
||||||
};
|
};
|
||||||
|
|
||||||
static char *sep = NULL;
|
static char *sep = NULL;
|
||||||
|
@ -84,9 +84,9 @@ prsep(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
swaplines(struct line *la, struct line *lb)
|
swaplines(struct jline *la, struct jline *lb)
|
||||||
{
|
{
|
||||||
struct line tmp;
|
struct jline tmp;
|
||||||
|
|
||||||
tmp = *la;
|
tmp = *la;
|
||||||
*la = *lb;
|
*la = *lb;
|
||||||
|
@ -94,7 +94,7 @@ swaplines(struct line *la, struct line *lb)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
prjoin(struct line *la, struct line *lb, size_t jfa, size_t jfb)
|
prjoin(struct jline *la, struct jline *lb, size_t jfa, size_t jfb)
|
||||||
{
|
{
|
||||||
struct spec *sp;
|
struct spec *sp;
|
||||||
struct field *joinfield;
|
struct field *joinfield;
|
||||||
|
@ -149,7 +149,7 @@ prjoin(struct line *la, struct line *lb, size_t jfa, size_t jfb)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
prline(struct line *lp)
|
prline(struct jline *lp)
|
||||||
{
|
{
|
||||||
size_t len = strlen(lp->text);
|
size_t len = strlen(lp->text);
|
||||||
|
|
||||||
|
@ -160,7 +160,7 @@ prline(struct line *lp)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
linecmp(struct line *la, struct line *lb, size_t jfa, size_t jfb)
|
jlinecmp(struct jline *la, struct jline *lb, size_t jfa, size_t jfb)
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
|
@ -179,7 +179,7 @@ linecmp(struct line *la, struct line *lb, size_t jfa, size_t jfb)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
addfield(struct line *lp, char *sp, size_t len)
|
addfield(struct jline *lp, char *sp, size_t len)
|
||||||
{
|
{
|
||||||
if (lp->nf >= lp->maxf) {
|
if (lp->nf >= lp->maxf) {
|
||||||
lp->fields = ereallocarray(lp->fields, (GROW * lp->maxf),
|
lp->fields = ereallocarray(lp->fields, (GROW * lp->maxf),
|
||||||
|
@ -201,10 +201,10 @@ prspanjoin(struct span *spa, struct span *spb, size_t jfa, size_t jfb)
|
||||||
prjoin(spa->lines[i], spb->lines[j], jfa, jfb);
|
prjoin(spa->lines[i], spb->lines[j], jfa, jfb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct line *
|
static struct jline *
|
||||||
makeline(char *s, size_t len)
|
makeline(char *s, size_t len)
|
||||||
{
|
{
|
||||||
struct line *lp;
|
struct jline *lp;
|
||||||
char *sp, *beg, *end;
|
char *sp, *beg, *end;
|
||||||
size_t i;
|
size_t i;
|
||||||
int eol = 0;
|
int eol = 0;
|
||||||
|
@ -212,7 +212,7 @@ makeline(char *s, size_t len)
|
||||||
if (s[len-1] == '\n')
|
if (s[len-1] == '\n')
|
||||||
s[len-1] = '\0';
|
s[len-1] = '\0';
|
||||||
|
|
||||||
lp = ereallocarray(NULL, INIT, sizeof(struct line));
|
lp = ereallocarray(NULL, INIT, sizeof(struct jline));
|
||||||
lp->text = s;
|
lp->text = s;
|
||||||
lp->fields = ereallocarray(NULL, INIT, sizeof(struct field));
|
lp->fields = ereallocarray(NULL, INIT, sizeof(struct field));
|
||||||
lp->nf = 0;
|
lp->nf = 0;
|
||||||
|
@ -274,7 +274,7 @@ addtospan(struct span *sp, FILE *fp, int reset)
|
||||||
|
|
||||||
if (sp->nl >= sp->maxl) {
|
if (sp->nl >= sp->maxl) {
|
||||||
sp->lines = ereallocarray(sp->lines, (GROW * sp->maxl),
|
sp->lines = ereallocarray(sp->lines, (GROW * sp->maxl),
|
||||||
sizeof(struct line *));
|
sizeof(struct jline *));
|
||||||
sp->maxl *= GROW;
|
sp->maxl *= GROW;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -288,7 +288,7 @@ initspan(struct span *sp)
|
||||||
{
|
{
|
||||||
sp->nl = 0;
|
sp->nl = 0;
|
||||||
sp->maxl = INIT;
|
sp->maxl = INIT;
|
||||||
sp->lines = ereallocarray(NULL, INIT, sizeof(struct line *));
|
sp->lines = ereallocarray(NULL, INIT, sizeof(struct jline *));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -385,7 +385,7 @@ join(FILE *fa, FILE *fb, size_t jfa, size_t jfb)
|
||||||
addtospan(&spb, fb, RESET);
|
addtospan(&spb, fb, RESET);
|
||||||
|
|
||||||
while (spa.nl && spb.nl) {
|
while (spa.nl && spb.nl) {
|
||||||
if ((cmp = linecmp(spa.lines[0], spb.lines[0], jfa, jfb)) < 0) {
|
if ((cmp = jlinecmp(spa.lines[0], spb.lines[0], jfa, jfb)) < 0) {
|
||||||
if (unpairsa)
|
if (unpairsa)
|
||||||
prline(spa.lines[0]);
|
prline(spa.lines[0]);
|
||||||
if (!addtospan(&spa, fa, RESET)) {
|
if (!addtospan(&spa, fa, RESET)) {
|
||||||
|
@ -419,7 +419,7 @@ join(FILE *fa, FILE *fb, size_t jfa, size_t jfb)
|
||||||
spa.nl++;
|
spa.nl++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (linecmp(spa.lines[spa.nl-1], spb.lines[0], jfa, jfb) == 0);
|
} while (jlinecmp(spa.lines[spa.nl-1], spb.lines[0], jfa, jfb) == 0);
|
||||||
|
|
||||||
/* read all consecutive matching lines from b */
|
/* read all consecutive matching lines from b */
|
||||||
do {
|
do {
|
||||||
|
@ -428,7 +428,7 @@ join(FILE *fa, FILE *fb, size_t jfa, size_t jfb)
|
||||||
spb.nl++;
|
spb.nl++;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (linecmp(spa.lines[0], spb.lines[spb.nl-1], jfa, jfb) == 0);
|
} while (jlinecmp(spa.lines[0], spb.lines[spb.nl-1], jfa, jfb) == 0);
|
||||||
|
|
||||||
if (pairs)
|
if (pairs)
|
||||||
prspanjoin(&spa, &spb, jfa, jfb);
|
prspanjoin(&spa, &spb, jfa, jfb);
|
||||||
|
|
8
sort.c
8
sort.c
|
@ -171,7 +171,7 @@ skipmodcmp(struct linebufline *a, struct linebufline *b, int flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
linecmp(struct linebufline *a, struct linebufline *b)
|
slinecmp(struct linebufline *a, struct linebufline *b)
|
||||||
{
|
{
|
||||||
int res = 0;
|
int res = 0;
|
||||||
long double x, y;
|
long double x, y;
|
||||||
|
@ -218,7 +218,7 @@ check(FILE *fp, const char *fname)
|
||||||
if (!prev.data && (prev.len = getline(&prev.data, &prevsize, fp)) < 0)
|
if (!prev.data && (prev.len = getline(&prev.data, &prevsize, fp)) < 0)
|
||||||
eprintf("getline:");
|
eprintf("getline:");
|
||||||
while ((cur.len = getline(&cur.data, &cursize, fp)) > 0) {
|
while ((cur.len = getline(&cur.data, &cursize, fp)) > 0) {
|
||||||
if (uflag > linecmp(&cur, &prev)) {
|
if (uflag > slinecmp(&cur, &prev)) {
|
||||||
if (!Cflag) {
|
if (!Cflag) {
|
||||||
weprintf("disorder %s: ", fname);
|
weprintf("disorder %s: ", fname);
|
||||||
fwrite(cur.data, 1, cur.len, stderr);
|
fwrite(cur.data, 1, cur.len, stderr);
|
||||||
|
@ -412,11 +412,11 @@ main(int argc, char *argv[])
|
||||||
eprintf("fopen %s:", outfile);
|
eprintf("fopen %s:", outfile);
|
||||||
|
|
||||||
qsort(linebuf.lines, linebuf.nlines, sizeof(*linebuf.lines),
|
qsort(linebuf.lines, linebuf.nlines, sizeof(*linebuf.lines),
|
||||||
(int (*)(const void *, const void *))linecmp);
|
(int (*)(const void *, const void *))slinecmp);
|
||||||
|
|
||||||
for (i = 0; i < linebuf.nlines; i++) {
|
for (i = 0; i < linebuf.nlines; i++) {
|
||||||
if (!uflag || i == 0 ||
|
if (!uflag || i == 0 ||
|
||||||
linecmp(&linebuf.lines[i], &linebuf.lines[i - 1])) {
|
slinecmp(&linebuf.lines[i], &linebuf.lines[i - 1])) {
|
||||||
fwrite(linebuf.lines[i].data, 1,
|
fwrite(linebuf.lines[i].data, 1,
|
||||||
linebuf.lines[i].len, ofp);
|
linebuf.lines[i].len, ofp);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user