OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [common/] [string.c] - Blame information for rev 265

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

Line No. Rev Author Line
1 2 marcus.erl
/*
2
    string.h -- String manipulation
3
    Implements (some) of the standard string routines
4
    Copyright (C) 2002 Richard Herveille, rherveille@opencores.org
5
 
6
    This file is part of OpenRISC 1000 Reference Platform Monitor (ORPmon)
7
 
8
    This program is free software; you can redistribute it and/or modify
9
    it under the terms of the GNU General Public License as published by
10
    the Free Software Foundation; either version 2 of the License, or
11
    (at your option) any later version
12
 
13
    This program is distributed in the hope that it will be useful,
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
    GNU General Public License for more details.
17
 
18
    You should have received a copy of the GNU General Public License
19
    along with this program; if not, write to the Free Software
20
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
*/
22
 
23
#include <stddef.h>
24
#include "string.h"
25
 
26
/* Basic string functions */
27
 
28
/*
29
  s t r l e n
30
 
31
  returns number of characters in s (not including terminating null character)
32
*/
33
size_t strlen(const char *s)
34
{
35
  size_t cnt = 0;
36
 
37
  /* count the length of string s, not including the \0 character */
38
  while (*s++)
39
    cnt++;
40
 
41
  return cnt;
42
}
43
 
44
/*
45
  s t r c p y
46
 
47
  Copy 'src' to 'dest'. Strings may not overlap.
48
*/
49
char *strcpy(char *dest, const char *src)
50
{
51
  char *d = dest;
52
 
53
  /* copy src to dest */
54
  while ( (*dest++ = *src++) )
55
  ;
56
 
57
  return d;
58
}
59
 
60
 
61
char *strncpy(char *dest, const char *src, size_t n)
62
{
63
  char *d = dest;
64
 
65
  /* copy src to dest */
66
  while ( *src && n ) {
67
    *dest++ = *src++;
68
    n--;
69
  }
70
 
71
  /* fill the remainder of d with nulls */
72
  while (n--)
73
    *dest++ = '\0';
74
 
75
  return d;
76
}
77
 
78
 
79
char *strcat(char *dest, const char *src)
80
{
81
  char *d = dest;
82
 
83
  /* find the end of the destination string */
84
  while (*dest++)
85
  ;
86
 
87
  /* append the source string to the destination string */
88
  while ( (*dest++ = *src++) )
89
  ;
90
 
91
  return d;
92
}
93
 
94
 
95
char *strncat(char *dest, const char *src, size_t n)
96
{
97
  char *d = dest;
98
 
99
  /* find the end of the destination string */
100
  while (*dest++)
101
  ;
102
 
103
  /* copy src to dest */
104
  while ( (*dest = *src) && n-- ) {
105
    dest++;
106
    src++;
107
  }
108
 
109
 
110
  /* add terminating '\0' character */
111
  *dest = '\0';
112
 
113
  return d;
114
}
115
 
116
 
117
int strcmp(const char *s1, const char *s2)
118
{
119
  while ( *s1 && (*s1 == *s2) ) {
120
    s1++;
121
    s2++;
122
  }
123
 
124
  return *s1 - *s2;
125
}
126
 
127
 
128
int strncmp(const char *s1, const char *s2, size_t n)
129
{
130
  while ( *s1 && (*s1 == *s2) && n-- ) {
131
    s1++;
132
    s2++;
133
  }
134
 
135
  return *s1 - *s2;
136
}
137
 
138
 
139
char *strchr(const char *s, int c)
140
{
141
  /* search for the character c */
142
  while (*s && (*s != c) )
143
    s++;
144
 
145
  return (char *)s;
146
}
147
 
148
 
149
char *strrchr(const char *s, int c)
150
{
151
  char *fnd = NULL;
152
 
153
  /* search for the character c */
154
  while (*s) {
155
    if (*s == c)
156
      fnd = (char *)s;
157
    s++;
158
  }
159
 
160
  return fnd;
161
}
162
 
163
 
164
/* Basic mem functions */
165
void *memcpy(void *dest, const void *src, size_t n)
166
{
167
  /* check if 'src' and 'dest' are on LONG boundaries */
168
  if ( (sizeof(unsigned long) -1) & ((unsigned long)dest | (unsigned long)src) )
169
  {
170
      /* no, do a byte-wide copy */
171
      char *cs = (char *) src;
172
      char *cd = (char *) dest;
173
 
174
      while (n--)
175
          *cd++ = *cs++;
176
  }
177
  else
178
  {
179
      /* yes, speed up copy process */
180
      /* copy as many LONGs as possible */
181
      long *ls = (long *)src;
182
      long *ld = (long *)dest;
183
 
184
      size_t cnt = n >> 2;
185
      while (cnt--)
186
        *ld++ = *ls++;
187
 
188
      /* finally copy the remaining bytes */
189
      char *cs = (char *) (src + (n & ~0x03));
190
      char *cd = (char *) (dest + (n & ~0x03));
191
 
192
      cnt = n & 0x3;
193
      while (cnt--)
194
        *cd++ = *cs++;
195
  }
196
 
197
  return dest;
198
}
199
 
200 140 julius
/* memcpy, return  checksum of bytes copied instead of pointer to dest */
201
unsigned char memcpy_crc(void *dest, const void *src, size_t n)
202
{
203
  unsigned char sum = 0;
204
  unsigned char tmp;
205 2 marcus.erl
 
206 140 julius
  char *cs = (char *) src;
207
  char *cd = (char *) dest;
208
 
209
  while (n--)
210
    {
211
      tmp = *cs++;
212
      sum += tmp;
213
      *cd++ = tmp;
214
    }
215
  return sum;
216
}
217
 
218 2 marcus.erl
void *memmove(void *dest, void *src, size_t n)
219
{
220
  char *d = dest;
221
  char *s = src;
222
 
223
  while (n--)
224
    *d++ = *s++;
225
 
226
  return dest;
227
}
228
 
229
 
230
int memcmp(const void *s1, const void *s2, size_t n)
231
{
232
  char *p1 = (void *)s1;
233
  char *p2 = (void *)s2;
234
 
235
  while ( (*p1 == *p2) && n-- ) {
236
    p1++;
237
    p2++;
238
  }
239
 
240
  return *p1 - *p2;
241
}
242
 
243
 
244 140 julius
void *memchr(const void *s, char c, size_t n)
245 2 marcus.erl
{
246
  char *p = (void *)s;
247
 
248
  /* search for the character c */
249
  while ( (*p != c) && n-- )
250
    p++;
251
 
252
  return (*p == c) ? p : NULL;
253
}
254
 
255
 
256
void *memset(void *s, int c, size_t n)
257
{
258
  char *p = s;
259
 
260
  while (n--)
261
    *p++ = c;
262
 
263
  return s;
264
}

powered by: WebSVN 2.1.0

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