Merge branch 'MSVC' of https://github.com/fsb4000/novacoin into fsb4000-MSVC
[novacoin.git] / src / leveldb / db / leveldb_main.cc
1 // Copyright (c) 2012 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 <stdio.h>
6 #include "leveldb/dumpfile.h"
7 #include "leveldb/env.h"
8 #include "leveldb/status.h"
9
10 namespace leveldb {
11 namespace {
12
13 class StdoutPrinter : public WritableFile {
14  public:
15   virtual Status Append(const Slice& data) {
16     fwrite(data.data(), 1, data.size(), stdout);
17     return Status::OK();
18   }
19   virtual Status Close() { return Status::OK(); }
20   virtual Status Flush() { return Status::OK(); }
21   virtual Status Sync() { return Status::OK(); }
22 };
23
24 bool HandleDumpCommand(Env* env, char** files, int num) {
25   StdoutPrinter printer;
26   bool ok = true;
27   for (int i = 0; i < num; i++) {
28     Status s = DumpFile(env, files[i], &printer);
29     if (!s.ok()) {
30       fprintf(stderr, "%s\n", s.ToString().c_str());
31       ok = false;
32     }
33   }
34   return ok;
35 }
36
37 }  // namespace
38 }  // namespace leveldb
39
40 static void Usage() {
41   fprintf(
42       stderr,
43       "Usage: leveldbutil command...\n"
44       "   dump files...         -- dump contents of specified files\n"
45       );
46 }
47
48 int main(int argc, char** argv) {
49   leveldb::Env* env = leveldb::Env::Default();
50   bool ok = true;
51   if (argc < 2) {
52     Usage();
53     ok = false;
54   } else {
55     std::string command = argv[1];
56     if (command == "dump") {
57       ok = leveldb::HandleDumpCommand(env, argv+2, argc-2);
58     } else {
59       Usage();
60       ok = false;
61     }
62   }
63   return (ok ? 0 : 1);
64 }