diff --git a/src/app/utilities.cpp b/src/app/utilities.cpp new file mode 100644 index 0000000..474b9f0 --- /dev/null +++ b/src/app/utilities.cpp @@ -0,0 +1,88 @@ +#include "utilities.h" + +char *addChar(char *pointer, const char character) +{ + char *tempAddr = NULL; + if(pointer == NULL) + { + tempAddr = (char *) realloc(pointer, 2*sizeof(char)); + if(tempAddr == NULL) + return NULL; + else + { + pointer = tempAddr; + pointer[0] = character; + pointer[1] = '\0'; + } + } + else + { + tempAddr = (char *) realloc(pointer, (strlen(pointer)+2)*sizeof(char)); + if(tempAddr == NULL) + { + free(pointer); + return NULL; + } + else + { + pointer = tempAddr; + pointer[strlen(pointer)+1] = '\0'; + pointer[strlen(pointer)] = character; + } + } + + return pointer; +} + +char *dateTimeFormater(char *pointer, const uint8_t value, const char character) +{ + if(pointer == NULL) + return pointer; + + if(value < 10) + { + sprintf(pointer,"%d", value); + *(pointer+1) = *(pointer);*(pointer) = character;*(pointer+2) = '\0'; + } + else + sprintf(pointer,"%d", value); + + return pointer; +} + +char *lastIndexOf(char *str, const char character) +{ + char *last(NULL), *current(str); + do + { + current = strchr(current, character); + if(current != NULL) + { + last = current; + if(*(current+1) == '\0')break; + + current += 1; + } + }while(current != NULL); + + return last; +} + +/** + * The monthNumTo3LetterAbbreviation function takes the month number from 1 to 12 and returns the abbreviation in a 3 letter + * format. + */ +uint32_t monthNumTo3LetterAbbreviation(const uint8_t monthNumber) +{ + /** + * This array contains months as 3 letter abbreviations + * Jan is written \0naJ and in hex : 0x006E614A an so on. They are thus 4Bytes aligned and easy to put and read back from prog mem :) + */ + static const uint32_t PROGMEM monthArray[] = {0x6E614A, 0x626546, 0x72614D, 0x727041, 0x79614D, 0x6E754A, 0x6C754A, 0x677541, 0x706553, 0x74634F, 0x766f4E, 0x636544}; + uint32_t toReturn(0x6E614A); //Default to Jan. + + if(monthNumber >= 1 && monthNumber <= 12) + toReturn = monthArray[monthNumber - 1]; + + return toReturn; +} diff --git a/src/app/utilities.h b/src/app/utilities.h new file mode 100644 index 0000000..8383c0c --- /dev/null +++ b/src/app/utilities.h @@ -0,0 +1,9 @@ +#ifndef UTILITIES_H +#define UTILITIES_H +#include + +char *addChar(char *pointer, const char character); +char *lastIndexOf(char *str, const char character); +char *dateTimeFormater(char *pointer, const uint8_t value, const char character); +uint32_t monthNumTo3LetterAbbreviation(const uint8_t monthNumber); +#endif // UTILITIES_H