[hackers] [sbase] Use queue.h-macros in sort(1) || FRIGN
commit e00cdf226ae45c349793a7adf91774b5095e5730
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Sun Aug 2 23:55:54 2015 +0200
Commit: sin <sin_AT_2f30.org>
CommitDate: Sun Aug 2 23:32:17 2015 +0100
Use queue.h-macros in sort(1)
This is much easier to read than having yet another handrolled
list implementation.
Tested and more or less clearly equivalent.
Now that I have uni-vac, I'll have enough time to refactor more.
diff --git a/sort.c b/sort.c
index c3bab4f..64cb466 100644
--- a/sort.c
+++ b/sort.c
_AT_@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>
+#include "queue.h"
#include "text.h"
#include "util.h"
_AT_@ -13,22 +14,17 @@ struct keydef {
int start_char;
int end_char;
int flags;
+ TAILQ_ENTRY(keydef) entry;
};
enum {
- MOD_N = 1 << 1,
- MOD_STARTB = 1 << 2,
- MOD_ENDB = 1 << 3,
- MOD_R = 1 << 4,
+ MOD_N = 1 << 0,
+ MOD_STARTB = 1 << 1,
+ MOD_ENDB = 1 << 2,
+ MOD_R = 1 << 3,
};
-struct kdlist {
- struct keydef keydef;
- struct kdlist *next;
-};
-
-static struct kdlist *head = NULL;
-static struct kdlist *tail = NULL;
+static TAILQ_HEAD(kdhead, keydef) kdhead = TAILQ_HEAD_INITIALIZER(kdhead);
static void addkeydef(char *, int);
static void check(FILE *);
_AT_@ -46,19 +42,15 @@ static char *col1, *col2;
static size_t col1siz, col2siz;
static void
-addkeydef(char *def, int flags)
+addkeydef(char *kdstr, int flags)
{
- struct kdlist *node;
+ struct keydef *kd;
- node = enmalloc(2, sizeof(*node));
- if (!head)
- head = node;
- if (parse_keydef(&node->keydef, def, flags))
+ kd = enmalloc(2, sizeof(*kd));
+ if (parse_keydef(kd, kdstr, flags))
enprintf(2, "faulty key definition\n");
- if (tail)
- tail->next = node;
- node->next = NULL;
- tail = node;
+
+ TAILQ_INSERT_TAIL(&kdhead, kd, entry);
}
static void
_AT_@ -85,26 +77,29 @@ linecmp(const char **a, const char **b)
{
int res = 0;
long double x, y;
- struct kdlist *node;
+ struct keydef *kd;
- for (node = head; node && res == 0; node = node->next) {
- columns((char *)*a, &node->keydef, &col1, &col1siz);
- columns((char *)*b, &node->keydef, &col2, &col2siz);
+ TAILQ_FOREACH(kd, &kdhead, entry) {
+ columns((char *)*a, kd, &col1, &col1siz);
+ columns((char *)*b, kd, &col2, &col2siz);
/* if -u is given, don't use default key definition
* unless it is the only one */
- if (uflag && node == tail && head != tail) {
+ if (uflag && kd == TAILQ_LAST(&kdhead, kdhead) &&
+ TAILQ_LAST(&kdhead, kdhead) != TAILQ_FIRST(&kdhead)) {
res = 0;
- } else if (node->keydef.flags & MOD_N) {
+ } else if (kd->flags & MOD_N) {
x = strtold(col1, NULL);
y = strtold(col2, NULL);
- res = x < y ? -1 : x > y;
+ res = (x < y) ? (-1) : (x > y);
} else {
res = strcmp(col1, col2);
}
- if (node->keydef.flags & MOD_R)
+ if (kd->flags & MOD_R)
res = -res;
+ if (res)
+ break;
}
return res;
_AT_@ -139,8 +134,7 @@ parse_keydef(struct keydef *kd, char *s, int flags)
kd->start_column = 1;
kd->start_char = 1;
- /* 0 means end of line */
- kd->end_column = 0;
+ kd->end_column = 0; /* 0 means end of line */
kd->end_char = 0;
kd->flags = flags;
_AT_@ -290,8 +284,8 @@ main(int argc, char *argv[])
} ARGEND;
/* -b shall only apply to custom key definitions */
- if (!head && global_flags)
- addkeydef("1", global_flags & ~(MOD_STARTB|MOD_ENDB));
+ if (TAILQ_EMPTY(&kdhead) && global_flags)
+ addkeydef("1", global_flags & ~(MOD_STARTB | MOD_ENDB));
addkeydef("1", global_flags & MOD_R);
if (!argc) {
Received on Mon Aug 03 2015 - 00:32:24 CEST
This archive was generated by hypermail 2.3.0
: Mon Aug 03 2015 - 00:36:11 CEST