Doxygen
Loading...
Searching...
No Matches
msc.cpp
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2021 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16// own header
17#include "msc.h"
18
19// other includes
20#include "config.h"
21#include "dir.h"
22#include "docnode.h"
23#include "docparser.h"
24#include "doxygen.h"
25#include "indexlist.h"
26#include "message.h"
27#include "mscgen_api.h"
28#include "portable.h"
29#include "stringutil.h"
30#include "textstream.h"
31#include "util.h"
32
33static const int maxCmdLine = 40960;
34
35static bool convertMapFile(TextStream &t,const DString &mapName,const DString &relPath,
36 const DString &context,const DString &srcFile,int srcLine)
37{
38 std::ifstream f = Portable::openInputStream(mapName);
39 if (!f.is_open())
40 {
41 err("failed to open map file {} for inclusion in the docs!\n"
42 "If you installed Graphviz/dot after a previous failing run, \n"
43 "try deleting the output directory and rerun doxygen.\n",mapName);
44 return false;
45 }
46 const int maxLineLen=1024;
47 char url[maxLineLen];
48 char ref[maxLineLen];
49 int x1=0, y1=0, x2=0, y2=0;
50 std::string line;
51 while (getline(f,line))
52 {
53 bool isRef = false;
54 //printf("ReadLine '%s'\n",line.c_str());
55 if (literal_at(line.c_str(),"rect"))
56 {
57 // obtain the url and the coordinates in the order used by graphviz-1.5
58 sscanf(line.c_str(),"rect %s %d,%d %d,%d",url,&x1,&y1,&x2,&y2);
59
60 if (dstrcmp(url,"\\ref")==0 || dstrcmp(url,"@ref")==0)
61 {
62 isRef = true;
63 sscanf(line.c_str(),"rect %s %s %d,%d %d,%d",ref,url,&x1,&y1,&x2,&y2);
64 }
65
66 // sanity checks
67 if (y2<y1) { int temp=y2; y2=y1; y1=temp; }
68 if (x2<x1) { int temp=x2; x2=x1; x1=temp; }
69
70
71 bool link = false;
72 if ( isRef )
73 {
74 // handle doxygen \ref tag URL reference
75
76 auto parser { createDocParser() };
77 auto dfAst { createRef( *parser.get(), url, context, srcFile, srcLine) };
78 auto dfAstImpl = dynamic_cast<const DocNodeAST*>(dfAst.get());
79 const DocRef *df = std::get_if<DocRef>(&dfAstImpl->root);
80 if (!df->file().empty() || !df->anchor().empty())
81 {
82 link = true;
83 t << "<area href=\"";
84 t << externalRef(relPath,df->ref());
85 }
86 if (!df->file().empty())
87 {
88 DString fn = df->file();
90 t << fn;
91 }
92 if (!df->anchor().empty())
93 {
94 t << "#" << df->anchor();
95 }
96 }
97 else
98 {
99 link = true;
100 t << "<area href=\"";
101 t << url;
102 }
103 if (link)
104 {
105 t << "\" shape=\"rect\" coords=\""
106 << x1 << "," << y1 << "," << x2 << "," << y2 << "\""
107 << " alt=\"\"/>\n";
108 }
109 }
110 }
111
112 return true;
113}
114
115static bool do_mscgen_generate(const DString& inFile,const DString& outFile,mscgen_format_t msc_format,
116 const DString &srcFile,int srcLine)
117{
118 auto mscgen_tool = Config_getString(MSCGEN_TOOL).stripWhiteSpace();
119 if (!mscgen_tool.empty()) // use external mscgen tool
120 {
121 DString type;
122 switch (msc_format)
123 {
124 case mscgen_format_png:
125 type = "png";
126 break;
127 case mscgen_format_eps:
128 type = "eps";
129 break;
130 case mscgen_format_svg:
131 type = "svg";
132 break;
133 case mscgen_format_pngmap:
134 case mscgen_format_svgmap:
135 type = "ismap";
136 break;
137 }
138 int exitcode = Portable::system(mscgen_tool,"-T"+type+" -o "+outFile+" "+inFile);
139 if (exitcode!=0)
140 {
141 err_full(srcFile,srcLine,"Problems running external tool {} given via MSCGEN_TOOL (exit status: {})."
142 " Look for typos in your msc file and check error messages above.",
143 mscgen_tool,exitcode);
144 return false;
145 }
146 }
147 else // use built-in mscgen tool
148 {
149 int code = mscgen_generate(inFile.data(),outFile.data(),msc_format);
150 if (code!=0)
151 {
152 err_full(srcFile,srcLine,"Problems generating msc output (error={}). Look for typos in you msc file '{}'",
153 mscgen_error2str(code),inFile);
154 return false;
155 }
156 }
157 return true;
158}
159
160void writeMscGraphFromFile(const DString &inFile,const DString &outDir,
161 const DString &outFile,MscOutputFormat format,
162 const DString &srcFile,int srcLine,bool toIndex
163 )
164{
165 DString absOutFile = outDir;
166 absOutFile+=Portable::pathSeparator();
167 absOutFile+=outFile;
168
169 mscgen_format_t msc_format = mscgen_format_png;
170 DString imgName = absOutFile;
171 switch (format)
172 {
174 msc_format = mscgen_format_png;
175 imgName+=".png";
176 break;
178 msc_format = mscgen_format_eps;
179 imgName+=".eps";
180 break;
182 msc_format = mscgen_format_svg;
183 imgName+=".svg";
184 break;
185 default:
186 return;
187 }
188 if (!do_mscgen_generate(inFile,imgName,msc_format,srcFile,srcLine))
189 {
190 return;
191 }
192
193 if ( (format==MscOutputFormat::EPS) && (Config_getBool(USE_PDFLATEX)) )
194 {
196 epstopdfArgs.sprintf("\"%s.eps\" --outfile=\"%s.pdf\"",
197 qPrint(absOutFile),qPrint(absOutFile));
198 if (Portable::system("epstopdf",epstopdfArgs)!=0)
199 {
200 err_full(srcFile,srcLine,"Problems running epstopdf when processing '{}.eps'. Check your TeX installation!", absOutFile);
201 }
202 else
203 {
204 Dir().remove((absOutFile + ".eps").data());
205 }
206 }
207
208 size_t i0 = imgName.rfind('/');
209 size_t i1 = imgName.rfind('\\');
210 size_t i = i0!=DString::npos && i1!=DString::npos ? std::max(i0,i1) :
211 i0!=DString::npos ? i0 : i1;
212 if (i!=DString::npos) // strip path
213 {
214 imgName=imgName.mid(i+1);
215 }
216 if (toIndex) Doxygen::indexList->addImageFile(imgName);
217
218}
219
220static DString getMscImageMapFromFile(const DString& inFile, const DString& /* outDir */,
221 const DString& relPath,const DString& context,
222 bool writeSVGMap,const DString &srcFile,int srcLine)
223{
224 DString outFile = inFile + ".map";
225
226 if (!do_mscgen_generate(inFile,outFile,
227 writeSVGMap ? mscgen_format_svgmap : mscgen_format_pngmap,
228 srcFile,srcLine))
229 return "";
230
231 TextStream t;
232 convertMapFile(t, outFile, relPath, context, srcFile, srcLine);
233
234 Dir().remove(outFile.str());
235
236 return t.str();
237}
238
240 const DString &outDir,
241 const DString &relPath,
242 const DString &baseName,
243 const DString &context,
244 MscOutputFormat format,
245 const DString &srcFile,
246 int srcLine
247 )
248{
249 DString mapName = baseName+".map";
250 t << "<img src=\"" << relPath << baseName << ".";
251 switch (format)
252 {
254 t << "png";
255 break;
257 t << "eps";
258 break;
260 t << "svg";
261 break;
262 default:
263 t << "unknown";
264 }
265 DString imap = getMscImageMapFromFile(inFile,outDir,relPath,context,format==MscOutputFormat::SVG,srcFile,srcLine);
266 if (!imap.empty())
267 {
268 t << "\" alt=\""
269 << baseName << "\" border=\"0\" usemap=\"#" << mapName << "\"/>\n";
270 t << "<map name=\"" << mapName << "\" id=\"" << mapName << "\">" << imap << "</map>\n";
271 }
272 else
273 {
274 t << "\" alt=\"" << baseName << "\" border=\"0\"/>\n";
275 }
276}
277
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
size_t rfind(char c, size_t pos=npos) const
Definition dstring.h:244
DString mid(size_t index, size_t len=npos) const
Definition dstring.h:318
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
static constexpr size_t npos
value used to indicate 'not found' or 'to the end of the string', matching std::string::npos
Definition dstring.h:178
DString & sprintf(const char *format,...)
Definition dstring.cpp:34
@ ExplicitSize
Definition dstring.h:131
const std::string & str() const
Definition dstring.h:645
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition dstring.h:157
Class representing a directory in the file system.
Definition dir.h:73
bool remove(const std::string &path, bool acceptsAbsPath=true) const
Definition dir.cpp:320
Class representing the abstract syntax tree of a documentation block.
Definition docnode.h:1471
Node representing a reference to some item.
Definition docnode.h:787
DString ref() const
Definition docnode.h:793
DString file() const
Definition docnode.h:791
DString anchor() const
Definition docnode.h:794
static IndexList * indexList
Definition doxygen.h:125
void addImageFile(const DString &name)
Definition indexlist.h:124
Text streaming class that buffers data.
Definition textstream.h:36
std::string str() const
Return the contents of the buffer as a std::string object.
Definition textstream.h:232
#define Config_getBool(name)
Definition config.h:33
#define Config_getString(name)
Definition config.h:32
static const int maxCmdLine
Definition dia.cpp:27
IDocParserPtr createDocParser()
factory function to create a parser
Definition docparser.cpp:59
IDocNodeASTPtr createRef(IDocParser &parserIntf, const DString &target, const DString &context, const DString &srcFile, int srcLine)
int dstrcmp(const char *str1, const char *str2)
Definition dstring.h:50
const char * qPrint(const char *s)
Definition dstring.h:783
#define err(fmt,...)
Definition message.h:127
#define err_full(file, line, fmt,...)
Definition message.h:132
void writeMscGraphFromFile(const DString &inFile, const DString &outDir, const DString &outFile, MscOutputFormat format, const DString &srcFile, int srcLine, bool toIndex)
Definition msc.cpp:160
static bool do_mscgen_generate(const DString &inFile, const DString &outFile, mscgen_format_t msc_format, const DString &srcFile, int srcLine)
Definition msc.cpp:115
static DString getMscImageMapFromFile(const DString &inFile, const DString &, const DString &relPath, const DString &context, bool writeSVGMap, const DString &srcFile, int srcLine)
Definition msc.cpp:220
void writeMscImageMapFromFile(TextStream &t, const DString &inFile, const DString &outDir, const DString &relPath, const DString &baseName, const DString &context, MscOutputFormat format, const DString &srcFile, int srcLine)
Definition msc.cpp:239
static bool convertMapFile(TextStream &t, const DString &mapName, const DString &relPath, const DString &context, const DString &srcFile, int srcLine)
Definition msc.cpp:35
MscOutputFormat
Definition msc.h:22
std::ifstream openInputStream(const DString &name, bool binary=false, bool openAtEnd=false)
Definition portable.cpp:692
int system(const DString &command, const DString &args, bool commandHasConsole=true)
Definition portable.cpp:121
DString pathSeparator()
Definition portable.cpp:390
Portable versions of functions that are platform dependent.
Some helper functions for std::string.
bool literal_at(const char *data, const char(&str)[N])
returns true iff data points to a substring that matches string literal str
Definition stringutil.h:101
void addHtmlExtensionIfMissing(DString &fName)
Definition util.cpp:3931
DString externalRef(const DString &relPath, const DString &ref)
Definition util.cpp:4538
A bunch of utility functions.