This is directly related to my other questions about using Google to translate words.
In this case I made a program that displays the whole Greek alphabet, and translates words ENG>GRC and GRC>ENG (Grescaro).
Anywho, when you try to translate a word or phrase a number of times (never seen any greater or lower than 18 on Windows, and it varies on Linux), no matter which direction (eng>grc or grc>eng) it will crash at COMPLETELY RANDOM places.
The test code is stripped out of Grescaro's, the last test I did on Linux gave me 12 tries, and it crashed at line 50:
with "Unhandled Exception:appstub.linux signal handler 11".
The code:
I originally didnt use everything against a single JSONReader instance, at first I was just creating one each time the user tried to translate a word.
NOTE: The Greek translation was formatted for use on a wxFrame, so it probably wont look correct in your console.
In this case I made a program that displays the whole Greek alphabet, and translates words ENG>GRC and GRC>ENG (Grescaro).
Anywho, when you try to translate a word or phrase a number of times (never seen any greater or lower than 18 on Windows, and it varies on Linux), no matter which direction (eng>grc or grc>eng) it will crash at COMPLETELY RANDOM places.
The test code is stripped out of Grescaro's, the last test I did on Linux gave me 12 tries, and it crashed at line 50:
Local url:String = "ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + word + "&langpair=" + langpair.Replace("|", "%7C")
with "Unhandled Exception:appstub.linux signal handler 11".
The code:
SuperStrict Framework bah.libcurl Import wx.wxApp 'Why is this needed? if I don't import wx.wxApp it crashes with a series of errors. Import wx.wxjson Import brl.retro Import pub.stdc Local translation:String, word:String = "Hello", langpair:String = "en|el" For Local i:Int = 1 To 18 translation = TranslationInterface.TranslateWord(word, langpair) DebugLog "Try #" + i + ", English to Greek: " + word + " to.. " + translation Delay 3000 'Don't flood Google! Next End 'Test wrapper for Translation (I beleive the crash [after precisely 18 {more or less on linux} tries at a translation] is caused by creating so many JSON objects, 'so lets try and make them single instance, just updating them everytime a phrase gets translated)....... nope, still crashing! Type TranslationInterface Global jreader:wxJSONReader = New wxJSONReader.Create(), root:wxJSONValue 'THANKS BRUCEY Function TranslateWord:String(word:String, langpair:String = "en|el") Local curl:TCurlEasy = TCurlEasy.Create(), res:Int, ltype:Int = 0 Local data:String 'As long as the source language is English it should already be good to go, otherwise 'we should convert it to UTF-8 (it works fine in our case, as we only have Greek>English and English>Greek) If Left(langpair, Instr(langpair, "|") - 1).ToLower() <> "en" word = _textConvertMaxToUTF8(word) word = curl.escape(word) word = word.Replace(" ", "+") ltype = 1 Else word = word.Replace(" ", "+") EndIf curl.setOptInt(CURLOPT_FOLLOWLOCATION, 1) curl.setWriteString() Local url:String = "ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + word + "&langpair=" + langpair.Replace("|", "%7C") curl.setOptString(CURLOPT_URL, url) curl.setOptString(CURLOPT_REFERER, "http://hai2you2.googlepages.com/") res = curl.perform() curl.CleanUp() If res = 0 data = curl.ToString() Else DebugLog "TranslateWord(~q" + url + "~q); ERROR: failed to obtain data (code: " + res + ")" data = Null End If If res = 0 'Local jreader:wxJSONReader = New wxJSONReader.Create(), root:wxJSONValue = jreader.ParseString(data) root = jreader.ParseString(data) If root <> Null Local respstatus:Int = root.Item("responseStatus").AsInt() If respstatus = 200 Local trtext:String = root.Item("responseData").Item("translatedText").AsString() If ltype = 0 trtext = _xmlConvertUTF8ToMax(trtext) End If Return trtext Else DebugLog "Received data, but it is invalid (code: " + respstatus + " )" End If EndIf Else DebugLog "Failed to send translation data to Google (code: " + res + ")" End If Return Null End Function End Type 'Conversion functions taken from bah.libxml Function _textConvertMaxToUTF8:String(Text:String) If Not text Then Return "" End If Local l:Int = text.length If l = 0 Then Return "" End If Local count:Int = 0 Local s:Byte[] = New Byte[l * 3] ' max possible is 3 x original size. For Local i:Int = 0 Until l Local char:Int = text[i] If char < 128 Then s[count] = char count:+ 1 Continue Else If char<2048 s[count] = char/64 | 192 count:+ 1 s[count] = char Mod 64 | 128 count:+ 1 Continue Else s[count] = char/4096 | 224 count:+ 1 s[count] = char/64 Mod 64 | 128 count:+ 1 s[count] = char Mod 64 | 128 count:+ 1 Continue EndIf Next Return String.fromBytes(s, count) End Function ' converts a UTF character array from byte-size characters to short-size characters ' based on the TextStream UTF code... Function _xmlConvertUTF8ToMax:String(s:Byte Ptr) If s Then Local l:Int = _strlen(s) Local b:Short[] = New Short[l] Local bc:Int = -1 Local c:Int Local d:Int Local e:Int For Local i:Int = 0 Until l bc:+1 c = s[i] If c<128 b[bc] = c Continue End If i:+1 d=s[i] If c<224 b[bc] = (c-192)*64+(d-128) Continue End If i:+1 e = s[i] If c < 240 b[bc] = (c-224)*4096+(d-128)*64+(e-128) Continue End If Next Return String.fromshorts(b, bc + 1) End If Return "" End Function ' _strlen needed for the _xmlConvertUTF8ToMax() function Extern Function _strlen:Int(s:Byte Ptr) = "strlen" End Extern
I originally didnt use everything against a single JSONReader instance, at first I was just creating one each time the user tried to translate a word.
NOTE: The Greek translation was formatted for use on a wxFrame, so it probably wont look correct in your console.