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
29bool Htags::useHtags = false;
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 DString &htmldir)
39{
40 StringVector inputSource = Config_getList(INPUT);
41 bool quiet = Config_getBool(QUIET);
42 bool warnings = Config_getBool(WARNINGS);
43 DString htagsOptions = ""; //Config_getString(HTAGS_OPTIONS);
44 DString projectName = Config_getString(PROJECT_NAME);
45 DString 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 {}. "
56 "Check the value of the INPUT tag in the configuration file.\n",
57 inputSource.back()
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 DString commandLine = " -g -s -a -n ";
70 if (!quiet) commandLine += "-v ";
71 if (warnings) commandLine += "-w ";
72 if (!htagsOptions.empty())
73 {
74 commandLine += ' ';
75 commandLine += htagsOptions;
76 }
77 if (!projectName.empty())
78 {
79 commandLine += "-t \"";
80 commandLine += projectName;
81 if (!projectNumber.empty())
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));
92 bool result=Portable::system("htags",commandLine,false)==0;
93 if (!result)
94 {
95 err("Problems running htags. Check your installation\n");
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 DString &htmlDir)
108{
109 DString 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 DString line(lineStr);
131 //printf("Read line: %s",qPrint(line));
132 if (size_t sep = line.find('\t'); sep!=DString::npos)
133 {
134 DString key = line.left(sep).stripWhiteSpace();
135 DString value = line.mid(sep+1).stripWhiteSpace();
136 size_t ext=value.rfind('.');
137 if (ext!=DString::npos) value=value.left(ext); // strip extension
138 g_symbolMap.emplace(key.str(),value.str());
139 //printf("Key/Value=(%s,%s)\n",qPrint(key),qPrint(value));
140 }
141 }
142 return true;
143 }
144 else
145 {
146 err("file {} cannot be opened\n",fileMapName);
147 }
148 }
149 return false;
150}
151
152/*! convert path name into the url in the hypertext generated by htags.
153 * \param path path name
154 * \returns URL nullptr: not found.
155 */
157{
158 DString url,symName=path;
159 DString dir = g_inputDir.absPath();
160 size_t dl=dir.length();
161 if (symName.length()>dl+1)
162 {
163 symName = symName.mid(dl+1);
164 }
165 if (!symName.empty())
166 {
167 auto it = g_symbolMap.find(symName.str());
168 //printf("path2URL=%s symName=%s result=%p\n",qPrint(path),qPrint(symName),result);
169 if (it!=g_symbolMap.end())
170 {
171 url = DString("HTML/"+it->second);
172 }
173 }
174 return url;
175}
176
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:88
DString()=default
size_t rfind(char c, size_t pos=npos) const
Definition dstring.h:248
DString mid(size_t index, size_t len=npos) const
Definition dstring.h:322
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:152
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:182
size_t find(char c, size_t pos=0) const
Definition dstring.h:243
DString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition dstring.h:341
DString left(size_t len) const
Definition dstring.h:310
const std::string & str() const
Definition dstring.h:649
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:155
Class representing a directory in the file system.
Definition dir.h:73
static std::string currentDirPath()
Definition dir.cpp:343
std::string absPath() const
Definition dir.cpp:365
void setPath(const std::string &path)
Definition dir.cpp:230
static bool setCurrent(const std::string &path)
Definition dir.cpp:351
bool exists() const
Definition dir.cpp:258
Minimal replacement for QFileInfo.
Definition fileinfo.h:26
bool exists() const
Definition fileinfo.cpp:30
bool isReadable() const
Definition fileinfo.cpp:44
#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:127
std::ifstream openInputStream(const DString &name, bool binary=false, bool openAtEnd=false)
Definition portable.cpp:676
int system(const DString &command, const DString &args, bool commandHasConsole=true)
Definition portable.cpp:105
Portable versions of functions that are platform dependent.
static DString path2URL(const DString &path)
Definition htags.cpp:156
static bool execute(const DString &htmldir)
Definition htags.cpp:38
static bool loadFilemap(const DString &htmldir)
Definition htags.cpp:107
static bool useHtags
Definition htags.h:23
A bunch of utility functions.