NCBI C Toolkit Cross Reference

C/connect/ncbi_ansi_ext.c


  1 /*  $Id: ncbi_ansi_ext.c,v 6.19 2006/03/07 18:15:14 lavr 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  * Author:  Anton Lavrentiev
 27  *
 28  * File Description:
 29  *   Non-ANSI, yet widely used functions
 30  *
 31  */
 32 
 33 #include "ncbi_ansi_ext.h"
 34 #include <ctype.h>
 35 #include <stdlib.h>
 36 
 37 
 38 #ifndef HAVE_STRDUP
 39 
 40 extern char* strdup(const char* str)
 41 {
 42     size_t size = strlen(str) + 1;
 43     char*   res = (char*) malloc(size);
 44     if (res)
 45         memcpy(res, str, size);
 46     return res;
 47 }
 48 
 49 #endif /*HAVE_STRDUP*/
 50 
 51 
 52 #ifndef HAVE_STRNDUP
 53 
 54 extern char* strndup(const char* str, size_t n)
 55 {
 56     const char* end = n   ? memchr(str, '\0', n) : 0;
 57     size_t     size = end ? (size_t)(end - str)  : n;
 58     char*       res = (char*) malloc(size + 1);
 59     if (res) {
 60         memcpy(res, str, size);
 61         res[size] = '\0';
 62     }
 63     return res;
 64 }
 65 
 66 #endif /*HAVE_STRNDUP*/
 67 
 68 
 69 #ifndef HAVE_STRCASECMP
 70 
 71 /* We assume that we're using ASCII-based charsets */
 72 extern int strcasecmp(const char* s1, const char* s2)
 73 {
 74     const unsigned char* p1 = (const unsigned char*) s1;
 75     const unsigned char* p2 = (const unsigned char*) s2;
 76     unsigned char c1, c2;
 77 
 78     if (p1 == p2)
 79         return 0;
 80 
 81     do {
 82         c1 = *p1++;
 83         c2 = *p2++;
 84         c1 = c1 >= 'A' && c1 <= 'Z' ? c1 + ('a' - 'A') : tolower(c1);
 85         c2 = c2 >= 'A' && c2 <= 'Z' ? c2 + ('a' - 'A') : tolower(c2);
 86     } while (c1  &&  c1 == c2);
 87 
 88     return c1 - c2;
 89 }
 90 
 91 
 92 extern int strncasecmp(const char* s1, const char* s2, size_t n)
 93 {
 94     const unsigned char* p1 = (const unsigned char*) s1;
 95     const unsigned char* p2 = (const unsigned char*) s2;
 96     unsigned char c1, c2;
 97 
 98     if (p1 == p2  ||  n == 0)
 99         return 0;
100 
101     do {
102         c1 = *p1++;
103         c2 = *p2++;
104         c1 = c1 >= 'A' && c1 <= 'Z' ? c1 + ('a' - 'A') : tolower(c1);
105         c2 = c2 >= 'A' && c2 <= 'Z' ? c2 + ('a' - 'A') : tolower(c2);
106     } while (--n > 0  &&  c1  &&  c1 == c2);
107 
108     return c1 - c2;
109 }
110 
111 #endif /*HAVE_STRCASECMP*/
112 
113 
114 extern char* strupr(char* s)
115 {
116     unsigned char* t = (unsigned char*) s;
117 
118     while ( *t ) {
119         *t = toupper(*t);
120         t++;
121     }
122     return s;
123 }
124 
125 
126 extern char* strlwr(char* s)
127 {
128     unsigned char* t = (unsigned char*) s;
129 
130     while ( *t ) {
131         *t = tolower(*t);
132         t++;
133     }
134     return s;
135 }
136 
137 
138 extern char* strncpy0(char* s1, const char* s2, size_t n)
139 {
140     *s1 = '\0';
141     return strncat(s1, s2, n);
142 }
143 
144 
145 /*
146  * --------------------------------------------------------------------------
147  * $Log: ncbi_ansi_ext.c,v $
148  * Revision 6.19  2006/03/07 18:15:14  lavr
149  * Optimize strndup()
150  *
151  * Revision 6.18  2006/03/07 17:54:44  ivanov
152  * Fixed compilation error in strndup
153  *
154  * Revision 6.17  2006/03/07 17:18:51  lavr
155  * +strndup
156  *
157  * Revision 6.16  2004/10/08 16:16:24  ivanov
158  * Removed extra ")"
159  *
160  * Revision 6.15  2004/10/08 15:45:19  lavr
161  * Make lower-case comparisons in no-case functions
162  *
163  * Revision 6.14  2002/10/29 22:18:29  lavr
164  * Comply with man strdup(2) in the implementation of strdup() here
165  *
166  * Revision 6.13  2002/10/28 18:55:26  lavr
167  * Fix change log to remove duplicate log entry for R6.12
168  *
169  * Revision 6.12  2002/10/28 18:52:07  lavr
170  * Conditionalize definitions of strdup() and str[n]casecmp()
171  *
172  * Revision 6.11  2002/10/28 15:41:56  lavr
173  * Use "ncbi_ansi_ext.h" privately
174  *
175  * Revision 6.10  2002/09/24 15:05:45  lavr
176  * Log moved to end
177  *
178  * Revision 6.9  2002/03/19 22:12:28  lavr
179  * strcasecmp and strncasecmp are optimized (for ASCII range)
180  *
181  * Revision 6.8  2001/12/04 15:57:22  lavr
182  * Tiny style adjustement
183  *
184  * Revision 6.7  2000/12/28 21:27:52  lavr
185  * ANSI C++ compliant use of malloc (explicit casting of result)
186  *
187  * Revision 6.6  2000/11/07 21:45:16  lavr
188  * Removed isupper/islower checking in strlwr/strupr
189  *
190  * Revision 6.5  2000/11/07 21:19:38  vakatov
191  * Compilation warning fixed;  plus, some code beautification...
192  *
193  * Revision 6.4  2000/10/18 21:15:53  lavr
194  * strupr and strlwr added
195  *
196  * Revision 6.3  2000/10/06 16:40:23  lavr
197  * <string.h> included now in <connect/ncbi_ansi_ext.h>
198  * conditional preprocessor statements removed
199  *
200  * Revision 6.2  2000/05/17 16:11:02  lavr
201  * Reorganized for use of HAVE_* defines
202  *
203  * Revision 6.1  2000/05/15 19:03:41  lavr
204  * Initial revision
205  *
206  * ==========================================================================
207  */
208 

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.