1 /* $Id: ncbisrti.h,v 6.3 2003/12/03 02:10:52 kans Exp $
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name:  $RCSfile: ncbisrti.h,v $
27 *
28 * Author:  Sergei Shavirin
29 *
30 * Initial Version Creation Date: 03/24/1997
31 *
32 * $Revision: 6.3 $
33 *
34 * File Description:
35 *         Internal include file for SORTing library
36 *
37 * $Log: ncbisrti.h,v $
38 * Revision 6.3  2003/12/03 02:10:52  kans
39 * added defines missing from Mac OS 10.3 headers
40 *
41 * Revision 6.2  1998/06/01 14:52:38  madden
42 * Change to using TmpNam
43 *
44 * Revision 6.1  1997/12/02 18:58:19  shavirin
45 * Chanded defines for sprintf to remove typecast warnings
46 *
47 * Revision 6.0  1997/08/25 18:53:39  madden
48 * Revision changed to 6.0
49 *
50 * Revision 1.1  1997/03/13 21:52:44  shavirin
51 * Initial revision
52 *
53 *
54 * ==========================================================================
55 */
56 
57 #ifndef _NCBISRTI_H_
58 #define _NCBISRTI_H_ ncbisrti_h
59 
60 #include <ncbisort.h>
61 
62 #ifdef OS_UNIX
63 #include <signal.h>
64 #endif
65 
66 /* Initial buffer size for in core sorting.  Will not grow unless a
67    line longer than this is seen. */
68 
69 /* Prefix for temporary file names. */
70 
71 #ifndef OS_UNIX
72 
73 #define SORTALLOC 32767
74 #define PREFIX NULL
75 
76 #else /* OS_UNIX */
77 
78 #define SORTALLOC 524288
79 #define PREFIX "/tmp"                      /* Default prefix */
80 
81 #endif /* OS_UNIX */
82 
83 /* Missing from /usr/include/gcc/darwin/3.3/machine/limits.h */
84 #ifdef __MWERKS__
85 #ifdef OS_UNIX_DARWIN
86 #ifndef __SCHAR_MAX__
87 #define __SCHAR_MAX__ 127
88 #endif
89 #endif
90 #endif
91 /* End missing from /usr/include/gcc/darwin/3.3/machine/limits.h */
92 
93 #define MERGEALLOC 16384
94 
95 #define UCHAR_LIM (UCHAR_MAX + 1)
96 #define UCHAR(c) ((Uchar) (c))
97 
98 /* Table of digits. */
99 static Int4 digits[UCHAR_LIM];
100 
101 /* Table of white space. */
102 static Int4 blanks[UCHAR_LIM];
103 
104 /* Translation table folding upper case to lower. */
105 static Uchar fold_tolower[UCHAR_LIM];
106 
107 /* Table mapping 3-letter month names to integers.
108    Alphabetic order allows binary search. */
109 
110 static struct month {
111     CharPtr name;
112     Int4 val;
113 } monthtab[] = {
114     {"apr", 4},
115     {"aug", 8},
116     {"dec", 12},
117     {"feb", 2},
118     {"jan", 1},
119     {"jul", 7},
120     {"jun", 6},
121     {"mar", 3},
122     {"may", 5},
123     {"nov", 11},
124     {"oct", 10},
125     {"sep", 9}
126 };
127 
128 /* During the merge phase, the number of files to merge at once. */
129 
130 #define NMERGE 16
131 static Boolean tables_initialized = FALSE;
132 
133 /* Lines are held in core as counted strings. */
134 typedef struct SORTLine
135 {
136   UcharPtr text;	 /* Text of the line. */
137   Int4     length;       /* Length not including final newline. */
138   UcharPtr keybeg;       /* Start of first key. */
139   UcharPtr keylim;	 /* Limit of first key. */
140 } SORTLine, PNTR SORTLinePtr;
141 
142 /* Arrays of lines. */
143 typedef struct SORTLines
144 {
145   SORTLinePtr lines;            /* Dynamically allocated array of lines. */
146   Int4 used;                    /* Number of slots used. */
147   Int4 alloc;                   /* Number of slots allocated. */
148 }SORTLines, PNTR SORTLinesPtr;
149 
150 /* Input buffers. */
151 typedef struct SORTBuffer
152 {
153   UcharPtr buf;			/* Dynamically allocated buffer. */
154   Int4 used;			/* Number of bytes used. */
155   Int4 alloc;			/* Number of bytes allocated. */
156   Int4 left;			/* Number of bytes left after line parsing. */
157 }SORTBuffer, PNTR SORTBufferPtr;
158 
159 /* The list of temporary files. */
160 
161 typedef struct SORTempNode
162 {
163   CharPtr name;
164   struct SORTempNode *next;
165 } SORTempNode, PNTR SORTempNodePtr;
166 
167 typedef struct SORTData
168 {
169   SORTempNodePtr temphead;
170   SORTKeyFieldPtr keyhead;
171   Int4 temp_seq;
172   Int4 sortalloc;
173   Int4 mergealloc;
174   Int4 linelength;
175   CharPtr prefix;
176   Boolean reverse;
177   Uchar tab;
178   Boolean unique;
179 } SORTData, PNTR SORTDataPtr;
180 
181 static SORTempNodePtr global_temphead;
182 
183 /* To handle ALL temp files opened by (may be)
184    multy-thread application */
185 
186 static void SORTCleanup (SORTempNodePtr temphead);
187 static void SORTDelTempName(CharPtr name, SORTempNodePtr temphead);
188 static SORTErrorCode SORTInitBuf (SORTBufferPtr buf, Int4 alloc);
189 static Int4 SORTFillBuf (SORTBufferPtr buf, FILE *fp);
190 static SORTErrorCode SORTInitLines (SORTLinesPtr lines, Int4 alloc);
191 static UcharPtr SORTBegField(SORTLinePtr line, SORTKeyFieldPtr key,
192                              SORTDataPtr sdp);
193 static UcharPtr SORTLimField(SORTLinePtr line, SORTKeyFieldPtr key,
194                              SORTDataPtr sdp);
195 static SORTErrorCode SORTFindLines(SORTBufferPtr buf, SORTLinesPtr lines,
196                           SORTDataPtr sdp);
197 static Int4 SORTFracCompare (UcharPtr a, UcharPtr b);
198 static Int4 SORTNumCompare (UcharPtr a, UcharPtr b);
199 static Int4 SORTGetMonth (UcharPtr s, Int4 len);
200 static Int4 SORTKeyCompare(SORTLinePtr a, SORTLinePtr b, SORTDataPtr sdp);
201 static Int4 SORTCompare(register SORTLinePtr a, register SORTLinePtr b,
202                         SORTDataPtr sdp);
203 static Int4 SORTMergeFPS(FILE *fps[], register Int4 nfps,
204                          FILE *ofp, SORTDataPtr sdp);
205 static void SORTArrayLines(SORTLinePtr lines,  Int4 nlines,
206                       SORTLinePtr temp, SORTDataPtr sdp);
207 static Int4 SORTCheckOrder(FILE *fp, SORTDataPtr sdp);
208 
209 #ifdef OS_UNIX
210 #ifdef OS_UNIX_IRIX
211 static void inthandler(void);
212 #else
213 static void inthandler(int i);
214 #endif
215 #endif
216 
217 #endif
218