comparison anagram/guisupport/help.cpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, A System for Syntax Directed Programming
3 * Copyright 2006 David A. Holland. All Rights Reserved.
4 * See the file COPYING for license and usage terms.
5 *
6 * help.cpp - help data.
7 *
8 * This file obsoletes the external AnaGram.hlp file found
9 * in older versions of AnaGram.
10 */
11
12 #include "port.h" // for OPTLINK
13
14 #include "agstring.h"
15 #include "assert.h"
16 #include "help.h"
17
18 //#define INCLUDE_LOGGING
19 #include "log.h"
20
21
22 #include "helpdata.h"
23
24
25 static const HelpTopic empty_topic = {
26 "Secret of Life",
27 "No help message for this topic."
28 };
29
30 const char *helptopic_gettitle(const HelpTopic *ht) {
31 return ht->title;
32 }
33
34 const char *helptopic_gettext(const HelpTopic *ht) {
35 return ht->body;
36 }
37
38 ////////////////////////////////////////////////////////////
39
40 static int OPTLINK helptable_sort(const void *ventrya, const void *ventryb) {
41 const helpentry *entrya = (const helpentry *)ventrya;
42 const helpentry *entryb = (const helpentry *)ventryb;
43 return stricmp(entrya->topic, entryb->topic);
44 }
45
46 static int OPTLINK helptable_search(const void *vkey, const void *ventryb) {
47 const char *key = (const char *)vkey;
48 const helpentry *entryb = (const helpentry *)ventryb;
49 return stricmp(key, entryb->topic);
50 }
51
52 static int OPTLINK topiclist_sort(const void *ventrya, const void *ventryb) {
53 const char *const *entrya = (const char *const *)ventrya;
54 const char *const *entryb = (const char *const *)ventryb;
55 return stricmp(*entrya, *entryb);
56 }
57
58 void help_init(void) {
59 LOGSECTION("help_init");
60 qsort(helptable, helptablenum, sizeof(helptable[0]), helptable_sort);
61 qsort(topiclist, numtopiclist, sizeof(topiclist[0]), topiclist_sort);
62 }
63
64 static const HelpTopic *try_help_topic(const char *topic) {
65 LOGSECTION("help_get");
66 const void *ventry;
67 const helpentry *entry;
68 ventry = bsearch(topic, helptable, helptablenum, sizeof(helptable[0]),
69 helptable_search);
70 if (!ventry) {
71 //fprintf(stderr, "warning: help topic <%s> not found\n", topic);
72 return NULL;
73 }
74 entry = (const helpentry *)ventry;
75 return entry->data;
76 }
77
78 ////////////////////////////////////////////////////////////
79
80 const HelpTopic *help_topic(const char *title) {
81 const HelpTopic *ht;
82
83 ht = try_help_topic(title);
84 if (ht) {
85 return ht;
86 }
87
88 AgString titlecopy = title;
89 size_t len = titlecopy.size();
90 if (len > 0 && titlecopy[len-1] == 's') {
91 titlecopy[len-1] = 0;
92 ht = try_help_topic(titlecopy.pointer());
93 if (ht) {
94 return ht;
95 }
96 }
97
98 // The old help code used to do this. But the new help compiler
99 // checks crossreferences (up to trailing 's') so it can't be
100 // triggered.
101 #if 0
102 char *t = titlecopy.pointer();
103 while (len) {
104 while (len && t[len] != ' ') len--;
105 while (len && (t[len] == ' ' || ispunct(t[len]))) len--;
106 t[len+1] = 0;
107 ht = try_help_topic(t);
108 if (ht) {
109 return ht;
110 }
111 }
112 #endif
113
114 return &empty_topic;
115 }
116
117 int help_topicexists(const char *title) {
118 // This is *not* supposed to check alternate spellings.
119 const HelpTopic *ht = try_help_topic(title);
120 return ht != NULL;
121 }
122
123 unsigned helpindex_num(void) {
124 return numtopiclist;
125 }
126
127 const char *helpindex_get(unsigned ix) {
128 assert(ix < numtopiclist);
129 return topiclist[ix];
130 }