NCBI C++ ToolKit
ncbi_ansi_ext.h
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef CONNECT___NCBI_ANSI_EXT__H
2 #define CONNECT___NCBI_ANSI_EXT__H
3 
4 /* $Id: ncbi_ansi_ext.h 99849 2023-05-16 20:13:01Z lavr $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Anton Lavrentiev
30  *
31  * File Description:
32 ///@file ncbi_ansi_ext.h
33  * Non-ANSI, yet widely used string/memory functions
34  *
35  */
36 
37 #include <connect/connect_export.h>
38 #include "ncbi_config.h"
39 #include <stddef.h>
40 #include <string.h>
41 
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 
48 #ifndef HAVE_STRNLEN
49 
50 # ifdef strnlen
51 # undef strnlen
52 # endif
53 # define strnlen NCBI_strnlen
54 
55 /** Return the number of characters in the string pointed to by "str" (not
56  * including the terminating '\0' character) but no more than "maxlen" (if the
57  * '\0' character hasn't been found within the first "maxlen" characters).
58  */
60 size_t strnlen(const char* str, size_t maxlen);
61 
62 #endif /*HAVE_STRNLEN*/
63 
64 
65 #ifndef HAVE_STRDUP
66 
67 # ifdef strdup
68 # undef strdup
69 # endif
70 # define strdup NCBI_strdup
71 
72 /** Create a copy of string "str". Return an identical malloc()'ed string,
73  * which must be explicitly deallocated by free() when no longer needed.
74  * Return NULL if the memory allocation failed.
75  */
77 char* strdup(const char* str);
78 
79 #elif defined(NCBI_COMPILER_MSVC)
80 # define strdup _strdup
81 #endif /*HAVE_STRDUP*/
82 
83 
84 #ifndef HAVE_STRNDUP
85 
86 # ifdef strndup
87 # undef strndup
88 # endif
89 # define strndup NCBI_strndup
90 
91 /** Create a copy of up to "n" first characters of string "str". Return a
92  * malloc()'ed and '\0'-terminated string, which must be explicitly
93  * deallocated by free() when no longer needed. Return NULL if the memory
94  * allocation failed.
95  * @note "str" is ignored (and, thus, may be NULL) when "n" is passed as 0,
96  * which results in creating an empty string ("") upon return.
97  */
99 char* strndup(const char* str, size_t n);
100 
101 #endif /*HAVE_STRNDUP*/
102 
103 
104 #ifdef NCBI_COMPILER_MSVC
105 
106 # define strcasecmp _stricmp
107 # define strncasecmp _strnicmp
108 
109 #elif !defined(HAVE_STRCASECMP)
110 
111 # ifdef strcasecmp
112 # undef strcasecmp
113 # undef strncasecmp
114 # endif
115 # define strcasecmp NCBI_strcasecmp
116 # define strncasecmp NCBI_strncasecmp
117 
118 /** Compare "s1" and "s2", ignoring case. Return less than, equal to or
119  * greater than zero if "s1" is lexicographically less than, equal to or
120  * greater than "s2", respectively.
121  */
123 int strcasecmp(const char* s1, const char* s2);
124 
125 /** Compare no more than "n" characters of "s1" and "s2", ignoring case.
126  * Return less than, equal to or greater than zero if the initial fragment of
127  * "s1" is lexicographically less than, equal to or greater than the same
128  * fragment of "s2", respectively.
129  */
131 int strncasecmp(const char* s1, const char* s2, size_t n);
132 
133 #endif /*NCBI_COMPILER_MSVC && HAVE_STRCASECMP*/
134 
135 
136 #ifdef strupr
137 # undef strupr
138 # undef strlwr
139 #endif
140 #define strupr NCBI_strupr
141 #define strlwr NCBI_strlwr
142 
143 /** Convert a string to all uppercase, then return pointer to the altered
144  * string. Because the conversion is made in place, the returned pointer is
145  * the same as the passed one.
146  */
147 char* strupr(char* s);
148 
149 /** Convert a string to all lowercase, then return pointer to the altered
150  * string. Because the conversion is made in place, the returned pointer is
151  * the same as the passed one.
152  */
153 char* strlwr(char* s);
154 
155 
156 /** Copy not more than "n" characters from string "s2" into "s1", and return
157  * the result, which is always '\0'-terminated.
158  * NOTE: The difference of this function from standard strncpy() is in that
159  * the result is _always_ '\0'-terminated and that the function does _not_ pad
160  * "s1" with null bytes should "s2" be shorter than "n" characters.
161  */
163 char* strncpy0(char* s1, const char* s2, size_t n);
164 
165 
166 #ifndef HAVE_MEMCCHR
167 
168 #ifdef memcchr
169 # undef memcchr
170 #endif
171 #define memcchr NCBI_memcchr
172 
173 /** Find the address of the first occurrence of a byte that is NOT char "c"
174  * within the "n" bytes of memory at the address "s". Return NULL if all
175  * bytes are "c".
176  */
177 void* memcchr(const void* s, int c, size_t n);
178 
179 #endif /*!HAVE_MEMCCHR*/
180 
181 
182 #ifndef HAVE_MEMRCHR
183 
184 #ifdef memrchr
185 # undef memrchr
186 #endif
187 #define memrchr NCBI_memrchr
188 
189 /** Find the address of the last occurrence of char "c" within "n" bytes of a
190  * memory block of size "n" beginning at the address "s". Return NULL if no
191  * such byte is found.
192  */
193 void* memrchr(const void* s, int c, size_t n);
194 
195 #endif /*!HAVE_MEMRCHR*/
196 
197 
198 /** Locale-independent double-to-ASCII conversion of value "f" into a character
199  * buffer pointed to by "s", with a specified precision (mantissa digits) "p".
200  * There is an internal limit on precision (so larger values of "p" will be
201  * silently truncated). The maximal representable whole part corresponds to
202  * the maximal signed long integer. Otherwise, the behavior is undefined.
203  * Return the pointer past the output string (points to the terminating '\0').
204  */
206 char* NCBI_simple_ftoa(char* s, double f, int p);
207 
208 
209 /** Locale-independent ASCII-to-double conversion of string "s". Does not work
210  * for scientific notation (values including exponent). Sets "e" to point to
211  * the character that stopped conversion. Clears "errno" but sets it non-zero
212  * in case of conversion errors. Maximal value for the whole part may not
213  * exceed the maximal signed long integer, and for mantissa -- unsigned long
214  * integer.
215  * Returns result of the conversion (on error sets errno, returns 0.0).
216  * @note e == s upon return if no valid input was found and consumed.
217  */
219 double NCBI_simple_atof(const char* s, char** e);
220 
221 
222 /** Return non-zero(true) if a block of memory based at "s" and of size "n" has
223  * any space characters (as defined by isspace(c) of <ctype.h>). Return zero
224  * (false) if no such characters were found. Note that "s" is not considered
225  * to be '\0'-terminated; that is, all "n" positions will be checked.
226  * @note If "n" is 0, then "s" is not getting accessed (and can be anything,
227  * including NULL), and the return value is always 0(false).
228  */
230 int/*bool*/ NCBI_HasSpaces(const char* s, size_t n);
231 
232 
233 #ifdef __cplusplus
234 } /* extern "C" */
235 #endif
236 
237 #endif /* CONNECT___NCBI_ANSI_EXT__H */
static const char * str(char *buf, int n)
Definition: stats.c:84
#define NCBI_XCONNECT_EXPORT
yy_size_t n
double NCBI_simple_atof(const char *s, char **e)
Locale-independent ASCII-to-double conversion of string "s".
int NCBI_HasSpaces(const char *s, size_t n)
Return non-zero(true) if a block of memory based at "s" and of size "n" has any space characters (as ...
#define memrchr
char * strncpy0(char *s1, const char *s2, size_t n)
Copy not more than "n" characters from string "s2" into "s1", and return the result,...
#define strnlen
Definition: ncbi_ansi_ext.h:53
#define strdup
Definition: ncbi_ansi_ext.h:70
#define strlwr
#define strncasecmp
#define strcasecmp
#define strndup
Definition: ncbi_ansi_ext.h:89
#define strupr
#define memcchr
char * NCBI_simple_ftoa(char *s, double f, int p)
Locale-independent double-to-ASCII conversion of value "f" into a character buffer pointed to by "s",...
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
Modified on Thu Jul 18 16:09:00 2024 by modify_doxy.py rev. 669887