added own itoa function to remove snprintf - fixed a bug on the processing of key end #10

This commit is contained in:
prozessorkern 2019-06-24 18:07:12 +02:00
parent bc8a9f1dce
commit 4546cdf9bd
1 changed files with 57 additions and 4 deletions

View File

@ -66,6 +66,53 @@ static void writeEcho( shellmatta_instance_t *inst,
}
}
/**
* @brief itoa like function to convert int to an ascii string
* @warning you have to provide a large enough buffer
* @param[in] value
* @param[in,out] buffer
* @param[in] base
* @return number of bytes in string
*/
static uint32_t shellItoa(int32_t value, char *buffer, uint32_t base)
{
char tempBuffer[34u];
uint32_t i;
uint32_t bufferIdx = 0u;
char digitValue;
/** -# check the base for plausibility */
if((2 <= base) && (16 >= base))
{
/** -# check for sign */
if(0 > value)
{
value = value * (-1);
buffer[0u] = '-';
bufferIdx += 1u;
}
/** -# loop through all digits in reverse order */
i = 0u;
do
{
digitValue = (char) (value % base);
tempBuffer[i] = (digitValue < 10u) ? ('0' + digitValue) : ('A' + digitValue);
value /= base;
i ++;
}while(value > 0);
/** -# store the string in the correct order onto the buffer */
while(i > 0u)
{
buffer[bufferIdx] = tempBuffer[i - 1u];
i --;
bufferIdx ++;
}
}
return bufferIdx;
}
/**
* @brief appends a byte to the history ring stack buffer
* @param[in] inst pointer to a shellmatta instance
@ -312,8 +359,11 @@ static void rewindCursor(shellmatta_instance_t *inst, uint32_t length)
length = SHELLMATTA_MIN (length, inst->cursor);
if(length > 0u)
{
size = snprintf(terminalCmd, sizeof(terminalCmd), "\e[%uD", length);
writeEcho(inst, terminalCmd, size);
terminalCmd[0] = '\e';
terminalCmd[1] = '[';
size = 2u + shellItoa(length, &terminalCmd[2], 10);
terminalCmd[size] = 'D';
writeEcho(inst, terminalCmd, size + 1u);
inst->cursor -= length;
}
}
@ -331,8 +381,11 @@ static void forwardCursor(shellmatta_instance_t *inst, uint32_t length)
length = SHELLMATTA_MAX (length, (inst->inputCount - inst->cursor));
if (length > 0u)
{
size = snprintf(terminalCmd, sizeof(terminalCmd), "\e[%uC", length);
writeEcho(inst, terminalCmd, size);
terminalCmd[0] = '\e';
terminalCmd[1] = '[';
size = 2u + shellItoa(length, &terminalCmd[2], 10);
terminalCmd[size] = 'C';
writeEcho(inst, terminalCmd, size + 1u);
inst->cursor += length;
}
}