HP85 GPIB Disk Emulator  1.0
HP85GPIBDiskEmulator
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
stringsup.c
Go to the documentation of this file.
1 
24 #include "user_config.h"
25 
26 #include <string.h>
27 #include "stringsup.h"
28 
29 // =============================================
30 // Character functions
31 // =============================================
32 
33 // =============================================
38 int
40 isdigit(int c)
41 {
42  if(c >= '0' && c <= '9')
43  return(1);
44  return(0);
45 }
46 
47 
48 // =============================================
55 int
57 isupper(int c)
58 {
59  if(c >= 'A' && c <= 'Z')
60  return(1);
61  return(0);
62 }
63 
64 
65 // =============================================
66 //@brief Is a character lower case
72 int
74 islower(int c)
75 {
76  if(c >= 'a' && c <= 'z')
77  return(1);
78  return(0);
79 }
80 
81 
82 // =============================================
89 int
91 tolower(int c)
92 {
93  if(isupper(c))
94  return(c - 'A' + 'a');
95  return(c);
96 }
97 
98 
99 // =============================================
105 MEMSPACE
106 int
107 WEAK_ATR
108 toupper(int c)
109 {
110  if(islower(c))
111  return(c - 'a' + 'A');
112  return(c);
113 }
114 
115 
121 MEMSPACE
122 void *memchr(const void *str, int c, size_t size)
123 {
124  const uint8_t *ptr = str;
125  while(size--)
126  {
127  if (*ptr++ == (uint8_t) c)
128  return (void *) (ptr - 1);
129  }
130  return NULL;
131 }
132 
133 
134 // =============================================
135 // String functions
136 // =============================================
137 // =============================================
141 MEMSPACE
142 size_t
143 WEAK_ATR
144 strlen(const char *str)
145 {
146  int len=0;
147 // String length
148  while(*str++)
149  ++len;
150  return(len);
151 }
152 
153 
158 MEMSPACE
159 WEAK_ATR char *
160 strcpy(char *dest, const char *src)
161 {
162  char *ptr = dest;
163  while(*src)
164  {
165  *ptr++ = *src++;
166  }
167  *ptr ++ = 0;
168  return (ptr);
169 }
170 
171 
177 MEMSPACE
178 WEAK_ATR
179 char * strncpy(char *dest, const char *src, size_t size)
180 {
181  char *ptr = dest;
182  while(*src && size)
183  {
184  *ptr++ = *src++;
185  size--;
186  }
187  while(size--)
188  *ptr++ = 0;
189  return (dest);
190 }
191 
192 
197 MEMSPACE
198 WEAK_ATR
199 char * strcat(char *dest, const char *src)
200 {
201  char *ptr = dest;
202  while(*ptr)
203  ++ptr;
204  strcpy(ptr,src);
205  return(dest);
206 }
207 
208 
213 MEMSPACE
214 WEAK_ATR
215 char * strncat(char *dest, const char *src, size_t max)
216 {
217  char *ptr = dest;
218  while(*ptr)
219  ++ptr;
220  strncpy(ptr,src,max);
221  return(dest);
222 }
223 
224 
225 // =============================================
230 MEMSPACE
231 void
232 WEAK_ATR
233 reverse(char *str)
234 {
235  char temp;
236  int i;
237  int len = strlen(str);
238 // Reverse
239 // We only exchange up to half way
240  for (i = 0; i < (len >> 1); i++)
241  {
242  temp = str[len - i - 1];
243  str[len - i - 1] = str[i];
244  str[i] = temp;
245  }
246 }
247 
248 
249 // =============================================
253 MEMSPACE
254 void
255 WEAK_ATR
256 strupper(char *str)
257 {
258  while(*str)
259  {
260  *str = toupper(*str);
261  ++str;
262  }
263 }
264 
265 
266 // =============================================
267 // String Matching
268 // =============================================
275 MEMSPACE
276 int
277 WEAK_ATR
278 strcmp(const char *str, const char *pat)
279 {
280  int ret = 0;
281  int c1,c2;
282  while (1)
283  {
284  c1 = *str++;
285  c2 = *pat++;
286  if ( (ret = c1 - c2) != 0 || c2 == 0)
287  break;
288  }
289  return(ret);
290 }
291 
292 
293 // =============================================
300 MEMSPACE
301 int
302 WEAK_ATR
303 strncmp(const char *str, const char *pat, size_t len)
304 {
305  int ret = 0;
306  int c1,c2;
307  while (len--)
308  {
309  c1 = *str++;
310  c2 = *pat++;
311  if ( (ret = c1 - c2) != 0 || c2 == 0)
312  break;
313  }
314  return(ret);
315 }
316 
317 
318 // =============================================
325 MEMSPACE
326 int
327 WEAK_ATR
328 strcasecmp(const char *str, const char *pat)
329 {
330  int ret = 0;
331  int c1,c2;
332  while (1)
333  {
334  c1 = toupper(*str++);
335  c2 = toupper(*pat++);
336  if ( (ret = c1 - c2) != 0 || c2 == 0)
337  break;
338  }
339  return(ret);
340 }
341 
342 
349 MEMSPACE
350 int
351 WEAK_ATR
352 strncasecmp(const char *str, const char *pat, size_t len)
353 {
354  int ret = 0;
355  int c1,c2;
356  while (len--)
357  {
358  c1 = toupper(*str++);
359  c2 = toupper(*pat++);
360  if ( (ret = c1 - c2) != 0 || c2 == 0)
361  break;
362  }
363  return(ret);
364 }
365 
366 
367 // =============================================
368 // String memory allocation functions
369 // =============================================
370 // =============================================
379 
380 MEMSPACE
381 char *strnalloc(char *str, int len)
382 {
383  char *ptr;
384 
385  if(!str)
386  return(NULL);
387  ptr = safecalloc(len+1,1);
388  if(!ptr)
389  return(ptr);
390  strncpy(ptr,str,len);
391  return(ptr);
392 
393 }
394 
395 
396 // =============================================
405 MEMSPACE
406 char *stralloc(char *str)
407 {
408  char *ptr;
409  int len;
410 
411  if(!str)
412  return(str);;
413  len = strlen(str);
414  ptr = safecalloc(len+1,1);
415  if(!ptr)
416  return(ptr);
417  strcpy(ptr,str);
418  return(ptr);
419 }
strncasecmp
MEMSPACE int WEAK_ATR strncasecmp(const char *str, const char *pat, size_t len)
Compare two strings without case maximum len bytes in size.
Definition: stringsup.c:352
toupper
MEMSPACE int WEAK_ATR toupper(int c)
Convert character to upper case, only if it is lower case.
Definition: stringsup.c:108
strupper
MEMSPACE void WEAK_ATR strupper(char *str)
UPPERCASE a string.
Definition: stringsup.c:256
tolower
MEMSPACE int WEAK_ATR tolower(int c)
Convert character to lower case, only if it is upper case.
Definition: stringsup.c:91
MEMSPACE
#define MEMSPACE
Definition: user_config.h:17
strcat
MEMSPACE WEAK_ATR char * strcat(char *dest, const char *src)
Append string.
Definition: stringsup.c:199
safecalloc
void * safecalloc(int size, int elements)
Safe Alloc - Display Error message if Calloc fails.
Definition: ram.c:122
strcasecmp
MEMSPACE int WEAK_ATR strcasecmp(const char *str, const char *pat)
Compare two strings without case.
Definition: stringsup.c:328
stringsup.h
Various string and character functions.
stralloc
MEMSPACE char * stralloc(char *str)
Allocate space for string.
Definition: stringsup.c:406
strncmp
MEMSPACE int WEAK_ATR strncmp(const char *str, const char *pat, size_t len)
Compare two strings maximum len bytes in size.
Definition: stringsup.c:303
strncat
MEMSPACE WEAK_ATR char * strncat(char *dest, const char *src, size_t max)
Append string of at most N bytes from src.
Definition: stringsup.c:215
memchr
MEMSPACE void * memchr(const void *str, int c, size_t size)
find a character in a string of maximum size
Definition: stringsup.c:122
reverse
MEMSPACE void WEAK_ATR reverse(char *str)
Reverse a string in place Example: abcdef -> fedcba.
Definition: stringsup.c:233
strcpy
MEMSPACE WEAK_ATR char * strcpy(char *dest, const char *src)
copy a string
Definition: stringsup.c:160
NULL
#define NULL
Definition: user_config.h:85
strcmp
MEMSPACE int WEAK_ATR strcmp(const char *str, const char *pat)
Compare two strings.
Definition: stringsup.c:278
strlen
MEMSPACE size_t WEAK_ATR strlen(const char *str)
String Length.
Definition: stringsup.c:144
islower
MEMSPACE int WEAK_ATR islower(int c)
Definition: stringsup.c:74
strncpy
MEMSPACE WEAK_ATR char * strncpy(char *dest, const char *src, size_t size)
copy a string of at most N characters
Definition: stringsup.c:179
strnalloc
MEMSPACE char * strnalloc(char *str, int len)
Allocate space for string with maximum size.
Definition: stringsup.c:381
isdigit
MEMSPACE int WEAK_ATR isdigit(int c)
test if a character is a digit
Definition: stringsup.c:40
WEAK_ATR
#define WEAK_ATR
Definition: stringsup.h:33
isupper
MEMSPACE int WEAK_ATR isupper(int c)
Is a character upper case.
Definition: stringsup.c:57