Doxygen
Loading...
Searching...
No Matches
rtfgen.cpp File Reference
#include <mutex>
#include <stdlib.h>
#include <algorithm>
#include <unordered_map>
#include "rtfgen.h"
#include "config.h"
#include "message.h"
#include "doxygen.h"
#include "util.h"
#include "diagram.h"
#include "language.h"
#include "dot.h"
#include "dotcallgraph.h"
#include "dotclassgraph.h"
#include "dotdirdeps.h"
#include "dotincldepgraph.h"
#include "version.h"
#include "pagedef.h"
#include "rtfstyle.h"
#include "rtfdocvisitor.h"
#include "docparser.h"
#include "dirdef.h"
#include "vhdldocgen.h"
#include "portable.h"
#include "groupdef.h"
#include "classlist.h"
#include "filename.h"
#include "namespacedef.h"
#include "dir.h"
#include "utf8.h"
#include "debug.h"
#include "datetime.h"
#include "outputlist.h"
#include "moduledef.h"
Include dependency graph for rtfgen.cpp:

Go to the source code of this file.

Macros

#define DBG_RTF(x)

Functions

static QCString dateToRTFDateString ()
static QCString docifyToString (const QCString &str)
static QCString makeIndexName (const QCString &s, int i)
static QCString objectLinkToString (const QCString &ref, const QCString &f, const QCString &anchor, const QCString &text)
bool isLeadBytes (int c)
static void encodeForOutput (TextStream &t, const QCString &s)
static bool preProcessFile (Dir &d, const QCString &infName, TextStream &t, bool bIncludeHeader=true, bool removeFile=true)
 VERY brittle routine inline RTF's included by other RTF's.
void testRTFOutput (const QCString &name)
 Tests the integrity of the result by counting brackets.
QCString rtfFormatBmkStr (const QCString &name)

Variables

static StringSet removeSet
static std::mutex g_rtfFormatMutex
static std::unordered_map< std::string, std::string > g_tagMap
static QCString g_nextTag ("AAAAAAAAAA")

Macro Definition Documentation

◆ DBG_RTF

#define DBG_RTF ( x)

Definition at line 55 of file rtfgen.cpp.

Referenced by preProcessFile().

Function Documentation

◆ dateToRTFDateString()

QCString dateToRTFDateString ( )
static

Definition at line 59 of file rtfgen.cpp.

60{
61 auto tm = getCurrentDateTime();
62 QCString result;
63 switch (Config_getEnum(TIMESTAMP))
64 {
65 case TIMESTAMP_t::YES:
66 case TIMESTAMP_t::DATETIME:
67 result.sprintf("\\yr%d\\mo%d\\dy%d\\hr%d\\min%d\\sec%d",
68 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
69 tm.tm_hour, tm.tm_min, tm.tm_sec);
70 break;
71 case TIMESTAMP_t::DATE:
72 result.sprintf("\\yr%d\\mo%d\\dy%d",
73 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday);
74 break;
75 case TIMESTAMP_t::NO:
76 return "";
77 }
78 return "{\\creatim " + result + "}\n";
79}
This is an alternative implementation of QCString.
Definition qcstring.h:101
QCString & sprintf(const char *format,...)
Definition qcstring.cpp:29
#define Config_getEnum(name)
Definition config.h:35
std::tm getCurrentDateTime()
Returns the filled in std::tm for the current date and time.
Definition datetime.cpp:30

References Config_getEnum, getCurrentDateTime(), and QCString::sprintf().

Referenced by RTFGenerator::endIndexSection().

◆ docifyToString()

QCString docifyToString ( const QCString & str)
static

Definition at line 81 of file rtfgen.cpp.

82{
83 QCString result;
84 result.reserve(str.length());
85 if (!str.isEmpty())
86 {
87 const char *p=str.data();
88 while (*p)
89 {
90 char c=*p++;
91
92 switch (c)
93 {
94 case '{': result += "\\{"; break;
95 case '}': result += "\\}"; break;
96 case '\\': result += "\\\\"; break;
97 default: result += c; break;
98 }
99 }
100 }
101 return result;
102}
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition qcstring.h:153
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:150
void reserve(size_t size)
Reserve space for size bytes without changing the string contents.
Definition qcstring.h:172
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition qcstring.h:159

References QCString::data(), QCString::isEmpty(), QCString::length(), and QCString::reserve().

Referenced by objectLinkToString().

◆ encodeForOutput()

void encodeForOutput ( TextStream & t,
const QCString & s )
static

Definition at line 2185 of file rtfgen.cpp.

2186{
2187 if (s==nullptr) return;
2188 QCString encoding;
2189 bool converted=FALSE;
2190 size_t l = s.length();
2191 static std::vector<char> enc;
2192 if (l*4>enc.size()) enc.resize(l*4); // worst case
2193 encoding.sprintf("CP%s",qPrint(theTranslator->trRTFansicp()));
2194 if (!encoding.isEmpty())
2195 {
2196 // convert from UTF-8 back to the output encoding
2197 void *cd = portable_iconv_open(encoding.data(),"UTF-8");
2198 if (cd!=reinterpret_cast<void *>(-1))
2199 {
2200 size_t iLeft=l;
2201 size_t oLeft=enc.size();
2202 const char *inputPtr = s.data();
2203 char *outputPtr = &enc[0];
2204 if (!portable_iconv(cd, &inputPtr, &iLeft, &outputPtr, &oLeft))
2205 {
2206 enc.resize(enc.size()-oLeft);
2207 converted=TRUE;
2208 }
2210 }
2211 }
2212 if (!converted) // if we did not convert anything, copy as is.
2213 {
2214 memcpy(enc.data(),s.data(),l);
2215 enc.resize(l);
2216 }
2217 bool multiByte = FALSE;
2218
2219 for (size_t i=0;i<enc.size();i++)
2220 {
2221 uint8_t c = static_cast<uint8_t>(enc.at(i));
2222
2223 if (c>=0x80 || multiByte)
2224 {
2225 char esc[10];
2226 qsnprintf(esc,10,"\\'%X",c); // escape sequence for SBCS and DBCS(1st&2nd bytes).
2227 t << esc;
2228
2229 if (!multiByte)
2230 {
2231 multiByte = isLeadBytes(c); // It may be DBCS Codepages.
2232 }
2233 else
2234 {
2235 multiByte = FALSE; // end of Double Bytes Character.
2236 }
2237 }
2238 else
2239 {
2240 t << c;
2241 }
2242 }
2243}
Translator * theTranslator
Definition language.cpp:71
int portable_iconv_close(void *cd)
size_t portable_iconv(void *cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
void * portable_iconv_open(const char *tocode, const char *fromcode)
#define qsnprintf
Definition qcstring.h:49
const char * qPrint(const char *s)
Definition qcstring.h:672
#define TRUE
Definition qcstring.h:37
#define FALSE
Definition qcstring.h:34
bool isLeadBytes(int c)
Definition rtfgen.cpp:2157

References QCString::data(), FALSE, QCString::isEmpty(), isLeadBytes(), QCString::length(), portable_iconv(), portable_iconv_close(), portable_iconv_open(), qPrint(), qsnprintf, QCString::sprintf(), theTranslator, and TRUE.

Referenced by preProcessFile().

◆ isLeadBytes()

bool isLeadBytes ( int c)

Definition at line 2157 of file rtfgen.cpp.

2158{
2159 bool result=false; // for SBCS Codepages (cp1252,1251 etc...);
2160
2161 QCString codePage = theTranslator->trRTFansicp();
2162
2163 if (codePage == "932") // cp932 (Japanese Shift-JIS)
2164 {
2165 result = (0x81<=c && c<=0x9f) || (0xe0<=c && c<=0xfc);
2166 }
2167 else if (codePage == "936") // cp936 (Simplified Chinese GBK)
2168 {
2169 result = 0x81<=c && c<=0xFE;
2170 }
2171 else if (codePage == "949") // cp949 (Korean)
2172 {
2173 result = 0x81<=c && c<=0xFE;
2174 }
2175 else if (codePage == "950") // cp950 (Traditional Chinese Big5)
2176 {
2177 result = 0x81<=c && c<=0xFE;
2178 }
2179
2180 return result;
2181}

References theTranslator.

Referenced by encodeForOutput().

◆ makeIndexName()

QCString makeIndexName ( const QCString & s,
int i )
static

◆ objectLinkToString()

QCString objectLinkToString ( const QCString & ref,
const QCString & f,
const QCString & anchor,
const QCString & text )
static

Definition at line 1548 of file rtfgen.cpp.

1550{
1551 QCString result;
1552 if (ref.isEmpty() && Config_getBool(RTF_HYPERLINKS))
1553 {
1554 QCString refName;
1555 if (!f.isEmpty())
1556 {
1557 refName+=stripPath(f);
1558 }
1559 if (!anchor.isEmpty())
1560 {
1561 refName+='_';
1562 refName+=anchor;
1563 }
1564
1565 result += "{\\field {\\*\\fldinst { HYPERLINK \\\\l \"";
1566 result += rtfFormatBmkStr(refName);
1567 result += "\" }{}";
1568 result += "}{\\fldrslt {\\cs37\\ul\\cf2 ";
1569
1570 result += docifyToString(text);
1571
1572 result += "}}}\n";
1573 }
1574 else
1575 {
1576 result += "{\\b ";
1577 result += docifyToString(text);
1578 result += "}";
1579 }
1580 return result;
1581}
#define Config_getBool(name)
Definition config.h:33
QCString rtfFormatBmkStr(const QCString &name)
Definition rtfgen.cpp:2873
static QCString docifyToString(const QCString &str)
Definition rtfgen.cpp:81
QCString stripPath(const QCString &s)
Definition util.cpp:5461

References Config_getBool, docifyToString(), QCString::isEmpty(), rtfFormatBmkStr(), and stripPath().

◆ preProcessFile()

bool preProcessFile ( Dir & d,
const QCString & infName,
TextStream & t,
bool bIncludeHeader = true,
bool removeFile = true )
static

VERY brittle routine inline RTF's included by other RTF's.

it is recursive and ugly.

Definition at line 2249 of file rtfgen.cpp.

2250{
2251 static bool rtfDebug = Debug::isFlagSet(Debug::Rtf);
2252 std::ifstream f = Portable::openInputStream(infName);
2253 if (!f.is_open())
2254 {
2255 err("problems opening rtf file '{}' for reading\n",infName);
2256 return false;
2257 }
2258
2259 const int maxLineLength = 10240;
2260 static QCString lineBuf(maxLineLength, QCString::ExplicitSize);
2261
2262 // scan until find end of header
2263 // this is EXTREEEEEEEMLY brittle. It works on OUR rtf
2264 // files because the first line before the body
2265 // ALWAYS contains "{\comment begin body}"
2266 std::string line;
2267 while (getline(f,line))
2268 {
2269 line+='\n';
2270 if (line.find("\\comment begin body")!=std::string::npos) break;
2271 if (bIncludeHeader) encodeForOutput(t,line.c_str());
2272 }
2273
2274 std::string prevLine;
2275 bool first=true;
2276 while (getline(f,line))
2277 {
2278 line+='\n';
2279 size_t pos=prevLine.find("INCLUDETEXT \"");
2280 if (pos!=std::string::npos)
2281 {
2282 size_t startNamePos = prevLine.find('"',pos)+1;
2283 size_t endNamePos = prevLine.find('"',startNamePos);
2284 std::string fileName = prevLine.substr(startNamePos,endNamePos-startNamePos);
2285 DBG_RTF(t << "{\\comment begin include " << fileName << "}\n")
2286 if (!preProcessFile(d,fileName.c_str(),t,FALSE)) return FALSE;
2287 DBG_RTF(t << "{\\comment end include " << fileName << "}\n")
2288 }
2289 else if (!first) // no INCLUDETEXT on this line
2290 {
2291 encodeForOutput(t,prevLine.c_str());
2292 }
2293 prevLine = line;
2294 first=false;
2295 }
2296 if (!bIncludeHeader) // skip final '}' in case we don't include headers
2297 {
2298 size_t pos = line.rfind('}');
2299 if (pos==std::string::npos)
2300 {
2301 err("Strange, the last char was not a '}}'\n");
2302 pos = line.length();
2303 }
2304 encodeForOutput(t,line.substr(0,pos).c_str());
2305 }
2306 else
2307 {
2308 encodeForOutput(t,line.c_str());
2309 }
2310 f.close();
2311 // remove temporary file
2312 if (!rtfDebug && removeFile) removeSet.insert(FileInfo(d.filePath(infName.str())).absFilePath());
2313 return TRUE;
2314}
@ Rtf
Definition debug.h:43
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:132
std::string filePath(const std::string &path, bool acceptsAbsPath=true) const
Definition dir.cpp:280
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
std::string absFilePath() const
Definition fileinfo.cpp:101
const std::string & str() const
Definition qcstring.h:537
@ ExplicitSize
Definition qcstring.h:133
#define err(fmt,...)
Definition message.h:127
std::ifstream openInputStream(const QCString &name, bool binary=false, bool openAtEnd=false)
Definition portable.cpp:676
#define DBG_RTF(x)
static StringSet removeSet
Definition rtfgen.cpp:57
static bool preProcessFile(Dir &d, const QCString &infName, TextStream &t, bool bIncludeHeader=true, bool removeFile=true)
VERY brittle routine inline RTF's included by other RTF's.
Definition rtfgen.cpp:2249
#define DBG_RTF(x)
Definition rtfgen.cpp:55
static void encodeForOutput(TextStream &t, const QCString &s)
Definition rtfgen.cpp:2185

References FileInfo::absFilePath(), DBG_RTF, encodeForOutput(), err, QCString::ExplicitSize, FALSE, Dir::filePath(), Debug::isFlagSet(), Portable::openInputStream(), preProcessFile(), removeSet, Debug::Rtf, QCString::str(), and TRUE.

Referenced by preProcessFile(), and RTFGenerator::preProcessFileInplace().

◆ rtfFormatBmkStr()

QCString rtfFormatBmkStr ( const QCString & name)

Definition at line 2873 of file rtfgen.cpp.

2874{
2875 std::lock_guard<std::mutex> lock(g_rtfFormatMutex);
2876
2877 // To overcome the 40-character tag limitation, we
2878 // substitute a short arbitrary string for the name
2879 // supplied, and keep track of the correspondence
2880 // between names and strings.
2881 auto it = g_tagMap.find(name.str());
2882 if (it!=g_tagMap.end()) // already known
2883 {
2884 return QCString(it->second);
2885 }
2886
2887 QCString tag = g_nextTag;
2888 auto result = g_tagMap.emplace(name.str(), g_nextTag.str());
2889
2890 if (result.second) // new item was added
2891 {
2892 // increment the next tag.
2893
2894 char* nxtTag = g_nextTag.rawData() + g_nextTag.length() - 1;
2895 for ( unsigned int i = 0; i < g_nextTag.length(); ++i, --nxtTag )
2896 {
2897 if ( ( ++(*nxtTag) ) > 'Z' )
2898 {
2899 *nxtTag = 'A';
2900 }
2901 else
2902 {
2903 // Since there was no carry, we can stop now
2904 break;
2905 }
2906 }
2907 }
2908
2909 Debug::print(Debug::Rtf,0,"Name = {} RTF_tag = {}\n",name,tag);
2910 return tag;
2911}
static void print(DebugMask mask, int prio, fmt::format_string< Args... > fmt, Args &&... args)
Definition debug.h:76
static std::mutex g_rtfFormatMutex
Definition rtfgen.cpp:2869
static QCString g_nextTag("AAAAAAAAAA")
static std::unordered_map< std::string, std::string > g_tagMap
Definition rtfgen.cpp:2870

References g_nextTag, g_rtfFormatMutex, g_tagMap, Rtf_Style_Default::name, Debug::print(), and Debug::Rtf.

Referenced by RTFGenerator::endDoxyAnchor(), objectLinkToString(), RTFDocVisitor::operator()(), RTFDocVisitor::operator()(), RTFDocVisitor::operator()(), RTFDocVisitor::operator()(), RTFDocVisitor::operator()(), RTFDocVisitor::startLink(), RTFGenerator::startTextLink(), RTFGenerator::writeAnchor(), RTFCodeGenerator::writeCodeLink(), RTFCodeGenerator::writeLineNumber(), RTFGenerator::writeRTFReference(), and RTFGenerator::writeStartAnnoItem().

◆ testRTFOutput()

void testRTFOutput ( const QCString & name)

Tests the integrity of the result by counting brackets.

Definition at line 2416 of file rtfgen.cpp.

2417{
2418 int bcount=0;
2419 int line=1;
2420 int c=0;
2421 std::ifstream f = Portable::openInputStream(name);
2422 if (f.is_open())
2423 {
2424 while ((c=f.get())!=-1)
2425 {
2426 if (c=='\\') // escape char
2427 {
2428 c=f.get();
2429 if (c==-1) break;
2430 }
2431 else if (c=='{') // open bracket
2432 {
2433 bcount++;
2434 }
2435 else if (c=='}') // close bracket
2436 {
2437 bcount--;
2438 if (bcount<0)
2439 {
2440 goto err;
2441 break;
2442 }
2443 }
2444 else if (c=='\n') // newline
2445 {
2446 line++;
2447 }
2448 }
2449 }
2450 if (bcount==0) return; // file is OK.
2451err:
2452 err("RTF integrity test failed at line {:d} of {} due to a bracket mismatch.\n"
2453 " Please try to create a small code example that produces this error \n"
2454 " and send that to doxygen@gmail.com.\n",line,name);
2455}

References err, Rtf_Style_Default::name, and Portable::openInputStream().

Referenced by RTFGenerator::preProcessFileInplace().

Variable Documentation

◆ g_nextTag

QCString g_nextTag("AAAAAAAAAA") ( "AAAAAAAAAA" )
static

Referenced by rtfFormatBmkStr().

◆ g_rtfFormatMutex

std::mutex g_rtfFormatMutex
static

Definition at line 2869 of file rtfgen.cpp.

Referenced by rtfFormatBmkStr().

◆ g_tagMap

std::unordered_map<std::string,std::string> g_tagMap
static

Definition at line 2870 of file rtfgen.cpp.

Referenced by rtfFormatBmkStr().

◆ removeSet

StringSet removeSet
static

Definition at line 57 of file rtfgen.cpp.

Referenced by preProcessFile(), and RTFGenerator::preProcessFileInplace().