Advertisement
MargaritaOwl

GetEnvironmentVariables

Feb 9th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <string>
  4. #include <windows.h>
  5. using namespace std;
  6.  
  7. string urlDecode(const string &SRC) {
  8.     string result;
  9.     char ch;
  10.     int i, ii;
  11.     for (i = 0; i<SRC.length(); i++) {
  12.         if (int(SRC[i]) == 37) {
  13.             sscanf(SRC.substr(i + 1, 2).c_str(), "%x", &ii);
  14.             ch = static_cast<char>(ii);
  15.             result += ch;
  16.             i = i + 2;
  17.         }
  18.         else {
  19.             result += SRC[i];
  20.         }
  21.     }
  22.     return result;
  23. }
  24.  
  25. string cp1251_to_utf8(const char *str)
  26. {
  27.     string res;
  28.     int result_u, result_c;
  29.  
  30.     result_u = MultiByteToWideChar(1251, 0, str, -1, 0, 0);
  31.  
  32.     if (!result_u)
  33.         return 0;
  34.  
  35.     wchar_t *ures = new wchar_t[result_u];
  36.  
  37.     if (!MultiByteToWideChar(1251, 0, str, -1, ures, result_u)) {
  38.         delete[] ures;
  39.         return 0;
  40.     }
  41.  
  42.  
  43.     result_c = WideCharToMultiByte(CP_UTF8, 0, ures, -1, 0, 0, 0, 0);
  44.  
  45.     if (!result_c) {
  46.         delete[] ures;
  47.         return 0;
  48.     }
  49.  
  50.     char *cres = new char[result_c];
  51.  
  52.     if (!WideCharToMultiByte(CP_UTF8, 0, ures, -1, cres, result_c, 0, 0)) {
  53.         delete[] cres;
  54.         return 0;
  55.     }
  56.     delete[]ures;
  57.     res.append(cres);
  58.     delete[]cres;
  59.     return res;
  60. }
  61.  
  62. int main()
  63. {
  64.     cout << cp1251_to_utf8("Content-Type: text/html\n\n<HTML><HEAD><meta charset='UTF-8'><TITLE>Ответ на запрос</TITLE><link rel='stylesheet' href='lab1-4.css'></HEAD>\n<body><div>\n");
  65.     cout << cp1251_to_utf8("<p style='line-height:100%; margin-left:10%;' align='center'><span style='font-size:14pt;'>Cgi-скрипт на С++.</span></p>\n");
  66.     char tempQuery[4096];
  67.     if (GetEnvironmentVariable("QUERY_STRING", tempQuery, 4096))
  68.     {
  69.         string query = tempQuery;
  70.         size_t i, start;
  71.         for (i = 0, start = 0; query[i] != '\0'; i++) {
  72.             if (query[i] == '=') {
  73.                 cout << cp1251_to_utf8("<p>Поле ") << urlDecode(query.substr(start, i - start)) << " : ";
  74.                 start = i + 1;
  75.             }
  76.             if (query[i] == '&') {
  77.                 cout << urlDecode(query.substr(start, i - start)) << "</p>\n";
  78.                 start = i + 1;
  79.             }
  80.         }
  81.         cout << urlDecode(query.substr(start, i - start)) << "</p>\n";
  82.     }
  83.     cout << cp1251_to_utf8("</div></BODY></HTML>\n");
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement