| [259] | 1 | ////////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 2 | // File     : stdlib.c | 
|---|
|  | 3 | // Date     : 05/12/2013 | 
|---|
|  | 4 | // Author   : Clément DEVIGNE | 
|---|
|  | 5 | // Copyright (c) UPMC-LIP6 | 
|---|
|  | 6 | /////////////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 7 |  | 
|---|
| [271] | 8 | #include <stdlib.h> | 
|---|
|  | 9 |  | 
|---|
| [647] | 10 | ///////////////////////// | 
|---|
| [580] | 11 | int atoi(const char *str) | 
|---|
| [259] | 12 | { | 
|---|
|  | 13 | int res  = 0; // Initialize result | 
|---|
|  | 14 | int sign = 1; // Initialize sign as positive | 
|---|
|  | 15 | int i    = 0; // Initialize index of first digit | 
|---|
|  | 16 |  | 
|---|
|  | 17 | if (str[0] == '-') //If number is negative, then update sign | 
|---|
|  | 18 | { | 
|---|
|  | 19 | sign = -1; | 
|---|
|  | 20 | i++;           // Also update index of first digit | 
|---|
|  | 21 | } | 
|---|
|  | 22 |  | 
|---|
|  | 23 | for (; str[i] != '\0'; ++i) // Iterate through all digits and update the result | 
|---|
|  | 24 | { | 
|---|
|  | 25 | res = res*10 + str[i] - '0'; | 
|---|
|  | 26 | } | 
|---|
|  | 27 |  | 
|---|
|  | 28 | // Return result with sign | 
|---|
|  | 29 | return sign*res; | 
|---|
|  | 30 | } | 
|---|
| [580] | 31 |  | 
|---|
| [647] | 32 | //////////////////////////// | 
|---|
| [580] | 33 | double atof(const char *str) | 
|---|
|  | 34 | { | 
|---|
|  | 35 | const char *pstr = str; | 
|---|
|  | 36 | double res = 0; | 
|---|
|  | 37 | double exp = 0.1; | 
|---|
|  | 38 | short sign = 1; | 
|---|
|  | 39 | short dec = 0; | 
|---|
|  | 40 |  | 
|---|
|  | 41 | while (*pstr != '\0') | 
|---|
|  | 42 | { | 
|---|
|  | 43 | if (*pstr == '-') | 
|---|
|  | 44 | { | 
|---|
|  | 45 | if (str != pstr) break; | 
|---|
|  | 46 | sign = -1; | 
|---|
|  | 47 | } | 
|---|
| [259] | 48 |  | 
|---|
| [580] | 49 | else if (*pstr == '.') | 
|---|
|  | 50 | { | 
|---|
|  | 51 | if (dec) break; | 
|---|
|  | 52 | dec = 1; | 
|---|
|  | 53 | } | 
|---|
|  | 54 |  | 
|---|
|  | 55 | else if (*pstr >= '0' && *pstr <= '9') | 
|---|
|  | 56 | { | 
|---|
|  | 57 | if (dec) | 
|---|
|  | 58 | { | 
|---|
|  | 59 | res = res + ((*pstr - '0')*exp); | 
|---|
|  | 60 | exp = exp / 10; | 
|---|
|  | 61 | } | 
|---|
|  | 62 | else | 
|---|
|  | 63 | { | 
|---|
|  | 64 | res = (res * 10) + (*pstr - '0'); | 
|---|
|  | 65 | } | 
|---|
|  | 66 | } | 
|---|
|  | 67 |  | 
|---|
|  | 68 | else | 
|---|
|  | 69 | { | 
|---|
|  | 70 | break; | 
|---|
|  | 71 | } | 
|---|
|  | 72 | pstr++; | 
|---|
|  | 73 | } | 
|---|
|  | 74 | return sign * res; | 
|---|
|  | 75 | } | 
|---|
|  | 76 |  | 
|---|
| [647] | 77 | /////////////////////////////////////////////////////////////// | 
|---|
| [382] | 78 | void * memcpy(void *_dst, const void * _src, unsigned int size) | 
|---|
| [352] | 79 | { | 
|---|
| [271] | 80 | unsigned int * dst = _dst; | 
|---|
|  | 81 | const unsigned int * src = _src; | 
|---|
|  | 82 | if (!((unsigned int) dst & 3) && !((unsigned int) src & 3) ) | 
|---|
| [352] | 83 | { | 
|---|
|  | 84 | while (size > 3) | 
|---|
|  | 85 | { | 
|---|
| [271] | 86 | *dst++ = *src++; | 
|---|
|  | 87 | size -= 4; | 
|---|
|  | 88 | } | 
|---|
| [352] | 89 | } | 
|---|
| [271] | 90 |  | 
|---|
|  | 91 | unsigned char *cdst = (unsigned char*)dst; | 
|---|
|  | 92 | unsigned char *csrc = (unsigned char*)src; | 
|---|
|  | 93 |  | 
|---|
| [352] | 94 | while (size--) | 
|---|
|  | 95 | { | 
|---|
| [271] | 96 | *cdst++ = *csrc++; | 
|---|
|  | 97 | } | 
|---|
|  | 98 | return _dst; | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
| [647] | 101 | ////////////////////////////////////////////////////////// | 
|---|
| [382] | 102 | inline void * memset(void * dst, int s, unsigned int size) | 
|---|
| [352] | 103 | { | 
|---|
| [271] | 104 | char * a = (char *) dst; | 
|---|
| [382] | 105 | while (size--) | 
|---|
| [352] | 106 | { | 
|---|
| [271] | 107 | *a++ = (char)s; | 
|---|
|  | 108 | } | 
|---|
|  | 109 | return dst; | 
|---|
|  | 110 | } | 
|---|
|  | 111 |  | 
|---|
| [647] | 112 | /////////////////////////////////// | 
|---|
|  | 113 | unsigned int strlen( char* string ) | 
|---|
|  | 114 | { | 
|---|
|  | 115 | unsigned int i = 0; | 
|---|
|  | 116 | while ( string[i] != 0 ) i++; | 
|---|
|  | 117 | return i; | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
|  | 120 | /////////////////////////////// | 
|---|
|  | 121 | unsigned int strcmp( char * s1, | 
|---|
|  | 122 | char * s2 ) | 
|---|
|  | 123 | { | 
|---|
|  | 124 | while (1) | 
|---|
|  | 125 | { | 
|---|
|  | 126 | if (*s1 != *s2) return 1; | 
|---|
|  | 127 | if (*s1 == 0)   break; | 
|---|
|  | 128 | s1++, s2++; | 
|---|
|  | 129 | } | 
|---|
|  | 130 | return 0; | 
|---|
|  | 131 | } | 
|---|
|  | 132 |  | 
|---|
|  | 133 | ///////////////////////// | 
|---|
|  | 134 | char* strcpy( char* dest, | 
|---|
|  | 135 | char* source ) | 
|---|
|  | 136 | { | 
|---|
|  | 137 | if (!dest || !source) return dest; | 
|---|
|  | 138 |  | 
|---|
|  | 139 | while (*source) | 
|---|
|  | 140 | { | 
|---|
|  | 141 | *(dest) = *(source); | 
|---|
|  | 142 | dest++; | 
|---|
|  | 143 | source++; | 
|---|
|  | 144 | } | 
|---|
|  | 145 | *dest = 0; | 
|---|
|  | 146 | return dest; | 
|---|
|  | 147 | } | 
|---|
|  | 148 |  | 
|---|
| [259] | 149 | // Local Variables: | 
|---|
|  | 150 | // tab-width: 4 | 
|---|
|  | 151 | // c-basic-offset: 4 | 
|---|
|  | 152 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) | 
|---|
|  | 153 | // indent-tabs-mode: nil | 
|---|
|  | 154 | // End: | 
|---|
|  | 155 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 | 
|---|