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