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