Debian Codegraph @

src/conv/svg/mwaw2svg.cpp

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include <cstring>
39 #include <fstream>
40 #include <iostream>
41 #include <sstream>
42
43 #include <librevenge/librevenge.h>
44 #include <librevenge-generators/librevenge-generators.h>
45 #include <librevenge-stream/librevenge-stream.h>
46
47 #include <libmwaw/libmwaw.hxx>
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include "helper.h"
54
55 #ifndef VERSION
56 #define VERSION "UNKNOWN VERSION"
57 #endif
58
59 static int printUsage()
60 {
61 printf("Usage: mwaw2svg [-h] [-v] [-t] [-b body | -o file.svg] <Mac Graphic/Presentation Document>\n");
62 printf("\n");
63 printf("Options:\n");
64 printf(" -h Shows this help message.\n");
65 printf(" -b body Defines the file's body name, the different drawings/slides will be stored in body0.svg, body1.svg, ...\n");
66 printf(" -o file.svg Stores the first drawing/slide in file.svg. Further drawing/slide are not saved.\n");
67 printf(" -t If set, all drawings/slides are send to file.svg or stdout. This option is only intended for debug/regression test.\n");
68 printf(" -v: Outputs mwaw2svg version. \n");
69 printf("\n");
70 printf("Note:\n");
71 printf("\t the content of the first drawing/slide is sent to stdout if neither -b nor -o options are given.\n");
72 return -1;
73 }
74
75 static int printVersion()
76 {
77 printf("mwaw2svg %s\n", VERSION);
78 return 0;
79 }
80
81 int main(int argc, char *argv[])
82 {
83 if (argc < 2)
84 return printUsage();
85
86 char const *output = 0;
87 char const *body = 0;
88 bool sendAll=false;
89 bool printHelp=false;
90 int ch, numOutput=0;
91
92 while ((ch = getopt(argc, argv, "hb:o:tv")) != -1) {
93 switch (ch) {
94 case 'b':
95 body=optarg;
96 ++numOutput;
97 break;
98 case 'o':
99 output=optarg;
100 ++numOutput;
101 break;
102 case 't':
103 sendAll=true;
104 break;
105 case 'v':
106 printVersion();
107 return 0;
108 default:
109 case 'h':
110 printHelp = true;
111 break;
112 }
113 }
114
115 if (argc != 1+optind || printHelp || numOutput>1) {
116 printUsage();
117 return -1;
118 }
119 char const *file=argv[optind];
120
121 MWAWDocument::Kind kind;
122 auto confidence = MWAWDocument::MWAW_C_NONE;
123 auto input=libmwawHelper::isSupported(file, confidence, kind);
124 if (!input || confidence != MWAWDocument::MWAW_C_EXCELLENT) {
125 printf("ERROR: Unsupported file format!\n");
126 return 1;
127 }
128 auto error=MWAWDocument::MWAW_R_OK;
129 librevenge::RVNGStringVector vec;
130
131 try {
132 if (kind == MWAWDocument::MWAW_K_DRAW || kind == MWAWDocument::MWAW_K_PAINT) {
133 librevenge::RVNGSVGDrawingGenerator listener(vec, "");
134 error = MWAWDocument::parse(input.get(), &listener);
135 }
136 else if (kind == MWAWDocument::MWAW_K_PRESENTATION) {
137 librevenge::RVNGSVGPresentationGenerator listener(vec);
138 error = MWAWDocument::parse(input.get(), &listener);
139 }
140 else {
141 fprintf(stderr,"ERROR: not a graphic/presentation document!\n");
142 return 1;
143 }
144 if (error==MWAWDocument::MWAW_R_OK && (vec.empty() || vec[0].empty()))
145 error = MWAWDocument::MWAW_R_UNKNOWN_ERROR;
146 }
147 catch (MWAWDocument::Result const &err) {
148 error=err;
149 }
150 catch (...) {
151 error=MWAWDocument::MWAW_R_UNKNOWN_ERROR;
152 }
153 if (libmwawHelper::checkErrorAndPrintMessage(error))
154 return 1;
155
156 if (body) {
157 for (unsigned i=0; i<vec.size(); ++i) {
158 std::stringstream s;
159 s << body << int(i) << ".svg";
160 std::ofstream out(s.str().c_str());
161 out << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
162 out << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"";
163 out << " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
164 out << vec[i].cstr() << std::endl;
165 }
166 }
167 else if (output) {
168 std::ofstream out(output);
169 for (unsigned i=0; i<vec.size(); ++i) {
170 out << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
171 out << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"";
172 out << " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
173 out << vec[i].cstr() << std::endl;
174 if (!sendAll) break;
175 }
176 }
177 else {
178 for (unsigned i=0; i<vec.size(); ++i) {
179 std::cout << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n";
180 std::cout << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"";
181 std::cout << " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
182 std::cout << vec[i].cstr() << std::endl;
183 if (!sendAll) break;
184 }
185 }
186 return 0;
187 }
188 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: