Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / strtol.h
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>
19 extern "C" {
20 #include <stdint.h>
21 }
22
23 long long strict_strtoll(const char *str, int base, std::string *err);
24
25 int strict_strtol(const char *str, int base, std::string *err);
26
27 double strict_strtod(const char *str, std::string *err);
28
29 float strict_strtof(const char *str, std::string *err);
30
31 uint64_t strict_sistrtoll(const char *str, std::string *err);
32
33 template<typename T>
34 T strict_si_cast(const char *str, std::string *err);
35
36 /* On enter buf points to the end of the buffer, e.g. where the least
37  * significant digit of the input number will be printed. Returns pointer to
38  * where the most significant digit were printed, including zero padding.
39  * Does NOT add zero at the end of buffer, this is responsibility of the caller.
40  */
41 template<typename T, const unsigned base = 10, const unsigned width = 1>
42 static inline
43 char* ritoa(T u, char *buf)
44 {
45   static_assert(std::is_unsigned<T>::value, "signed types are not supported");
46   static_assert(base <= 16, "extend character map below to support higher bases");
47   unsigned digits = 0;
48   while (u) {
49     *--buf = "0123456789abcdef"[u % base];
50     u /= base;
51     digits++;
52   }
53   while (digits++ < width)
54     *--buf = '0';
55   return buf;
56 }
57
58 #endif