![](/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@114:472502b31a12, 2015-04-20 (annotated)
- Committer:
- klauss
- Date:
- Mon Apr 20 14:40:09 2015 +0000
- Revision:
- 114:472502b31a12
- Parent:
- 113:db67ae00550e
- Child:
- 117:e9facba9db27
antes de implementar os retrys da header -> cbx
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 | 114:472502b31a12 | 23 | if( num < 0 //&& base == 10 |
klauss | 114:472502b31a12 | 24 | ){ |
klauss | 0:4d17cd9c8f9d | 25 | isNegative = true; |
klauss | 0:4d17cd9c8f9d | 26 | num = -num; |
klauss | 0:4d17cd9c8f9d | 27 | } |
klauss | 0:4d17cd9c8f9d | 28 | while( num != 0 ){ |
klauss | 0:4d17cd9c8f9d | 29 | int rem = num % base; |
klauss | 0:4d17cd9c8f9d | 30 | str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0'; |
klauss | 0:4d17cd9c8f9d | 31 | num = num/base; |
klauss | 0:4d17cd9c8f9d | 32 | } |
klauss | 0:4d17cd9c8f9d | 33 | if( isNegative ) |
klauss | 0:4d17cd9c8f9d | 34 | str[i++] = '-'; |
klauss | 0:4d17cd9c8f9d | 35 | |
klauss | 0:4d17cd9c8f9d | 36 | str[i] = '\0'; // Append string terminator |
klauss | 0:4d17cd9c8f9d | 37 | |
klauss | 0:4d17cd9c8f9d | 38 | reverse( str, i ); |
klauss | 0:4d17cd9c8f9d | 39 | return( str ); |
klauss | 0:4d17cd9c8f9d | 40 | } |
klauss | 0:4d17cd9c8f9d | 41 | |
klauss | 74:81c47fff88a5 | 42 | int convert_ext_to_port( int ext ){ |
klauss | 74:81c47fff88a5 | 43 | if( ext < 1000 ) return ext; |
klauss | 74:81c47fff88a5 | 44 | |
klauss | 74:81c47fff88a5 | 45 | else{ |
klauss | 74:81c47fff88a5 | 46 | int aux = 0; |
klauss | 74:81c47fff88a5 | 47 | aux = ( ext / 1000 ) * 1000; |
klauss | 74:81c47fff88a5 | 48 | ext -= aux; |
klauss | 74:81c47fff88a5 | 49 | aux += ext % 100; |
klauss | 74:81c47fff88a5 | 50 | return( aux ); |
klauss | 74:81c47fff88a5 | 51 | } |
klauss | 0:4d17cd9c8f9d | 52 | } |
klauss | 0:4d17cd9c8f9d | 53 | |
klauss | 74:81c47fff88a5 | 54 | void xstrcpy(uint8_t * dest, const uint8_t * src ){ |
klauss | 74:81c47fff88a5 | 55 | while (*src) *dest++ = *src++; |
klauss | 74:81c47fff88a5 | 56 | *dest = 0; |
klauss | 0:4d17cd9c8f9d | 57 | } |
klauss | 0:4d17cd9c8f9d | 58 | |
klauss | 78:1353744f01e1 | 59 | void xmemcpy(uint8_t * dest, const uint8_t * src, uint16_t size ){ |
klauss | 74:81c47fff88a5 | 60 | while (size--) *dest++ = *src++; |
klauss | 0:4d17cd9c8f9d | 61 | } |
klauss | 0:4d17cd9c8f9d | 62 | |
klauss | 74:81c47fff88a5 | 63 | void xmemcpy32(uint32_t * dest, uint32_t * src, uint16_t size){ |
klauss | 74:81c47fff88a5 | 64 | size >>= 2; |
klauss | 74:81c47fff88a5 | 65 | while (size--) *dest++ = *src++; |
klauss | 78:1353744f01e1 | 66 | } |
klauss | 78:1353744f01e1 | 67 | |
klauss | 78:1353744f01e1 | 68 | uint8_t xstrmatch(const uint8_t * s1, const uint8_t * s2){ |
klauss | 78:1353744f01e1 | 69 | while (*s1) { |
klauss | 78:1353744f01e1 | 70 | if (*s1++ != *s2++) return 0; /* does not match */ |
klauss | 78:1353744f01e1 | 71 | } |
klauss | 78:1353744f01e1 | 72 | if ( *s2 == 0 ) |
klauss | 78:1353744f01e1 | 73 | return 1; /* matches */ |
klauss | 78:1353744f01e1 | 74 | else |
klauss | 78:1353744f01e1 | 75 | return 0; /* does not match */ |
klauss | 81:3656f00ab3db | 76 | } |
klauss | 81:3656f00ab3db | 77 | |
klauss | 81:3656f00ab3db | 78 | uint8_t xmemmatch(const uint8_t * s1, const uint8_t * s2, uint16_t size){ |
klauss | 81:3656f00ab3db | 79 | while (size--) { |
klauss | 81:3656f00ab3db | 80 | if (*s1++ != *s2++) return 0; /* does not match */ |
klauss | 81:3656f00ab3db | 81 | } |
klauss | 81:3656f00ab3db | 82 | return 1; /* matches */ |
klauss | 74:81c47fff88a5 | 83 | } |