source: soft/giet_vm/giet_libs/string.c @ 777

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