bada90dec390bf6cb9314022b118f40ed2d21397
[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 #ifndef lint
29 static char sccsid[] = "@(#)issort.c 1.3 89/07/17 Copyr 1988 Sun Micro";
30 #endif
31
32 /*
33  * Copyright (c) 1988 by Sun Microsystems, Inc.
34  */
35
36 /*
37  * issort.c
38  *
39  * Description:
40  *      ISAM sort package (sort in memory)
41  */
42 #include <stdlib.h>
43
44 #include "isam_impl.h"
45
46 extern char *_isunix_malloc();
47
48 /* 
49  * _issort_create() 
50  *
51  * Create a sorter
52  */
53
54 Issort *
55 _issort_create(int reclen, int nrecs, int (*compfunc)())
56 {       
57     Issort      *p;
58
59     p = (Issort *)_ismalloc(sizeof(*p));     /* Allocate new sorter */
60     memset((char *)p, 0,sizeof(*p));
61
62     p->ist_reclength = reclen;               /* Record length in bytes */
63     p->ist_allocrecs = nrecs;                /* Maximum number of records */
64                                              /* that can inserted */
65     p->ist_nrecs = 0;                        /* Current number of records */
66     p->ist_currec;                           /* Current position */
67     p->ist_compf = compfunc;                 /* Comparison function */
68
69     p->ist_array = _ismalloc((unsigned)(reclen * nrecs)); /* Allocate array */
70                                              /* for storing records */
71
72     return(p);
73 }
74
75 /* 
76  * _issort_destroy() 
77  *
78  * Destroy a sorter 
79  */
80
81 void
82 _issort_destroy(Issort *srt)
83 {
84     assert(srt->ist_array != (char *) 0);
85
86     (void) free(srt->ist_array);
87     (void) free((char *)srt);
88 }
89
90 /* 
91  * _issort_insert() 
92  *
93  * Insert record to sorter 
94  */
95
96 void
97 _issort_insert(Issort *srt, char *record)
98 {
99     assert(srt->ist_nrecs < srt->ist_allocrecs);
100
101     memcpy(srt->ist_array + srt->ist_nrecs * srt->ist_reclength,record,
102            srt->ist_reclength);
103
104     srt->ist_currec = srt->ist_nrecs++;
105 }
106
107 /* 
108  * _issort_sort() 
109  *
110  *  Sort records 
111  */
112
113 void
114 _issort_sort(Issort *srt)
115 {
116     if (srt->ist_nrecs > 1)
117         qsort(srt->ist_array,srt->ist_nrecs,srt->ist_reclength,srt->ist_compf);
118
119     _issort_rewind(srt);                     /* Rewind for subsequent reads */
120 }
121
122 /* 
123  * _issort_rewind() 
124  * 
125  * Rewind sorter 
126  */
127
128 void
129 _issort_rewind(Issort *srt)
130 {
131     srt->ist_currec = 0;
132 }
133
134 /* 
135  * _issort_read() 
136  * 
137  * Read record from sorter 
138  */
139
140 char *
141 _issort_read(Issort *srt)
142 {
143     return((srt->ist_currec < srt->ist_nrecs) ?
144         (srt->ist_array + srt->ist_currec++ * srt->ist_reclength) :
145             (char *) 0);
146 }