source:
soft/giet_vm/giet_libs/string.c
@
784
Last change on this file since 784 was 781, checked in by , 9 years ago | |
---|---|
|
|
File size: 1.8 KB |
Rev | Line | |
---|---|---|
[258] | 1 | ////////////////////////////////////////////////////////////////////////////////// |
2 | // File : string.c | |
3 | // Date : 23/05/2013 | |
4 | // Author : Alexandre JOANNOU, Laurent LAMBERT | |
5 | // Copyright (c) UPMC-LIP6 | |
6 | /////////////////////////////////////////////////////////////////////////////////// | |
7 | ||
[781] | 8 | /////////////////////////////////////////////////////// |
[777] | 9 | char * strcpy (char * destination, const char * source) |
[258] | 10 | { |
[781] | 11 | if (!destination || !source) return destination; |
[258] | 12 | |
[781] | 13 | char* src = source; |
14 | char* dst = destination; | |
15 | do | |
16 | { | |
17 | *dst = *src; | |
18 | dst++; | |
19 | src++; | |
20 | } while (*src); | |
[258] | 21 | |
[781] | 22 | *dst = 0; |
23 | ||
[258] | 24 | return destination; |
25 | } | |
26 | ||
[777] | 27 | |
[781] | 28 | //////////////////////////////////////////////////// |
[777] | 29 | char * strncpy(char * dest, const char * src, int n) |
[258] | 30 | { |
[777] | 31 | int i; |
[781] | 32 | for (i = 0; i < n && src[i] != '\0'; i++) |
33 | { | |
[777] | 34 | dest[i] = src[i]; |
35 | } | |
[781] | 36 | for (; i < n ; i++) |
37 | { | |
[777] | 38 | dest[i] = '\0'; |
39 | } | |
[781] | 40 | |
[777] | 41 | return dest; |
42 | } | |
43 | ||
44 | ||
[781] | 45 | ///////////////////////////////////////////////// |
[777] | 46 | int strcmp (const char * str1, const char * str2) |
47 | { | |
[781] | 48 | if (!str1 || !str2) |
49 | { | |
50 | return -123456; // return a value out of the char's bounds | |
[777] | 51 | } |
[258] | 52 | |
[781] | 53 | while (*str1 && *str1 == *str2) |
54 | { | |
[258] | 55 | str1++; |
56 | str2++; | |
57 | } | |
58 | ||
59 | return (*str1 - *str2); | |
60 | } | |
61 | ||
[781] | 62 | ///////////////////////////// |
[777] | 63 | int strlen (const char * str) |
[607] | 64 | { |
[777] | 65 | const char * s = str; |
[607] | 66 | |
[781] | 67 | while (*s) |
68 | { | |
[607] | 69 | s++; |
[777] | 70 | } |
[607] | 71 | return (s - str); |
72 | } | |
73 | ||
[777] | 74 | |
[781] | 75 | ////////////////////////////////////// |
[777] | 76 | char * strchr(const char * str, int c) |
77 | { | |
78 | const char * s = str; | |
79 | const char ch = c; | |
[781] | 80 | while (*s != ch && *s != '\0') |
81 | { | |
[777] | 82 | s++; |
83 | } | |
84 | ||
85 | return (char *) s; | |
86 | } | |
87 | ||
88 | ||
[258] | 89 | // Local Variables: |
90 | // tab-width: 4 | |
91 | // c-basic-offset: 4 | |
92 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) | |
93 | // indent-tabs-mode: nil | |
94 | // End: | |
95 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
Note: See TracBrowser
for help on using the repository browser.