My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
msc.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  *
4  *
5  * Copyright (C) 1997-2015 by Dimitri van Heesch.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation under the terms of the GNU General Public License is hereby
9  * granted. No representations are made about the suitability of this software
10  * for any purpose. It is provided "as is" without express or implied warranty.
11  * See the GNU General Public License for more details.
12  *
13  * Documents produced by Doxygen are derivative works derived from the
14  * input used in their production; they are not affected by this license.
15  *
16  */
17 
18 #include "msc.h"
19 #include "portable.h"
20 #include "config.h"
21 #include "message.h"
22 #include "docparser.h"
23 #include "doxygen.h"
24 #include "util.h"
25 #include "ftextstream.h"
26 
27 #include <qdir.h>
28 
29 static const int maxCmdLine = 40960;
30 
31 static bool convertMapFile(FTextStream &t,const char *mapName,const QCString relPath,
32  const QCString &context)
33 {
34  QFile f(mapName);
35  if (!f.open(IO_ReadOnly))
36  {
37  err("failed to open map file %s for inclusion in the docs!\n"
38  "If you installed Graphviz/dot after a previous failing run, \n"
39  "try deleting the output directory and rerun doxygen.\n",mapName);
40  return FALSE;
41  }
42  const int maxLineLen=1024;
43  char buf[maxLineLen];
44  char url[maxLineLen];
45  char ref[maxLineLen];
46  int x1,y1,x2,y2;
47  while (!f.atEnd())
48  {
49  bool isRef = FALSE;
50  int numBytes = f.readLine(buf,maxLineLen);
51  buf[numBytes-1]='\0';
52  //printf("ReadLine `%s'\n",buf);
53  if (qstrncmp(buf,"rect",4)==0)
54  {
55  // obtain the url and the coordinates in the order used by graphviz-1.5
56  sscanf(buf,"rect %s %d,%d %d,%d",url,&x1,&y1,&x2,&y2);
57 
58  if (qstrcmp(url,"\\ref")==0 || qstrcmp(url,"@ref")==0)
59  {
60  isRef = TRUE;
61  sscanf(buf,"rect %s %s %d,%d %d,%d",ref,url,&x1,&y1,&x2,&y2);
62  }
63 
64  // sanity checks
65  if (y2<y1) { int temp=y2; y2=y1; y1=temp; }
66  if (x2<x1) { int temp=x2; x2=x1; x1=temp; }
67 
68  t << "<area href=\"";
69 
70  if ( isRef )
71  {
72  // handle doxygen \ref tag URL reference
73  DocRef *df = new DocRef( (DocNode*) 0, url, context );
74  t << externalRef(relPath,df->ref(),TRUE);
75  if (!df->file().isEmpty()) t << df->file() << Doxygen::htmlFileExtension;
76  if (!df->anchor().isEmpty()) t << "#" << df->anchor();
77  delete df;
78  }
79  else
80  {
81  t << url;
82  }
83  t << "\" shape=\"rect\" coords=\""
84  << x1 << "," << y1 << "," << x2 << "," << y2 << "\""
85  << " alt=\"\"/>" << endl;
86  }
87  }
88 
89  return TRUE;
90 }
91 
92 void writeMscGraphFromFile(const char *inFile,const char *outDir,
93  const char *outFile,MscOutputFormat format)
94 {
95  QCString absOutFile = outDir;
96  absOutFile+=portable_pathSeparator();
97  absOutFile+=outFile;
98 
99  // chdir to the output dir, so dot can find the font file.
100  QCString oldDir = QDir::currentDirPath().utf8();
101  // go to the html output directory (i.e. path)
102  QDir::setCurrent(outDir);
103  //printf("Going to dir %s\n",QDir::currentDirPath().data());
104  QCString mscExe = Config_getString(MSCGEN_PATH)+"mscgen"+portable_commandExtension();
105  QCString mscArgs;
106  QCString extension;
107  switch (format)
108  {
109  case MSC_BITMAP:
110  mscArgs+="-T png";
111  extension=".png";
112  break;
113  case MSC_EPS:
114  mscArgs+="-T eps";
115  extension=".eps";
116  break;
117  case MSC_SVG:
118  mscArgs+="-T svg";
119  extension=".svg";
120  break;
121  default:
122  goto error; // I am not very fond of goto statements, but when in Rome...
123  }
124  mscArgs+=" -i \"";
125  mscArgs+=inFile;
126 
127  mscArgs+="\" -o \"";
128  mscArgs+=outFile;
129  mscArgs+=extension+"\"";
130  int exitCode;
131 // printf("*** running: %s %s outDir:%s %s\n",mscExe.data(),mscArgs.data(),outDir,outFile);
133  if ((exitCode=portable_system(mscExe,mscArgs,FALSE))!=0)
134  {
136  goto error;
137  }
139  if ( (format==MSC_EPS) && (Config_getBool(USE_PDFLATEX)) )
140  {
141  QCString epstopdfArgs(maxCmdLine);
142  epstopdfArgs.sprintf("\"%s.eps\" --outfile=\"%s.pdf\"",
143  outFile,outFile);
145  if (portable_system("epstopdf",epstopdfArgs)!=0)
146  {
147  err("Problems running epstopdf. Check your TeX installation!\n");
148  }
150  }
151 
152 error:
153  QDir::setCurrent(oldDir);
154 }
155 
156 QCString getMscImageMapFromFile(const QCString& inFile, const QCString& outDir,
157  const QCString& relPath,const QCString& context)
158 {
159  QCString outFile = inFile + ".map";
160 
161 
162  //printf("*** running:getMscImageMapFromFile \n");
163  // chdir to the output dir, so dot can find the font file.
164  QCString oldDir = QDir::currentDirPath().utf8();
165  // go to the html output directory (i.e. path)
166  QDir::setCurrent(outDir);
167  //printf("Going to dir %s\n",QDir::currentDirPath().data());
168 
169  QCString mscExe = Config_getString(MSCGEN_PATH)+"mscgen"+portable_commandExtension();
170  QCString mscArgs = "-T ismap -i \"";
171  mscArgs+=inFile;
172  mscArgs+="\" -o \"";
173  mscArgs+=outFile + "\"";
174 
175  int exitCode;
177  if ((exitCode=portable_system(mscExe,mscArgs,FALSE))!=0)
178  {
180  QDir::setCurrent(oldDir);
181  return "";
182  }
184 
185  QGString result;
186  FTextStream tmpout(&result);
187  convertMapFile(tmpout, outFile, relPath, context);
188  QDir().remove(outFile);
189 
190  QDir::setCurrent(oldDir);
191  return result.data();
192 }
193 
194 void writeMscImageMapFromFile(FTextStream &t,const QCString &inFile,
195  const QCString &outDir,
196  const QCString &relPath,
197  const QCString &baseName,
198  const QCString &context,
199  MscOutputFormat format
200  )
201 {
202  QCString mapName = baseName+".map";
203  t << "<img src=\"" << relPath << baseName << ".";
204  switch (format)
205  {
206  case MSC_BITMAP:
207  t << "png";
208  break;
209  case MSC_EPS:
210  t << "eps";
211  break;
212  case MSC_SVG:
213  t << "svg";
214  break;
215  default:
216  t << "unknown";
217  }
218  t << "\" alt=\""
219  << baseName << "\" border=\"0\" usemap=\"#" << mapName << "\"/>" << endl;
220  QCString imap = getMscImageMapFromFile(inFile,outDir,relPath,context);
221  t << "<map name=\"" << mapName << "\" id=\"" << mapName << "\">" << imap << "</map>" << endl;
222 }
223