| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title format.cpp | ||
| 3 | // \author mstarch | ||
| 4 | // \brief cpp file for c-string format function as a implementation using snprintf | ||
| 5 | // ====================================================================== | ||
| 6 | #include <Fw/Types/StringUtils.hpp> | ||
| 7 | #include <Fw/Types/scan.hpp> | ||
| 8 | #include <cstdio> | ||
| 9 | #include <limits> | ||
| 10 | |||
| 11 | 5 | Fw::ScanStatus Fw::stringScan(FwSizeType& count, | |
| 12 | const char* source, | ||
| 13 | FwSizeType maximumSize, | ||
| 14 | const char* formatString, | ||
| 15 | ...) { | ||
| 16 | 5 | va_list args; | |
| 17 | 5 | va_start(args, formatString); | |
| 18 |
1/1✓ Branch 1 taken 5 times.
|
5 | Fw::ScanStatus status = Fw::stringScan(count, source, maximumSize, formatString, args); |
| 19 | 5 | va_end(args); | |
| 20 | 5 | return status; | |
| 21 | } | ||
| 22 | |||
| 23 | 6 | Fw::ScanStatus Fw::stringScan(FwSizeType& count, | |
| 24 | const char* source, | ||
| 25 | FwSizeType maximumSize, | ||
| 26 | const char* formatString, | ||
| 27 | va_list args) { | ||
| 28 | 6 | Fw::ScanStatus scanStatus = Fw::ScanStatus::SUCCESS; | |
| 29 | 6 | count = 0; | |
| 30 | // Check format string | ||
| 31 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | if (formatString == nullptr) { |
| 32 | 1 | scanStatus = Fw::ScanStatus::INVALID_FORMAT_STRING; | |
| 33 | } | ||
| 34 | // Must allow the compiler to choose the correct type for comparison | ||
| 35 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
|
5 | else if (maximumSize > std::numeric_limits<size_t>::max()) { |
| 36 | ✗ | scanStatus = Fw::ScanStatus::SIZE_OVERFLOW; | |
| 37 | } | ||
| 38 | // Check for null-termination of the source string bounded by maximumSize | ||
| 39 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
|
5 | else if (StringUtils::string_length(source, maximumSize) >= maximumSize) { |
| 40 | 1 | scanStatus = Fw::ScanStatus::UNTERMINATED_SOURCE_STRING; | |
| 41 | } else { | ||
| 42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | const int scannedFields = vsscanf(source, formatString, args); |
| 43 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (scannedFields < 0) { |
| 44 | ✗ | scanStatus = Fw::ScanStatus::OTHER_ERROR; | |
| 45 | } else { | ||
| 46 | 4 | count = static_cast<FwSizeType>(scannedFields); | |
| 47 | } | ||
| 48 | } | ||
| 49 | 6 | return scanStatus; | |
| 50 | } | ||
| 51 |