Debian Codegraph @

kwsGenerator.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsGenerator.cxx
5
6 Copyright (c) Kitware, Inc. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
12
13 =========================================================================*/
14 #include "kwsGenerator.h"
15 #include "kwsXMLReader.h"
16 #include <stdlib.h> // atoi
17 #include <string.h>
18
19 namespace kws {
20
21 struct DirectorySorting
22 {
23 bool operator()(const std::string& a,const std::string& b)
24 {
25 return a.compare(b) < 0;
26 }
27 };
28
29 struct FilenameSorting
30 {
31 bool operator()(const std::string& a,const std::string& b)
32 {
33 std::string nameA = kwssys::SystemTools::GetFilenameName(a);
34 std::string nameB = kwssys::SystemTools::GetFilenameName(b);
35 return nameA.compare(nameB) < 0;
36 }
37 };
38
39 /** Constructor */
40 Generator::Generator()
41 {
42 m_Parsers = nullptr;
43 m_ProjectTitle = "";
44 m_ProjectLogo = "Logo.gif";
45 m_KWStyleLogo = "kwstylelogo.jpg";
46 m_MaxDirectoryDepth = 99;
47 m_ErrorThreshold = 0;
48 }
49
50 /** Destructor */
51 Generator::~Generator() = default;
52
53 /** Read the configuration file */
54 void Generator::ReadConfigurationFile(const char* configFile)
55 {
56 kws::XMLReader reader;
57 if(reader.Open(configFile))
58 {
59 m_ProjectTitle = reader.GetValue("Project");
60 m_ProjectLogo = reader.GetValue("ProjectLogo");
61 m_KWStyleLogo = reader.GetValue("KWStyleLogo");
62 m_ErrorThreshold = atoi(reader.GetValue("ErrorThreshold").c_str());
63 reader.Close();
64 }
65 }
66
67 /** Generate the description of the test.
68 * For the moment we assume that all the test have the same description */
69 bool Generator::GenerateDescription(const char* dir)
70 {
71 std::cout << "Generating Description...";
72
73 std::string filename = dir;
74 if((dir[strlen(dir)-1] != '/') && dir[strlen(dir)-1] != '\\')
75 {
76 filename += "/";
77 }
78 filename += "KWSDescription.html";
79
80 std::ofstream file;
81 file.open(filename.c_str(), std::ios::binary | std::ios::out);
82 if(!file.is_open())
83 {
84 std::cout << "Cannot open file for writing: " << std::endl;
85 return false;
86 }
87
88 std::string title = filename;
89 if (!m_ProjectTitle.empty()) {
90 title = "Description for " + m_ProjectTitle;
91 }
92 this->CreateHeader(&file,title.c_str());
93
94 file << "<br />" << std::endl;
95
96 if (!m_Parsers->empty()) {
97 ParserVectorType::const_iterator it = m_Parsers->begin();
98 for(unsigned int i=0;i<NUMBER_ERRORS;i++)
99 {
100 if((*it).GetTestDescription(i) != "NA")
101 {
102 file << "<font color=\"" << ErrorColor[i] << "\">" << std::endl;
103 file << ErrorTag[i] << std::endl;
104 file << "</font> : " << std::endl;
105 file << (*it).GetTestDescription(i).c_str() << std::endl;
106 file << "<br />" << std::endl;
107 }
108 }
109 }
110
111 this->CreateFooter(&file);
112 file.close();
113
114 std::cout << "done" << std::endl;
115 return true;
116 }
117
118 /** Generate the Matrix representation */
119 bool Generator::GenerateMatrix(const char* dir,bool showAllErrors)
120 {
121 std::cout << "Generating Matrix...";
122
123 std::string filename = dir;
124 if((dir[strlen(dir)-1] != '/') && dir[strlen(dir)-1] != '\\')
125 {
126 filename += "/";
127 }
128 filename += "KWSMatrix.html";
129
130 std::ofstream file;
131 file.open(filename.c_str(), std::ios::binary | std::ios::out);
132 if(!file.is_open())
133 {
134 std::cout << "Cannot open file for writing: " << std::endl;
135 return false;
136 }
137
138 std::string title = filename;
139 if (!m_ProjectTitle.empty()) {
140 title = "Matrix for " + m_ProjectTitle;
141 }
142 this->CreateHeader(&file,title.c_str());
143
144 // Contruct the table
145 file << R"(<table width="100%" border="0" height="1">)" << std::endl;
146 file << "<tr>" << std::endl;
147 file << " <td width=\"10%\"> " << std::endl;
148 file << " <div align=\"center\">Filename</div>" << std::endl;
149 file << " </td>" << std::endl;
150
151 unsigned int i = 0;
152 // Check the number of tests that have been performed
153 int tests[NUMBER_ERRORS];
154 for(i=0;i<NUMBER_ERRORS;i++)
155 {
156 tests[i] = -1;
157 }
158
159 ParserVectorType::const_iterator it = m_Parsers->begin();
160 while(it != m_Parsers->end())
161 {
162 for(i=0;i<NUMBER_ERRORS;i++)
163 {
164 if((*it).HasBeenPerformed(i))
165 {
166 tests[i] = 0;
167 }
168 }
169 it++;
170 }
171 unsigned int nTests = 0;
172 for(i=0;i<NUMBER_ERRORS;i++)
173 {
174 if(tests[i]==0)
175 {
176 nTests++;
177 }
178 }
179
180 unsigned int width = 90/nTests;
181 for(i=0;i<NUMBER_ERRORS;i++)
182 {
183 if(tests[i]==-1)
184 {
185 continue;
186 }
187 file << " <td width=\"" << width << "%\">" << std::endl;
188 file << " <div align=\"center\">" << ErrorTag[i] << "</div>" << std::endl;
189 file << " </td>" << std::endl;
190 }
191 file << "</tr>" << std::endl;
192
193 bool gotErrors = false;
194
195 // The sorting is tricky.
196 std::vector<std::string> directories;
197 std::vector<std::string> filenames;
198 std::vector<std::string> singleFilenames;
199
200 it = m_Parsers->begin();
201 while(it != m_Parsers->end())
202 {
203 std::string localdir = kwssys::SystemTools::GetFilenamePath((*it).GetFilename());
204 if (!localdir.empty()) {
205 directories.push_back(localdir);
206 } else {
207 singleFilenames.push_back((*it).GetFilename());
208 }
209 filenames.push_back((*it).GetFilename());
210 it++;
211 }
212
213 // We sort first by directory
214 std::sort(directories.begin(),directories.end(),DirectorySorting());
215
216 // Then by filenames
217 std::sort(filenames.begin(),filenames.end(),FilenameSorting());
218 std::sort(singleFilenames.begin(),singleFilenames.end(),FilenameSorting());
219
220 // Add the singleFilenames first
221 std::vector<std::string> sortedFilenames;
222 std::vector<std::string>::const_iterator itSF = singleFilenames.begin();
223 while(itSF != singleFilenames.end())
224 {
225 sortedFilenames.push_back(*itSF);
226 itSF++;
227 }
228
229
230 // Then merge the two lists
231 std::vector<std::string>::const_iterator itDir = directories.begin();
232 std::string currentDir = "";
233 while(itDir != directories.end())
234 {
235 if(strcmp((*itDir).c_str(),currentDir.c_str()))
236 {
237 currentDir = *itDir;
238 std::vector<std::string>::const_iterator itFiles = filenames.begin();
239 while(itFiles != filenames.end())
240 {
241 if(!strcmp(kwssys::SystemTools::GetFilenamePath(*itFiles).c_str(),currentDir.c_str()))
242 {
243 sortedFilenames.push_back(*itFiles);
244 }
245 itFiles++;
246 }
247 }
248 itDir++;
249 }
250
251
252 std::vector<std::string>::const_iterator itSorted = sortedFilenames.begin();
253
254 std::string currentPath = "";
255 while(itSorted != sortedFilenames.end())
256 {
257 Parser parser;
258 // Find the current parser
259 it = m_Parsers->begin();
260 while(it != m_Parsers->end())
261 {
262 if(!strcmp((*it).GetFilename().c_str(),(*itSorted).c_str()))
263 {
264 parser = *it;
265 break;
266 }
267 it++;
268 }
269
270 if (parser.GetFilename().empty()) {
271 std::cout << "CANNOT FIND PARSER!" << std::endl;
272 continue;
273 }
274
275 if (!showAllErrors && parser.GetErrors().empty()) {
276 itSorted++;
277 continue;
278 }
279
280 gotErrors = true;
281
282 // If we have a new directory we show it on a new line
283 std::string filenamePath = kwssys::SystemTools::GetFilenamePath(parser.GetFilename());
284
285 if(currentPath != filenamePath)
286 {
287 file << "<tr>" << std::endl;
288 file << " <td width=\"10%\"></td>" << std::endl;
289 file << R"( <td bgcolor="#CCCCCC" width="70%" colspan=")" << nTests
290 << R"("><div align="">)" << std::endl;
291 ;
292 file << filenamePath.c_str() << std::endl;
293 file << "</div></td></tr>" << std::endl;
294 currentPath = filenamePath;
295 }
296
297 // Replace '/' by '_' (and strip any colon)
298 std::string localfilename = parser.GetFilename();
299 if(long int pos = localfilename.find(":/") != std::string::npos)
300 {
301 localfilename = localfilename.substr(pos+2,localfilename.size()-pos-2);
302 }
303 if(long int pos = localfilename.find(":\\") != std::string::npos)
304 {
305 localfilename = localfilename.substr(pos+2,localfilename.size()-pos-2);
306 }
307
308 auto slash = static_cast<long int>(localfilename.find_last_of("/"));
309 unsigned int index = 0;
310 while (slash != -1 && index < m_MaxDirectoryDepth) {
311 localfilename.replace(slash, 1, "_");
312 slash = static_cast<long int>(localfilename.find_last_of("/"));
313 index++;
314 }
315 slash = static_cast<long int>(localfilename.find_last_of("/"));
316 if(slash != -1)
317 {
318 localfilename = localfilename.substr(slash+1,localfilename.size()-slash-1);
319 }
320
321 slash = static_cast<long int>(parser.GetFilename().find_last_of("/"));
322
323 std::string nameofclass = parser.GetFilename().substr(slash+1,(parser.GetFilename().size())-slash-1);
324 std::string filenamecorrect = nameofclass;
325 localfilename += ".html";
326
327 // Fill in the table
328 file << "<tr>" << std::endl;
329 file << " <td width=\"10%\"> " << std::endl;
330 file << R"( <div align="center"> <a href=")" << localfilename.c_str()
331 << "\">" << filenamecorrect.c_str() << "</a></div>" << std::endl;
332 file << " </td>" << std::endl;
333
334 width = 90/nTests;
335 for(i=0;i<NUMBER_ERRORS;i++)
336 {
337 if(tests[i]==-1)
338 {
339 continue;
340 }
341
342 // Count the number of errors for this type of error
343 unsigned int nerror = 0;
344
345 Parser::ErrorVectorType errors = parser.GetErrors();
346 Parser::ErrorVectorType::const_iterator itError = errors.begin();
347 while(itError != errors.end())
348 {
349 if((*itError).number == i)
350 {
351 tests[i]++;
352 nerror++;
353 }
354 itError++;
355 }
356
357 file << " <td width=\"" << width << "%\"";
358 if(nerror == 0)
359 {
360 if(!(*it).HasBeenPerformed(i))
361 {
362 file << "bgcolor=\"#cccccc\"";
363 }
364 else
365 {
366 file << "bgcolor=\"#00aa00\"";
367 }
368 }
369 else if(nerror <= m_ErrorThreshold)
370 {
371 file << "bgcolor=\"#ffcc66\"";
372 }
373 else
374 {
375 file << "bgcolor=\"#ff6666\"";
376 }
377 file << ">" << std::endl;
378 file << " <div align=\"center\">" << nerror << "</div>" << std::endl;
379 file << " </td>" << std::endl;
380 }
381 file << "</tr>" << std::endl;
382
383 itSorted++;
384 }
385
386 // If we have error reporting we display a summary
387 if(gotErrors)
388 {
389 file << "<tr>" << std::endl;
390 file << " <td width=\"10%\"> " << std::endl;
391 file << " <div align=\"center\"><b>Summary</b></div>" << std::endl;
392 file << " </td>" << std::endl;
393
394 width = 90/nTests;
395 for (int nerror : tests) {
396 // Count the number of errors for this type of error
397 if (nerror == -1) {
398 continue;
399 }
400
401 file << " <td width=\"" << width << "%\"";
402 if(nerror == 0)
403 {
404 /*if(!(*it).HasBeenPerformed(i))
405 {
406 file << "bgcolor=\"#cccccc\"";
407 }
408 else
409 {*/
410 file << "bgcolor=\"#00aa00\"";
411 /*}*/
412 }
413 else if(nerror <= (int)m_ErrorThreshold)
414 {
415 file << "bgcolor=\"#ffcc66\"";
416 }
417 else
418 {
419 file << "bgcolor=\"#ff6666\"";
420 }
421 file << ">" << std::endl;
422 file << " <div align=\"center\"><b>" << nerror << "</b></div>" << std::endl;
423 file << " </td>" << std::endl;
424 }
425 file << "</tr>" << std::endl;
426 }
427 file << "</table>" << std::endl;
428
429 // If no error we say it
430 if(!gotErrors)
431 {
432 file << "<div align=\"center\"><b>No error reported</b></div>" << std::endl;
433 }
434
435 this->CreateFooter(&file);
436 file.close();
437
438 std::cout << "done" << std::endl;
439 return true;
440 }
441
442 /** Generate the HTML files */
443 bool Generator::GenerateHTML(const char* dir,bool showAllErrors)
444 {
445 // Check if the directory exists. If not create it
446 kwssys::Directory directory;
447 if(!directory.Load(dir))
448 {
449 std::cout << "Creating HTML directory: " << dir << std::endl;
450 if(!kwssys::SystemTools::MakeDirectory(dir))
451 {
452 std::cout << "Cannot create HTML directory: " << dir << std::endl;
453 }
454 }
455
456 std::string imagedir = dir;
457 imagedir += "/images";
458
459 // Copy the m_ProjectLogo and m_KWStyleLogo to the images directory
460 if (!m_ProjectLogo.empty() || !m_KWStyleLogo.empty()) {
461 if(!kwssys::SystemTools::MakeDirectory(imagedir.c_str()))
462 {
463 std::cout << "Cannot create images directory: "
464 << imagedir.c_str() << std::endl;
465 }
466 }
467
468 if (!m_ProjectLogo.empty()) {
469 std::string imageProjectLogo = imagedir;
470 imageProjectLogo += "/";
471 imageProjectLogo += kwssys::SystemTools::GetFilenameName(m_ProjectLogo.c_str());
472 if(!kwssys::SystemTools::CopyFileIfDifferent(m_ProjectLogo.c_str(),imageProjectLogo.c_str()))
473 {
474 std::cout << "Cannot copy the project logo file: "
475 << m_ProjectLogo.c_str() << " into "
476 << imageProjectLogo.c_str() << std::endl;
477 }
478 }
479
480 if (!m_KWStyleLogo.empty()) {
481 std::string imageKWStyleLogo = imagedir;
482 imageKWStyleLogo += "/";
483 imageKWStyleLogo += kwssys::SystemTools::GetFilenameName(m_KWStyleLogo.c_str());
484 if(!kwssys::SystemTools::CopyFileIfDifferent(m_KWStyleLogo.c_str(),imageKWStyleLogo.c_str()))
485 {
486 std::cout << "Cannot copy the KWStyle logo file: "
487 << m_KWStyleLogo.c_str() << " into "
488 << imageKWStyleLogo.c_str() << std::endl;
489 }
490 }
491
492 // Generate the matrix representation
493 this->GenerateMatrix(dir,showAllErrors);
494 this->GenerateDescription(dir);
495
496 std::cout << "Generating HTML...";
497
498 // For each file we generate an html page
499 ParserVectorType::const_iterator it = m_Parsers->begin();
500 while(it != m_Parsers->end())
501 {
502 if (!showAllErrors && (*it).GetErrors().empty()) {
503 it++;
504 continue;
505 }
506
507 std::ofstream file;
508
509 if ((*it).GetFilename().empty()) {
510 it++;
511 continue;
512 }
513
514 // Extract the filename
515 // Replace '/' by '_'
516 std::string filename = dir;
517 filename += "/";
518 std::string filename2 = (*it).GetFilename();
519 if(long int pos = filename2.find(":/") != std::string::npos)
520 {
521 filename2 = filename2.substr(pos+2,filename2.size()-pos-2);
522 }
523 if(long int pos = filename2.find(":\\") != std::string::npos)
524 {
525 filename2 = filename2.substr(pos+2,filename2.size()-pos-2);
526 }
527
528 auto slash = static_cast<long int>(filename2.find_last_of("/"));
529 unsigned int i = 0;
530 while (slash != -1 && i < m_MaxDirectoryDepth) {
531 filename2.replace(slash, 1, "_");
532 slash = static_cast<long int>(filename2.find_last_of("/"));
533 i++;
534 }
535 slash = static_cast<long int>(filename2.find_last_of("/"));
536 if(slash != -1)
537 {
538 filename2 = filename2.substr(slash+1,filename2.size()-slash-1);
539 }
540 filename += filename2;
541
542 filename += ".html";
543
544 file.open(filename.c_str(), std::ios::binary | std::ios::out);
545 if(!file.is_open())
546 {
547 std::cout << "Cannot open file for writing: " << std::endl;
548 return false;
549 }
550
551 this->CreateHeader(&file,filename.c_str());
552
553 file << R"(<table width="100%" border="0" height="1">)" << std::endl;
554
555 // To speedup the process we list the lines that have errors
556 using ErrorLineType = std::pair<long, std::vector<long>>;
557 std::vector<ErrorLineType> errorLines;
558
559 const Parser::ErrorVectorType errors = (*it).GetErrors();
560 auto itError = errors.begin();
561 while (itError != errors.end()) {
562 for (unsigned long index = (*itError).line; index <= (*itError).line2;
563 index++) {
564 ErrorLineType errLine;
565 errLine.first = index;
566
567 // Check if the line already exists
568 auto errorLineIt = errorLines.begin();
569 while (errorLineIt != errorLines.end()) {
570 if ((*errorLineIt).first == static_cast<int>(index)) {
571 break;
572 }
573 errorLineIt++;
574 }
575
576 if (errorLineIt != errorLines.end()) {
577 (*errorLineIt).second.push_back((*itError).number);
578 } else {
579 errLine.second.push_back((*itError).number);
580 errorLines.push_back(errLine);
581 }
582 }
583 itError++;
584 }
585
586 bool comment = false;
587 unsigned long nLines = (*it).GetNumberOfLines();
588 for(i=0;i<nLines;i++)
589 {
590 // Look in the errors if there is a match for this line
591 long error = -1;
592 std::string errorTag = "";
593
594 std::vector<ErrorLineType>::const_iterator errorLineIt = errorLines.begin();
595 while(errorLineIt != errorLines.end())
596 {
597 if((*errorLineIt).first == static_cast<int>(i+1))
598 {
599 auto err = (*errorLineIt).second.begin();
600 while(err != (*errorLineIt).second.end())
601 {
602 error = *err;
603 if (errorTag.empty()) {
604 errorTag += (*it).GetErrorTag(error);
605 } else {
606 errorTag += ",";
607 errorTag += (*it).GetErrorTag(error);
608 }
609 err++;
610 }
611 break;
612 }
613 errorLineIt++;
614 }
615
616 if(error>=0)
617 {
618 file << "<tr bgcolor=\"" << ErrorColor[error] << "\">" << std::endl;
619 }
620 else
621 {
622 file << "<tr>" << std::endl;
623 }
624
625 // First column is the line number
626 file << "<td height=\"1\">" << i+1 << "</td>" << std::endl;
627
628 // Second column is the error tag
629 file << "<td height=\"1\">" << errorTag.c_str() << "</td>" << std::endl;
630
631 std::string l = (*it).GetLine(i);
632
633 // If the error is of type INDENT we show the problem as *
634 if(errorTag.find("IND") != std::string::npos)
635 {
636 unsigned int k = 0;
637 while((l[k] == ' ') || (l[k] == '\n'))
638 {
639 if(l[k] == ' ')
640 {
641 l[k]='*';
642 }
643 k++;
644 }
645 }
646
647 // If the error is of type extra spaces we show the problem as *
648 if(errorTag.find("ESP") != std::string::npos)
649 {
650 int k = static_cast<int>(l.size())-1;
651 while(k>0 && (l[k] == ' '))
652 {
653 l[k]='*';
654 k--;
655 }
656 }
657
658 // Remove the first \n
659 auto p = static_cast<long int>(l.find('\n'));
660 if (p != -1) {
661 l.replace(p, 1, "");
662 }
663
664 // Replace < and >
665 auto inf = static_cast<long int>(l.find("<", 0));
666 while (inf != -1) {
667 l.replace(inf, 1, "&lt;");
668 inf = static_cast<long int>(l.find("<", 0));
669 }
670
671 auto sup = static_cast<long int>(l.find(">", 0));
672 while (sup != -1) {
673 l.replace(sup, 1, "&gt;");
674 sup = static_cast<long int>(l.find(">", 0));
675 }
676
677 // Replace the space by &nbsp;
678 auto space = static_cast<long int>(l.find(' ', 0));
679 while (space != -1) {
680 l.replace(space, 1, "&nbsp;");
681 space = static_cast<long int>(l.find(' ', space + 1));
682 }
683
684 // Show the comments in green
685 if(comment)
686 {
687 l.insert(0,"<font color=\"#009933\">");
688 }
689
690 // Show the comments in green
691 space = static_cast<long int>(l.find("//",0));
692 if(space != -1)
693 {
694 l.insert(space,"<font color=\"#009933\">");
695 l += "<font>";
696 }
697 else
698 {
699 space = static_cast<long int>(l.find("/*",0));
700 while(space != -1)
701 {
702 comment = true;
703 l.insert(space,"<font color=\"#009933\">");
704 space = static_cast<long int>(l.find("/*",space+23));
705 }
706
707 if(comment)
708 {
709 l.insert(l.size(),"</font>");
710 }
711
712 space = static_cast<long int>(l.find("*/",0));
713
714 while(space != -1)
715 {
716 comment = false;
717 l.insert(space+2,"</font>");
718 space = static_cast<long int>(l.find("*/",space+8));
719 }
720 }
721 file
722 << R"(<td height="1"><font face="Courier New, Courier, mono" size="2">)"
723 << l.c_str() << "</font></td>" << std::endl;
724 file << "</tr>" << std::endl;
725 }
726
727 file << "</table>" << std::endl;
728
729 this->CreateFooter(&file);
730
731 file.close();
732 it++;
733 }
734
735 std::cout << "done" << std::endl;
736 return true;
737 }
738
739 /** Create Header */
740 bool Generator::CreateHeader(std::ostream * file,const char* title)
741 {
742 *file << "<html>" << std::endl;
743 *file << "<head>" << std::endl;
744 *file << R"( <meta name="description" content="kitware.com" />)" << std::endl;
745 *file
746 << R"( <meta name="keywords" content="kwstyle,Kitware,Style,Checker,Dart" />)"
747 << std::endl;
748 *file << R"( <meta name="author" content="Kitware" />)" << std::endl;
749 *file << R"( <meta name="revisit-after" content="2 days" />)" << std::endl;
750 *file << R"( <meta name="robots" content="all" />)" << std::endl;
751 *file << " <title>KWStyle - " << title << "</title>" << std::endl;
752 *file << "</head>" << std::endl;
753
754 // Now create the top frame
755 *file << R"(<table width="100%" border="0">)" << std::endl;
756 *file << " <tr>" << std::endl;
757 *file << R"( <td width="15%" height="2"><img src="images/)"
758 << kwssys::SystemTools::GetFilenameName(m_ProjectLogo.c_str())
759 << "\"></td>" << std::endl;
760 *file << R"( <td width="85%" height="2" bgcolor="#0099CC"> )" << std::endl;
761
762 // remove the last extension
763 std::string tit = title;
764 auto pos = static_cast<long int>(tit.find_last_of("."));
765 if (pos != -1) {
766 tit = tit.substr(0, pos);
767 }
768 pos = static_cast<long int>(tit.find_last_of("/"));
769 if(pos!=-1)
770 {
771 tit = tit.substr(pos+1,tit.size()-pos-1);
772 }
773
774 *file
775 << R"( <div align="left"><b><font color="#FFFFFF" size="5">KWStyle - )"
776 << tit.c_str() << "</font></b></div>" << std::endl;
777 *file << " </td>" << std::endl;
778 *file << " </tr>" << std::endl;
779 *file << "</table>" << std::endl;
780 *file << R"(<table width="100%" border="0">)" << std::endl;
781 *file << " <tr> " << std::endl;
782 *file << R"( <td width="15%" height="30" >&nbsp;</td>)" << std::endl;
783 *file << R"( <td height="30" width="12%" bgcolor="#0099CC"> )"
784 << std::endl;
785 *file
786 << R"( <div align="center"><a href="KWSMatrix.html">Matrix View</a></div>)"
787 << std::endl;
788 *file << " </td>" << std::endl;
789 *file << R"( <td width="10%" height="30" bgcolor="#0099CC" >)"
790 << std::endl;
791 *file
792 << R"( <div align="center"><a href="KWSDescription.html">Description</a></div>)"
793 << std::endl;
794 *file << " </td>" << std::endl;
795 *file << R"( <td width="63%" height="30"> )" << std::endl;
796 *file << " <div align=\"left\"><b></b></div>" << std::endl;
797 *file << " <div align=\"right\"></div>" << std::endl;
798 *file << " </td>" << std::endl;
799 *file << " </tr>" << std::endl;
800 *file << "</table>" << std::endl;
801 *file << "<hr size=\"1\">";
802 return true;
803 }
804
805 /** Create Footer */
806 bool Generator::CreateFooter(std::ostream * file)
807 {
808 *file << "<hr size=\"1\">";
809 *file << R"(<table width="100%" border="0">)";
810 *file << "<tr>";
811 *file << "<td>Generated by <a href=\"https://public.kitware.com/KWStyle\">KWStyle</a> 1.0b on <i>" << kwssys::SystemTools::GetCurrentDateTime("%A %B,%d at %I:%M:%S%p") << "</i></td>";
812 *file << "<td>";
813 if (!m_KWStyleLogo.empty()) {
814 *file << R"(<div align="center"><img src="images/)"
815 << kwssys::SystemTools::GetFilenameName(m_KWStyleLogo.c_str())
816 << R"(" height="49" /></div>)" << std::endl;
817 }
818 *file << "</td>";
819 *file << "<td>";
820 *file
821 << R"(<div align="right"><a href="http://www.kitware.com">&copy; Kitware Inc.</a></div></td>)";
822 *file << "</tr>";
823 *file << "</table>";
824 *file << "<br />";
825
826 *file << "</html>" << std::endl;
827
828 return true;
829 }
830
831 /** Export the HTML report */
832 void Generator::ExportHTML(std::ostream & output)
833 {
834 // For each file we generate an html page
835 ParserVectorType::const_iterator it = m_Parsers->begin();
836 while(it != m_Parsers->end())
837 {
838 if ((*it).GetFilename().empty()) {
839 it++;
840 continue;
841 }
842
843 // Extract the filename
844 std::string filename = "";
845 long int slash = static_cast<long int>((*it).GetFilename().find_last_of("/"));
846 if(slash == -1)
847 {
848 slash = 0;
849 }
850 std::string nameofclass = (*it).GetFilename().substr(slash+1,((*it).GetFilename().size())-slash-1);
851 filename += nameofclass;
852 filename += ".html";
853
854 //this->CreateHeader(&output,filename.c_str());
855
856 output << R"(<table width="100%" border="0" height="1">)" << std::endl;
857
858 bool comment = false;
859 for(unsigned int i=0;i<(*it).GetNumberOfLines();i++)
860 {
861 // Look in the errors if there is a match for this line
862 long error = -1;
863 std::string errorTag = "";
864
865 const Parser::ErrorVectorType errors = (*it).GetErrors();
866 auto itError = errors.begin();
867 while(itError != errors.end())
868 {
869 if( ((i+1>=(*itError).line) && (i+1<=(*itError).line2))
870 )
871 {
872 if (errorTag.empty()) {
873 errorTag += (*it).GetErrorTag((*itError).number);
874 } else {
875 errorTag += ",";
876 errorTag += (*it).GetErrorTag((*itError).number);
877 }
878 error = (*itError).number;
879 }
880 itError++;
881 }
882
883 if(error>=0)
884 {
885 output << "<tr bgcolor=\"" << ErrorColor[error] << "\">" << std::endl;
886 }
887 else
888 {
889 output << "<tr>" << std::endl;
890 }
891
892 // First column is the line number
893 output << "<td height=\"1\">" << i+1 << "</td>" << std::endl;
894
895 // Second column is the error tag
896 output << "<td height=\"1\">" << errorTag.c_str() << "</td>" << std::endl;
897
898 std::string l = (*it).GetLine(i);
899
900 // If the error is of type INDENT we show the problem as _
901 if(errorTag.find("IND") != std::string::npos)
902 {
903 unsigned int k = 0;
904 while((l[k] == ' ') || (l[k] == '\n'))
905 {
906 if(l[k] == ' ')
907 {
908 l[k]='*';
909 }
910 k++;
911 }
912 }
913
914 // Remove the first \n
915 auto p = static_cast<long int>(l.find('\n'));
916 if (p != -1) {
917 l.replace(p, 1, "");
918 }
919
920 // Replace < and >
921 auto inf = static_cast<long int>(l.find("<", 0));
922 while (inf != -1) {
923 l.replace(inf, 1, "&lt;");
924 inf = static_cast<long int>(l.find("<", 0));
925 }
926
927 auto sup = static_cast<long int>(l.find(">", 0));
928 while (sup != -1) {
929 l.replace(sup, 1, "&gt;");
930 sup = static_cast<long int>(l.find(">", 0));
931 }
932
933 // Replace the space by &nbsp;
934 auto space = static_cast<long int>(l.find(' ', 0));
935 while (space != -1) {
936 l.replace(space, 1, "&nbsp;");
937 space = static_cast<long int>(l.find(' ', space + 1));
938 }
939
940 // Show the comments in green
941 if(comment)
942 {
943 l.insert(0,"<font color=\"#009933\">");
944 }
945
946 // Show the comments in green
947 space = static_cast<long int>(l.find("//",0));
948 if(space != -1)
949 {
950 l.insert(space,"<font color=\"#009933\">");
951 l += "<font>";
952 }
953 else // if we have a line like // */ this is a single line comment
954 {
955 space = static_cast<long int>(l.find("/*",0));
956 while(space != -1)
957 {
958 comment = true;
959 l.insert(space,"<font color=\"#009933\">");
960 space = static_cast<long int>(l.find("/*",space+23));
961 }
962
963 if(comment)
964 {
965 l.insert(l.size(),"</font>");
966 }
967
968 space = static_cast<long int>(l.find("*/",0));
969 while(space != -1)
970 {
971 comment = false;
972 l.insert(space+2,"</font>");
973 space = static_cast<long int>(l.find("*/",space+8));
974 }
975 }
976
977 output
978 << R"(<td height="1"><font face="Courier New, Courier, mono" size="2">)"
979 << l.c_str() << "</font></td>" << std::endl;
980 output << "</tr>" << std::endl;
981 }
982
983 output << "</table>" << std::endl;
984
985 this->CreateFooter(&output);
986 it++;
987 }
988 }
989
990 /** Generate dart files */
991 bool Generator::GenerateDart(const char* dir,int maxError,
992 bool group,std::string url,
993 double time,std::string basedirectory)
994 {
995 std::cout << "Generating Dart...";
996
997 // We try to find the TAG file
998 std::string dirname = dir;
999 if(dir[strlen(dir)-1] != '/' && dir[strlen(dir)-1] != '\\')
1000 {
1001 dirname += "/";
1002 }
1003 dirname += "Testing/";
1004
1005 std::string tag = dirname+"TAG";
1006
1007 std::ifstream tagfile;
1008 tagfile.open(tag.c_str(), std::ios::binary | std::ios::in);
1009 if(!tagfile.is_open())
1010 {
1011 std::cout << "Cannot open file for reading: " << tag.c_str() << std::endl;
1012 return false;
1013 }
1014
1015 // We read the first line of the file which gives the path to the actual
1016 // directory
1017 char* bufferTag = new char[255];
1018 tagfile.getline(bufferTag,255);
1019
1020 if(bufferTag[strlen(bufferTag)-1] == '\r')
1021 {
1022 bufferTag[strlen(bufferTag)-1] = '\0';
1023 }
1024
1025 dirname += bufferTag;
1026 dirname += "/";
1027
1028 delete [] bufferTag;
1029
1030 // We should have a Configure.xml file in the directory
1031 std::string configname = dirname+"Configure.xml";
1032
1033 std::ifstream configfile;
1034
1035 configfile.open(configname.c_str(), std::ios::binary | std::ios::in);
1036 if(!configfile.is_open())
1037 {
1038 std::cout << "Cannot open file for reading: " << configname.c_str() << std::endl;
1039 return false;
1040 }
1041
1042 // Create the Build.xml file
1043 std::ofstream file;
1044 std::string filename = dirname+"Build.xml";
1045
1046 file.open(filename.c_str(), std::ios::binary | std::ios::out);
1047 if(!file.is_open())
1048 {
1049 std::cout << "Cannot open file for writing: " << std::endl;
1050 return false;
1051 }
1052
1053 // Generate the header
1054 char* buffer = new char[255];
1055 configfile.getline(buffer,255);
1056 file << buffer << std::endl;
1057 configfile.getline(buffer,255);
1058 file << buffer << std::endl;
1059
1060 std::string currentLine = buffer;
1061 while(currentLine.find(">") == std::string::npos)
1062 {
1063 configfile.getline(buffer,255);
1064 currentLine = buffer;
1065 file << buffer << std::endl;
1066 }
1067
1068 delete [] buffer;
1069
1070 file << "<Build>" << std::endl;
1071 file << " <StartDateTime>";
1072 file << kwssys::SystemTools::GetCurrentDateTime("%b %d %H:%M:%S %z");
1073 file << "</StartDateTime>" << std::endl;
1074 file << " <StartBuildTime>";
1075 file << static_cast<unsigned int>(kwssys::SystemTools::GetTime());
1076 file << "</StartBuildTime>" << std::endl;
1077 file << "<BuildCommand>KWStyle</BuildCommand>" << std::endl;
1078
1079 int nErrors = 0;
1080 ParserVectorType::const_iterator it = m_Parsers->begin();
1081 while(it != m_Parsers->end())
1082 {
1083 std::string sourcefile = (*it).GetFilename();
1084 if (!basedirectory.empty()) {
1085 sourcefile = sourcefile.substr(basedirectory.size());
1086 }
1087
1088 bool first = true;
1089 const Parser::ErrorVectorType errors = (*it).GetErrors();
1090 auto itError = errors.begin();
1091 while(itError != errors.end())
1092 {
1093 if((!group) || (first && group))
1094 {
1095 file << "<Error>" << std::endl;
1096 file << " <BuildLogLine>1</BuildLogLine>" << std::endl;
1097 file << " <SourceFile>";
1098 file << sourcefile.c_str();
1099 file << "</SourceFile>" << std::endl;
1100 file << " <SourceLineNumber>";
1101 file << (*itError).line;
1102 file << "</SourceLineNumber>" << std::endl;
1103
1104 if (!url.empty()) {
1105 // We had a link to the dashboard
1106 /*long int posslash = (*it).GetFilename().find_last_of("/");
1107 long int posbackslash = (*it).GetFilename().find_last_of("\\");
1108
1109 long int pos = 0;
1110 if(posslash != -1 && posslash>posbackslash)
1111 {
1112 pos = posslash;
1113 }
1114
1115 if(posbackslash != -1 && posbackslash>posslash)
1116 {
1117 pos = posbackslash;
1118 }
1119
1120 if(pos == 0)
1121 {
1122 pos++;
1123 }
1124 std::string htmlfile = (*it).GetFilename().substr(pos+1,(*it).GetFilename().size()-pos-1);
1125 */
1126
1127 std::string htmlfile = (*it).GetFilename();
1128 if(long int pos = htmlfile.find(":/") != std::string::npos)
1129 {
1130 htmlfile = htmlfile.substr(pos+2,htmlfile.size()-pos-2);
1131 }
1132 if(long int pos = htmlfile.find(":\\") != std::string::npos)
1133 {
1134 htmlfile = htmlfile.substr(pos+2,htmlfile.size()-pos-2);
1135 }
1136
1137 auto slash = static_cast<long int>(htmlfile.find_last_of("/"));
1138 unsigned int i = 0;
1139 while (slash != -1 && i < m_MaxDirectoryDepth) {
1140 htmlfile.replace(slash, 1, "_");
1141 slash = static_cast<long int>(htmlfile.find_last_of("/"));
1142 i++;
1143 }
1144 slash = static_cast<long int>(htmlfile.find_last_of("/"));
1145 if(slash != -1)
1146 {
1147 htmlfile = htmlfile.substr(slash+1,htmlfile.size()-slash-1);
1148 }
1149
1150 htmlfile += ".html";
1151 file << "<Url>"<< url.c_str() << "/" << htmlfile.c_str() << "</Url>" << std::endl;
1152 file << "<UrlName>View KWStyle File</UrlName>" << std::endl;
1153 }
1154 first = false;
1155 file << " <Text>";
1156 }
1157
1158 file << sourcefile.c_str();
1159 file << " : ";
1160 file << (*it).GetErrorTag((*itError).number);
1161 file << " : ";
1162 std::string desc = (*itError).description;
1163 auto pos = static_cast<long int>(desc.find("&", 0));
1164 while(pos != -1)
1165 {
1166 desc.replace(pos,1,"&amp;");
1167 pos = static_cast<long int>(desc.find("&",pos+3));
1168 }
1169 pos = static_cast<long int>(desc.find("<"));
1170 while(pos != -1)
1171 {
1172 desc.replace(pos,1,"&#x03C;");
1173 pos = static_cast<long int>(desc.find("<"));
1174 }
1175 pos = static_cast<long int>(desc.find(">"));
1176 while(pos != -1)
1177 {
1178 desc.replace(pos,1,"&#x03E;");
1179 pos = static_cast<long int>(desc.find(">"));
1180 }
1181
1182 desc += " ["+(*it).GetTestDescription((*itError).number)+"] (";
1183 constexpr size_t length = 255;
1184 char* val = new char[length];
1185 snprintf(val,length,"%ld",(*itError).line);
1186 desc += val;
1187 desc += ")\n";
1188 delete [] val;
1189
1190 file << desc;
1191
1192 if(!group)
1193 {
1194 file << "</Text>" << std::endl;
1195 // Show the actual error in the precontext
1196 file << " <PreContext>";
1197 //file << (*it).GetTestDescription((*itError).number) << std::endl;
1198 file << "</PreContext>" << std::endl;
1199 file << "<PostContext>";
1200 file << "</PostContext>" << std::endl;
1201 file << "<RepeatCount>0</RepeatCount>" << std::endl;
1202 file << "</Error>" << std::endl;
1203 }
1204 itError++;
1205 nErrors++;
1206 if((maxError != -1) && (nErrors >= maxError))
1207 {
1208 break;
1209 }
1210 }
1211
1212 if(group && !first)
1213 {
1214 file << "</Text>" << std::endl;
1215 // Show the actual error in the precontext
1216 file << " <PreContext>";
1217 //file << (*it).GetTestDescription((*itError).number) << std::endl;
1218 file << "</PreContext>" << std::endl;
1219 file << "<PostContext>";
1220 file << "</PostContext>" << std::endl;
1221 file << "<RepeatCount>0</RepeatCount>" << std::endl;
1222 file << "</Error>" << std::endl;
1223 }
1224
1225 if((maxError != -1) && (nErrors >= maxError))
1226 {
1227 break;
1228 }
1229 it++;
1230 }
1231
1232 // Write the footer
1233 file << R"( <Log Encoding="base64" Compression="/bin/gzip">)" << std::endl;
1234 file << " </Log>" << std::endl;
1235 file << " <EndDateTime>";
1236 file << kwssys::SystemTools::GetCurrentDateTime("%b %d %I:%M:%S %z");
1237 file << "</EndDateTime>" << std::endl;
1238
1239 double time1 = kwssys::SystemTools::GetTime();
1240
1241 constexpr size_t length = 10;
1242 char *timestr = new char[length];
1243 snprintf(timestr, length, "%.1f", (time1 - time) / 60.0);
1244
1245 file << " <ElapsedMinutes>" << timestr << "</ElapsedMinutes></Build>"
1246 << std::endl;
1247 file << "</Site>" << std::endl;
1248
1249 delete[] timestr;
1250 configfile.close();
1251 file.close();
1252 std::cout << "Done." << std::endl;
1253 return true;
1254 }
1255
1256 /** Generate a simple XML report of the errors */
1257 bool Generator::ExportXML(const char* filename)
1258 {
1259 std::ofstream file;
1260
1261 file.open(filename, std::ios::binary | std::ios::out);
1262 if(!file.is_open())
1263 {
1264 std::cout << "Cannot open file for writing: " << std::endl;
1265 return false;
1266 }
1267
1268
1269 ParserVectorType::const_iterator it = m_Parsers->begin();
1270 while(it != m_Parsers->end())
1271 {
1272 const Parser::ErrorVectorType errors = (*it).GetErrors();
1273 auto itError = errors.begin();
1274 while(itError != errors.end())
1275 {
1276 file << "<Error>" << std::endl;
1277 file << " <SourceLineNumber1>" << (*itError).line << "</SourceLineNumber1>" << std::endl;
1278 file << " <SourceLineNumber2>" << (*itError).line2 << "</SourceLineNumber2>" << std::endl;
1279 file << " <ErrorNumber>" << (*itError).number << "</ErrorNumber>" << std::endl;
1280 file << "</Error>" << std::endl;
1281 itError++;
1282 }
1283 it++;
1284 }
1285
1286 file.close();
1287
1288 return true;
1289 }
1290
1291
1292 } // end namespace kws