Character-set-isation and configurability in the WinHelp backend.
[sgt/halibut] / misc.c
diff --git a/misc.c b/misc.c
index 6f4ddd4..aa05878 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -2,8 +2,13 @@
  * misc.c: miscellaneous useful items
  */
 
+#include <stdarg.h>
 #include "halibut.h"
 
+char *adv(char *s) {
+    return s + 1 + strlen(s);
+}
+
 struct stackTag {
     void **data;
     int sp;
@@ -189,10 +194,25 @@ int compare_wordlists(word *a, word *b) {
            }
        }
 
+#ifdef HAS_WCSCOLL
+       {
+           wchar_t a[2], b[2];
+           int ret;
+
+           a[0] = pos[0].c;
+           b[0] = pos[1].c;
+           a[1] = b[1] = L'\0';
+
+           ret = wcscoll(a, b);
+           if (ret)
+               return ret;
+       }
+#else
        if (pos[0].c < pos[1].c)
            return -1;
        else if (pos[0].c > pos[1].c)
            return +1;
+#endif
 
        if (!pos[0].c)
            break;                     /* they're equal */
@@ -208,23 +228,22 @@ int compare_wordlists(word *a, word *b) {
     return compare_wordlists_literally(a, b);
 }
 
-void mark_attr_ends(paragraph *sourceform) {
-    paragraph *p;
+void mark_attr_ends(word *words)
+{
     word *w, *wp;
-    for (p = sourceform; p; p = p->next) {
-       wp = NULL;
-       for (w = p->words; w; w = w->next) {
-           if (isattr(w->type)) {
-               int before = (wp && isattr(wp->type) &&
-                             sameattr(wp->type, w->type));
-               int after = (w->next && isattr(w->next->type) &&
-                            sameattr(w->next->type, w->type));
-               w->aux |= (before ?
-                          (after ? attr_Always : attr_Last) :
-                          (after ? attr_First : attr_Only));
-           }
-           wp = w;
+
+    wp = NULL;
+    for (w = words; w; w = w->next) {
+       if (isattr(w->type)) {
+           int before = (wp && isattr(wp->type) &&
+                         sameattr(wp->type, w->type));
+           int after = (w->next && isattr(w->next->type) &&
+                        sameattr(w->next->type, w->type));
+           w->aux |= (before ?
+                      (after ? attr_Always : attr_Last) :
+                      (after ? attr_First : attr_Only));
        }
+       wp = w;
     }
 }
 
@@ -479,3 +498,65 @@ void wrap_free(wrappedline *w) {
        w = t;
     }
 }
+
+void cmdline_cfg_add(paragraph *cfg, char *string)
+{
+    wchar_t *ustring;
+    int upos, ulen, pos, len;
+
+    ulen = 0;
+    while (cfg->keyword[ulen])
+       ulen += 1 + ustrlen(cfg->keyword+ulen);
+    len = 0;
+    while (cfg->origkeyword[len])
+       len += 1 + strlen(cfg->origkeyword+len);
+
+    ustring = ufroma_locale_dup(string);
+
+    upos = ulen;
+    ulen += 2 + ustrlen(ustring);
+    cfg->keyword = resize(cfg->keyword, ulen);
+    ustrcpy(cfg->keyword+upos, ustring);
+    cfg->keyword[ulen-1] = L'\0';
+
+    pos = len;
+    len += 2 + strlen(string);
+    cfg->origkeyword = resize(cfg->origkeyword, len);
+    strcpy(cfg->origkeyword+pos, string);
+    cfg->origkeyword[len-1] = '\0';
+
+    sfree(ustring);
+}
+
+paragraph *cmdline_cfg_new(void)
+{
+    paragraph *p;
+
+    p = mknew(paragraph);
+    memset(p, 0, sizeof(*p));
+    p->type = para_Config;
+    p->next = NULL;
+    p->fpos.filename = "<command line>";
+    p->fpos.line = p->fpos.col = -1;
+    p->keyword = ustrdup(L"\0");
+    p->origkeyword = dupstr("\0");
+
+    return p;
+}
+
+paragraph *cmdline_cfg_simple(char *string, ...)
+{
+    va_list ap;
+    char *s;
+    paragraph *p;
+
+    p = cmdline_cfg_new();
+    cmdline_cfg_add(p, string);
+
+    va_start(ap, string);
+    while ((s = va_arg(ap, char *)) != NULL)
+       cmdline_cfg_add(p, s);
+    va_end(ap);
+
+    return p;
+}