1. ----------------------------------------
  2. -- Converts Windows-1252 (CP-1252) encoded file to UTF-8 file
  3. -- see http//en.wikipedia.org/wiki/Windows-1252 for details
  4. -- concernig difference of ISO-8859-1 and CP-1252
  5. -- Notice: This is the default encoding for Director 10 and older on the PC.
  6. ----------------------------------------
  7. on cp1252_to_utf8_file (inputFile, outputFile)
  8.   convert_text_file (inputFile, "windows-1252", outputFile, "utf-8")
  9. end
  10.  
  11. ----------------------------------------
  12. -- Converts ISO-8859-1 encoded file to UTF-8 file
  13. ----------------------------------------
  14. on iso88591_to_utf8_file (inputFile, outputFile)
  15.   convert_text_file (inputFile, "iso-8859-1", outputFile, "utf-8")
  16. end
  17.  
  18. ----------------------------------------
  19. -- converts text files from one encoding to another
  20. -- requires D11.5+ and fileio xtra
  21. -- for supported encodings check: put _system.getInstalledCharSets()
  22. ----------------------------------------
  23. on convert_text_file (inputFile, inputEncoding, outputFile, outputEncoding)
  24.   inputBA = file_get_bytes(inputFile)
  25.   outputBA = convert_text_ba (inputBa, inputEncoding, outputEncoding)
  26.   file_put_bytes(outputFile, outputBa)
  27. end
  28.  
  29. ----------------------------------------
  30. -- converts text in bytearray from one encoding to another
  31. -- requires D11.5+
  32. -- for supported encodings check: put _system.getInstalledCharSets()
  33. ----------------------------------------
  34. on convert_text_ba (inputBa, inputEncoding, outputEncoding)
  35.   inputBa.position = 1
  36.   utf8 = inputBa.readRawString(inputBa.length, inputEncoding)
  37.   tmp = bytearray()
  38.   tmp.writeString(utf8, outputEncoding)
  39.   tmp.position = 5
  40.   return tmp.readByteArray(tmp.length-4)
  41. end
  42.  
  43. ----------------------------------------
  44. -- returns file as ByteArray
  45. ----------------------------------------
  46. on file_get_bytes (tFile)
  47.   fp = xtra("fileIO").new()  
  48.   fp.openFile(tFile, 1)
  49.   err = fp.status()
  50.   if (err) then return false
  51.   data = fp.readByteArray(fp.getLength())
  52.   fp.closeFile()
  53.   fp = 0
  54.   return data
  55. end
  56.  
  57. ----------------------------------------
  58. -- saves ByteArray to file
  59. ----------------------------------------
  60. on file_put_bytes (tFile, tByteArray)
  61.   fp = xtra("fileIO").new()  
  62.   fp.openFile(tFile, 2)
  63.   err = fp.status()
  64.   if not (err) then fp.delete()
  65.   else if (err and not (err = -37)) then return false
  66.   fp.createFile(tFile)
  67.   err = fp.status()
  68.   if (err) then return false
  69.   fp.openFile(tFile, 2)
  70.   err = fp.status()
  71.   if (err) then return false
  72.   fp.writeByteArray(tByteArray)
  73.   fp.closeFile()
  74.   fp=0
  75.   return true
  76. end
  77.  
[raw code]