| 38 | } |
| 39 | |
| 40 | bool |
| 41 | Range::fromStringClosed(char const *const rangestr) |
| 42 | { |
| 43 | static char const *const BYTESTR = "bytes="; |
| 44 | static size_t const BYTESTRLEN = strlen(BYTESTR); |
| 45 | |
| 46 | m_beg = m_end = -1; // initialize invalid |
| 47 | |
| 48 | // make sure this is in byte units |
| 49 | if (0 != strncmp(BYTESTR, rangestr, BYTESTRLEN)) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | // advance past any white space |
| 54 | char const *pstr = rangestr + BYTESTRLEN; |
| 55 | while ('\0' != *pstr && isblank(*pstr)) { |
| 56 | ++pstr; |
| 57 | } |
| 58 | |
| 59 | // rip out any whitespace |
| 60 | char rangebuf[1024]; |
| 61 | int const rangelen = sizeof(rangebuf); |
| 62 | char *pbuf = rangebuf; |
| 63 | while ('\0' != *pstr && (pbuf - rangebuf) < rangelen) { |
| 64 | if (!isblank(*pstr)) { |
| 65 | *pbuf++ = *pstr; |
| 66 | } |
| 67 | ++pstr; |
| 68 | } |
| 69 | *pbuf = '\0'; |
| 70 | |
| 71 | int const rlen = (pbuf - rangebuf); |
| 72 | |
| 73 | int consumed = 0; |
| 74 | |
| 75 | // last 'n' bytes - result in range with negative begin and 0 end |
| 76 | int64_t endbytes = 0; |
| 77 | char const *const fmtend = "-%" PRId64 "%n"; |
| 78 | int const fieldsend = sscanf(rangebuf, fmtend, &endbytes, &consumed); |
| 79 | if (1 == fieldsend) { |
| 80 | if (rlen == consumed) { |
| 81 | m_beg = -endbytes; |
| 82 | m_end = 0; |
| 83 | return true; |
| 84 | } else { |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // normal range <front>-<back> |
| 90 | char const *const fmtclosed = "%" PRId64 "-%" PRId64 "%n"; |
| 91 | int64_t front = 0; |
| 92 | int64_t back = 0; |
| 93 | |
| 94 | int const fieldsclosed = sscanf(rangebuf, fmtclosed, &front, &back, &consumed); |
| 95 | if (2 == fieldsclosed) { |
| 96 | if (0 <= front && front <= back && rlen == consumed) { |
| 97 | m_beg = front; |
no outgoing calls