configure: create some convenience AC_SUBST's for the global includes
[oweals/cde.git] / cde / lib / tt / mini_isam / issort.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                   */
24 /*%%  (c) Copyright 1993, 1994 International Business Machines Corp.     */
25 /*%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                    */
26 /*%%  (c) Copyright 1993, 1994 Novell, Inc.                              */
27 /*%%  $XConsortium: issort.c /main/3 1995/10/23 11:44:55 rswiston $                                                      */
28
29 /*
30  * Copyright (c) 1988 by Sun Microsystems, Inc.
31  */
32
33 /*
34  * issort.c
35  *
36  * Description:
37  *      ISAM sort package (sort in memory)
38  */
39 #include <stdlib.h>
40
41 #include "isam_impl.h"
42
43 extern char *_isunix_malloc();
44
45 /* 
46  * _issort_create() 
47  *
48  * Create a sorter
49  */
50
51 Issort *
52 _issort_create(int reclen, int nrecs, int (*compfunc)(char *, char *))
53 {       
54     Issort      *p;
55
56     p = (Issort *)_ismalloc(sizeof(*p));     /* Allocate new sorter */
57     memset((char *)p, 0,sizeof(*p));
58
59     p->ist_reclength = reclen;               /* Record length in bytes */
60     p->ist_allocrecs = nrecs;                /* Maximum number of records */
61                                              /* that can inserted */
62     p->ist_nrecs = 0;                        /* Current number of records */
63     p->ist_currec = 0;                       /* Current position */
64     p->ist_compf = compfunc;                 /* Comparison function */
65
66     p->ist_array = _ismalloc((unsigned)(reclen * nrecs)); /* Allocate array */
67                                              /* for storing records */
68
69     return(p);
70 }
71
72 /* 
73  * _issort_destroy() 
74  *
75  * Destroy a sorter 
76  */
77
78 void
79 _issort_destroy(Issort *srt)
80 {
81     assert(srt->ist_array != (char *) 0);
82
83     (void) free(srt->ist_array);
84     (void) free((char *)srt);
85 }
86
87 /* 
88  * _issort_insert() 
89  *
90  * Insert record to sorter 
91  */
92
93 void
94 _issort_insert(Issort *srt, char *record)
95 {
96     assert(srt->ist_nrecs < srt->ist_allocrecs);
97
98     memcpy(srt->ist_array + srt->ist_nrecs * srt->ist_reclength,record,
99            srt->ist_reclength);
100
101     srt->ist_currec = srt->ist_nrecs++;
102 }
103
104 /* 
105  * _issort_sort() 
106  *
107  *  Sort records 
108  */
109
110 void
111 _issort_sort(Issort *srt)
112 {
113     if (srt->ist_nrecs > 1)
114         qsort(srt->ist_array,srt->ist_nrecs,srt->ist_reclength,srt->ist_compf);
115
116     _issort_rewind(srt);                     /* Rewind for subsequent reads */
117 }
118
119 /* 
120  * _issort_rewind() 
121  * 
122  * Rewind sorter 
123  */
124
125 void
126 _issort_rewind(Issort *srt)
127 {
128     srt->ist_currec = 0;
129 }
130
131 /* 
132  * _issort_read() 
133  * 
134  * Read record from sorter 
135  */
136
137 char *
138 _issort_read(Issort *srt)
139 {
140     return((srt->ist_currec < srt->ist_nrecs) ?
141         (srt->ist_array + srt->ist_currec++ * srt->ist_reclength) :
142             (char *) 0);
143 }