Doxygen
Loading...
Searching...
No Matches
htags.cpp
Go to the documentation of this file.
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
30
32static 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 */
38bool 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();
90 Dir::setCurrent(g_inputDir.absPath());
91 //printf("CommandLine=[%s]\n",qPrint(commandLine));
92 bool result=Portable::system("htags",commandLine,FALSE)==0;
93 if (!result)
94 {
95 err("Problems running %s. Check your installation\n", "htags");
96 }
97 Dir::setCurrent(oldDir);
98 return result;
99}
100
101
102/*! load filemap and make index.
103 * \param htmlDir of HTML directory generated by htags(1).
104 * \retval TRUE success
105 * \retval FALSE error
106 */
107bool Htags::loadFilemap(const QCString &htmlDir)
108{
109 QCString fileMapName = htmlDir+"/HTML/FILEMAP";
110 FileInfo fi(fileMapName.str());
111 /*
112 * Construct FILEMAP dictionary.
113 *
114 * In FILEMAP, URL includes 'html' suffix but we cut it off according
115 * to the method of FileDef class.
116 *
117 * FILEMAP format:
118 * <NAME>\t<HREF>.html\n
119 * QDICT:
120 * dict[<NAME>] = <HREF>
121 */
122 if (fi.exists() && fi.isReadable())
123 {
124 std::ifstream f = Portable::openInputStream(fileMapName);
125 if (f.is_open())
126 {
127 std::string lineStr;
128 while (getline(f,lineStr))
129 {
130 QCString line(lineStr);
131 //printf("Read line: %s",qPrint(line));
132 int sep = line.find('\t');
133 if (sep!=-1)
134 {
135 QCString key = line.left(sep).stripWhiteSpace();
136 QCString value = line.mid(sep+1).stripWhiteSpace();
137 int ext=value.findRev('.');
138 if (ext!=-1) value=value.left(ext); // strip extension
139 g_symbolMap.emplace(key.str(),value.str());
140 //printf("Key/Value=(%s,%s)\n",qPrint(key),qPrint(value));
141 }
142 }
143 return true;
144 }
145 else
146 {
147 err("file %s cannot be opened\n",qPrint(fileMapName));
148 }
149 }
150 return false;
151}
152
153/*! convert path name into the url in the hypertext generated by htags.
154 * \param path path name
155 * \returns URL nullptr: not found.
156 */
158{
159 QCString url,symName=path;
160 QCString dir = g_inputDir.absPath();
161 size_t dl=dir.length();
162 if (symName.length()>dl+1)
163 {
164 symName = symName.mid(dl+1);
165 }
166 if (!symName.isEmpty())
167 {
168 auto it = g_symbolMap.find(symName.str());
169 //printf("path2URL=%s symName=%s result=%p\n",qPrint(path),qPrint(symName),result);
170 if (it!=g_symbolMap.end())
171 {
172 url = QCString("HTML/"+it->second);
173 }
174 }
175 return url;
176}
177
Class representing a directory in the file system.
Definition dir.h:75
static std::string currentDirPath()
Definition dir.cpp:340
static bool setCurrent(const std::string &path)
Definition dir.cpp:348
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
bool exists() const
Definition fileinfo.cpp:30
bool isReadable() const
Definition fileinfo.cpp:44
This is an alternative implementation of QCString.
Definition qcstring.h:101
int find(char c, int index=0, bool cs=TRUE) const
Definition qcstring.cpp:43
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition qcstring.h:153
QCString mid(size_t index, size_t len=static_cast< size_t >(-1)) const
Definition qcstring.h:226
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:150
QCString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition qcstring.h:245
const std::string & str() const
Definition qcstring.h:537
int findRev(char c, int index=-1, bool cs=TRUE) const
Definition qcstring.cpp:91
QCString left(size_t len) const
Definition qcstring.h:214
#define Config_getList(name)
Definition config.h:38
#define Config_getBool(name)
Definition config.h:33
#define Config_getString(name)
Definition config.h:32
std::vector< std::string > StringVector
Definition containers.h:33
static std::unordered_map< std::string, std::string > g_symbolMap
Definition htags.cpp:32
static Dir g_inputDir
Definition htags.cpp:31
#define err(fmt,...)
Definition message.h:84
std::ifstream openInputStream(const QCString &name, bool binary=false, bool openAtEnd=false)
Definition portable.cpp:676
int system(const QCString &command, const QCString &args, bool commandHasConsole=true)
Definition portable.cpp:106
Portable versions of functions that are platform dependent.
const char * qPrint(const char *s)
Definition qcstring.h:672
#define FALSE
Definition qcstring.h:34
static QCString path2URL(const QCString &path)
Definition htags.cpp:157
static bool execute(const QCString &htmldir)
Definition htags.cpp:38
static bool loadFilemap(const QCString &htmldir)
Definition htags.cpp:107
static bool useHtags
Definition htags.h:23
A bunch of utility functions.