OpenCores
URL https://opencores.org/ocsvn/mips789/mips789/trunk

Subversion Repositories mips789

[/] [mips789/] [tags/] [arelease/] [Clib/] [stringlib.c] - Blame information for rev 56

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 23 mcupro
#include "stringlib.h"
2
/*--------------------------------------------------------------------
3
 * TITLE: ANSI C Library
4
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
5
 * DATE CREATED: 12/17/05
6
 * FILENAME: libc.c
7
 * PROJECT: Plasma CPU core
8
 * COPYRIGHT: Software placed into the public domain by the author.
9
 *    Software 'as is' without warranty.  Author liable for nothing.
10
 * DESCRIPTION:
11
 *    Subset of the ANSI C library
12
 *--------------------------------------------------------------------*/
13
 
14
char*strcpy(char*dst,const char*src)
15
{
16
    int c ;
17
    do
18
    {
19
        c=*dst++=*src++;
20
    }
21
    while(c);
22
    return dst ;
23
}
24
 
25
 
26
char*strncpy(char*dst,const char*src,int count)
27
{
28
    int c=1 ;
29
    while(count-->0&&c)
30
    c=*dst++=*src++;
31
    *dst=0 ;
32
    return dst ;
33
}
34
 
35
 
36
char*strcat(char*dst,const char*src)
37
{
38
    int c ;
39
    while(*dst)
40
    ++dst ;
41
    do
42
    {
43
        c=*dst++=*src++;
44
    }
45
    while(c);
46
    return dst ;
47
}
48
 
49
 
50
char*strncat(char*dst,const char*src,int count)
51
{
52
    int c=1 ;
53
    while(*dst&&--count>0)
54
    ++dst ;
55
    while(--count>0&&c)
56
    c=*dst++=*src++;
57
    *dst=0 ;
58
    return dst ;
59
}
60
 
61
 
62
int strcmp(const char*string1,const char*string2)
63
{
64
    int diff,c ;
65
    for(;;)
66
    {
67
        diff=*string1++-(c=*string2++);
68
        if(diff)
69
        return diff ;
70
        if(c==0)
71
        return 0 ;
72
    }
73
}
74
 
75
 
76
int strncmp(const char*string1,const char*string2,int count)
77
{
78
    int diff,c ;
79
    while(count-->0)
80
    {
81
        diff=*string1++-(c=*string2++);
82
        if(diff)
83
        return diff ;
84
        if(c==0)
85
        return 0 ;
86
    }
87
    return 0 ;
88
}
89
 
90
 
91
char*strstr(const char*string,const char*find)
92
{
93
    int i ;
94
    for(;;)
95
    {
96
        for(i=0;string[i]==find[i]&&find[i];++i);
97
        if(find[i]==0)
98
        return(char*)string ;
99
        if(*string++==0)
100
        return NULL ;
101
    }
102
}
103
 
104
 
105
int strlen(const char*string)
106
{
107
    const char*base=string ;
108
    while(*string++);
109
    return string-base-1 ;
110
}
111
 
112
 
113
void*memcpy(void*dst,const void*src,unsigned long bytes)
114
{
115
    uint8*Dst=(uint8*)dst ;
116
    uint8*Src=(uint8*)src ;
117
    while((int)bytes-->0)
118
    *Dst++=*Src++;
119
    return dst ;
120
}
121
 
122
 
123
void*memmove(void*dst,const void*src,unsigned long bytes)
124
{
125
    uint8*Dst=(uint8*)dst ;
126
    uint8*Src=(uint8*)src ;
127
    if(Dst<Src)
128
    {
129
        while((int)bytes-->0)
130
        *Dst++=*Src++;
131
    }
132
    else
133
    {
134
        Dst+=bytes ;
135
        Src+=bytes ;
136
        while((int)bytes-->0)
137
        *--Dst=*--Src ;
138
    }
139
    return dst ;
140
}
141
 
142
 
143
int memcmp(const void*cs,const void*ct,unsigned long bytes)
144
{
145
    uint8*Dst=(uint8*)cs ;
146
    uint8*Src=(uint8*)ct ;
147
    int diff ;
148
    while((int)bytes-->0)
149
    {
150
        diff=*Dst++-*Src++;
151
        if(diff)
152
        return diff ;
153
    }
154
    return 0 ;
155
}
156
 
157
 
158
void*memset(void*dst,int c,unsigned long bytes)
159
{
160
    uint8*Dst=(uint8*)dst ;
161
    while((int)bytes-->0)
162
    *Dst++=(uint8)c ;
163
    return dst ;
164
}
165
 
166
 
167
int abs(int n)
168
{
169
    return n>=0?n:-n ;
170
}
171
 
172
 
173
static uint32 Rand1=0x1f2bcda3,Rand2=0xdeafbeef,Rand3=0xc5134306 ;
174
int rand(void)
175
{
176
    int shift ;
177
    Rand1+=0x13423123+Rand2 ;
178
    Rand2+=0x2312fdea+Rand3 ;
179
    Rand3+=0xf2a12de1 ;
180
    shift=Rand3&31 ;
181
    Rand1=(Rand1<<(32-shift))|(Rand1>>shift);
182
    Rand3^=Rand1 ;
183
    shift=(Rand3>>8)&31 ;
184
    Rand2=(Rand2<<(32-shift))|(Rand2>>shift);
185
    return Rand1 ;
186
}
187
 
188
 
189
void srand(unsigned int seed)
190
{
191
    Rand1=seed ;
192
}
193
 
194
 
195
long strtol(const char*s,const char**end,int base)
196
{
197
    int i ;
198
    unsigned long ch,value=0,neg=0 ;
199
 
200
    if(s[0]=='-')
201
    {
202
        neg=1 ;
203
        ++s ;
204
    }
205
    if(s[0]=='0'&&s[1]=='x')
206
    {
207
        base=16 ;
208
        s+=2 ;
209
    }
210
    for(i=0;i<=8;++i)
211
    {
212
        ch=*s++;
213
        if('0'<=ch&&ch<='9')
214
        ch-='0' ;
215
        else if('A'<=ch&&ch<='Z')
216
        ch=ch-'A'+10 ;
217
        else if('a'<=ch&&ch<='z')
218
        ch=ch-'a'+10 ;
219
        else
220
        break ;
221
        value=value*base+ch ;
222
    }
223
    if(end)
224
    *end=s-1 ;
225
    if(neg)
226
    value=-(int)value ;
227
    return value ;
228
}
229
 
230
 
231
int atoi(const char*s)
232
{
233
    return strtol(s,NULL,10);
234
}
235
 
236
 
237
char*itoa(int num,char*dst,int base)
238
{
239
    int digit,negate=0,place ;
240
    char c,text[20];
241
 
242
    if(base==10&&num<0)
243
    {
244
        num=-num ;
245
        negate=1 ;
246
    }
247
    text[16]=0 ;
248
    for(place=15;place>=0;--place)
249
    {
250
        digit=(unsigned int)num%(unsigned int)base ;
251
        if(num==0&&place<15&&base==10&&negate)
252
        {
253
            c='-' ;
254
            negate=0 ;
255
        }
256
        else if(digit<10)
257
        c=(char)('0'+digit);
258
        else
259
        c=(char)('a'+digit-10);
260
        text[place]=c ;
261
        num=(unsigned int)num/(unsigned int)base ;
262
        if(num==0&&negate==0)
263
        break ;
264
    }
265
    strcpy(dst,text+place);
266
    return dst ;
267
}
268
 
269
 
270
int sprintf(char*s,const char*format,int arg0)
271
{
272
    int argv[2];
273
    int argc=0,width,length ;
274
    char f,text[20],fill ;
275
    argv[0]=arg0 ;
276
 
277
    for(;;)
278
    {
279
        f=*format++;
280
        if(f==0)
281
        return argc ;
282
        else if(f=='%')
283
        {
284
            width=0 ;
285
            fill=' ' ;
286
            f=*format++;
287
            while('0'<=f&&f<='9')
288
            {
289
                width=width*10+f-'0' ;
290
                f=*format++;
291
            }
292
            if(f=='.')
293
            {
294
                fill='0' ;
295
                f=*format++;
296
            }
297
            if(f==0)
298
            return argc ;
299
 
300
            if(f=='d')
301
            {
302
                memset(s,fill,width);
303
                itoa(argv[argc++],text,10);
304
                length=(int)strlen(text);
305
                if(width<length)
306
                width=length ;
307
                strcpy(s+width-length,text);
308
            }
309
            else if(f=='x'||f=='f')
310
            {
311
                memset(s,'0',width);
312
                itoa(argv[argc++],text,16);
313
                length=(int)strlen(text);
314
                if(width<length)
315
                width=length ;
316
                strcpy(s+width-length,text);
317
            }
318
            else if(f=='c')
319
            {
320
                *s++=(char)argv[argc++];
321
                *s=0 ;
322
            }
323
            else if(f=='s')
324
            {
325
                length=strlen((char*)argv[argc]);
326
                if(width>length)
327
                {
328
                    memset(s,' ',width-length);
329
                    s+=width-length ;
330
                }
331
                strcpy(s,(char*)argv[argc++]);
332
            }
333
            s+=strlen(s);
334
        }
335
        else if(f=='\\')
336
        {
337
            f=*format++;
338
            if(f==0)
339
            return argc ;
340
            else if(f=='n')
341
            *s++='\n' ;
342
            else if(f=='r')
343
            *s++='\r' ;
344
            else if(f=='t')
345
            *s++='\t' ;
346
        }
347
        else
348
        {
349
            *s++=f ;
350
        }
351
        *s=0 ;
352
    }
353
}
354
 
355
 
356
int sscanf(const char*s,const char*format,int arg0)
357
{
358
    int argv[2];
359
    int argc=0 ;
360
    char f,*ptr ;
361
 
362
    argv[0]=arg0 ;
363
    for(;;)
364
    {
365
        if(*s==0)
366
        return argc ;
367
        f=*format++;
368
        if(f==0)
369
        return argc ;
370
        else if(f=='%')
371
        {
372
            while(isspace(*s))
373
            ++s ;
374
            f=*format++;
375
            if(f==0)
376
            return argc ;
377
            if(f=='d')
378
            *(int*)argv[argc++]=strtol(s,&s,10);
379
            else if(f=='x')
380
            *(int*)argv[argc++]=strtol(s,&s,16);
381
            else if(f=='c')
382
            *(char*)argv[argc++]=*s++;
383
            else if(f=='s')
384
            {
385
                ptr=(char*)argv[argc++];
386
                while(!isspace(*s))
387
                *ptr++=*s++;
388
                *ptr=0 ;
389
            }
390
        }
391
        else
392
        {
393
            if(f=='\\')
394
            {
395
                f=*format++;
396
                if(f==0)
397
                return argc ;
398
                else if(f=='n')
399
                f='\n' ;
400
                else if(f=='r')
401
                f='\r' ;
402
                else if(f=='t')
403
                f='\t' ;
404
            }
405
            while(*s&&*s!=f)
406
            ++s ;
407
            if(*s)
408
            ++s ;
409
        }
410
    }
411
}
412
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.