-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAmosString.cpp
208 lines (171 loc) · 4.11 KB
/
AmosString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __amigaos4__
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/retroMode.h>
#endif
#include "amoskittens.h"
#include "amosString.h"
#include "amosstring.h"
int cust_memcmp(char *s1,char *s2, int n)
{
while ((*s1==*s2)&&(n>0))
{
s1++;s2++; n--;
}
return n;
}
#ifdef __no_stdlib__
// equal or not, don't need to be perfect
void cust_memcpy(char *d, char *s, int n )
{
while ((*s)&&(n)) {*d=*s; s++; d++; n--; }
}
#define memcpy(d,s,n) cust_memcpy((char *)d, (char *)s,n)
#else
#include <string.h>
#endif
struct stringData *alloc_amos_string( int size )
{
struct stringData *newstr;
allocNewString(size,newstr);
if (newstr)
{
newstr -> size = size;
(&newstr -> ptr)[size]=0; // unlike AmosPro, Amos kitten strings should be 0 terminaled, so they will work with standard OS libraryes and C libs.
}
return newstr;
}
struct stringData *amos_strdup( struct stringData *var )
{
struct stringData *newstr;
allocNewString(var->size,newstr);
if (newstr)
{
newstr -> size = var -> size;
(&newstr -> ptr)[var -> size]=0; // unlike AmosPro, Amos kitten strings should be 0 terminaled, so they will work with standard OS libraryes and C libs.
memcpy(&(newstr -> ptr),&(var -> ptr),var->size);
return newstr;
}
return NULL;
}
struct stringData *amos_strndup( struct stringData *var, int len )
{
struct stringData *newstr;
if (var->size<len) len = var->size;
allocNewString(len,newstr);
if (newstr)
{
newstr -> size = len;
(&newstr -> ptr)[len]=0; // unlike AmosPro, Amos kitten strings should be 0 terminaled, so they will work with standard OS libraryes and C libs.
memcpy(&(newstr -> ptr),&(var -> ptr),len);
}
return newstr;
}
struct stringData *amos_mid( struct stringData *string, int start, int len )
{
struct stringData *newstr;
if ( start >= string->size ) start = string->size;
if ( (string->size - start) <len) len = (string->size - start);
if (len<0) len =0;
allocNewString(len,newstr);
if (newstr)
{
newstr -> size = len;
memcpy(&(newstr->ptr),&(string->ptr)+start,len);
(&newstr->ptr)[len]=0;
}
return newstr;
}
struct stringData *amos_right( struct stringData *var, int len )
{
struct stringData *newstr;
if (var->size<len) len = var->size;
allocNewString(len,newstr);
if (newstr)
{
newstr -> size = len;
(&newstr -> ptr)[len]=0; // unlike AmosPro, Amos kitten strings should be 0 terminaled, so they will work with standard OS libraryes and C libs.
memcpy(&(newstr -> ptr),&(var -> ptr) + (var -> size - len),len);
}
return newstr;
}
int amos_instr( struct stringData *string,int start,struct stringData *find )
{
char *p = &(string -> ptr) + start;
int l;
int n;
if (string -> size < find -> size) return 0;
l = string -> size - start - (find -> size -1);
for (n=0;n<l;n++)
{
if (cust_memcmp(p+n,&(find -> ptr),find -> size)==0)
{
return n+start+1;
}
}
return 0;
}
struct stringData *toAmosString( const char *txt,int len)
{
struct stringData *newstr;
const char *ptr;
int _l = 0;
ptr = txt; while ((*ptr)&&(_l<len)) _l++;
if (_l<len) len = _l;
allocNewString(len,newstr);
if (newstr)
{
newstr -> size = len;
(&newstr -> ptr)[len]=0; // unlike AmosPro, Amos kitten strings should be 0 terminaled, so they will work with standard OS libraryes and C libs.
memcpy(&(newstr -> ptr),txt,len);
}
return newstr;
}
struct stringData *toAmosString_char( const char *adr, char t)
{
struct stringData *ret;
const char *c;
int size = 0;
for (c=adr;*c!=t;c++) size++;
allocNewString(size,ret);
if (ret)
{
char *d = &ret -> ptr;
ret -> size = 0;
for (c=adr;(*c!=t);c++)
{
*d=*c;
d++;
ret -> size++;
}
*d = 0;
}
return ret;
}
struct stringData *toAmosString_len_or_char( const char *adr, int len, char t)
{
struct stringData *ret;
const char *c;
const char *adr_end;
int size = 0;
for (c=adr;(*c!=t)&&(size<len);c++) size++;
allocNewString(size,ret);
if (ret)
{
char *d = &ret -> ptr;
adr_end = adr + len;
ret -> size = 0;
for (c=adr;((c<adr_end) && (*c!=t));c++)
{
*d=*c;
d++;
ret -> size++;
}
*d=0;
}
return ret;
}