#include <iostream>
#include <math.h>
#include <stdio.h>
#include <windows.h>
#include <tlhelp32.h>
#include <iomanip>
#include <TlHelp32.h>
#include <comdef.h>
#include <tchar.h>
#include <WinInet.h>
#include <ctype.h>
#include <string>
#include <algorithm>
#pragma comment(lib, "WinInet.lib")
using namespace std ;
char TranslateToLangCode [ 6 ] = { 0 };
char TranslateFromLangCode [ 6 ] = { 0 };
std::string failed(const char* what)
{
char buffer[128];
sprintf_s(buffer, 128, "%s failed", what);
std::string failedStr = buff;
return failedStr;
}
std::string NetRead(const char* szUrl)
{
int result;
HINTERNET hInternet;
result = 0;
hInternet = InternetOpen (_T("imaged"),
INTERNET_OPEN_TYPE_DIRECT, //__in DWORD dwAccessType
NULL, //__in LPCTSTR lpszProxyName,
NULL, //__in LPCTSTR lpszProxyBypass,
NULL //_in DWORD dwFlags
);
DWORD dwSize;
TCHAR szHead[15];
BYTE szTemp[1024];
HINTERNET hConnect;
szHead[0] = '\0';
std::string output = "";
if ( !(hConnect = InternetOpenUrl( hInternet, szUrl, szHead, 15, INTERNET_FLAG_DONT_CACHE, 0)))
{
return failed("InternetOpenUrl");
}
do
{
if (!InternetReadFile (hConnect, szTemp, 1024, &dwSize) )
{
return failed("InternetReadFile");
}
if (!dwSize)
break;
else
output.append((const char*)szTemp);
} while (true);
int i, j;
for(i=0; output[i]!='\0'; i++)
{
while ( ! ( ( output[i] >= 'a' && output[i] <= 'z') || (output[i] >= 'A' && output[i] <= 'Z' || output[i] == '\0' || output[i] == ' ' ) ) )
{
for(j=i;output[j]!='\0';++j)
{
output[j]=output[j+1];
}
output[j]='\0';
}
}
InternetCloseHandle(hInternet);
return output;
}
int main ( )
{
SetConsoleTitle ( "Translator X3-R" ) ;
Sleep ( 500 ) ;
char input[512] = { 0 };
char inss[512] = { 0 };
sprintf_s(TranslateToLangCode, 6, "es");
sprintf_s(TranslateFromLangCode, 6, "en");
while ( true )
{
if (GetAsyncKeyState(VK_UP))
{
Sleep(1000);
std::cout << "Type the thing you want to translate:" << endl;
cin.getline(input, 512);
sprintf_s(inss, 512, "http://translate.google.com/translate_a/t?client=p&text=%s&sl=%s&tl=%s&ie=UTF-8&oe=UTF-8&multires=0", input, TranslateFromLangCode, TranslateToLangCode);
std::string out = NetRead(inss);
std::cout << out << endl;
Sleep(1000);
}
if (GetAsyncKeyState(VK_DOWN))
{
Sleep(1000);
std::cout << "Type the language code you wish to translate from:" << endl;
cin.getline(TranslateFromLangCode, 6);
std::cout << "Language to Translate from code is now: " << TranslateFromLangCode << endl;
std::cout << "Type the language code you wish to translate to:" << endl;
cin.getline(TranslateToLangCode, 6);
std::cout << "Language to Translate to code is now: " << TranslateToLangCode << endl;
Sleep(1000);
}
}
return 0 ;
}