/bitcoin/src/leveldb/util/status.cc
Line | Count | Source |
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 | 5.08M | Status::Status(Code code, const Slice& msg, const Slice& msg2) { |
22 | 5.08M | assert(code != kOk); Branch (22:3): [True: 5.08M, False: 0]
|
23 | 5.08M | const uint32_t len1 = static_cast<uint32_t>(msg.size()); |
24 | 5.08M | const uint32_t len2 = static_cast<uint32_t>(msg2.size()); |
25 | 5.08M | const uint32_t size = len1 + (len2 ? (2 + len2) : 0); Branch (25:33): [True: 33.2k, False: 5.05M]
|
26 | 5.08M | char* result = new char[size + 5]; |
27 | 5.08M | memcpy(result, &size, sizeof(size)); |
28 | 5.08M | result[4] = static_cast<char>(code); |
29 | 5.08M | memcpy(result + 5, msg.data(), len1); |
30 | 5.08M | if (len2) { Branch (30:7): [True: 33.2k, False: 5.05M]
|
31 | 33.2k | result[5 + len1] = ':'; |
32 | 33.2k | result[6 + len1] = ' '; |
33 | 33.2k | memcpy(result + 7 + len1, msg2.data(), len2); |
34 | 33.2k | } |
35 | 5.08M | state_ = result; |
36 | 5.08M | } |
37 | | |
38 | 24 | std::string Status::ToString() const { |
39 | 24 | if (state_ == nullptr) { Branch (39:7): [True: 24, False: 0]
|
40 | 24 | return "OK"; |
41 | 24 | } else { |
42 | 0 | char tmp[30]; |
43 | 0 | const char* type; |
44 | 0 | switch (code()) { |
45 | 0 | case kOk: Branch (45:7): [True: 0, False: 0]
|
46 | 0 | type = "OK"; |
47 | 0 | break; |
48 | 0 | case kNotFound: Branch (48:7): [True: 0, False: 0]
|
49 | 0 | type = "NotFound: "; |
50 | 0 | break; |
51 | 0 | case kCorruption: Branch (51:7): [True: 0, False: 0]
|
52 | 0 | type = "Corruption: "; |
53 | 0 | break; |
54 | 0 | case kNotSupported: Branch (54:7): [True: 0, False: 0]
|
55 | 0 | type = "Not implemented: "; |
56 | 0 | break; |
57 | 0 | case kInvalidArgument: Branch (57:7): [True: 0, False: 0]
|
58 | 0 | type = "Invalid argument: "; |
59 | 0 | break; |
60 | 0 | case kIOError: Branch (60:7): [True: 0, False: 0]
|
61 | 0 | type = "IO error: "; |
62 | 0 | break; |
63 | 0 | default: Branch (63:7): [True: 0, False: 0]
|
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 | 24 | } |
76 | | |
77 | | } // namespace leveldb |