Skip to content

Commit

Permalink
Merge pull request lancaster-university#325 from dpgeorge/image-remov…
Browse files Browse the repository at this point in the history
…e-atoi

Rewrite MicroBitImage parsing code so it doesn't depend on atoi()
  • Loading branch information
finneyj authored Dec 19, 2017
2 parents 6fa716f + fbfee49 commit 2cff906
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions source/types/MicroBitImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,8 @@ MicroBitImage::MicroBitImage(const char *s)
int count = 0;
int digit = 0;

char parseBuf[10];

const char *parseReadPtr;
char *parseWritePtr;
int parseValue;
uint8_t *bitmapPtr;

if (s == NULL)
Expand Down Expand Up @@ -169,24 +167,26 @@ MicroBitImage::MicroBitImage(const char *s)

// Second pass: collect the data.
parseReadPtr = s;
parseWritePtr = parseBuf;
parseValue = -1;
bitmapPtr = this->getBitmap();

while (*parseReadPtr)
{
if (isdigit(*parseReadPtr))
{
*parseWritePtr = *parseReadPtr;
parseWritePtr++;
if (parseValue < 0)
{
parseValue = 0;
}
parseValue = parseValue * 10 + *parseReadPtr - '0';
}
else
{
*parseWritePtr = 0;
if (parseWritePtr > parseBuf)
if (parseValue >= 0)
{
*bitmapPtr = atoi(parseBuf);
*bitmapPtr = parseValue;
bitmapPtr++;
parseWritePtr = parseBuf;
parseValue = -1;
}
}

Expand Down

0 comments on commit 2cff906

Please sign in to comment.