| 1 |
// Copyright (c) 1996 James Clark |
| 2 |
// See the file copying.txt for copying permission. |
| 3 |
|
| 4 |
#include "config.h" |
| 5 |
#include "DssslApp.h" |
| 6 |
#include "SgmlFOTBuilder.h" |
| 7 |
#include "RtfFOTBuilder.h" |
| 8 |
#include "TeXFOTBuilder.h" |
| 9 |
#include "TransformFOTBuilder.h" |
| 10 |
#ifdef JADE_HTML |
| 11 |
#include "HtmlFOTBuilder.h" |
| 12 |
#endif |
| 13 |
#ifdef JADE_MIF |
| 14 |
#include "MifFOTBuilder.h" |
| 15 |
#endif |
| 16 |
#ifdef JADE_TEXT |
| 17 |
#include "TextFOTBuilder.h" |
| 18 |
#endif |
| 19 |
#include <OpenSP/OutputCharStream.h> |
| 20 |
#include <OpenSP/macros.h> |
| 21 |
#include <OpenSP/sptchar.h> |
| 22 |
#include "JadeMessages.h" |
| 23 |
#include <OpenSP/ErrnoMessageArg.h> |
| 24 |
#include <OpenSP/OutputByteStream.h> |
| 25 |
#include "dsssl_ns.h" |
| 26 |
|
| 27 |
#include <errno.h> |
| 28 |
|
| 29 |
#ifdef DSSSL_NAMESPACE |
| 30 |
namespace DSSSL_NAMESPACE { |
| 31 |
#endif |
| 32 |
|
| 33 |
class JadeApp : public DssslApp { |
| 34 |
public: |
| 35 |
enum { u = 72000 }; |
| 36 |
JadeApp(); |
| 37 |
void processOption(AppChar opt, const AppChar *arg); |
| 38 |
FOTBuilder *makeFOTBuilder(const FOTBuilder::Description *&); |
| 39 |
private: |
| 40 |
enum OutputType { fotType, |
| 41 |
rtfType, |
| 42 |
#ifdef JADE_HTML |
| 43 |
htmlType, |
| 44 |
#endif |
| 45 |
texType, |
| 46 |
#ifdef JADE_MIF |
| 47 |
mifType, |
| 48 |
#endif |
| 49 |
sgmlType, |
| 50 |
xmlType, |
| 51 |
#ifdef JADE_TEXT |
| 52 |
txtType |
| 53 |
#endif |
| 54 |
}; |
| 55 |
static const AppChar *const outputTypeNames[]; |
| 56 |
OutputType outputType_; |
| 57 |
String<AppChar> outputFilename_; |
| 58 |
Vector<StringC> outputOptions_; |
| 59 |
FileOutputByteStream outputFile_; |
| 60 |
}; |
| 61 |
|
| 62 |
const JadeApp::AppChar *const JadeApp::outputTypeNames[] = { |
| 63 |
SP_T("fot"), |
| 64 |
SP_T("rtf"), |
| 65 |
#ifdef JADE_HTML |
| 66 |
SP_T("html"), |
| 67 |
#endif |
| 68 |
SP_T("tex"), |
| 69 |
#ifdef JADE_MIF |
| 70 |
SP_T("mif"), |
| 71 |
#endif |
| 72 |
SP_T("sgml"), |
| 73 |
SP_T("xml"), |
| 74 |
#ifdef JADE_TEXT |
| 75 |
SP_T("txt"), |
| 76 |
#endif |
| 77 |
}; |
| 78 |
|
| 79 |
JadeApp::JadeApp() |
| 80 |
: DssslApp(u), outputType_(fotType) |
| 81 |
{ |
| 82 |
registerOption('t', SP_T("output-type"), JadeMessages::outputType, |
| 83 |
JadeMessages::tHelp); |
| 84 |
registerOption('o', SP_T("output-file"), JadeMessages::file, |
| 85 |
JadeMessages::oHelp); |
| 86 |
} |
| 87 |
|
| 88 |
void JadeApp::processOption(AppChar opt, const AppChar *arg) |
| 89 |
{ |
| 90 |
switch (opt) { |
| 91 |
case 't': |
| 92 |
{ |
| 93 |
const AppChar *sub = tcschr(arg, SP_T('-')); |
| 94 |
size_t len = sub ? sub - arg : tcslen(arg); |
| 95 |
for (size_t i = 0;; i++) { |
| 96 |
if (i >= SIZEOF(outputTypeNames)) { |
| 97 |
message(JadeMessages::unknownType, StringMessageArg(convertInput(arg))); |
| 98 |
break; |
| 99 |
} |
| 100 |
if (tcsncmp(arg, outputTypeNames[i], len) == 0) { |
| 101 |
outputType_ = OutputType(i); |
| 102 |
break; |
| 103 |
} |
| 104 |
} |
| 105 |
if (sub) { |
| 106 |
StringC tem(convertInput(sub)); |
| 107 |
StringC arg; |
| 108 |
for (size_t i = 0; i < tem.size(); i++) { |
| 109 |
if (tem[i] == '-') { |
| 110 |
if (arg.size()) |
| 111 |
outputOptions_.push_back(arg); |
| 112 |
arg.resize(0); |
| 113 |
} |
| 114 |
else |
| 115 |
arg += tem[i]; |
| 116 |
} |
| 117 |
if (arg.size()) |
| 118 |
outputOptions_.push_back(arg); |
| 119 |
} |
| 120 |
} |
| 121 |
break; |
| 122 |
case 'o': |
| 123 |
if (*arg == 0) |
| 124 |
message(JadeMessages::emptyOutputFilename); |
| 125 |
else |
| 126 |
outputFilename_.assign(arg, tcslen(arg)); |
| 127 |
break; |
| 128 |
default: |
| 129 |
DssslApp::processOption(opt, arg); |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
FOTBuilder *JadeApp::makeFOTBuilder(const FOTBuilder::Description *&descr) |
| 135 |
{ |
| 136 |
if (outputFilename_.size() == 0) { |
| 137 |
if (defaultOutputBasename_.size() != 0) { |
| 138 |
#ifdef SP_WIDE_SYSTEM |
| 139 |
outputFilename_ = defaultOutputBasename_; |
| 140 |
#else |
| 141 |
outputFilename_ = codingSystem()->convertOut(defaultOutputBasename_); |
| 142 |
// convertOut adds a nul |
| 143 |
outputFilename_.resize(outputFilename_.size() - 1); |
| 144 |
#endif |
| 145 |
} |
| 146 |
else |
| 147 |
outputFilename_.assign(SP_T("jade-out"), 8); |
| 148 |
outputFilename_ += SP_T('.'); |
| 149 |
const AppChar *ext = outputTypeNames[outputType_]; |
| 150 |
outputFilename_.append(ext, tcslen(ext)); |
| 151 |
} |
| 152 |
switch (outputType_) { |
| 153 |
#ifdef JADE_HTML |
| 154 |
case htmlType: |
| 155 |
#endif |
| 156 |
case sgmlType: |
| 157 |
case xmlType: |
| 158 |
break; |
| 159 |
default: |
| 160 |
outputFilename_ += 0; |
| 161 |
if (!outputFile_.open(outputFilename_.data())) { |
| 162 |
message(JadeMessages::cannotOpenOutputError, |
| 163 |
StringMessageArg(CmdLineApp::convertInput(outputFilename_.data())), |
| 164 |
ErrnoMessageArg(errno)); |
| 165 |
return 0; |
| 166 |
} |
| 167 |
break; |
| 168 |
} |
| 169 |
switch (outputType_) { |
| 170 |
case rtfType: |
| 171 |
unitsPerInch_ = 20*72; // twips |
| 172 |
return makeRtfFOTBuilder(&outputFile_, outputOptions_, entityManager(), systemCharset(), this, descr); |
| 173 |
case texType: |
| 174 |
return makeTeXFOTBuilder(&outputFile_, this, descr); |
| 175 |
#ifdef JADE_HTML |
| 176 |
case htmlType: |
| 177 |
return makeHtmlFOTBuilder(outputFilename_, this, descr); |
| 178 |
#endif /* JADE_HTML */ |
| 179 |
#ifdef JADE_MIF |
| 180 |
case mifType: |
| 181 |
return makeMifFOTBuilder(outputFilename_, entityManager(), systemCharset(), this, descr); |
| 182 |
#endif /* JADE_MIF */ |
| 183 |
case fotType: |
| 184 |
return makeSgmlFOTBuilder(new RecordOutputCharStream( |
| 185 |
new EncodeOutputCharStream(&outputFile_, |
| 186 |
outputCodingSystem_)), descr); |
| 187 |
case sgmlType: |
| 188 |
case xmlType: |
| 189 |
return makeTransformFOTBuilder(this, outputType_ == xmlType, outputOptions_, descr); |
| 190 |
#ifdef JADE_TEXT |
| 191 |
case txtType: |
| 192 |
return makeTextFOTBuilder(&outputFile_, this, descr); |
| 193 |
#endif /* JADE_TEXT */ |
| 194 |
default: |
| 195 |
break; |
| 196 |
} |
| 197 |
CANNOT_HAPPEN(); |
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
#ifdef DSSSL_NAMESPACE |
| 202 |
} |
| 203 |
#endif |
| 204 |
|
| 205 |
#ifdef DSSSL_NAMESPACE |
| 206 |
SP_DEFINE_APP(DSSSL_NAMESPACE::JadeApp) |
| 207 |
#else |
| 208 |
SP_DEFINE_APP(JadeApp) |
| 209 |
#endif |