1. ----------------------------------------
  2. -- Converts ISO-8859-1 encoded string to "binary" UTF-8 string (D10 only)
  3. ----------------------------------------
  4. on iso88591_to_utf8_string (str)
  5.   ret = ""
  6.   len = str.length
  7.   repeat with i = 1 to len
  8.     n = chartonum(str.char[i])
  9.     if (n < 128) then
  10.       put numtochar(n) after ret  
  11.     else
  12.       put numtochar(bitOr(192, n/64)) after ret
  13.       put numtochar(bitOr(128, bitAnd(n,63))) after ret
  14.     end if
  15.   end repeat
  16.   return ret
  17. end
  18.  
  19. ----------------------------------------
  20. -- Converts ISO-8859-1 encoded file to UTF-8 file (D10 only)
  21. -- requires fileio xtra
  22. ----------------------------------------
  23. on iso88591_to_utf8_file (inputFile, outputFile)
  24.   inputStr = file_get_contents(inputFile)
  25.   outputStr = iso88591_to_utf8 (inputStr)
  26.   file_put_contents(outputFile, outputStr)
  27. end
  28.  
  29. ----------------------------------------
  30. -- Converts Windows-1252 (CP-1252) encoded string to "binary" UTF-8 string (D10 only)
  31. -- see http//en.wikipedia.org/wiki/Windows-1252 for details
  32. -- concernig difference of ISO-8859-1 and CP-1252
  33. -- Notice: This is the default encoding for Director 10 and older on the PC.
  34. ----------------------------------------
  35. on cp1252_to_utf8_string (str)
  36.   ret = ""
  37.   len = str.length
  38.   cp = ["€","","‚","ƒ","„","…","†","‡","ˆ","‰","Š","‹","Œ","","Ž","","","‘","’","“","”","•","–","—","˜","™","š","›","œ","","ž","Ÿ"]
  39.   repeat with i = 1 to len
  40.     n = chartonum(str.char[i])
  41.     if (n < 128) then
  42.       put numtochar(n) after ret
  43.     else if n<=159 then
  44.       put cp[n-127] after ret      
  45.     else
  46.       put numtochar(bitOr(192, n/64)) after ret
  47.       put numtochar(bitOr(128, bitAnd(n,63))) after ret
  48.     end if
  49.   end repeat
  50.   return ret
  51. end
  52.  
  53. ----------------------------------------
  54. -- Converts ISO-8859-1 encoded file to UTF-8 file (D10 only)
  55. -- requires fileio xtra
  56. ----------------------------------------
  57. on iso88591_to_utf8_file (inputFile, outputFile)
  58.   inputStr = file_get_contents(inputFile)
  59.   outputStr = cp1252_to_utf8_string (inputStr)
  60.   file_put_contents(outputFile, outputStr)
  61. end
  62.  
  63. ----------------------------------------
  64. -- Converts Windows-1252 (CP-1252) encoded string to "binary" UTF-8 string (D10 only).
  65. -- This version is usually faster because it takes into account that
  66. -- there are more ASCII characters than non-ASCII characters, therefor,
  67. -- instead of converting char by char as the in the original version,
  68. -- the code identifies contigous ASCII chunks and copies them at once.
  69. ----------------------------------------
  70. on cp1252_to_utf8_string_faster (str)
  71.   ret = ""
  72.   len = str.length
  73.   cp = ["€","","‚","ƒ","„","…","†","‡","ˆ","‰","Š","‹","Œ","","Ž","","","‘","’","“","”","•","–","—","˜","™","š","›","œ","","ž","Ÿ"]
  74.   repeat with i = 1 to len
  75.     j = i
  76.     repeat while true
  77.       n = chartonum(str.char[j])
  78.       if n>127 then exit repeat -- non-ascii char found
  79.       j = j+1
  80.       if j>len then
  81.         put str.char[i..len] after ret -- copy last ascii chunk
  82.         return ret
  83.       end if
  84.     end repeat
  85.     if j>i then
  86.       put str.char[i..j-1] after ret -- copy ascii chunk at once
  87.       i = j
  88.     end if
  89.     if n<=159 then
  90.       put cp[n-127] after ret
  91.     else
  92.       put numtochar(bitOr(192, n/64)) after ret
  93.       put numtochar(bitOr(128, bitAnd(n,63))) after ret
  94.     end if
  95.   end repeat
  96.   return ret
  97. end
  98.  
  99. ----------------------------------------
  100. -- reads whole (non-binary) file into string
  101. ----------------------------------------
  102. on file_get_contents (tFile)
  103.   fp = xtra("fileIO").new()
  104.   fp.openFile(tFile,1)
  105.   err = fp.status()
  106.   if (err) then return false
  107.   ret = fp.readFile()
  108.   fp.closeFile()
  109.   fp = 0
  110.   return ret
  111. end
  112.  
  113. ----------------------------------------
  114. -- saves (also binary) string as file
  115. ----------------------------------------
  116. on file_put_contents (tFile, tString)
  117.   fp = xtra("fileIO").new()
  118.   fp.openFile(tFile, 1)
  119.   err = fp.status()
  120.   if not (err) then fp.delete()
  121.   else if (err and not (err = -37)) then return false
  122.   fp.createFile(tFile)
  123.   err = fp.status()
  124.   if (err) then return false
  125.   fp.openFile(tFile, 2)
  126.   err = fp.status()
  127.   if (err) then return false
  128.   fp.writeString(tString)
  129.   fp.closeFile()
  130.   fp=0
  131.   return true
  132. end
  133.  
[raw code]