Atlas 0.7.0
Networking protocol for the Worldforge system.
atlas_convert.cpp
1// This file may be redistributed and modified only under the terms of
2// the GNU Lesser General Public License (See COPYING for details).
3// Copyright (C) 2002 Michael Koch
4
5#include <Atlas/Codecs/XML.h>
6#include <Atlas/Codecs/Bach.h>
7#include <Atlas/Codecs/Packed.h>
8#include <Atlas/Message/QueuedDecoder.h>
9#include <Atlas/Message/MEncoder.h>
10#include <Atlas/Formatter.h>
11
12#include <fstream>
13#include <iostream>
14#include <cstdlib>
15
16#include <unistd.h>
17
18int option_format = 0;
19int option_spacing = -1;
20namespace {
21Atlas::Codec* getCodec(std::string type, std::iostream& stream, Atlas::Message::DecoderBase& decoder) {
22 if (type == "XML")
23 return new Atlas::Codecs::XML(stream, stream, decoder);
24 else if (type == "Bach")
25 return new Atlas::Codecs::Bach(stream, stream, decoder);
26 else if (type == "Packed")
27 return new Atlas::Codecs::Packed(stream, stream, decoder);
28/*
29 else if (type == "Binary")
30 return new Atlas::Codecs::Binary(stream, decoder);
31*/
32 else {
33 std::cout << "Unknown CODEC required!" << std::endl;
34 exit(0);
35 }
36}
37
38int convert(const std::string& file_in, const std::string& codec_in,
39 const std::string& file_out, const std::string& codec_out) {
40 std::cout << "Convert " << codec_in << " to " << codec_out << std::endl;
41
42 std::fstream in, out;
43
44 in.open(file_in.c_str(), std::ios::in);
45
46 if (!in.is_open()) {
47 std::cerr << "Unable to open " << file_in << " for input"
48 << std::endl << std::flush;
49 return 1;
50 }
51
52 out.open(file_out.c_str(), std::ios::out);
53
54 if (!out.is_open()) {
55 std::cerr << "Unable to open " << file_out << " for output"
56 << std::endl << std::flush;
57 return 1;
58 }
59
60 std::cout << "Reading... ";
61
63 Atlas::Codec* inCodec = getCodec(codec_in, in, decoder);
64 while (!in.eof()) {
65 inCodec->poll();
66 }
67
68 std::cout << "done." << std::endl;
69 std::cout << "Writing... ";
70
71 Atlas::Codec* outCodec = getCodec(codec_out, out, decoder);
72 Atlas::Bridge* bridge;
73
74 if (option_format) {
75 Atlas::Formatter* format;
76 bridge = format = new Atlas::Formatter(out, *outCodec);
77 if (option_spacing != -1) {
78 format->setSpacing(option_spacing);
79 }
80 } else {
81 bridge = outCodec;
82 }
83
84 Atlas::Message::Encoder encoder(*bridge);
85 encoder.streamBegin();
86 while (decoder.queueSize() > 0) {
87 Atlas::Message::MapType msg(decoder.popMessage());
88 encoder.streamMessageElement(msg);
89 }
90 encoder.streamEnd();
91
92 std::cout << "done." << std::endl;
93
94 out.close();
95 in.close();
96
97 return 0;
98}
99
100void usage(const char* program) {
101 std::cout << "usage: " << program
102 << " [-i infile] [-o outfile] <input file> <output file>"
103 << std::endl;
104 std::cout << "Supported Codecs: XML Back Packed"
105 << std::endl << std::flush;
106}
107}
108int main( int argc, char** argv )
109{
110 std::string codec_in("XML"),
111 codec_out("Bach");
112
113 while (true) {
114 int c = getopt(argc, argv, "fs:i:o:");
115 if (c == -1) {
116 break;
117 } else if (c == 'f') {
118 option_format = 1;
119 } else if (c == 's') {
120 option_spacing = strtol(optarg, nullptr, 0);
121 } else if (c == 'i') {
122 codec_in = optarg;
123 } else if (c == 'o') {
124 codec_out = optarg;
125 }
126 }
127
128 if ((argc - optind) != 2) {
129 usage(argv[0]);
130 return 1;
131 }
132
133 std::string file_in(argv[argc - 2]);
134 std::string file_out(argv[argc - 1]);
135
136 std::cout << "Reading from " << file_in << " to " << file_out << std::endl << std::flush;
137
138 // convert file
139 return convert(file_in, codec_in, file_out, codec_out);
140}
MapType popMessage()
Pop an object from the front of the message queue.
Definition: QueuedDecoder.h:45
size_t queueSize()
Retrieve the current size of the message queue.
Definition: QueuedDecoder.h:41