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// own header
17#include "htags.h"
18
19// standard includes
20#include <cstdio>
21#include <string>
22#include <unordered_map>
23
24// other includes
25#include "config.h"
26#include "dir.h"
27#include "fileinfo.h"
28#include "message.h"
29#include "portable.h"
30#include "util.h"
31
32bool Htags::useHtags = false;
33
35static std::unordered_map<std::string,std::string> g_symbolMap;
36
37/*! constructs command line of htags(1) and executes it.
38 * \retval true success
39 * \retval false an error has occurred.
40 */
41bool Htags::execute(const DString &htmldir)
42{
43 StringVector inputSource = Config_getList(INPUT);
44 bool quiet = Config_getBool(QUIET);
45 bool warnings = Config_getBool(WARNINGS);
46 DString htagsOptions = ""; //Config_getString(HTAGS_OPTIONS);
47 DString projectName = Config_getString(PROJECT_NAME);
48 DString projectNumber = Config_getString(PROJECT_NUMBER);
49
50 if (inputSource.empty())
51 {
53 }
54 else if (inputSource.size()==1)
55 {
56 g_inputDir.setPath(inputSource.back());
57 if (!g_inputDir.exists())
58 err("Cannot find directory {}. "
59 "Check the value of the INPUT tag in the configuration file.\n",
60 inputSource.back()
61 );
62 }
63 else
64 {
65 err("If you use USE_HTAGS then INPUT should specify a single directory.\n");
66 return false;
67 }
68
69 /*
70 * Construct command line for htags(1).
71 */
72 DString commandLine = " -g -s -a -n ";
73 if (!quiet) commandLine += "-v ";
74 if (warnings) commandLine += "-w ";
75 if (!htagsOptions.empty())
76 {
77 commandLine += ' ';
78 commandLine += htagsOptions;
79 }
80 if (!projectName.empty())
81 {
82 commandLine += "-t \"";
83 commandLine += projectName;
84 if (!projectNumber.empty())
85 {
86 commandLine += '-';
87 commandLine += projectNumber;
88 }
89 commandLine += "\" ";
90 }
91 commandLine += " \"" + htmldir + "\"";
92 std::string oldDir = Dir::currentDirPath();
94 //printf("CommandLine=[%s]\n",qPrint(commandLine));
95 bool result=Portable::system("htags",commandLine,false)==0;
96 if (!result)
97 {
98 err("Problems running htags. Check your installation\n");
99 }
100 Dir::setCurrent(oldDir);
101 return result;
102}
103
104
105/*! load filemap and make index.
106 * \param htmlDir of HTML directory generated by htags(1).
107 * \retval true success
108 * \retval false error
109 */
110bool Htags::loadFilemap(const DString &htmlDir)
111{
112 DString fileMapName = htmlDir+"/HTML/FILEMAP";
113 FileInfo fi(fileMapName.str());
114 /*
115 * Construct FILEMAP dictionary.
116 *
117 * In FILEMAP, URL includes 'html' suffix but we cut it off according
118 * to the method of FileDef class.
119 *
120 * FILEMAP format:
121 * <NAME>\t<HREF>.html\n
122 * QDICT:
123 * dict[<NAME>] = <HREF>
124 */
125 if (fi.exists() && fi.isReadable())
126 {
127 std::ifstream f = Portable::openInputStream(fileMapName);
128 if (f.is_open())
129 {
130 std::string lineStr;
131 while (getline(f,lineStr))
132 {
133 DString line(lineStr);
134 //printf("Read line: %s",qPrint(line));
135 if (size_t sep = line.find('\t'); sep!=DString::npos)
136 {
137 DString key = line.left(sep).stripWhiteSpace();
138 DString value = line.mid(sep+1).stripWhiteSpace();
139 size_t ext=value.rfind('.');
140 if (ext!=DString::npos) value=value.left(ext); // strip extension
141 g_symbolMap.emplace(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 {} cannot be opened\n",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 nullptr: not found.
158 */
160{
161 DString url,symName=path;
162 DString dir = g_inputDir.absPath();
163 size_t dl=dir.length();
164 if (symName.length()>dl+1)
165 {
166 symName = symName.mid(dl+1);
167 }
168 if (!symName.empty())
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 = DString("HTML/"+it->second);
175 }
176 }
177 return url;
178}
179
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
size_t find(char c, size_t pos=0) const
Definition dstring.h:239
DString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition dstring.h:337
DString left(size_t len) const
Definition dstring.h:306
const std::string & str() const
Definition dstring.h:645
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:151
Class representing a directory in the file system.
Definition dir.h:73
static std::string currentDirPath()
Definition dir.cpp:348
std::string absPath() const
Definition dir.cpp:370
void setPath(const std::string &path)
Definition dir.cpp:235
static bool setCurrent(const std::string &path)
Definition dir.cpp:356
bool exists() const
Definition dir.cpp:263
Minimal replacement for QFileInfo.
Definition fileinfo.h:26
bool exists() const
Definition fileinfo.cpp:34
bool isReadable() const
Definition fileinfo.cpp:48
#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:35
static Dir g_inputDir
Definition htags.cpp:34
#define err(fmt,...)
Definition message.h:127
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
Portable versions of functions that are platform dependent.
static DString path2URL(const DString &path)
Definition htags.cpp:159
static bool execute(const DString &htmldir)
Definition htags.cpp:41
static bool loadFilemap(const DString &htmldir)
Definition htags.cpp:110
static bool useHtags
Definition htags.h:23
A bunch of utility functions.