Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
logging.cc
Go to the documentation of this file.
1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 
5 #include "util/logging.h"
6 
7 #include <errno.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include "leveldb/env.h"
12 #include "leveldb/slice.h"
13 
14 namespace leveldb {
15 
16 void AppendNumberTo(std::string* str, uint64_t num) {
17  char buf[30];
18  snprintf(buf, sizeof(buf), "%llu", (unsigned long long) num);
19  str->append(buf);
20 }
21 
22 void AppendEscapedStringTo(std::string* str, const Slice& value) {
23  for (size_t i = 0; i < value.size(); i++) {
24  char c = value[i];
25  if (c >= ' ' && c <= '~') {
26  str->push_back(c);
27  } else {
28  char buf[10];
29  snprintf(buf, sizeof(buf), "\\x%02x",
30  static_cast<unsigned int>(c) & 0xff);
31  str->append(buf);
32  }
33  }
34 }
35 
36 std::string NumberToString(uint64_t num) {
37  std::string r;
38  AppendNumberTo(&r, num);
39  return r;
40 }
41 
42 std::string EscapeString(const Slice& value) {
43  std::string r;
44  AppendEscapedStringTo(&r, value);
45  return r;
46 }
47 
48 bool ConsumeChar(Slice* in, char c) {
49  if (!in->empty() && (*in)[0] == c) {
50  in->remove_prefix(1);
51  return true;
52  } else {
53  return false;
54  }
55 }
56 
58  uint64_t v = 0;
59  int digits = 0;
60  while (!in->empty()) {
61  char c = (*in)[0];
62  if (c >= '0' && c <= '9') {
63  ++digits;
64  const int delta = (c - '0');
65  static const uint64_t kMaxUint64 = ~static_cast<uint64_t>(0);
66  if (v > kMaxUint64/10 ||
67  (v == kMaxUint64/10 && delta > kMaxUint64%10)) {
68  // Overflow
69  return false;
70  }
71  v = (v * 10) + delta;
72  in->remove_prefix(1);
73  } else {
74  break;
75  }
76  }
77  *val = v;
78  return (digits > 0);
79 }
80 
81 } // namespace leveldb
std::string * value
Definition: version_set.cc:270
bool empty() const
Definition: slice.h:46
void remove_prefix(size_t n)
Definition: slice.h:59
bool ConsumeDecimalNumber(Slice *in, uint64_t *val)
Definition: logging.cc:57
size_t size() const
Definition: slice.h:43
unsigned long long uint64_t
Definition: stdint.h:22
std::string EscapeString(const Slice &value)
Definition: logging.cc:42
bool ConsumeChar(Slice *in, char c)
Definition: logging.cc:48
std::string NumberToString(uint64_t num)
Definition: logging.cc:36
void AppendNumberTo(std::string *str, uint64_t num)
Definition: logging.cc:16
void AppendEscapedStringTo(std::string *str, const Slice &value)
Definition: logging.cc:22