summaryrefslogtreecommitdiff
path: root/support/dktools/dk3srch.c
blob: 31b2c9ff483c63b5b7a5be87fd0f39ec55e49747 (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
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk3srch.ctr
*/

/*
Copyright (C) 2011-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file dk3srch.c The dk3srch module.
*/


#line 61 "dk3srch.ctr"

#include "dk3all.h"






#line 67 "dk3srch.ctr"



/**	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;
  

#line 136 "dk3srch.ctr"
  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)) {	

#line 141 "dk3srch.ctr"
      dk3search_node_delete(back); back = NULL;
    }
  } 

#line 144 "dk3srch.ctr"
  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. */
  

#line 199 "dk3srch.ctr"
  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);
      }
    }
  } 

#line 210 "dk3srch.ctr"
  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;
    }
  } 

#line 236 "dk3srch.ctr"
  return back;
}



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