Changeset af8bda0 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:19Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2987160
Parents:
d91b329
git-author:
Dzejrou <dzejrou@…> (2017-12-18 12:28:07)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:19)
Message:

c: implemented a string to int64_t parsing function

Location:
uspace/lib/c
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/str.c

    rd91b329 raf8bda0  
    16891689}
    16901690
     1691/** Convert string to int64_t.
     1692 *
     1693 * @param nptr   Pointer to string.
     1694 * @param endptr If not NULL, pointer to the first invalid character
     1695 *               is stored here.
     1696 * @param base   Zero or number between 2 and 36 inclusive.
     1697 * @param strict Do not allow any trailing characters.
     1698 * @param result Result of the conversion.
     1699 *
     1700 * @return EOK if conversion was successful.
     1701 *
     1702 */
     1703int str_int64_t(const char *nptr, const char **endptr, unsigned int base,
     1704    bool strict, int64_t *result)
     1705{
     1706        assert(result != NULL);
     1707
     1708        bool neg;
     1709        char *lendptr;
     1710        uint64_t unsigned_result;
     1711        int ret = str_uint(nptr, &lendptr, base, &neg, &unsigned_result);
     1712
     1713        if (endptr != NULL)
     1714                *endptr = (char *) lendptr;
     1715
     1716        if (ret != EOK)
     1717                return ret;
     1718
     1719        /* Do not allow negative values */
     1720        if (neg) {
     1721                if (unsigned_result == UINT64_MAX)
     1722                        return EINVAL;
     1723
     1724                *result = -(int64_t)unsigned_result;
     1725        } else
     1726                *result = unsigned_result;
     1727
     1728        /* Check whether we are at the end of
     1729           the string in strict mode */
     1730        if ((strict) && (*lendptr != 0))
     1731                return EINVAL;
     1732
     1733        return EOK;
     1734}
     1735
    16911736/** Convert string to size_t.
    16921737 *
  • uspace/lib/c/include/str.h

    rd91b329 raf8bda0  
    126126extern errno_t str_size_t(const char *, const char **, unsigned int, bool,
    127127    size_t *);
     128extern int str_int64_t(const char *, const char **, unsigned int, bool,
     129    int64_t *);
    128130
    129131extern void order_suffix(const uint64_t, uint64_t *, char *);
Note: See TracChangeset for help on using the changeset viewer.