Last active
November 7, 2017 11:01
-
-
Save iKlotho/d2af01962218cf44fd31 to your computer and use it in GitHub Desktop.
C Language Translate Console Program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*++ | |
Module Name: | |
main.c | |
Vers : v1.1 | |
--*/ | |
#define _CRT_SECURE_NO_WARNINGS | |
#define WIN32_LEAN_AND_MEAN | |
#define _WINSOCK_DEPRECATED_NO_WARNINGS | |
// Standart Libraries | |
#include <windows.h> | |
#include <iostream> | |
#include <locale.h> | |
#include <winsock2.h> | |
#include <stdlib.h> | |
#include <conio.h> | |
#include <stdio.h> | |
#include <tchar.h> | |
#include <thread> | |
#include <string> | |
#include <time.h> | |
using namespace std; | |
//----------------------------------------// | |
#pragma comment (lib, "Ws2_32.lib") | |
#pragma comment (lib, "Mswsock.lib") | |
#pragma comment (lib, "AdvApi32.lib") | |
#define HOSTNAME "translate.google.com" | |
short port = 80; | |
SOCKET GetConnectedSocket(const char* hostname, short port) | |
{ | |
SOCKET client = 0; | |
struct hostent *host = NULL; | |
struct sockaddr_in info = { 0 }; | |
if (!(host = gethostbyname(hostname))) | |
{ | |
std::cout << "HOST error : " << WSAGetLastError() << std::endl; | |
return INVALID_SOCKET; | |
} | |
info.sin_family = AF_INET; | |
info.sin_port = htons((short)port); | |
info.sin_addr = *((struct in_addr *)host->h_addr); | |
if (INVALID_SOCKET == (client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))) | |
{ | |
std::cout << "SOCKET error : " << WSAGetLastError() << std::endl; | |
return INVALID_SOCKET; | |
} | |
if (connect(client, (struct sockaddr *)&info, sizeof(struct sockaddr))) | |
{ | |
std::cout << "CONNECT error : " << WSAGetLastError() << std::endl; | |
closesocket(client); | |
return INVALID_SOCKET; | |
} | |
return client; | |
} | |
void GetTranslate() | |
{ | |
int i, j, len; // döngüler | |
char request[4096] = | |
"GET /translate_a/t?client=t&text=%s&hl=en&sl=en&tl=tr HTTP/1.1\r\n" | |
"Host: translate.google.com\r\n" | |
"User-Agent: App/1.7\r\n\r\n"; // json ceviri dosyasını istemek icin | |
char name[100]; // cevirilecek metin | |
char buffer[32 * 1024] = { 0 }; // gelen metni saklamak icin | |
char shadow[1024] = { 0 }; // [[[" bu aradaki ceviri metnini okumak icin " | |
char buf[32 * 1024] = { 0 }; // gelen metni ve requesti üzerine yazıp gondermek icin | |
char *str, *ptr; // [[[" ve " arasını cekmek icin | |
WSADATA wsaData; | |
SOCKET sock = INVALID_SOCKET; | |
WSAStartup(MAKEWORD(2, 2), &wsaData); | |
sock = GetConnectedSocket(HOSTNAME, 80); | |
if (sock == INVALID_SOCKET) { | |
std::cout << "Socket error : " << WSAGetLastError() << std::endl; | |
} | |
printf("Kelime: "); | |
scanf("%[^\n]%*c", name); | |
// request'teki text=%s kısmına kullanıcıdan gelen name degıskenı atanır. | |
len = sprintf(buf, request, name); | |
send(sock, buf, len, 0); // cevirelecek metni gonder | |
len = 0; | |
// ]'a kadar oku | |
while (TRUE) | |
{ | |
int ret = 0; | |
ret = recv(sock, buffer + len, sizeof(buffer) - len, 0); | |
if (ret <= 0) { | |
break; | |
} | |
if (strstr(buffer, "]")){ | |
break; | |
} | |
len += ret; | |
} | |
closesocket(sock); | |
str = buffer; | |
// [[["çeviri buraya geldi" | |
while (TRUE) | |
{ | |
str = strstr(str, "[[[\""); | |
if (str == NULL) { | |
break; | |
} | |
ptr = strstr(str, "\","); | |
if (ptr == NULL) { | |
break; | |
} | |
*ptr = 0; | |
sprintf(shadow + strlen(shadow), "%s ", str + strlen("[[[\"")); | |
str = ptr + 1; | |
} | |
printf("Gelen : %s\n", shadow); | |
} | |
int main() | |
{ | |
setlocale(LC_ALL, "turkish"); // türkce karakterler icin | |
printf("\t\t\t++++ConsoleTranslate v1.1++++\n\n"); | |
while (TRUE){ | |
GetTranslate(); | |
printf("\n"); | |
} | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment