summaryrefslogtreecommitdiff
path: root/support/dktools/dk3srch.ctr
blob: f80a2c57009f5b2e273843bd728b254312b3dc67 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2011-xxxx
license		=	bsd



%%	header

#include "dk3conf.h"
#include "dk3types.h"

#ifdef __cplusplus
extern "C" {
#endif

/**	Open search structure, allocate memory.
	@param	i	Flag: Inverse sort order.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Pointer to new structure on success, NULL on error.
*/
dk3_search_t *
dk3search_open_app(int i, dk3_app_t *app);

/**	Close search structure, release memory.
	@param	sp	Search structure to close.
*/
void
dk3search_close(dk3_search_t *sp);

/**	Add one result to result set.
	@param	sp	Search structure.
	@param	fn	File name to add.
	@return	1 on success, 0 on error (not enough memory).
*/
int
dk3search_add(dk3_search_t *sp, dkChar const *fn);

/**	Reset result traversal to traverse results again.
	@param	sp	Search structure.
*/
void
dk3search_reset(dk3_search_t *sp);

/**	Get next result entry.
	@param	sp	Search structure.
	@return	Pointer to next file name or NULL.
*/
dkChar const *
dk3search_next(dk3_search_t *sp);


#ifdef __cplusplus
}
#endif



%%	module

#include "dk3all.h"



$(trace-include)



/**	One result node.
*/
typedef struct _dk3_search_node_t_ {
  unsigned long	n;	/**< Node number. */
  dkChar const	*fn;	/**< File name. */
} dk3_search_node_t;



/**	Compare two line nodes.
	@param	l	Left node.
	@param	r	Right node.
	@param	cr	Comparison criteria.
	@return	Comparison result.
*/
static
int
node_compare(void const *l, void const *r, int cr)
{
  int back = 0;
  dk3_search_node_t const	*pl;	/* Left node. */
  dk3_search_node_t const	*pr;	/* Right node. */
  if(l) {
    if(r) {
      pl = (dk3_search_node_t const *)l;
      pr = (dk3_search_node_t const *)r;
      if(pl->n > pr->n) { back = 1; }
      else { if(pl->n < pr->n) { back = -1; } }
    } else { back = 1; }
  } else {
    if(r) { back = -1; }
  }
  if(cr) { back = 0 - back; }
  return back;
}



/**	Delete one node, release memory.
	@param	n	Node to delete.
*/
static
void
dk3search_node_delete(dk3_search_node_t *n)
{
  if(n) {
    dk3_release(n->fn);
    n->n = 0UL;
    dk3_delete(n);
  }
}



/**	Create new node, allocate memory.
	@param	n	Node number.
	@param	fn	File name.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Pointer to new node on success, NULL on error.
*/
static
dk3_search_node_t *
dk3search_node_new_app(unsigned long n, dkChar const *fn, dk3_app_t *app)
{
  dk3_search_node_t *back = NULL;
  $? "+ dk3search_node_new_app \"%!ds\"", fn
  back = dk3_new_app(dk3_search_node_t,1,app);
  if(back) {
    back->n = n;
    back->fn = dk3str_dup_app(fn, app);
    if(!(back->fn)) {	$? "! failed to create string copy"
      dk3search_node_delete(back); back = NULL;
    }
  } $? "- dk3search_node_new_app %!8s", TR_8PTR(back)
  return back;
}



void
dk3search_close(dk3_search_t *sp)
{
  dk3_search_node_t *n = NULL;
  if(sp) {
    if(sp->s_fn) {
      if(sp->i_fn) {
        dk3sto_it_reset(sp->i_fn);
	while((n = (dk3_search_node_t *)dk3sto_it_next(sp->i_fn)) != NULL) {
	  dk3search_node_delete(n);
	}
        dk3sto_it_close(sp->i_fn);
      }
      dk3sto_close(sp->s_fn);
    } sp->s_fn = NULL; sp->i_fn = NULL;
    sp->app = NULL;
    dk3_delete(sp);
  }
}



dk3_search_t *
dk3search_open_app(int i, dk3_app_t *app)
{
  dk3_search_t *back = NULL;
  back = dk3_new_app(dk3_search_t,1,app);
  if(back) {
    back->s_fn = NULL; back->i_fn = NULL; back->inverted = (i ? 1 : 0);
    back->nf = 0UL; back->app = app;
    back->s_fn = dk3sto_open_app(app);
    if(back->s_fn) {
      back->i_fn = dk3sto_it_open(back->s_fn);
      dk3sto_set_comp(back->s_fn, node_compare, 0);
    }
    if(!((back->s_fn) && (back->i_fn))) {
      dk3search_close(back); back = NULL;
    }
  }
  return back;
}



int
dk3search_add(dk3_search_t *sp, dkChar const *fn)
{
  int back = 0;
  dk3_search_node_t *n = NULL;	/* New node. */
  $? "+ dk3search_add \"%!ds\"", TR_STR(fn)
  if((sp) && (fn)) {
    n = dk3search_node_new_app(sp->nf, fn, sp->app);
    if(n) {
      if(dk3sto_add(sp->s_fn, n)) {
        back = 1;
	sp->nf += 1UL;
      } else {
        dk3search_node_delete(n);
      }
    }
  } $? "- dk3search_add %d", back
  return back;
}



void
dk3search_reset(dk3_search_t *sp)
{
  if(sp) {
    dk3sto_it_reset(sp->i_fn);
  }
}



dkChar const *
dk3search_next(dk3_search_t *sp)
{
  dkChar const *back = NULL;
  dk3_search_node_t	*node = NULL;	/* Next node. */
  if(sp) {
    node = (dk3_search_node_t *)dk3sto_it_next(sp->i_fn);
    if(node) {
      back = node->fn;
    }
  } $? "= dk3search_next \"%!ds\"", TR_STR(back)
  return back;
}



/* vim: set ai sw=2 : */