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

Go to the source code of this file.

Macros

#define DBG_RTF(x)

Functions

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

Variables

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

Macro Definition Documentation

◆ DBG_RTF

#define DBG_RTF ( x)

Definition at line 58 of file rtfgen.cpp.

Referenced by preProcessFile().

Function Documentation

◆ dateToRTFDateString()

DString dateToRTFDateString ( )
static

Definition at line 62 of file rtfgen.cpp.

63{
64 auto tm = getCurrentDateTime();
65 DString result;
66 switch (Config_getEnum(TIMESTAMP))
67 {
68 case TIMESTAMP_t::YES:
69 case TIMESTAMP_t::DATETIME:
70 result.sprintf("\\yr%d\\mo%d\\dy%d\\hr%d\\min%d\\sec%d",
71 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
72 tm.tm_hour, tm.tm_min, tm.tm_sec);
73 break;
74 case TIMESTAMP_t::DATE:
75 result.sprintf("\\yr%d\\mo%d\\dy%d",
76 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday);
77 break;
78 case TIMESTAMP_t::NO:
79 return "";
80 }
81 return "{\\creatim " + result + "}\n";
82}
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
DString & sprintf(const char *format,...)
Definition dstring.cpp:34
#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:31

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

Referenced by RTFGenerator::endIndexSection().

◆ docifyToString()

DString docifyToString ( const DString & str)
static

Definition at line 84 of file rtfgen.cpp.

85{
86 DString result;
87 result.reserve(str.length());
88 if (!str.empty())
89 {
90 const char *p=str.data();
91 while (*p)
92 {
93 char c=*p++;
94
95 switch (c)
96 {
97 case '{': result += "\\{"; break;
98 case '}': result += "\\}"; break;
99 case '\\': result += "\\\\"; break;
100 default: result += c; break;
101 }
102 }
103 }
104 return result;
105}
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
void reserve(size_t size)
Reserve space for size bytes without changing the string contents.
Definition dstring.h:217
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition dstring.h:157
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:151

References DString::data(), DString::empty(), DString::length(), and DString::reserve().

Referenced by RTFGenerator::docify(), objectLinkToString(), and RTFGenerator::writeInheritedSectionTitle().

◆ encodeForOutput()

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

Definition at line 2189 of file rtfgen.cpp.

2190{
2191 if (s==nullptr) return;
2192 DString encoding;
2193 bool converted=false;
2194 size_t l = s.length();
2195 static std::vector<char> enc;
2196 if (l*4>enc.size()) enc.resize(l*4); // worst case
2197 encoding.sprintf("CP%s",qPrint(theTranslator->trRTFansicp()));
2198 if (!encoding.empty())
2199 {
2200 // convert from UTF-8 back to the output encoding
2201 void *cd = portable_iconv_open(encoding.data(),"UTF-8");
2202 if (cd!=reinterpret_cast<void *>(-1))
2203 {
2204 size_t iLeft=l;
2205 size_t oLeft=enc.size();
2206 const char *inputPtr = s.data();
2207 char *outputPtr = &enc[0];
2208 if (!portable_iconv(cd, &inputPtr, &iLeft, &outputPtr, &oLeft))
2209 {
2210 enc.resize(enc.size()-oLeft);
2211 converted=true;
2212 }
2214 }
2215 }
2216 if (!converted) // if we did not convert anything, copy as is.
2217 {
2218 memcpy(enc.data(),s.data(),l);
2219 enc.resize(l);
2220 }
2221 bool multiByte = false;
2222
2223 for (size_t i=0;i<enc.size();i++)
2224 {
2225 uint8_t c = static_cast<uint8_t>(enc.at(i));
2226
2227 if (c>=0x80 || multiByte)
2228 {
2229 char esc[10];
2230 snprintf(esc,10,"\\'%X",c); // escape sequence for SBCS and DBCS(1st&2nd bytes).
2231 t << esc;
2232
2233 if (!multiByte)
2234 {
2235 multiByte = isLeadBytes(c); // It may be DBCS Codepages.
2236 }
2237 else
2238 {
2239 multiByte = false; // end of Double Bytes Character.
2240 }
2241 }
2242 else
2243 {
2244 t << c;
2245 }
2246 }
2247}
virtual DString trRTFansicp()=0
const char * qPrint(const char *s)
Definition dstring.h:783
Translator * theTranslator
Definition language.cpp:76
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)
bool isLeadBytes(int c)
Definition rtfgen.cpp:2161

References DString::data(), DString::empty(), isLeadBytes(), DString::length(), portable_iconv(), portable_iconv_close(), portable_iconv_open(), qPrint(), DString::sprintf(), theTranslator, and Translator::trRTFansicp().

Referenced by preProcessFile().

◆ isLeadBytes()

bool isLeadBytes ( int c)

Definition at line 2161 of file rtfgen.cpp.

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

References theTranslator, and Translator::trRTFansicp().

Referenced by encodeForOutput().

◆ makeIndexName()

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

◆ objectLinkToString()

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

Definition at line 1552 of file rtfgen.cpp.

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

References Config_getBool, docifyToString(), DString::empty(), rtfFormatBmkStr(), and stripPath().

Referenced by RTFGenerator::writeInheritedSectionTitle(), and RTFGenerator::writeObjectLink().

◆ preProcessFile()

bool preProcessFile ( Dir & d,
const DString & 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 2253 of file rtfgen.cpp.

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

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

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

◆ rtfFormatBmkStr()

DString rtfFormatBmkStr ( const DString & name)

Definition at line 2880 of file rtfgen.cpp.

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

References g_nextTag, g_rtfFormatMutex, g_tagMap, DString::length(), Rtf_Style_Default::name, Debug::print(), DString::rawData(), Debug::Rtf, and DString::str().

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 DString & name)

Tests the integrity of the result by counting brackets.

Definition at line 2423 of file rtfgen.cpp.

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

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

Referenced by RTFGenerator::preProcessFileInplace().

Variable Documentation

◆ g_nextTag

DString g_nextTag("AAAAAAAAAA") ( "AAAAAAAAAA" )
static

Referenced by rtfFormatBmkStr().

◆ g_rtfFormatMutex

std::mutex g_rtfFormatMutex
static

Definition at line 2876 of file rtfgen.cpp.

Referenced by rtfFormatBmkStr().

◆ g_tagMap

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

Definition at line 2877 of file rtfgen.cpp.

Referenced by rtfFormatBmkStr().

◆ removeSet

StringSet removeSet
static

Definition at line 60 of file rtfgen.cpp.

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