Skip to content

Instantly share code, notes, and snippets.

@anyakichi
Created October 2, 2011 07:25
Show Gist options
  • Save anyakichi/1257187 to your computer and use it in GitHub Desktop.
Save anyakichi/1257187 to your computer and use it in GitHub Desktop.
diff -ur multiskkserv-20100128-ac.orig/src/multiskkserv.c multiskkserv-20100128-ac/src/multiskkserv.c
--- multiskkserv-20100128-ac.orig/src/multiskkserv.c 2011-10-02 13:28:12.000000000 +0900
+++ multiskkserv-20100128-ac/src/multiskkserv.c 2011-10-02 13:28:21.000000000 +0900
@@ -263,6 +263,128 @@
#endif
}
+static int
+join(char *buf, int maxlen, char *sep, char **list)
+{
+#ifndef MIN
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#endif
+ int len;
+
+ if (*list == NULL) {
+ *buf = '\0';
+ return 0;
+ }
+
+ if (sep == NULL)
+ sep = "";
+
+ strlcpy(buf, *list, maxlen);
+
+ for (list++; *list != NULL; list++) {
+ strlcat(buf, sep, maxlen);
+ len = strlcat(buf, *list, maxlen);
+ if (len >= maxlen)
+ return maxlen;
+ }
+
+ return len;
+}
+
+static int
+split(char *str, const char *sep, char **list, int maxlen)
+{
+ int i;
+ char *last, *p;
+
+ for ((p = strtok_r(str, sep, &last)), i = 0; p != NULL && i < maxlen - 1;
+ (p = strtok_r(NULL, sep, &last)), i++)
+ list[i] = p;
+
+ list[i] = NULL;
+
+ if (p != NULL)
+ i++;
+
+ return i;
+}
+
+static int
+extend(void **a, void **b, size_t len)
+{
+ void **p;
+
+ if (a == NULL || b == NULL || len == 0)
+ return 0;
+
+ for (p = a; *p != NULL; p++)
+ ;
+
+ while (*b != NULL && p - a < len - 1)
+ *p++ = *b++;
+
+ *p = NULL;
+
+ if (*b != NULL)
+ return p - a + 1;
+ else
+ return p - a;
+}
+
+#define MAXWORDS 512
+#define MAXANNOS 8
+static void
+reorganize_result(char *rbuf, int rlen)
+{
+#ifndef __arraycount
+#define __arraycount(x) (sizeof(x) / sizeof(x[0]))
+#endif
+ int len, i, j;
+ char *tokens[MAXWORDS];
+ char *word_list[MAXWORDS][MAXANNOS];
+ char **listp;
+ char buf[rlen];
+ char temp[rlen];
+
+ strlcpy(buf, rbuf, rlen);
+
+ len = split(buf, "/", tokens, __arraycount(tokens));
+ if (len == __arraycount(tokens))
+ return;
+ tokens[len] = NULL;
+
+ for (listp = tokens + 1, i = 0; *listp != NULL; listp++) {
+ len = split(*listp, ";", word_list[i], __arraycount(word_list[i]));
+ if (len > 2)
+ return;
+ for (j = 0; j < i; j++) {
+ if (strcmp(word_list[i][0], word_list[j][0]) == 0) {
+ extend((void **)word_list[j], (void **)&word_list[i][1],
+ __arraycount(word_list[j]));
+ break;
+ }
+ }
+ if (i == j) {
+ word_list[i][len] = NULL;
+ i++;
+ }
+ }
+ word_list[i][0] = NULL;
+
+ rbuf[1] = '\0';
+ for (i = 0; i < __arraycount(word_list); i++) {
+ if (word_list[i][0] == NULL)
+ break;
+ strlcat(rbuf, "/", rlen);
+ strlcat(rbuf, word_list[i][0], rlen);
+ if (word_list[i][1] != NULL) {
+ strlcat(rbuf, ";", rlen);
+ join(temp, sizeof(temp), ",", &word_list[i][1]);
+ strlcat(rbuf, temp, rlen);
+ }
+ }
+}
+
/* Must be prime */
#define HASH_SIZE 4099
static void
@@ -355,6 +477,7 @@
result[rlen] = '/';
result[rlen + 1] = '\n';
result[rlen + 2] = '\0';
+ reorganize_result(result, sizeof(result));
write(out, result, strlen(result));
} else {
rbuf[0] = SKKSERV_S_NOT_FOUND;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment