ESP8266_swiss_army_board/src/app/definition.cpp

85 lines
1.8 KiB
C++

#include "definition.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;
}
char *monthNumTo3LetterAbbreviation(char *buffer, const uint8_t monthNumber)
{
static const char monthArray[][4] = {{"Jan"},{"Feb"},{"Mar"},{"Apr"},{"May"},{"Jun"},{"Jul"},{"Aug"},{"Sep"},{"Oct"},{"Nov"},{"Dec"}};
if(buffer == NULL) return NULL;
if(monthNumber >= 1 && monthNumber <= 12)
strcpy_P(buffer, monthArray[monthNumber-1]);
else //Default is january
strcpy_P(buffer, monthArray[0]);
return buffer;
}