template.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* template.ts :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: frfrey <frfrey@student.42lyon.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2020/07/22 10:41:27 by frfrey #+# #+# */
  9. /* Updated: 2020/07/22 10:41:27 by frfrey ### ########lyon.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. import * as vscode from 'vscode';
  13. import * as fs from 'fs';
  14. export const getTemplate = ( name: string | undefined, filePathCpp: vscode.Uri , filePathHpp: vscode.Uri ) => {
  15. if (name)
  16. {
  17. let len = name.length;
  18. len += 17;
  19. len = 80 - len - 1
  20. let star = "*"
  21. for (; len > 0; len--)
  22. {
  23. star += "*"
  24. }
  25. let classcpp = "#include \"" + name + ".hpp\"\n" +
  26. "\n/*\n** ------------------------------- CONSTRUCTOR --------------------------------\n*/\n\n" +
  27. name + "::" + name + "()\n{\n}\n\n" +
  28. name + "::" + name + "( const " + name + " & src )\n{\n}\n\n" +
  29. "\n/*\n** -------------------------------- DESTRUCTOR --------------------------------\n*/\n\n" +
  30. name + "::~" + name + "()\n{\n}\n\n" +
  31. "\n/*\n** --------------------------------- OVERLOAD ---------------------------------\n*/\n\n" +
  32. name + " & " + name + "::operator=( " + name + " const & rhs )\n{\n //if ( this != &rhs )\n" +
  33. " //{\n //this->_value = rhs.getValue();\n //}\n" +
  34. " return *this;\n}\n\n" +
  35. "std::ostream & operator<<( std::ostream & o, " + name + " const & i )\n" +
  36. "{\n //o << \"Value = \" << i.getValue();\n return o;\n}\n\n" +
  37. "\n/*\n** --------------------------------- METHODS ----------------------------------\n*/\n\n" +
  38. "\n/* ************************************************************************** */";
  39. let classhpp = "#ifndef " + name.toUpperCase() + "_HPP\n" +
  40. "# define " + name.toUpperCase() + "_HPP\n\n" +
  41. "# include <iostream>\n" +
  42. "# include <string>\n\n" +
  43. "class " + name + "\n{\n" +
  44. "\n" +
  45. " public:\n" +
  46. "\n" +
  47. " " + name + "();\n" +
  48. " " + name + "( " + name + " const & src );\n" +
  49. " ~" + name + "();\n" +
  50. "\n" +
  51. " " + name + " & operator=( " + name + " const & rhs );"+
  52. "\n\n" +
  53. " private:\n\n" +
  54. "};\n\n" +
  55. "std::ostream & operator<<( std::ostream & o, " + name + " const & i );"+
  56. "\n\n" +
  57. "#endif /* *" + star + " " + name.toUpperCase() + "_H */";
  58. fs.writeFile(filePathCpp.fsPath, classcpp, function (err: any) { if (err) return console.log(err); });
  59. fs.writeFile(filePathHpp.fsPath, classhpp, function (err: any) { if (err) return console.log(err); });
  60. vscode.window.showInformationMessage("Files : " + name + ".cpp, " + name + ".hpp created !");
  61. }
  62. }
  63. export const getTemplateTpp = ( name: string | undefined, filePathTpp: vscode.Uri ) => {
  64. if (name)
  65. {
  66. let len = name.length;
  67. len += 19;
  68. len = 80 - len - 1
  69. let star = "*"
  70. for (; len > 0; len--)
  71. {
  72. star += "*"
  73. }
  74. let classtpp = "#ifndef " + name.toUpperCase() + "_TPP\n" +
  75. "# define " + name.toUpperCase() + "_TPP\n\n" +
  76. "# include <iostream>\n" +
  77. "# include <string>\n\n" +
  78. "class " + name + "\n{\n" +
  79. "\n" +
  80. " public:\n" +
  81. "\n" +
  82. " " + name + "() {};\n" +
  83. " " + name + "( " + name + " const & src ) {};\n" +
  84. " ~" + name + "() {};\n" +
  85. "\n" +
  86. " " + name + " & operator=( " + name + " const & rhs )\n {\n //if ( this != &rhs )\n" +
  87. " //{\n //this->_value = rhs.getValue();\n //}\n" +
  88. " return *this;\n }\n\n" +
  89. "\n\n" +
  90. " private:\n\n" +
  91. "};\n\n" +
  92. "#endif /* *" + star + " " + name.toUpperCase() + "_TPP */";
  93. fs.writeFile(filePathTpp.fsPath, classtpp, function (err: any) { if (err) return console.log(err); });
  94. vscode.window.showInformationMessage("Files : " + name + ".tpp created !");
  95. }
  96. }
  97. export const getTemplateFull = ( name: string, filePathCpp: vscode.Uri, filePathHpp: vscode.Uri, valueArray: Array<string | undefined>, typeArray: Array<string | undefined> ) => {
  98. let len = name.length;
  99. len += 17;
  100. len = 80 - len - 1
  101. let star = "*"
  102. for (; len > 0; len--)
  103. {
  104. star += "*"
  105. }
  106. let classcpp = "#include \"" + name + ".hpp\"\n\n" +
  107. name + "::" + name + "()\n{\n}\n\n" +
  108. name + "::" + name + "( const " + name + " & object )\n{\n}\n\n" +
  109. name + "::~" + name + "()\n{\n std::cout << \"Destructor called\" << std::endl;\n}\n\n" +
  110. name + " & " + name + "::operator=( " + name + " const & rhs )\n{\n //if ( this != &rhs )\n" +
  111. " //this->_value = rhs.getValue();\n" +
  112. " return *this;\n}\n\n" +
  113. "std::ostream & operator<<( std::ostream & o, " + name + " const & i )\n" +
  114. "{\n //o << \"Value = \" << i.getValue();\n return o;\n}\n\n" +
  115. "\n/* ************************************************************************** */";
  116. let classhpp = "#ifndef " + name.toUpperCase() + "_HPP\n" +
  117. "# define " + name.toUpperCase() + "_HPP\n\n" +
  118. "# include <iostream>\n\n" +
  119. "class " + name + "\n{\n" +
  120. "\n" +
  121. " public:\n" +
  122. "\n" +
  123. " " + name + "();\n" +
  124. " " + name + "( " + name + " const & src );\n" +
  125. " ~" + name + "();\n" +
  126. "\n" +
  127. " " + name + " & operator=( " + name + " const & rhs );"+
  128. "\n\n" +
  129. " private:\n\n";
  130. for (var i = 0; i < valueArray.length; i++)
  131. {
  132. classhpp += " ";
  133. if (typeArray[i] == 'string')
  134. classhpp += "std::" + typeArray[i];
  135. else
  136. classhpp += typeArray[i];
  137. classhpp += " _" + valueArray[i] + ";\n";
  138. }
  139. classhpp += "};\n\n" +
  140. "std::ostream & operator<<( std::ostream & o, " + name + " const & i );"+
  141. "\n\n" +
  142. "#endif /* *" + star + " " + name.toUpperCase() + "_H */";
  143. fs.writeFile(filePathCpp.fsPath, classcpp, function (err: any) { if (err) return console.log(err); });
  144. fs.writeFile(filePathHpp.fsPath, classhpp, function (err: any) { if (err) return console.log(err); });
  145. vscode.window.showInformationMessage("Files : " + name + ".cpp, " + name + ".hpp created !");
  146. }