1 // Author: Bruce Allen
2 // Created: 2/25/2013
3 //
4 // The software provided here is released by the Naval Postgraduate
5 // School, an agency of the U.S. Department of Navy. The software
6 // bears no warranty, either expressed or implied. NPS does not assume
7 // legal liability nor responsibility for a User's use of the software
8 // or the results of such use.
9 //
10 // Please note that within the United States, copyright protection,
11 // under Section 105 of the United States Code, Title 17, is not
12 // available for any work of the United States Government and/or for
13 // any works created by United States Government employees. User
14 // acknowledges that this software contains work which was created by
15 // NPS government employees and is therefore in the public domain and
16 // not subject to copyright.
17 //
18 // Released into the public domain on February 25, 2013 by Bruce Allen.
19
20 /**
21 * \file
22 * Import from data in JSON format. Lines are one of source data,
23 * block hash data, or comment.
24 */
25
26 #include <config.h>
27 // this process of getting WIN32 defined was inspired
28 // from i686-w64-mingw32/sys-root/mingw/include/windows.h.
29 // All this to include winsock2.h before windows.h to avoid a warning.
30 #if defined(__MINGW64__) && defined(__cplusplus)
31 # ifndef WIN32
32 # define WIN32
33 # endif
34 #endif
35 #ifdef WIN32
36 // including winsock2.h now keeps an included header somewhere from
37 // including windows.h first, resulting in a warning.
38 #include <winsock2.h>
39 #endif
40
41 #include <sstream>
42 #include "../src_libhashdb/hashdb.hpp"
43 #include "progress_tracker.hpp"
44 #include <fstream>
45
import_json(hashdb::import_manager_t & manager,progress_tracker_t & progress_tracker,std::istream & in)46 void import_json(hashdb::import_manager_t& manager,
47 progress_tracker_t& progress_tracker,
48 std::istream& in) {
49 std::string line;
50 size_t line_number = 0;
51 while(getline(in, line)) {
52 ++line_number;
53
54 // skip comment lines
55 if (line[0] == '#') {
56 continue;
57 }
58
59 // skip empty lines
60 if (line.size() == 0) {
61 continue;
62 }
63
64 // import JSON
65 std::string error_message = manager.import_json(line);
66 if (error_message.size() != 0) {
67 std::cerr << "Invalid line " << line_number
68 << " error: " << error_message
69 << ": '" << line << "'\n";
70 } else {
71 progress_tracker.track();
72 }
73 }
74 }
75
76