/root/bitcoin/src/leveldb/util/status.cc
Line | Count | Source (jump to first uncovered line) |
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 "leveldb/status.h" |
6 | | |
7 | | #include <stdio.h> |
8 | | |
9 | | #include "port/port.h" |
10 | | |
11 | | namespace leveldb { |
12 | | |
13 | 0 | const char* Status::CopyState(const char* state) { |
14 | 0 | uint32_t size; |
15 | 0 | memcpy(&size, state, sizeof(size)); |
16 | 0 | char* result = new char[size + 5]; |
17 | 0 | memcpy(result, state, size + 5); |
18 | 0 | return result; |
19 | 0 | } |
20 | | |
21 | 0 | Status::Status(Code code, const Slice& msg, const Slice& msg2) { |
22 | 0 | assert(code != kOk); |
23 | 0 | const uint32_t len1 = static_cast<uint32_t>(msg.size()); |
24 | 0 | const uint32_t len2 = static_cast<uint32_t>(msg2.size()); |
25 | 0 | const uint32_t size = len1 + (len2 ? (2 + len2) : 0); |
26 | 0 | char* result = new char[size + 5]; |
27 | 0 | memcpy(result, &size, sizeof(size)); |
28 | 0 | result[4] = static_cast<char>(code); |
29 | 0 | memcpy(result + 5, msg.data(), len1); |
30 | 0 | if (len2) { |
31 | 0 | result[5 + len1] = ':'; |
32 | 0 | result[6 + len1] = ' '; |
33 | 0 | memcpy(result + 7 + len1, msg2.data(), len2); |
34 | 0 | } |
35 | 0 | state_ = result; |
36 | 0 | } |
37 | | |
38 | 0 | std::string Status::ToString() const { |
39 | 0 | if (state_ == nullptr) { |
40 | 0 | return "OK"; |
41 | 0 | } else { |
42 | 0 | char tmp[30]; |
43 | 0 | const char* type; |
44 | 0 | switch (code()) { |
45 | 0 | case kOk: |
46 | 0 | type = "OK"; |
47 | 0 | break; |
48 | 0 | case kNotFound: |
49 | 0 | type = "NotFound: "; |
50 | 0 | break; |
51 | 0 | case kCorruption: |
52 | 0 | type = "Corruption: "; |
53 | 0 | break; |
54 | 0 | case kNotSupported: |
55 | 0 | type = "Not implemented: "; |
56 | 0 | break; |
57 | 0 | case kInvalidArgument: |
58 | 0 | type = "Invalid argument: "; |
59 | 0 | break; |
60 | 0 | case kIOError: |
61 | 0 | type = "IO error: "; |
62 | 0 | break; |
63 | 0 | default: |
64 | 0 | snprintf(tmp, sizeof(tmp), |
65 | 0 | "Unknown code(%d): ", static_cast<int>(code())); |
66 | 0 | type = tmp; |
67 | 0 | break; |
68 | 0 | } |
69 | 0 | std::string result(type); |
70 | 0 | uint32_t length; |
71 | 0 | memcpy(&length, state_, sizeof(length)); |
72 | 0 | result.append(state_ + 5, length); |
73 | 0 | return result; |
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | | } // namespace leveldb |