page.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <fstream>
  2. #include <string>
  3. #include "page.h"
  4. using namespace std;
  5. #define INDEX_FOOTER1 "\n\t<footer>\n\t<ul>\n\t<li>"
  6. #define INDEX_FOOTER2 "\n\t<li><a href=\"manifiesto.html\">Manifiesto</a>\n\t<li><a href=\"en/index.html\">English</a>\n\t</ul>\n\t</footer>\n\t</div>\n</body>\n</html>"
  7. #define INDEX_FOOTER2_EN "\n\t<li><a href=\"manifesto.html\">Manifesto</a>\n\t<li><a href=\"../index.html\">Español</a>\n\t</ul>\n\t</footer>\n\t</div>\n</body>\n</html>"
  8. page::~page(){}
  9. page::page(){}
  10. /*
  11. * This constructor done index pages. Index pages have a different
  12. * footer with the version.
  13. * root:
  14. * 0: root directory
  15. * 1: relative directories
  16. * language:
  17. * 0: spanish
  18. * 1: english
  19. */
  20. page::page(string name, int language, int root, string version){
  21. this->name=name;
  22. string aux;
  23. if(language == 1){
  24. aux="en/";
  25. this->footer=INDEX_FOOTER1 + version + INDEX_FOOTER2_EN;
  26. }else{
  27. this->footer=INDEX_FOOTER1 + version + INDEX_FOOTER2;
  28. }
  29. if(root == 0){
  30. aux+="relative";
  31. }else{
  32. aux+="root";
  33. }
  34. this->header=load(HEAD + aux);
  35. this->body=load(BODY + name);
  36. }
  37. /*
  38. *
  39. * Constructor for every page.
  40. * root:
  41. * 0: root directory
  42. * 1: relative directories
  43. * language:
  44. * 0: Spanish
  45. * 1: English
  46. */
  47. page::page(string name, int language, int root){
  48. this->name=name;
  49. string aux;
  50. if(language == 1){
  51. aux="en/";
  52. }
  53. if(root == 0){
  54. aux+="relative";
  55. }else{
  56. aux+="root";
  57. }
  58. this->header=load(HEAD + aux);
  59. this->footer=load(FOOT + aux);
  60. this->body=load(BODY + name);
  61. }
  62. page& page::operator =(const page& P)
  63. {
  64. if (this != &P) { // avoid avoid self-allocation
  65. name=P.name;
  66. header=P.header;
  67. body=P.body;
  68. footer=P.footer;
  69. }
  70. return *this;
  71. }
  72. /*
  73. * 1.- Check if string constructor doesn't set garbage
  74. * 2.- If fstream can use string variable
  75. */
  76. string page::load(string file){
  77. ifstream reader(file);
  78. string line;
  79. string loaded_text;
  80. while(getline(reader,line))
  81. loaded_text+=line + "\n";
  82. reader.close();
  83. return loaded_text;
  84. }
  85. void page::save(string text){
  86. ofstream writer(LASTFOLDER + this->name +".html");
  87. writer << text;
  88. writer.close();
  89. }