![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Modularizando o src
Dependencies: EALib EthernetInterface_vz mbed-rtos mbed
Fork of header_main_colinas_V0-20-09-14 by
utils.cpp@113:db67ae00550e, 2015-04-16 (annotated)
- Committer:
- klauss
- Date:
- Thu Apr 16 12:57:13 2015 +0000
- Revision:
- 113:db67ae00550e
- Parent:
- 112:6ae726539ab9
- Child:
- 114:472502b31a12
valide esse fw
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
klauss | 0:4d17cd9c8f9d | 1 | #include "utils.h" |
klauss | 0:4d17cd9c8f9d | 2 | |
klauss | 74:81c47fff88a5 | 3 | int ls_comp( const void * a, const void * b ){ return( *( int * )a - *( int * )b ); } |
klauss | 68:b54993674190 | 4 | |
klauss | 0:4d17cd9c8f9d | 5 | void reverse( char str[], int length ){ |
klauss | 0:4d17cd9c8f9d | 6 | int start = 0; |
klauss | 0:4d17cd9c8f9d | 7 | int end = length -1; |
klauss | 0:4d17cd9c8f9d | 8 | while (start < end){ |
klauss | 0:4d17cd9c8f9d | 9 | swap(*(str+start), *(str+end)); |
klauss | 0:4d17cd9c8f9d | 10 | start++; |
klauss | 0:4d17cd9c8f9d | 11 | end--; |
klauss | 0:4d17cd9c8f9d | 12 | } |
klauss | 0:4d17cd9c8f9d | 13 | } |
klauss | 0:4d17cd9c8f9d | 14 | char* itoa(int num, char* str, int base){ |
klauss | 0:4d17cd9c8f9d | 15 | int i = 0; |
klauss | 0:4d17cd9c8f9d | 16 | bool isNegative = false; |
klauss | 0:4d17cd9c8f9d | 17 | |
klauss | 0:4d17cd9c8f9d | 18 | if( num == 0 ){ |
klauss | 0:4d17cd9c8f9d | 19 | str[i++] = '0'; |
klauss | 0:4d17cd9c8f9d | 20 | str[i] = '\0'; |
klauss | 0:4d17cd9c8f9d | 21 | return str; |
klauss | 0:4d17cd9c8f9d | 22 | } |
klauss | 113:db67ae00550e | 23 | if( num < 0 && base == 10 ){ |
klauss | 0:4d17cd9c8f9d | 24 | isNegative = true; |
klauss | 0:4d17cd9c8f9d | 25 | num = -num; |
klauss | 0:4d17cd9c8f9d | 26 | } |
klauss | 0:4d17cd9c8f9d | 27 | while( num != 0 ){ |
klauss | 0:4d17cd9c8f9d | 28 | int rem = num % base; |
klauss | 0:4d17cd9c8f9d | 29 | str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; |
klauss | 0:4d17cd9c8f9d | 30 | num = num/base; |
klauss | 0:4d17cd9c8f9d | 31 | } |
klauss | 0:4d17cd9c8f9d | 32 | if( isNegative ) |
klauss | 0:4d17cd9c8f9d | 33 | str[i++] = '-'; |
klauss | 0:4d17cd9c8f9d | 34 | |
klauss | 0:4d17cd9c8f9d | 35 | str[i] = '\0'; // Append string terminator |
klauss | 0:4d17cd9c8f9d | 36 | |
klauss | 0:4d17cd9c8f9d | 37 | reverse( str, i ); |
klauss | 0:4d17cd9c8f9d | 38 | return( str ); |
klauss | 0:4d17cd9c8f9d | 39 | } |
klauss | 0:4d17cd9c8f9d | 40 | |
klauss | 74:81c47fff88a5 | 41 | int convert_ext_to_port( int ext ){ |
klauss | 74:81c47fff88a5 | 42 | if( ext < 1000 ) return ext; |
klauss | 74:81c47fff88a5 | 43 | |
klauss | 74:81c47fff88a5 | 44 | else{ |
klauss | 74:81c47fff88a5 | 45 | int aux = 0; |
klauss | 74:81c47fff88a5 | 46 | aux = ( ext / 1000 ) * 1000; |
klauss | 74:81c47fff88a5 | 47 | ext -= aux; |
klauss | 74:81c47fff88a5 | 48 | aux += ext % 100; |
klauss | 74:81c47fff88a5 | 49 | return( aux ); |
klauss | 74:81c47fff88a5 | 50 | } |
klauss | 0:4d17cd9c8f9d | 51 | } |
klauss | 0:4d17cd9c8f9d | 52 | |
klauss | 74:81c47fff88a5 | 53 | void xstrcpy(uint8_t * dest, const uint8_t * src ){ |
klauss | 74:81c47fff88a5 | 54 | while (*src) *dest++ = *src++; |
klauss | 74:81c47fff88a5 | 55 | *dest = 0; |
klauss | 0:4d17cd9c8f9d | 56 | } |
klauss | 0:4d17cd9c8f9d | 57 | |
klauss | 78:1353744f01e1 | 58 | void xmemcpy(uint8_t * dest, const uint8_t * src, uint16_t size ){ |
klauss | 74:81c47fff88a5 | 59 | while (size--) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 60 | } |
klauss | 0:4d17cd9c8f9d | 61 | |
klauss | 74:81c47fff88a5 | 62 | void xmemcpy32(uint32_t * dest, uint32_t * src, uint16_t size){ |
klauss | 74:81c47fff88a5 | 63 | size >>= 2; |
klauss | 74:81c47fff88a5 | 64 | while (size--) *dest++ = *src++; |
klauss | 78:1353744f01e1 | 65 | } |
klauss | 78:1353744f01e1 | 66 | |
klauss | 78:1353744f01e1 | 67 | uint8_t xstrmatch(const uint8_t * s1, const uint8_t * s2){ |
klauss | 78:1353744f01e1 | 68 | while (*s1) { |
klauss | 78:1353744f01e1 | 69 | if (*s1++ != *s2++) return 0; /* does not match */ |
klauss | 78:1353744f01e1 | 70 | } |
klauss | 78:1353744f01e1 | 71 | if ( *s2 == 0 ) |
klauss | 78:1353744f01e1 | 72 | return 1; /* matches */ |
klauss | 78:1353744f01e1 | 73 | else |
klauss | 78:1353744f01e1 | 74 | return 0; /* does not match */ |
klauss | 81:3656f00ab3db | 75 | } |
klauss | 81:3656f00ab3db | 76 | |
klauss | 81:3656f00ab3db | 77 | uint8_t xmemmatch(const uint8_t * s1, const uint8_t * s2, uint16_t size){ |
klauss | 81:3656f00ab3db | 78 | while (size--) { |
klauss | 81:3656f00ab3db | 79 | if (*s1++ != *s2++) return 0; /* does not match */ |
klauss | 81:3656f00ab3db | 80 | } |
klauss | 81:3656f00ab3db | 81 | return 1; /* matches */ |
klauss | 74:81c47fff88a5 | 82 | } |