forked from JadeMatrix/debencode
/
debencode.cpp
97 lines (77 loc) · 2.42 KB
/
debencode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* debencode.cpp
*
* Standalone command-line debencode utility
*
* debencode copyright (C) 2015 Joseph Durel [ jadematrix.art@gmail.com ]
* debencode homepage: [ github.com/JadeMatrix/debencode ]
*/
/* INCLUDES *******************************************************************//******************************************************************************/
#include <iostream>
#include <map>
#include <fstream>
#include "debencode.hpp"
/* DECLARATIONS ***************************************************************//******************************************************************************/
enum parse_type
{
INVALID,
BENCODE,
JSON,
XML
};
/* DEFINITIONS ****************************************************************//******************************************************************************/
// #ifdef DEBENCODE_STANDALONE
inline int print_usage( const char* name )
{
std::cout << "Usage: " << name << " [ bencode | json | xml ] file\n";
return 0x01;
}
int main( int argc, char* argv[] )
{
if( argc != 3 )
return print_usage( argv[ 0 ] );
else
{
// Quick get the decode type
parse_type type = INVALID;
{
std::map< std::string, parse_type > chooser;
chooser[ "bencode" ] = BENCODE;
chooser[ "json" ] = JSON;
chooser[ "xml" ] = XML;
std::map< std::string,
parse_type >::iterator found = chooser.find( argv[ 1 ] );
if( found != chooser.end() )
type = found -> second;
}
if( type == INVALID )
return print_usage( argv[ 0 ] );
// Open file
std::ifstream file( argv[ 2 ] );
if( !file.is_open() )
{
std::cout << "Could not open file " << argv[ 2 ] << '\n';
return 0x02;
}
// Parse file & create value object
dbnc::value value( file );
// Serialize to std::cout
switch( type )
{
case BENCODE:
value.serializeBencode( std::cout );
break;
case JSON:
value.serializeJSON( std::cout );
break;
case XML:
value.serializeXML( std::cout );
break;
default:
// Never gets here
break;
}
}
return 0x00;
}
// #endif