summaryrefslogtreecommitdiff
path: root/support/dktools/hbindex.ctr
blob: 2e2f872dcf79247b0facb9addee0b98ada2477eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
%%	options
copyright owner	=	Dirk Krause
copyright year	=	2017-xxxx
SPDX-License-Identifier:	BSD-3-Clause

%%	header

#ifdef __cplusplus
extern "C" {
#endif

/**	Create index entry, allocate memory.
	@param	tx	Index text.
	@param	fn	File name.
	@param	an	Anchor name (optional).
	@param	ul	Anchor number, used if no anchor name specified.
	@param	app	Application structure for diagnostics, may be NULL.
	@param	nptr	Current node.
	@return	Pointer to new entry on success, NULL on error.
*/
hb_index_entry_t *
hbindex_new(
  dkChar const		*tx,
  dkChar const		*fn,
  dkChar const		*an,
  unsigned long	const	 ul,
  dk3_app_t		*app,
  hb_node_t		*nptr
);

/**	Destroy index entry, release memory.
	@param	pi	Index entry to destroy.
*/
void
hbindex_delete(hb_index_entry_t *pi);

/**	Compare two index entries.
	@param	l	Left entry.
	@param	r	Right entry or name.
	@param	cr	Comparison criteria (0=object/object, 1=object/name).
	@return	Comparison result.
*/
int
hbindex_compare(void const *l, void const *r, int cr);

/**	Write anchor name for index entry (either given name or number-based).
	@param	job	Job structure.
	@param	in	Index entry.
	@return	1 on success, 0 on error.
*/
int
hb_index_write_anchor(hb_job_t *job, hb_index_entry_t *in);

#ifdef __cplusplus
}
#endif

%%	module


#include "dk3all.h"
#include "htmlbook.h"



$!trace-include



int
hbindex_compare(void const *l, void const *r, int cr)
{
  hb_index_entry_t const	*pl;
  hb_index_entry_t const	*pr;
  int			 back	= 0;
  $? "+ hbindex_compare PTR=%d PTR=%d", TR_IPTR(l), TR_IPTR(r)
  if(l) {
    if(r) {
      pl = (hb_index_entry_t const *)l;
      switch(cr) {
        case 1: {
	  back = dk3str_casecmp(pl->tx, (dkChar const *)r);
	} break;
	default: {
	  pr = (hb_index_entry_t const *)r;
	  back = dk3str_casecmp(pl->tx, pr->tx);
	  if(0 == back) {
#if VERSION_BEFORE_20140423
	    if(pl->fn) {
	      if(pr->fn) {
	        back = dk3str_fncmp(pl->fn, pr->fn);
	      } else back = 1;
	    } else {
	      if(pr->fn) back = -1;
	    }
#else
	    if (pl->no) {
	      if (pr->no) {
	        if ((pl->no)->objno > (pr->no)->objno) {
		  back = 1;
		} else {
		  if ((pr->no)->objno > (pl->no)->objno) {
		    back = -1;
		  }
		}
	      } else back = 1;
	    } else {
	      if (pr->no) back = -1;
	    }
#endif
	  }
	  if(0 == back) {
	    if(pl->an) {
	      if(pr->an) {
	        back = dk3str_cmp(pl->an, pr->an);
	      } else back = 1;
	    } else {
	      if(pr->an) back = -1;
	      else {
	        if(pl->ul > pr->ul) {
		  back = 1;
		} else {
		  if(pl->ul < pr->ul) {
		    back = -1;
		  }
		}
	      }
	    }
	  }
	} break;
      }
      if(-1 > back) { back = -1; }
      if( 1 < back) { back =  1; }
    } else {
      back = 1;
    }
  } else {
    if(r) {
      back = -1;
    }
  } $? "- hbindex_compare %d", back
  return back;
}



void
hbindex_delete(hb_index_entry_t *pi)
{
  $? "+ hbindex_delete"
  if(pi) {
    $? ". anchor = \"%!ds\"", TR_STR(pi->an)
    $? ". number = %lu", pi->ul
    $? ". file   = \"%!ds\"", TR_STR(pi->fn)
    $? ". text   = \"%!ds\"", TR_STR(pi->tx)
    pi->no = NULL;
    dk3_release(pi->tx);
    dk3_release(pi->fn);
    dk3_release(pi->an);
    pi->ul = 0UL;
    dk3_delete(pi);
  } $? "- hbindex_delete"
}


hb_index_entry_t *
hbindex_new(
  dkChar const		*tx,
  dkChar const		*fn,
  dkChar const		*an,
  unsigned long	const	 ul,
  dk3_app_t		*app,
  hb_node_t		*nptr
)
{
  hb_index_entry_t	*back	= NULL;	/* New index entry */
  int			 ok	= 0;
  $? "+ hbindex_new \"%!ds\" \"%!ds\" \"%!ds\", %lu", TR_STR(tx), TR_STR(fn), TR_STR(an), ul
  if((tx) && (fn)) {
    back = dk3_new_app(hb_index_entry_t,1,app);
    if(back) {
      back->no = nptr;
      back->tx = NULL;
      back->fn = NULL;
      back->an = NULL;
      back->ul = ul;
      back->tx = dk3str_dup_app(tx,app);
      back->fn = dk3str_dup_app(fn,app);
      if((back->tx) && (back->fn)) {
        ok = 1;
	if(an) {
	  back->an = dk3str_dup_app(an,app);
	  if(!(back->an)) { ok = 0; }
	}
      }
      if(!(ok)) {
        hbindex_delete(back);
	back = NULL;
      }
    }
  } $? "- hbindex_new PTR=%d", TR_IPTR(back)
  return back;
}



int
hb_index_write_anchor(hb_job_t *job, hb_index_entry_t *in)
{
  int			 back = 0;
  $? "+ hb_index_write_anchor"
  if((job) && (in)) {
    back = 1;
    if(in->an) {
      if(!hbhtml_url_output_for_text(job, in->an)) {
        back = 0;
      }
    } else {
#if DK3_SIZEOF_LONG > 4
      fprintf(job->of, "hb_i%016lx", in->ul);
#else
      fprintf(job->of, "hb_i%08lx", in->ul);
#endif
    }
  } $? "- hb_index_write_anchor %d", back
  return back;
}