Doxygen
Loading...
Searching...
No Matches
dotgraph.cpp
Go to the documentation of this file.
1/******************************************************************************
2*
3* Copyright (C) 1997-2019 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#include <mutex>
17#include <regex>
18
19#include "config.h"
20#include "doxygen.h"
21#include "indexlist.h"
22#include "md5hash.h"
23#include "message.h"
24#include "util.h"
25
26#include "dot.h"
27#include "dotrunner.h"
28#include "dotgraph.h"
29#include "dotnode.h"
30#include "dotfilepatcher.h"
31#include "fileinfo.h"
32#include "portable.h"
33#include "textstream.h"
34
35//DString DotGraph::DOT_FONTNAME; // will be initialized in initDot
36//int DotGraph::DOT_FONTSIZE; // will be initialized in initDot
37
38/*! Checks if a file "baseName".md5 exists. If so the contents
39* are compared with \a md5. If equal false is returned.
40* The .md5 is created or updated after successful creation of the output file.
41*/
42static bool sameMd5Signature(const DString &baseName,
43 const DString &md5)
44{
45 bool same = false;
46 char md5stored[33];
47 md5stored[0]=0;
48 std::ifstream f = Portable::openInputStream(baseName+".md5",true);
49 if (f.is_open())
50 {
51 // read checksum
52 f.read(md5stored,32);
53 md5stored[32]='\0';
54 // compare checksum
55 if (!f.fail() && md5==md5stored)
56 {
57 same = true;
58 }
59 //printf("sameSignature(%s,%s==%s)=%d\n",qPrint(baseName),md5stored,qPrint(md5),same);
60 }
61 else
62 {
63 //printf("sameSignature(%s) not found\n",qPrint(baseName));
64 }
65 return same;
66}
67
68static bool deliverablesPresent(const DString &file1,const DString &file2)
69{
70 bool file1Ok = true;
71 bool file2Ok = true;
72 if (!file1.empty())
73 {
74 FileInfo fi(file1.str());
75 file1Ok = (fi.exists() && fi.size()>0);
76 }
77 if (!file2.empty())
78 {
79 FileInfo fi(file2.str());
80 file2Ok = (fi.exists() && fi.size()>0);
81 }
82 return file1Ok && file2Ok;
83}
84
85static bool insertMapFile(TextStream &out,const DString &mapFile,
86 const DString &relPath,const DString &mapLabel)
87{
88 FileInfo fi(mapFile.str());
89 if (fi.exists() && fi.size()>0) // reuse existing map file
90 {
91 TextStream t;
92 DotFilePatcher::convertMapFile(t,mapFile,relPath,false);
93 if (!t.empty())
94 {
95 out << "<map name=\"" << mapLabel << "\" id=\"" << mapLabel << "\">\n";
96 out << t.str();
97 out << "</map>\n";
98 }
99 return true;
100 }
101 return false; // no map file yet, need to generate it
102}
103
104//--------------------------------------------------------------------
105
107{
109 ("." + getDotImageExtension()) : (Config_getBool(USE_PDFLATEX) ? ".pdf" : ".eps"));
110}
111
113
115 TextStream& t, // output stream for the code file (html, ...)
116 GraphOutputFormat gf, // bitmap(png/svg) or ps(eps/pdf)
117 EmbeddedOutputFormat ef, // html, latex, ...
118 const DString &path, // output folder
119 const DString &fileName, // name of the code file (for code patcher)
120 const DString &relPath, // output folder relative to code file
121 bool generateImageMap, // in case of bitmap, shall there be code generated?
122 int graphId) // number of this graph in the current code, used in svg code
123{
124 m_graphFormat = gf;
125 m_textFormat = ef;
126 m_dir = Dir(path.str());
127 m_fileName = fileName;
128 m_relPath = relPath;
129 m_generateImageMap = generateImageMap;
130 m_graphId = graphId;
131
132 m_absPath = m_dir.absPath() + "/";
134
136
138
140 {
141 std::lock_guard<std::mutex> lock(g_dotIndexListMutex);
143 }
144
145 generateCode(t);
146
147 return m_baseName;
148}
149
151{
152 if (!m_dir.exists())
153 {
154 term("Output dir {} does not exist!\n", m_dir.path());
155 }
156
157 // calculate md5
158 DString sigStr = md5str(m_theGraph.view());
159
160 // already queued files are processed again in case the output format has changed
161
162 if (sameMd5Signature(absBaseName(), sigStr) &&
165 )
166 )
167 {
168 // all needed files are there
169 return false;
170 }
171
172 // need to rebuild the image
173
174 // write .dot file because image was new or has changed
175 std::ofstream f = Portable::openOutputStream(absDotName());
176 if (!f.is_open())
177 {
178 err("Could not open file {} for writing\n",absDotName());
179 return true;
180 }
181 f << m_theGraph;
182 f.close();
183
185 {
186 // run dot to create a bitmap image
188 }
190 {
191 // run dot to create a .eps image
192 if (Config_getBool(USE_PDFLATEX))
193 {
195 }
196 else
197 {
199 }
200 }
201 return true;
202}
203
205{
206 DString imgExt = getDotImageExtension();
208 {
209 t << "<para>\n";
210 t << " <informalfigure>\n";
211 t << " <mediaobject>\n";
212 t << " <imageobject>\n";
213 t << " <imagedata";
214 t << " width=\"50%\" align=\"center\" valign=\"middle\" scalefit=\"0\" fileref=\"" << m_relPath << m_baseName << "." << imgExt << "\">";
215 t << "</imagedata>\n";
216 t << " </imageobject>\n";
217 t << " </mediaobject>\n";
218 t << " </informalfigure>\n";
219 t << "</para>\n";
220 }
221 else if (m_graphFormat==GraphOutputFormat::BITMAP && m_generateImageMap) // produce HTML to include the image
222 {
223 if (imgExt=="svg") // add link to SVG file without map file
224 {
225 if (!m_noDivTag) t << "<div class=\"center\">";
226 if (m_regenerate || !DotFilePatcher::writeSVGFigureLink(t,m_relPath,m_baseName,absImgName())) // need to patch the links in the generated SVG file
227 {
228 if (m_regenerate)
229 {
231 createFilePatcher(absImgName())->
232 addSVGConversion(m_relPath,false,DString(),m_zoomable,m_graphId);
233 }
234 int mapId = DotManager::instance()->
235 createFilePatcher(m_fileName)->
236 addSVGObject(m_baseName,absImgName(),m_relPath);
237 t << "<!-- " << "SVG " << mapId << " -->";
238 }
239 if (!m_noDivTag) t << "</div>\n";
240 }
241 else // add link to bitmap file with image map
242 {
243 if (!m_noDivTag) t << "<div class=\"center\">";
244 t << "<img src=\"" << relImgName() << "\" border=\"0\" usemap=\"#"
245 << DotFilePatcher::mapLabelToId(getMapLabel()) << "\" loading=\"lazy\" alt=\"" << getImgAltText() << "\"/>";
246 if (!m_noDivTag) t << "</div>";
247 t << "\n";
249 {
250 int mapId = DotManager::instance()->
251 createFilePatcher(m_fileName)->
253 t << "<!-- MAP " << mapId << " -->\n";
254 }
255 }
256 }
257 else if (m_graphFormat==GraphOutputFormat::EPS) // produce tex to include the .eps image
258 {
260 {
261 int figId = DotManager::instance()->
262 createFilePatcher(m_fileName)->
263 addFigure(m_baseName,absBaseName(),false /*true*/);
264 t << "\n% FIG " << figId << "\n";
265 }
266 }
267}
268
270{
271 t << "digraph ";
272 if (title.empty())
273 {
274 t << "\"Dot Graph\"";
275 }
276 else
277 {
278 t << "\"" << convertToXML(title) << "\"";
279 }
280 t << "\n{\n";
281 if (Config_getBool(INTERACTIVE_SVG)) // insert a comment to force regeneration when this
282 // option is toggled
283 {
284 t << " // INTERACTIVE_SVG=YES\n";
285 }
286 t << " // LATEX_PDF_SIZE\n"; // write placeholder for LaTeX PDF bounding box size replacement
287 t << " bgcolor=\"transparent\";\n";
288 DString c = Config_getString(DOT_COMMON_ATTR);
289 if (!c.empty()) c += ",";
290 t << " edge [" << c << Config_getString(DOT_EDGE_ATTR) << "];\n";
291 t << " node [" << c << Config_getString(DOT_NODE_ATTR) << "];\n";
292}
293
295{
296 t << "}\n";
297}
298
300 GraphType gt,
301 GraphOutputFormat format,
302 const DString &rank, // either "LR", "RL", or ""
303 bool renderParents,
304 bool backArrows,
305 const DString &title,
306 DString &graphStr)
307{
308 //printf("computeMd5Signature\n");
309 TextStream md5stream;
310 writeGraphHeader(md5stream,title);
311 if (!rank.empty())
312 {
313 md5stream << " rankdir=\"" << rank << "\";\n";
314 }
315 root->clearWriteFlag();
316 root->write(md5stream, gt, format, gt!=GraphType::CallGraph && gt!=GraphType::Dependency, true, backArrows);
317 if (renderParents)
318 {
319 for (const auto &pn : root->parents())
320 {
321 if (pn->isVisible())
322 {
323 const auto &children = pn->children();
324 auto child_it = std::find(children.begin(),children.end(),root);
325 size_t index = child_it - children.begin();
326 root->writeArrow(md5stream, // stream
327 gt, // graph type
328 format, // output format
329 pn, // child node
330 &pn->edgeInfo()[index], // edge info
331 false, // topDown?
332 backArrows // point back?
333 );
334 }
335 pn->write(md5stream, // stream
336 gt, // graph type
337 format, // output format
338 true, // topDown?
339 false, // toChildren?
340 backArrows // backward pointing arrows?
341 );
342 }
343 }
344 writeGraphFooter(md5stream);
345
346 graphStr=md5stream.str();
347}
348
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:88
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:152
std::string_view view() const
Definition dstring.h:166
size_t size() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:158
const std::string & str() const
Definition dstring.h:649
Class representing a directory in the file system.
Definition dir.h:73
std::string absPath() const
Definition dir.cpp:365
std::string path() const
Definition dir.cpp:235
bool exists() const
Definition dir.cpp:258
static DString mapLabelToId(const DString &mapLabel)
static bool writeVecGfxFigure(TextStream &out, const DString &baseName, const DString &figureName)
static bool writeSVGFigureLink(TextStream &out, const DString &relPath, const DString &baseName, const DString &absImgName)
Check if a reference to a SVG figure can be written and do so if possible.
static bool convertMapFile(TextStream &t, const DString &mapName, const DString &relPath, bool urlOnly=false, const DString &context=DString())
static void computeGraph(DotNode *root, GraphType gt, GraphOutputFormat format, const DString &rank, bool renderParents, bool backArrows, const DString &title, DString &graphStr)
Definition dotgraph.cpp:299
DString m_baseName
Definition dotgraph.h:94
bool m_noDivTag
Definition dotgraph.h:98
Dir m_dir
Definition dotgraph.h:87
int m_graphId
Definition dotgraph.h:91
EmbeddedOutputFormat m_textFormat
Definition dotgraph.h:86
DString m_theGraph
Definition dotgraph.h:95
static void writeGraphFooter(TextStream &t)
Definition dotgraph.cpp:294
bool m_urlOnly
Definition dotgraph.h:100
DString absDotName() const
Definition dotgraph.h:79
GraphOutputFormat m_graphFormat
Definition dotgraph.h:85
DString m_fileName
Definition dotgraph.h:88
void generateCode(TextStream &t)
Definition dotgraph.cpp:204
bool prepareDotFile()
Definition dotgraph.cpp:150
DString absImgName() const
Definition dotgraph.h:81
virtual DString getImgAltText() const
Definition dotgraph.h:74
virtual DString getBaseName() const =0
DString m_relPath
Definition dotgraph.h:89
bool m_doNotAddImageToIndex
Definition dotgraph.h:97
DString relImgName() const
Definition dotgraph.h:82
virtual DString getMapLabel() const =0
bool m_regenerate
Definition dotgraph.h:96
virtual DString absMapName() const
Definition dotgraph.h:72
bool m_zoomable
Definition dotgraph.h:99
DString writeGraph(TextStream &t, GraphOutputFormat gf, EmbeddedOutputFormat ef, const DString &path, const DString &fileName, const DString &relPath, bool writeImageMap=true, int graphId=-1)
Definition dotgraph.cpp:114
DString m_absPath
Definition dotgraph.h:93
DString absBaseName() const
Definition dotgraph.h:78
DString imgName() const
Definition dotgraph.cpp:106
static void writeGraphHeader(TextStream &t, const DString &title=DString())
Definition dotgraph.cpp:269
virtual void computeTheGraph()=0
friend class DotNode
Definition dotgraph.h:36
bool m_generateImageMap
Definition dotgraph.h:90
void addJob(const DotJob &newJob)
Definition dot.cpp:93
static DotManager * instance()
Definition dot.cpp:79
void writeArrow(TextStream &t, GraphType gt, GraphOutputFormat f, const DotNode *cn, const EdgeInfo *ei, bool topDown, bool pointBack=true) const
Definition dotnode.cpp:603
void write(TextStream &t, GraphType gt, GraphOutputFormat f, bool topDown, bool toChildren, bool backArrows)
Definition dotnode.cpp:654
void clearWriteFlag()
Definition dotnode.cpp:885
const DotNodeRefVector & parents() const
Definition dotnode.h:123
static IndexList * indexList
Definition doxygen.h:125
Minimal replacement for QFileInfo.
Definition fileinfo.h:26
bool exists() const
Definition fileinfo.cpp:30
size_t size() const
Definition fileinfo.cpp:23
void addImageFile(const DString &name)
Definition indexlist.h:124
Text streaming class that buffers data.
Definition textstream.h:36
bool empty() const
Returns true iff the buffer is empty.
Definition textstream.h:256
std::string str() const
Return the contents of the buffer as a std::string object.
Definition textstream.h:232
#define Config_getEnumAsString(name)
Definition config.h:36
#define Config_getBool(name)
Definition config.h:33
#define Config_getString(name)
Definition config.h:32
static bool sameMd5Signature(const DString &baseName, const DString &md5)
Definition dotgraph.cpp:42
std::mutex g_dotIndexListMutex
Definition dotgraph.cpp:112
static bool deliverablesPresent(const DString &file1, const DString &file2)
Definition dotgraph.cpp:68
static bool insertMapFile(TextStream &out, const DString &mapFile, const DString &relPath, const DString &mapLabel)
Definition dotgraph.cpp:85
GraphType
Definition dotgraph.h:31
@ Dependency
Definition dotgraph.h:31
@ CallGraph
Definition dotgraph.h:31
EmbeddedOutputFormat
Definition dotgraph.h:30
GraphOutputFormat
Definition dotgraph.h:29
DString md5str(const std::string_view &str)
Definition md5hash.h:33
#define err(fmt,...)
Definition message.h:127
#define term(fmt,...)
Definition message.h:137
std::ifstream openInputStream(const DString &name, bool binary=false, bool openAtEnd=false)
Definition portable.cpp:676
std::ofstream openOutputStream(const DString &name, bool append=false)
Definition portable.cpp:665
Portable versions of functions that are platform dependent.
DString convertToXML(const DString &s, bool keepEntities, const bool citeEntry)
Definition util.cpp:3234
DString getDotImageExtension()
Definition util.cpp:4966
A bunch of utility functions.