2015-08-12 10:53:41 +00:00
|
|
|
/**
|
|
|
|
* Compatibility / portability funcitons and constants for the MicroBit DAL.
|
|
|
|
*/
|
2015-09-02 11:42:24 +00:00
|
|
|
#include "mbed.h"
|
2015-10-25 21:51:33 +00:00
|
|
|
#include "ErrorNo.h"
|
2015-08-12 10:53:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs an in buffer reverse of a given char array
|
|
|
|
* @param s the char* to reverse.
|
2015-10-25 21:51:33 +00:00
|
|
|
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
|
2015-08-12 10:53:41 +00:00
|
|
|
*/
|
2015-10-25 21:51:33 +00:00
|
|
|
int string_reverse(char *s)
|
2015-08-12 10:53:41 +00:00
|
|
|
{
|
|
|
|
//sanity check...
|
|
|
|
if(s == NULL)
|
2015-10-25 21:51:33 +00:00
|
|
|
return MICROBIT_INVALID_PARAMETER;
|
2015-08-12 10:53:41 +00:00
|
|
|
|
|
|
|
char *j;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
j = s + strlen(s) - 1;
|
|
|
|
|
|
|
|
while(s < j)
|
|
|
|
{
|
|
|
|
c = *s;
|
|
|
|
*s++ = *j;
|
|
|
|
*j-- = c;
|
|
|
|
}
|
2015-10-25 21:51:33 +00:00
|
|
|
|
|
|
|
return MICROBIT_OK;
|
2015-08-12 10:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a given integer into a base 10 ASCII equivalent.
|
|
|
|
*
|
|
|
|
* @param n The number to convert.
|
|
|
|
* @param s Pointer to a buffer in which to store the resulting string.
|
2015-10-25 21:51:33 +00:00
|
|
|
* @return MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
|
2015-08-12 10:53:41 +00:00
|
|
|
*/
|
2015-10-25 21:51:33 +00:00
|
|
|
int itoa(int n, char *s)
|
2015-08-12 10:53:41 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
2015-09-29 22:51:20 +00:00
|
|
|
int positive = (n >= 0);
|
2015-08-12 10:53:41 +00:00
|
|
|
|
2015-10-25 21:51:33 +00:00
|
|
|
if (s == NULL)
|
|
|
|
return MICROBIT_INVALID_PARAMETER;
|
|
|
|
|
2015-08-12 10:53:41 +00:00
|
|
|
// Record the sign of the number,
|
|
|
|
// Ensure our working value is positive.
|
2015-09-29 22:51:20 +00:00
|
|
|
if (positive)
|
2015-08-12 10:53:41 +00:00
|
|
|
n = -n;
|
|
|
|
|
|
|
|
// Calculate each character, starting with the LSB.
|
|
|
|
do {
|
2015-09-29 22:51:20 +00:00
|
|
|
s[i++] = abs(n % 10) + '0';
|
|
|
|
} while (abs(n /= 10) > 0);
|
2015-08-12 10:53:41 +00:00
|
|
|
|
|
|
|
// Add a negative sign as needed
|
2015-09-29 22:51:20 +00:00
|
|
|
if (!positive)
|
2015-08-12 10:53:41 +00:00
|
|
|
s[i++] = '-';
|
|
|
|
|
|
|
|
// Terminate the string.
|
|
|
|
s[i] = '\0';
|
|
|
|
|
|
|
|
// Flip the order.
|
|
|
|
string_reverse(s);
|
2015-10-25 21:51:33 +00:00
|
|
|
|
|
|
|
return MICROBIT_OK;
|
2015-08-12 10:53:41 +00:00
|
|
|
}
|
2015-09-02 11:42:24 +00:00
|
|
|
|