[412] | 1 | /* |
---|
| 2 | * string.c - Character string handling API implementation. |
---|
| 3 | * |
---|
| 4 | * Author Alain Greiner (2016,2017) |
---|
| 5 | * |
---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 7 | * |
---|
| 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #define NULL (void *)0 |
---|
| 25 | |
---|
| 26 | /////////////////////////// |
---|
| 27 | inline int tolower( int c ) |
---|
| 28 | { |
---|
| 29 | if (c >= 'A' && c <= 'Z') return (c | 0x20); |
---|
| 30 | else return c; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | /////////////////////////// |
---|
| 35 | inline int toupper( int c ) |
---|
| 36 | { |
---|
| 37 | if (c >= 'a' && c <= 'z') return (c & ~(0x20)); |
---|
| 38 | else return c; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /////////////////////////////////////// |
---|
| 42 | unsigned int strlen( const char * str ) |
---|
| 43 | { |
---|
| 44 | register const char * ptr = str; |
---|
| 45 | |
---|
| 46 | while (* ptr) ptr++; |
---|
| 47 | |
---|
| 48 | return (ptr - str); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /////////////////////////////////////// |
---|
| 52 | unsigned int strnlen( const char * str, |
---|
| 53 | unsigned int maxlen ) |
---|
| 54 | { |
---|
| 55 | register const char *ptr = str; |
---|
| 56 | |
---|
| 57 | while (*ptr && (maxlen-- > 0)) ptr++; |
---|
| 58 | |
---|
| 59 | return (ptr - str); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | //////////////////////////// |
---|
| 63 | int strcmp( const char * s1, |
---|
| 64 | const char * s2 ) |
---|
| 65 | { |
---|
| 66 | |
---|
| 67 | while (*s1 && *s1 == *s2) |
---|
| 68 | { |
---|
| 69 | s1 ++; |
---|
| 70 | s2 ++; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | return (*s1 - *s2); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | ////////////////////////////// |
---|
| 77 | int strncmp( const char * s1, |
---|
| 78 | const char * s2, |
---|
| 79 | unsigned int n ) |
---|
| 80 | { |
---|
| 81 | if (n == 0) |
---|
| 82 | return s1 - s2; // pseudo random result... |
---|
| 83 | |
---|
| 84 | while (*s1 && (*s1 == *s2) && (n > 1)) |
---|
| 85 | { |
---|
| 86 | s1 ++; |
---|
| 87 | s2 ++; |
---|
| 88 | n--; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | return (*s1 - *s2); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | ////////////////////////////////// |
---|
| 95 | int strcasecmp( const char * str1, |
---|
| 96 | const char * str2 ) |
---|
| 97 | { |
---|
| 98 | char c1; |
---|
| 99 | char c2; |
---|
| 100 | |
---|
| 101 | do |
---|
| 102 | { |
---|
| 103 | c1 = (char)toupper( (int)*++str1 ); |
---|
| 104 | c2 = (char)toupper( (int)*++str2 ); |
---|
| 105 | c2 = toupper(*++str2); |
---|
| 106 | } |
---|
| 107 | while(c1 && c1 == c2); |
---|
| 108 | |
---|
| 109 | return (c1 - c2); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /////////////////////////// |
---|
| 113 | char * strcpy (char * dest, |
---|
| 114 | char * src ) |
---|
| 115 | { |
---|
| 116 | char *src_ptr = src; |
---|
| 117 | char *dst_ptr = dest; |
---|
| 118 | |
---|
| 119 | while(*src_ptr) *(dst_ptr++) = *(src_ptr++); |
---|
| 120 | |
---|
| 121 | *dst_ptr = 0; |
---|
| 122 | return dest; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | //////////////////////////////////// |
---|
| 126 | char * strncpy( char * dest, |
---|
| 127 | char * src, |
---|
| 128 | unsigned int n ) |
---|
| 129 | { |
---|
| 130 | unsigned int i; |
---|
| 131 | |
---|
| 132 | for (i = 0; (i < n) && (src[i] != '\0') ; i++) dest[i] = src[i]; |
---|
| 133 | |
---|
| 134 | for (; i < n; i++) dest[i] = '\0'; |
---|
| 135 | |
---|
| 136 | return dest; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | ////////////////////////////// |
---|
| 140 | char * strstr( char * s, |
---|
| 141 | const char * find ) |
---|
| 142 | { |
---|
| 143 | char sc; |
---|
| 144 | char c; |
---|
| 145 | unsigned int len; |
---|
| 146 | |
---|
| 147 | if ((c = *find++) != 0) |
---|
| 148 | { |
---|
| 149 | len = strlen( find ); |
---|
| 150 | do |
---|
| 151 | { |
---|
| 152 | do |
---|
| 153 | { |
---|
| 154 | if ((sc = *s++) == 0) return NULL; |
---|
| 155 | } |
---|
| 156 | while (sc != c); |
---|
| 157 | } |
---|
| 158 | while (strncmp(s, find, len) != 0); |
---|
| 159 | s--; |
---|
| 160 | } |
---|
| 161 | return s; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | ////////////////////////////// |
---|
| 165 | char * strchr( const char * s, |
---|
| 166 | int c ) |
---|
| 167 | { |
---|
| 168 | while(*s && *s != (char) c) s++; |
---|
| 169 | |
---|
| 170 | if(*s == (char) c) return (char*)s; |
---|
| 171 | |
---|
| 172 | return NULL; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /////////////////////////////// |
---|
| 176 | char * strrchr( const char * t, |
---|
| 177 | int c ) |
---|
| 178 | { |
---|
| 179 | register char ch; |
---|
| 180 | register const char * l = 0; |
---|
| 181 | |
---|
| 182 | ch = c; |
---|
| 183 | for (;;) |
---|
| 184 | { |
---|
| 185 | if (*t == ch) l=t; |
---|
| 186 | if (!*t) return (char*)l; |
---|
| 187 | ++t; |
---|
| 188 | } |
---|
| 189 | return (char*)l; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | |
---|