Advertisement
QuantumWarpCode

Output.cpp

Jul 23rd, 2015
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. void Output::show()
  4. {
  5. #ifdef _WIN32
  6.     HWND hWnd = GetConsoleWindow();
  7.     ShowWindow(hWnd, SW_SHOW);
  8.     return;
  9. #else
  10.     Output::expectedError("No linux equivalent of SW_SHOW", 502);
  11.     return;
  12. #endif
  13. }
  14.  
  15. void Output::hide()
  16. {
  17. #ifdef _WIN32
  18.     HWND hWnd = GetConsoleWindow();
  19.     ShowWindow(hWnd, SW_HIDE);
  20.     return;
  21. #else
  22.     Output::expectedError("No linux equivalent of SW_HIDE", 503);
  23.     return;
  24. #endif
  25. }
  26.  
  27. void Output::end()
  28. {
  29. #ifdef _WIN32
  30.     Output::show();
  31. #endif
  32. #ifdef _MSC_VER
  33.     std::cout << "Press any key to continue..." << std::endl;
  34.     _getch();
  35.     return;
  36. #else
  37. #   ifdef __MINGW32__
  38.     std::cout << "Press any key to continue..." << std::endl;
  39.     _getch();
  40.     return
  41. #   else
  42.     std::cout << "Press the enter key to continue..." << std::endl;
  43.     std::cin.get();
  44.     return;
  45. #   endif
  46. #endif
  47. }
  48.  
  49. void Output::header(char* text)
  50. {
  51.     std::cout << "=====" << text << "=====" << std::endl;
  52.     return;
  53. }
  54.  
  55. void Output::log(char* text, char* log)
  56. {
  57.     std::ofstream logfile;
  58.     logfile.open(log + std::string(".log"), std::ios::app);
  59.     logfile << text << std::endl;
  60.     logfile.close();
  61.     return;
  62. }
  63.  
  64. void Output::log(const char* text, char* log)
  65. {
  66.     std::ofstream logfile;
  67.     logfile.open(log + std::string(".log"), std::ios::app);
  68.     logfile << text << std::endl;
  69.     logfile.close();
  70.     return;
  71. }
  72.  
  73. void Output::log(std::string text, char* log)
  74. {
  75.     std::ofstream logfile;
  76.     logfile.open(log + std::string(".log"), std::ios::app);
  77.     logfile << text << std::endl;
  78.     logfile.close();
  79.     return;
  80. }
  81.  
  82. void Output::clear(char* log)
  83. {
  84.     std::ofstream logfile;
  85.     logfile.open(log + std::string(".log"), std::ios::trunc);
  86.     logfile.close();
  87.     return;
  88. }
  89.  
  90. void Output::criticalError(char* text, int error){
  91.     std::cerr << text << "... Exiting with error code:" << error << std::endl;
  92.     Output::log(text, "CriticalError");
  93.     Output::endNoMessage();
  94.     return;
  95. }
  96.  
  97. void Output::expectedError(char* text, int error){
  98.     std::cerr << text << "...  Exception with error code:" << error << std::endl;
  99.     Output::log(text, "ExpectedError");
  100.     return;
  101. }
  102.  
  103. void Output::endNoMessage()
  104. {
  105. #ifdef _WIN32
  106.     Output::show();
  107. #endif
  108. #ifdef _MSC_VER
  109.     _getch();
  110.     return;
  111. #else
  112. #   ifdef __MINGW32__
  113.     _getch();
  114.     return
  115. #   else
  116.     std::cin.get();
  117.     return;
  118. #   endif
  119. #endif
  120. }
  121.  
  122. void Output::systemCommand(int command)
  123. {
  124. #ifdef __linux__
  125.     if (command == 0){
  126.         std::system("shutdown");
  127.         return;
  128.     }
  129.     else if (command == 1){
  130.         Output::expectedError("No pause command for linux", 501);
  131.         return;
  132.     }
  133. #endif
  134. #ifdef _WIN32
  135.     if (command == 0){
  136.         std::system("shutdown.exe");
  137.         return;
  138.     }
  139.     else if (command == 1){
  140.         std::system("pause");
  141.         return;
  142.     }
  143. #endif
  144. }
  145.  
  146. wchar_t* Output::ExePath(void /*char* relpath*/) {
  147.     wchar_t buffer[MAX_PATH];
  148.     GetModuleFileName(NULL, buffer, MAX_PATH);
  149.     //_fullpath(buffer, relpath);
  150.     /*
  151.     std::string::size_type pos = std::string(buffer).find_last_of("\\/");
  152.     return std::string(buffer).substr(0, pos);
  153.     */
  154.     return buffer;
  155. }
  156.  
  157. std::string Output::readFile(char* filename) {
  158.     FILE* infile;
  159.     int tempint;
  160.     tempint = fopen_s(&infile, filename, "r");
  161.     if (tempint != 0) {
  162.         std::cout << "Error " << tempint << " when reading file " << filename << std::endl;
  163.         return "Error reading file.";
  164.     }
  165.  
  166.     boolean loop = true;
  167.     int loops = 0;
  168.     char temptext[2];
  169.     std::string returntext;
  170.     int index = 0;
  171.     int length = 0;
  172.  
  173.     while (loop) {
  174.         fgets(temptext, 2, infile);
  175.         if (temptext != NULL && std::feof(infile) != 1) {
  176.             if (loops == 0) {
  177.                 returntext = temptext;
  178.             }
  179.             else {
  180.                 returntext += temptext;
  181.             }
  182.             loops++;
  183.             length++;
  184.         }
  185.         else {
  186.             loop = false;
  187.         }
  188.  
  189.     }
  190.  
  191.     fclose(infile);
  192.     return returntext;
  193. }
  194.  
  195. std::string Output::readFile(std::string filename) {
  196.     FILE* infile;
  197.     int tempint;
  198.     tempint = fopen_s(&infile, filename.c_str(), "r");
  199.     if (tempint != 0) {
  200.         std::cout << "Error " << tempint << " when reading file " << filename << std::endl;
  201.         return "Error reading file.";
  202.     }
  203.  
  204.     boolean loop = true;
  205.     int loops = 0;
  206.     char temptext[2];
  207.     std::string returntext;
  208.     int index = 0;
  209.     int length = 0;
  210.  
  211.     while (loop) {
  212.         fgets(temptext, 2, infile);
  213.         if (temptext != NULL && std::feof(infile) != 1) {
  214.             if (loops == 0) {
  215.                 returntext = temptext;
  216.             }
  217.             else {
  218.                 returntext += temptext;
  219.             }
  220.             loops++;
  221.             length++;
  222.         }
  223.         else {
  224.             loop = false;
  225.         }
  226.  
  227.     }
  228.  
  229.     fclose(infile);
  230.     return returntext;
  231. }
  232.  
  233. std::string Output::fileStringFix(std::string string) {
  234.     char* slash = "/";
  235.     char* rslash = "\\";
  236.     int rslashI = 92;
  237.  
  238.     if (string.find_first_of(slash[0]) != 2 && string.find_first_of(rslash[0]) != 2) {
  239.         std::cout << "Appending C:\\ as drive directory." << std::endl;
  240.         std::string newString = "C:\\";
  241.         newString += string;
  242.         string = newString;
  243.     }
  244.  
  245.     int i = 0;
  246.     while (i < string.length()) {
  247.         if (Output::charToInt(string[i]) == Output::charToInt(slash[0])) {
  248.             string[i] = intToChar(rslashI);
  249.         }
  250.         i++;
  251.     }
  252.     return string;
  253. }
  254.  
  255. int Output::charToInt(char letter) {
  256.     int number;
  257.     number = letter;
  258.     return number;
  259. }
  260.  
  261. char Output::intToChar(unsigned int number) {
  262.     int letter;
  263.     letter = number;
  264.     return letter;
  265. }
  266.  
  267. std::string* SPML::readStringToArray(std::string inString) {
  268.     char temp = intToChar(0);
  269.     std::string* sArray = { "", "" };
  270.     std::string space = " ";
  271.     std::string newline = "\n";
  272.     std::string colon = ":";
  273.  
  274.     int arrayIndex = 0;
  275.     int stringIndex = 0;
  276.  
  277.     int i = 0;
  278.     while (i < inString.length()) {
  279.         temp = inString[i];
  280.         if (temp != space[0] && temp != newline[0] && temp != colon[0]) {
  281.             sArray[arrayIndex][stringIndex] = temp;
  282.             stringIndex += 1;
  283.         }
  284.         else if(temp == colon[0]) {
  285.             arrayIndex += 1;
  286.         }
  287.         else if (temp == newline[0]) {
  288.             stringIndex = 0;
  289.             arrayIndex += 1;
  290.         }
  291.     }
  292.  
  293.     return sArray;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement