1 /* $Id: ncbi_std.h 417725 2013-11-08 18:44:19Z rafanovi $
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  *
27  */
28 
29 /** @file ncbi_std.h
30  * Type and macro definitions from C toolkit that are not defined in C++
31  * toolkit.
32  */
33 
34 
35 
36 #ifndef ALGO_BLAST_CORE__NCBI_STD
37 #define ALGO_BLAST_CORE__NCBI_STD
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <math.h>
44 #include <ctype.h>
45 #include <assert.h>
46 
47 #define NCBI_XBLAST_EXPORT
48 
49 /* which toolkit are we using? */
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 #ifndef NCBI_RESTRICT /* now defined in the C++ Toolkit */
56 /** For some reason, ICC claims a suitable __STDC_VERSION__ but then
57    barfs on restrict. Added ECC per Haruna Cofer */
58 #if defined(__ICC) || defined(__ECC)
59 #define NCBI_RESTRICT __restrict
60 #elif __STDC_VERSION__ >= 199901  &&  (!defined(__IBMC__) || defined(__C99_RESTRICT))
61 #define NCBI_RESTRICT restrict
62 #else
63 #define NCBI_RESTRICT
64 #endif
65 #endif
66 
67 /* inlining support -- compiler dependent */
68 #if defined(__cplusplus)  ||  __STDC_VERSION__ >= 199901
69 /** C++ and C99 both guarantee "inline" */
70 #define NCBI_INLINE inline
71 #elif defined(__GNUC__)
72 /* So does GCC, normally, but it may be running with strict options
73    that require the extra underscores */
74 #define NCBI_INLINE __inline__
75 #elif defined(_MSC_VER)  ||  defined(__sgi) || defined(HPUX)
76 /* MSVC and (older) MIPSpro always require leading underscores */
77 #define NCBI_INLINE __inline
78 #else
79 /** "inline" seems to work on our remaining in-house compilers
80    (WorkShop, Compaq, ICC, MPW) */
81 #define NCBI_INLINE inline
82 #endif
83 
84 #ifdef _MSC_VER
85 #define strcasecmp _stricmp
86 #define strdup _strdup
87 #define snprintf _snprintf
88 #endif
89 
90 #ifndef _NCBISTD_ /* if we're not in the C toolkit... */
91 /** bool replacment for C */
92 #ifndef TRUE
93 /** bool replacment for C indicating true. */
94 #define TRUE 1
95 #endif
96 #ifndef FALSE
97 /** bool replacment for C indicating false. */
98 #define FALSE 0
99 #endif
100 #endif
101 
102 /** macro for assert. */
103 #ifndef ASSERT
104 #define ASSERT assert
105 #endif
106 
107 #ifndef MIN
108 /** returns smaller of a and b. */
109 #define MIN(a,b)	((a)>(b)?(b):(a))
110 #endif
111 
112 #ifndef MAX
113 /** returns larger of a and b. */
114 #define MAX(a,b)	((a)>=(b)?(a):(b))
115 #endif
116 
117 #ifndef ABS
118 /** returns absolute value of a (|a|) */
119 #define ABS(a)	((a)>=0?(a):-(a))
120 #endif
121 
122 #ifndef SIGN
123 /** return +1 for a > 0, -1 for a < 0 */
124 #define SIGN(a)	((a)>0?1:((a)<0?-1:0))
125 #endif
126 
127 /* low-level ANSI-style functions */
128 
129 #ifndef _NCBISTD_ /* if we're not in the C toolkit ... */
130 
131 #ifndef UINT4_MAX
132 /** largest number represented by unsigned int. */
133 #define UINT4_MAX     4294967295U
134 #endif
135 
136 #ifndef INT4_MAX
137 /** largest nubmer represented by signed int */
138 #define INT4_MAX    2147483647
139 #endif
140 
141 #ifndef INT4_MIN
142 /** Smallest (most negative) number represented by signed int */
143 #define INT4_MIN    (-2147483647-1)
144 #endif
145 
146 #ifndef NCBIMATH_LN2
147 /** natural log of 2. */
148 #define NCBIMATH_LN2      0.69314718055994530941723212145818
149 #endif
150 
151 #ifndef INT2_MAX
152 /** largest number represented by signed (two byte) short */
153 #define INT2_MAX    32767
154 #endif
155 
156 #ifndef INT2_MIN
157 /** smallest (most negative) number represented by signed (two byte) short */
158 #define INT2_MIN    (-32768)
159 #endif
160 
161 #ifndef INT1_MAX
162 /** largest number represented by signed short (one byte) */
163 #define INT1_MAX    127
164 #endif
165 
166 #ifndef INT1_MIN
167 /** smallest (most negative) number represented by signed short (one byte) */
168 #define INT1_MIN    (-128)
169 #endif
170 
171 #ifndef DIM
172 /** dimension of an array. */
173 #define DIM(A) (sizeof(A)/sizeof((A)[0]))
174 #endif
175 
176 #ifndef NULLB
177 /** terminating byte of a char* string. */
178 #define NULLB '\0'
179 #endif
180 
181 #endif /* _NCBISTD_ */
182 
183 /** 64-bit integers */
184 #ifndef NCBI_CONST_INT8 /* C Toolkit */
185 #  ifdef INT64_C /* stdint.h should have this */
186 #    define NCBI_CONST_INT8(v)   INT64_C(v)
187 #    define NCBI_CONST_UINT8(v)  UINT64_C(v)
188 #  elif defined(_MSC_VER)
189 #    define NCBI_CONST_INT8(v)   v##i64
190 #    define NCBI_CONST_UINT8(v)  v##ui64
191 #  else /* Try treating as (unsigned) long long */
192 #    define NCBI_CONST_INT8(v)   v##LL
193 #    define NCBI_CONST_UINT8(v)  v##ULL
194 #  endif
195 #endif
196 
197 /** Copies memory using memcpy and malloc
198  * @param orig memory to be copied [in]
199  * @param size amount to be copied [in]
200  * @return pointer to newly allocated memory. NULL if orig NULL, size is zero,
201  *   or allocation fails.
202  */
203 NCBI_XBLAST_EXPORT
204 void* BlastMemDup (const void *orig, size_t size);
205 
206 
207 /******************************************************************************/
208 
209 /** A generic linked list node structure */
210 typedef struct ListNode {
211 	Uint1 choice;   /**< to pick a choice */
212 	void *ptr;              /**< attached data */
213 	struct ListNode *next;  /**< next in linked list */
214 } ListNode;
215 
216 /** Create a new list node
217  * @param vnp Pointer to the start of the list, may be NULL [in]
218  * @return newly allocated node
219  */
220 NCBI_XBLAST_EXPORT
221 ListNode* ListNodeNew (ListNode* vnp);
222 
223 /** Add a node to the list.
224  * @param head Pointer to the start of the list, if *head is NULL will
225  *  be Pointer to new node. [in] [out]
226  * @return New node
227  */
228 NCBI_XBLAST_EXPORT
229 ListNode* ListNodeAdd (ListNode** head);
230 
231 /** Add a node to the list with a given choice and data pointer.
232  * @param head Pointer to the start of the list, if *head is NULL will
233  *  be Pointer to new node. [in] [out]
234  * @param choice Choice value for the new node. [in]
235  * @param value Data pointer for the new node. [in]
236  * @return New node
237  */
238 NCBI_XBLAST_EXPORT
239 ListNode* ListNodeAddPointer (ListNode** head, Uint1 choice, void *value);
240 
241 /** Free all list's nodes, does not attempt to free data.
242  * @param vnp objects to be freed [in]
243  * @return NULL
244  */
245 NCBI_XBLAST_EXPORT
246 ListNode* ListNodeFree (ListNode* vnp);
247 
248 /** Free nodes as well as data (vnp->ptr) assuming it is one contiguous chunk.
249  * @param vnp objects to be freed [in]
250  * @return NULL
251  */
252 NCBI_XBLAST_EXPORT
253 ListNode* ListNodeFreeData (ListNode* vnp);
254 
255 /** Add a node to the list with a provided choice, and attached data
256  * pointing to a provided string.
257  * @param head Pointer to the start of the list, if *head is NULL will
258  *  be Pointer to new node. [in] [out]
259  * @param choice sets the "choice" field in ListNode [in]
260  * @param str char* buffer to be copied [in]
261  * @return newly allocated node
262  */
263 NCBI_XBLAST_EXPORT
264 ListNode* ListNodeCopyStr (ListNode** head, Uint1 choice, const char* str);
265 
266 #ifdef __cplusplus
267 }
268 #endif
269 
270 #endif /* !ALGO_BLAST_CORE__NCBI_STD */
271