]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/strtol.h
update sources to 12.2.8
[ceph.git] / ceph / src / common / strtol.h
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2011 New Dream Network
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#ifndef CEPH_COMMON_STRTOL_H
16#define CEPH_COMMON_STRTOL_H
17
18#include <string>
19extern "C" {
20#include <stdint.h>
21}
22
23long long strict_strtoll(const char *str, int base, std::string *err);
24
25int strict_strtol(const char *str, int base, std::string *err);
26
27double strict_strtod(const char *str, std::string *err);
28
29float strict_strtof(const char *str, std::string *err);
30
1adf2230
AA
31uint64_t strict_iecstrtoll(const char *str, std::string *err);
32
33template<typename T>
34T strict_iec_cast(const char *str, std::string *err);
35
7c673cae
FG
36uint64_t strict_sistrtoll(const char *str, std::string *err);
37
38template<typename T>
39T strict_si_cast(const char *str, std::string *err);
40
41/* On enter buf points to the end of the buffer, e.g. where the least
42 * significant digit of the input number will be printed. Returns pointer to
43 * where the most significant digit were printed, including zero padding.
44 * Does NOT add zero at the end of buffer, this is responsibility of the caller.
45 */
46template<typename T, const unsigned base = 10, const unsigned width = 1>
47static inline
48char* ritoa(T u, char *buf)
49{
50 static_assert(std::is_unsigned<T>::value, "signed types are not supported");
51 static_assert(base <= 16, "extend character map below to support higher bases");
52 unsigned digits = 0;
53 while (u) {
54 *--buf = "0123456789abcdef"[u % base];
55 u /= base;
56 digits++;
57 }
58 while (digits++ < width)
59 *--buf = '0';
60 return buf;
61}
62
63#endif