//=========================================================================== // RECURSIVE BABEL FISH TRANSLATION // Using the component NMHTTP1 // Nick Gessler - 7 March 2006 // // This program used the Get method sending out the following data stream: // http://world.altavista.com/tr?doit=done&urltext=cryptology&lp=en_de // | URL | needed | textToTranslate | fromTo | // All spaces must be replaced by %20 //=========================================================================== #include #include //#include // correct, but makes no difference // #include // incorrect #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //=========================================================================== // VARIABLES //=========================================================================== AnsiString header; AnsiString body; AnsiString textToTranslate; AnsiString translatedText; AnsiString getString; AnsiString stringToSend; AnsiString languages; AnsiString beginningString; bool error = false; // In a first set of experiments, int positionOfTextToTranslate; int positionOfTranslatedText; int positionOfSpace; // Refined search variables which find any text. int startOfTextToTranslate; int lengthOfTextToTranslate; int startOfTranslatedText; int lengthOfTranslatedText; //=========================================================================== // FUNCTIONS //=========================================================================== //------------------------------------------------------- Get textToTranslate AnsiString getTextToTranslate (void) { return(Form1->MemoToGo->Text); } //-------------------------------------------------- Replace spaces with %20s void stripSpaces (void) { while (positionOfSpace = textToTranslate.Pos(" ")) { textToTranslate = textToTranslate.SubString(1, positionOfSpace - 1) + textToTranslate.SubString (positionOfSpace + 1, textToTranslate.Length() - positionOfSpace) ; textToTranslate.Insert("%20", positionOfSpace); } } //---------------------------------------------------------- Build Get String void buildGetString (void) { getString = "http://world.altavista.com/tr?doit=done&urltext=" + textToTranslate + "&lp=" + languages; } //------------------------------------------------------------- Get Languages AnsiString getLanguages (void) { languages = Form1->ComboBoxLanguages->Text; languages = languages.SubString(1, 5); return languages; } //--------------------------------------------- Extract Translated Text int extractTranslatedText (void) { if (body.Pos("Translation Error")) error = true; if (true) { // this seems to work OK startOfTranslatedText = body.Pos("style=padding:10px;>") + 20; translatedText = body.SubString(startOfTranslatedText, 100); lengthOfTranslatedText = translatedText.Pos("<") - 1; if (startOfTranslatedText == 0) error = true; } else { // this doesn't work too well startOfTranslatedText = body.Pos("\"q\"") + 11; translatedText = body.SubString(startOfTranslatedText, 100); lengthOfTranslatedText = translatedText.Pos("\"") - 1; if (startOfTranslatedText == 0) error = true; } translatedText = translatedText.SubString(1, lengthOfTranslatedText); } //--------------------------------------------------------------------- Do It void doit (void) { textToTranslate = getTextToTranslate(); stripSpaces(); languages = getLanguages(); buildGetString(); Form1->NMHTTP1->Get(getString); body = Form1->NMHTTP1->Body; // extract TextToTranslate from the body // find where it begins startOfTextToTranslate = body.Pos("\"trtext\"") + 10; // start a new string there textToTranslate = body.SubString(startOfTextToTranslate, 100); // find the length of the text we want lengthOfTextToTranslate = textToTranslate.Pos("<") - 1; // trim irrelevant material from the end of the string textToTranslate = textToTranslate.SubString(1, lengthOfTextToTranslate); // display it Form1->MemoToGo->Text = textToTranslate; extractTranslatedText(); Form1->MemoReturned->Text = translatedText; } //--------------------------------------------------------------- One Circuit void oneCircuit (void) { stripSpaces(); languages = "en_it"; buildGetString(); Form1->NMHTTP1->Get(getString); body = Form1->NMHTTP1->Body; if (extractTranslatedText() == 0) goto exit; if (Form1->RadioGroupShowAllLanguages->ItemIndex) Form1->MemoLost->Lines->Add(translatedText); textToTranslate = translatedText; stripSpaces(); languages = "it_fr"; buildGetString(); Form1->NMHTTP1->Get(getString); body = Form1->NMHTTP1->Body; if (error) goto exit; extractTranslatedText(); if (Form1->RadioGroupShowAllLanguages->ItemIndex) Form1->MemoLost->Lines->Add(translatedText); textToTranslate = translatedText; stripSpaces(); languages = "fr_de"; buildGetString(); Form1->NMHTTP1->Get(getString); body = Form1->NMHTTP1->Body; if (error) goto exit; extractTranslatedText(); if (Form1->RadioGroupShowAllLanguages->ItemIndex) Form1->MemoLost->Lines->Add(translatedText); textToTranslate = translatedText; stripSpaces(); languages = "de_en"; buildGetString(); Form1->NMHTTP1->Get(getString); body = Form1->NMHTTP1->Body; if (error) goto exit; extractTranslatedText(); Form1->MemoLost->Lines->Add(translatedText); textToTranslate = translatedText; exit: } //------------------------------------------------------------------ Get Lost void getLost (void) { textToTranslate = Form1->MemoLost->Text; if (!error) { for (int i = 0; i < 5; i++) { oneCircuit(); } } Form1->MemoLost->Color = 0x00F2E7E3; } //=========================================================================== // EVENT HANDLERS //=========================================================================== void __fastcall TForm1::NMHTTP1AuthenticationNeeded(TObject *Sender) { AnsiString AnID, APass; InputQuery("Authentication required", "Enter a user ID", AnID); InputQuery("Authentication required", "Enter a password", APass); NMHTTP1->HeaderInfo->UserId = AnID; NMHTTP1->HeaderInfo->Password = APass; ShowMessage("Authentication info in place, please retry previous command"); } //--------------------------------------------------------------------------- void __fastcall TForm1::NMHTTP1Failure(CmdType Cmd) { MemoHeader->Text = NMHTTP1->Header; MemoBody->Text = NMHTTP1->Body; switch(Cmd) { case CmdGET: MemoStatus->Lines->Add("HTTP GET Failed"); case CmdPOST: MemoStatus->Lines->Add("HTTP Post Failed"); case CmdHEAD: MemoStatus->Lines->Add("HTTP HEAD Failed"); case CmdOPTIONS: MemoStatus->Lines->Add("HTTP OPTIONS Failed"); case CmdTRACE: MemoStatus->Lines->Add("HTTP TRACE Failed"); case CmdPUT: MemoStatus->Lines->Add("HTTP PUT Failed"); case CmdDELETE: MemoStatus->Lines->Add("HTTP Delete Failed"); } } //--------------------------------------------------------------------------- void __fastcall TForm1::NMHTTP1Redirect(bool &Handled) { if (MessageDlg( "This site is redirecting you to another site. Allow redirect?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) == mrNo) Handled = TRUE; } //--------------------------------------------------------------------------- void __fastcall TForm1::NMHTTP1Success(CmdType Cmd) { if (NMHTTP1->CookieIn != "") MemoCookie->Text = NMHTTP1->CookieIn; MemoHeader->Text = NMHTTP1->Header; MemoBody->Text = NMHTTP1->Body; switch(Cmd) { case CmdGET: MemoStatus->Lines->Add("HTTP GET Successful"); case CmdPOST: MemoStatus->Lines->Add("HTTP POST Successful"); case CmdHEAD: MemoStatus->Lines->Add("HTTP HEAD Successful"); case CmdOPTIONS: MemoStatus->Lines->Add("HTTP OPTIONS Successful"); case CmdTRACE: MemoStatus->Lines->Add("HTTP TRACE Successful"); case CmdPUT: MemoStatus->Lines->Add("HTTP PUT Successful"); case CmdDELETE: MemoStatus->Lines->Add("HTTP DELETE Successful"); } } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonDoItClick(TObject *Sender) { error = false; doit (); } //--------------------------------------------------------------------------- void __fastcall TForm1::ButtonGetLostClick(TObject *Sender) { error = false; getLost(); } //--------------------------------------------------------------------------- void __fastcall TForm1::MemoLostMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { Form1->MemoLost->Color = clWhite; } //---------------------------------------------------------- That's All Folks