Doxygen
htags.cpp
浏览该文件的文档.
1 /******************************************************************************
2  *
3  * Copyright (C) 1997-2015 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 <stdio.h>
17 
18 #include <unordered_map>
19 #include <string>
20 
21 #include "htags.h"
22 #include "util.h"
23 #include "message.h"
24 #include "config.h"
25 #include "portable.h"
26 #include "fileinfo.h"
27 #include "dir.h"
28 
29 bool Htags::useHtags = FALSE;
30 
31 static Dir g_inputDir;
32 static std::unordered_map<std::string,std::string> g_symbolMap;
33 
34 /*! constructs command line of htags(1) and executes it.
35  * \retval TRUE success
36  * \retval FALSE an error has occurred.
37  */
38 bool Htags::execute(const QCString &htmldir)
39 {
40  const StringVector &inputSource = Config_getList(INPUT);
41  bool quiet = Config_getBool(QUIET);
42  bool warnings = Config_getBool(WARNINGS);
43  QCString htagsOptions = ""; //Config_getString(HTAGS_OPTIONS);
44  QCString projectName = Config_getString(PROJECT_NAME);
45  QCString projectNumber = Config_getString(PROJECT_NUMBER);
46 
47  if (inputSource.empty())
48  {
50  }
51  else if (inputSource.size()==1)
52  {
53  g_inputDir.setPath(inputSource.back());
54  if (!g_inputDir.exists())
55  err("Cannot find directory %s. "
56  "Check the value of the INPUT tag in the configuration file.\n",
57  inputSource.back().c_str()
58  );
59  }
60  else
61  {
62  err("If you use USE_HTAGS then INPUT should specify a single directory.\n");
63  return FALSE;
64  }
65 
66  /*
67  * Construct command line for htags(1).
68  */
69  QCString commandLine = " -g -s -a -n ";
70  if (!quiet) commandLine += "-v ";
71  if (warnings) commandLine += "-w ";
72  if (!htagsOptions.isEmpty())
73  {
74  commandLine += ' ';
75  commandLine += htagsOptions;
76  }
77  if (!projectName.isEmpty())
78  {
79  commandLine += "-t \"";
80  commandLine += projectName;
81  if (!projectNumber.isEmpty())
82  {
83  commandLine += '-';
84  commandLine += projectNumber;
85  }
86  commandLine += "\" ";
87  }
88  commandLine += " \"" + htmldir + "\"";
89  std::string oldDir = Dir::currentDirPath();
91  //printf("CommandLine=[%s]\n",qPrint(commandLine));
93  bool result=Portable::system("htags",commandLine,FALSE)==0;
94  if (!result)
95  {
96  err("Problems running %s. Check your installation\n", "htags");
97  }
99  Dir::setCurrent(oldDir);
100  return result;
101 }
102 
103 
104 /*! load filemap and make index.
105  * \param htmlDir of HTML directory generated by htags(1).
106  * \retval TRUE success
107  * \retval FALSE error
108  */
109 bool Htags::loadFilemap(const QCString &htmlDir)
110 {
111  QCString fileMapName = htmlDir+"/HTML/FILEMAP";
112  FileInfo fi(fileMapName.str());
113  /*
114  * Construct FILEMAP dictionary.
115  *
116  * In FILEMAP, URL includes 'html' suffix but we cut it off according
117  * to the method of FileDef class.
118  *
119  * FILEMAP format:
120  * <NAME>\t<HREF>.html\n
121  * QDICT:
122  * dict[<NAME>] = <HREF>
123  */
124  if (fi.exists() && fi.isReadable())
125  {
126  std::ifstream f(fileMapName.str(),std::ifstream::in);
127  if (f.is_open())
128  {
129  std::string lineStr;
130  while (getline(f,lineStr))
131  {
132  QCString line(lineStr);
133  //printf("Read line: %s",qPrint(line));
134  int sep = line.find('\t');
135  if (sep!=-1)
136  {
137  QCString key = line.left(sep).stripWhiteSpace();
138  QCString value = line.mid(sep+1).stripWhiteSpace();
139  int ext=value.findRev('.');
140  if (ext!=-1) value=value.left(ext); // strip extension
141  g_symbolMap.insert(std::make_pair(key.str(),value.str()));
142  //printf("Key/Value=(%s,%s)\n",qPrint(key),qPrint(value));
143  }
144  }
145  return true;
146  }
147  else
148  {
149  err("file %s cannot be opened\n",qPrint(fileMapName));
150  }
151  }
152  return false;
153 }
154 
155 /*! convert path name into the url in the hypertext generated by htags.
156  * \param path path name
157  * \returns URL NULL: not found.
158  */
160 {
161  QCString url,symName=path;
162  QCString dir = g_inputDir.absPath();
163  int dl=dir.length();
164  if ((int)symName.length()>dl+1)
165  {
166  symName = symName.mid(dl+1);
167  }
168  if (!symName.isEmpty())
169  {
170  auto it = g_symbolMap.find(symName.str());
171  //printf("path2URL=%s symName=%s result=%p\n",qPrint(path),qPrint(symName),result);
172  if (it!=g_symbolMap.end())
173  {
174  url = QCString("HTML/"+it->second);
175  }
176  }
177  return url;
178 }
179 
StringVector
std::vector< std::string > StringVector
Definition: containers.h:32
Dir::currentDirPath
static std::string currentDirPath()
Definition: dir.cpp:282
fileinfo.h
Htags::path2URL
static QCString path2URL(const QCString &path)
Definition: htags.cpp:159
Dir
Class representing a directory in the file system
Definition: dir.h:68
QCString::length
uint length() const
Returns the length of the string, not counting the 0-terminator.
Definition: qcstring.h:147
Htags::loadFilemap
static bool loadFilemap(const QCString &htmldir)
Definition: htags.cpp:109
g_symbolMap
static std::unordered_map< std::string, std::string > g_symbolMap
Definition: htags.cpp:32
Htags::useHtags
static bool useHtags
Definition: htags.h:23
QCString::findRev
int findRev(char c, int index=-1, bool cs=TRUE) const
Definition: qcstring.cpp:86
QCString::isEmpty
bool isEmpty() const
Returns TRUE iff the string is empty
Definition: qcstring.h:144
QCString::str
std::string str() const
Definition: qcstring.h:442
err
void err(const char *fmt,...)
Definition: message.cpp:203
g_inputDir
static Dir g_inputDir
Definition: htags.cpp:31
QCString::find
int find(char c, int index=0, bool cs=TRUE) const
Definition: qcstring.cpp:38
QCString::stripWhiteSpace
QCString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition: qcstring.h:243
FileInfo::exists
bool exists() const
Definition: fileinfo.cpp:30
QCString::left
QCString left(size_t len) const
Definition: qcstring.h:212
message.h
Portable::sysTimerStart
void sysTimerStart()
Definition: portable.cpp:470
htags.h
Dir::absPath
std::string absPath() const
Definition: dir.cpp:305
Portable::system
int system(const QCString &command, const QCString &args, bool commandHasConsole=true)
Definition: portable.cpp:42
Dir::setCurrent
static bool setCurrent(const std::string &path)
Definition: dir.cpp:290
Htags::execute
static bool execute(const QCString &htmldir)
Definition: htags.cpp:38
QCString::mid
QCString mid(size_t index, size_t len=static_cast< size_t >(-1)) const
Definition: qcstring.h:224
Config_getBool
#define Config_getBool(name)
Definition: config.h:33
FileInfo
Minimal replacement for QFileInfo.
Definition: fileinfo.h:22
qPrint
const char * qPrint(const char *s)
Definition: qcstring.h:589
Config_getString
#define Config_getString(name)
Definition: config.h:32
Dir::setPath
void setPath(const std::string &path)
Definition: dir.cpp:171
config.h
Portable::sysTimerStop
void sysTimerStop()
Definition: portable.cpp:475
FileInfo::isReadable
bool isReadable() const
Definition: fileinfo.cpp:44
Dir::exists
bool exists() const
Definition: dir.cpp:199
portable.h
Portable versions of functions that are platform dependent.
dir.h
util.h
A bunch of utility functions.
Config_getList
#define Config_getList(name)
Definition: config.h:37
FALSE
#define FALSE
Definition: qcstring.h:33
QCString
This is an alternative implementation of QCString.
Definition: qcstring.h:108