Rev 193 | Rev 268 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 193 | Rev 259 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /* |
1 | /* |
2 | This file is part of a common library |
2 | This file is part of a common library |
3 | Copyright (C) 2002-6 Toby Thain, toby@telegraphics.com.au |
3 | Copyright (C) 2002-6 Toby Thain, toby@telegraphics.com.au |
4 | 4 | ||
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
9 | 9 | ||
10 | This program is distributed in the hope that it will be useful, |
10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. |
13 | GNU General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU General Public License |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ |
18 | */ |
19 | 19 | ||
20 | #include <string.h> |
20 | #include <string.h> |
21 | #include <stdlib.h> |
21 | #include <stdlib.h> |
22 | 22 | ||
23 | #include "str.h" |
23 | #include "str.h" |
24 | #include "sprintf_tiny.h" |
24 | #include "sprintf_tiny.h" |
25 | 25 | ||
26 | // convert C (null-terminated) to Pascal (length byte) string |
26 | // convert C (null-terminated) to Pascal (length byte) string |
27 | // no bounds checking |
27 | // no bounds checking |
28 | unsigned char *myc2pstr(char *s){ |
28 | unsigned char *myc2pstr(char *s){ |
29 | size_t n = strlen(s); |
29 | size_t n = strlen(s); |
30 | memmove(s+1,s,n); |
30 | memmove(s+1,s,n); |
31 | *s = (unsigned char)n; |
31 | *s = (unsigned char)n; |
32 | return (unsigned char*)s; |
32 | return (unsigned char*)s; |
33 | } |
33 | } |
34 | 34 | ||
35 | // convert Pascal string to C string |
35 | // convert Pascal string to C string |
36 | // no bounds checking |
36 | // no bounds checking |
37 | char *myp2cstr(unsigned char *s){ |
37 | char *myp2cstr(unsigned char *s){ |
38 | int n = *s; |
38 | int n = *s; |
39 | memmove(s,s+1,n); |
39 | memmove(s,s+1,n); |
40 | s[n] = 0; |
40 | s[n] = 0; |
41 | return (char*)s; |
41 | return (char*)s; |
42 | } |
42 | } |
43 | 43 | ||
44 | // copy Pascal string to C string |
44 | // copy Pascal string to C string |
45 | // no bounds checking |
45 | // no bounds checking |
46 | char *myp2cstrcpy(char *dst,const unsigned char *src){ |
46 | char *myp2cstrcpy(char *dst,const unsigned char *src){ |
47 | memcpy(dst,src+1,src[0]); |
47 | memcpy(dst,src+1,src[0]); |
48 | dst[src[0]] = 0; |
48 | dst[src[0]] = 0; |
49 | return dst; |
49 | return dst; |
50 | } |
50 | } |
51 | unsigned char *myc2pstrcpy(unsigned char *dst,const char *src){ |
51 | unsigned char *myc2pstrcpy(unsigned char *dst,const char *src){ |
52 | size_t n = strlen(src); |
52 | size_t n = strlen(src); |
53 | *dst = n <= 255 ? (unsigned char)n : 255; |
53 | *dst = n <= 255 ? (unsigned char)n : 255; |
54 | memcpy(dst+1,src,*dst); |
54 | memcpy(dst+1,src,*dst); |
55 | return dst; |
55 | return dst; |
56 | } |
56 | } |
57 | 57 | ||
58 | // copy null-terminated string; |
58 | // copy null-terminated string; |
59 | // returns pointer after last character copied |
59 | // returns pointer after last character copied |
60 | char *cat(char *d,char *s){ |
60 | char *cat(char *d,char *s){ |
61 | while( (*d = *s++) ) |
61 | while( (*d = *s++) ) |
62 | d++; |
62 | d++; |
63 | return d; |
63 | return d; |
64 | } |
64 | } |
65 | 65 | ||
66 | void *my_memset(void *dst, int val, size_t len){ |
66 | void *my_memset(void *dst, int val, size_t len){ |
67 | char *p; |
67 | char *p; |
68 | for(p=(char*)dst;len--;) |
68 | for(p=(char*)dst;len--;) |
69 | *p++ = val; |
69 | *p++ = val; |
70 | return dst; |
70 | return dst; |
71 | } |
71 | } |
72 | char *my_strdup(char *s){ |
72 | char *my_strdup(char *s){ |
73 | size_t n = strlen(s); |
73 | size_t n = strlen(s); |
74 | char *p = (char*)malloc(n+1); |
74 | char *p = (char*)malloc(n+1); |
75 | if(p) |
75 | if(p) |
76 | memcpy(p,s,n+1); |
76 | memcpy(p,s,n+1); |
77 | return p; |
77 | return p; |
78 | } |
78 | } |
79 | 79 | ||
80 | unsigned char *PLcstrcpy(unsigned char *s1,const char *s2){ |
80 | unsigned char *PLcstrcpy(unsigned char *s1,const char *s2){ |
81 | size_t n = strlen(s2); |
81 | size_t n = strlen(s2); |
82 | if(n>255) |
82 | if(n>255) |
83 | n = 255; |
83 | n = 255; |
84 | memcpy(s1+1,s2,n); |
84 | memcpy(s1+1,s2,n); |
85 | *s1 = (unsigned char)n; |
85 | *s1 = (unsigned char)n; |
86 | return s1; |
86 | return s1; |
87 | } |
87 | } |
88 | 88 | ||
89 | unsigned char *PLcstrcat(unsigned char * str1,const char * s2){ |
89 | unsigned char *PLcstrcat(unsigned char * str1,const char * s2){ |
90 | size_t n = strlen(s2); |
90 | size_t n = strlen(s2); |
91 | if(str1[0]+n > 255) |
91 | if(str1[0]+n > 255) |
92 | n = 255 - str1[0]; |
92 | n = 255 - str1[0]; |
93 | memcpy(str1+1+str1[0],s2,n); |
93 | memcpy(str1+1+str1[0],s2,n); |
94 | str1[0] += (unsigned char)n; |
94 | str1[0] += (unsigned char)n; |
95 | return str1; |
95 | return str1; |
96 | } |
96 | } |