Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implementation of CHTMLFile. see HTML.h for details
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. //
  8. //------------------------------------------------------------------------------------------------------
  9. // $Log: $
  10. //
  11. // $NoKeywords: $
  12. //=============================================================================//
  13. #pragma warning (disable:4786)
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <string>
  18. #include "TFStatsApplication.h"
  19. #include "util.h"
  20. #include "html.h"
  21. //readability aids used when calling constructor
  22. const bool CHTMLFile::printBody=true;
  23. const bool CHTMLFile::dontPrintBody=false;
  24. const bool CHTMLFile::linkStyle=true;
  25. const bool CHTMLFile::dontLinkStyle=false;
  26. using namespace std;
  27. //------------------------------------------------------------------------------------------------------
  28. // Function: CHTMLFile::CHTMLFile
  29. // Purpose:
  30. // Input: filename - name of the html file that will be written
  31. // title - title of the html document
  32. // fPrintBody - true if the <body> tag is to be written.
  33. // bgimage - name of a background image, if desired
  34. // leftmarg - pixels on the left margin (if desired)
  35. // topmarg - pixels on the top margin (if desired)
  36. //------------------------------------------------------------------------------------------------------
  37. CHTMLFile::CHTMLFile(const char* filenm,const char* title,bool fPrintBody,const char* bgimage,int leftmarg, int topmarg)
  38. {
  39. strcpy(filename,filenm);
  40. open(filename);
  41. write("<HEAD>\n");
  42. write("<TITLE> %s </TITLE>\n",title);
  43. string csshttppath(g_pApp->supportHTTPPath);
  44. csshttppath+="/style.css";
  45. write("<link rel=\"stylesheet\" href=\"%s\" type=\"text/css\">\n",csshttppath.c_str());
  46. write("</HEAD>\n");
  47. fBody=fPrintBody;
  48. if (fBody)
  49. {
  50. write("<BODY leftmargin=%li topmargin=%li ",leftmarg,topmarg);
  51. if (bgimage)
  52. write("background=%s",bgimage);
  53. else
  54. write("bgcolor = black");
  55. write(">\n");
  56. }
  57. }
  58. //------------------------------------------------------------------------------------------------------
  59. // Function: CHTMLFile::open
  60. // Purpose: opens the html file, and writes <html>
  61. // Input: filename - the name of the file to open
  62. //------------------------------------------------------------------------------------------------------
  63. void CHTMLFile::open(const char* filename)
  64. {
  65. out=fopen(filename,"wt");
  66. if (!out)
  67. g_pApp->fatalError("Can't open output file \"%s\"!\nPlease make sure that the file does not exist OR\nif the file does exit, make sure it is not read-only",filename);
  68. write("<HTML>\n");
  69. }
  70. //------------------------------------------------------------------------------------------------------
  71. // Function: CHTMLFile::write
  72. // Purpose: writes a string to the html file
  73. // Input: fmt - format string, like printf suite of functions
  74. // ... - list of arguments
  75. //------------------------------------------------------------------------------------------------------
  76. void CHTMLFile::write(const char* fmt,...)
  77. {
  78. va_list va;
  79. va_start(va,fmt);
  80. vfprintf(out,fmt,va);
  81. }
  82. //------------------------------------------------------------------------------------------------------
  83. // Function: CHTMLFile::close
  84. // Purpose: closes the html file, closing <body> and <html> tags if needed
  85. //------------------------------------------------------------------------------------------------------
  86. void CHTMLFile::close()
  87. {
  88. if (!out)
  89. return;
  90. if (fBody)
  91. write("</BODY>\n");
  92. write("</HTML>\n\n");
  93. #ifndef WIN32
  94. chmod(filename,PERMIT);
  95. #endif
  96. fclose(out);
  97. out=NULL;
  98. }
  99. //------------------------------------------------------------------------------------------------------
  100. // Function: CHTMLFile::~CHTMLFile
  101. // Purpose: Destructor. closes the file
  102. //------------------------------------------------------------------------------------------------------
  103. CHTMLFile::~CHTMLFile()
  104. {
  105. close();
  106. }
  107. void CHTMLFile::hr(int len,bool alignleft)
  108. {
  109. write("<hr %s width=%li>\n",alignleft?"align=left":"",len);
  110. }