Lee_M's avatar
Lee_M
Community Hero
3 years ago
Status:
New Idea

aqFile encoding update

I save a file using JavaScript using
aqFile.WriteToTextFile(filename, File_entry, aqFile.ctUTF8);


The files save as expected but with an encoding issue

I have found the issue is that the file is saved as UTF-8 BOM and NOT UTF-8 as expected

 

Current fucntionality

Code function Saved encoding
aqFile.ctANSI UTF-8
aqFile.ctUnicode UTF-16 LE BOM
aqFile.ctUTF8 UTF-8 BOM



 

I would like this function to be updated to reflect the correct encodings

(possibly aqFile.cteANSI - new names as not to change any legacy functionality people have programmed in)

aqFile.ctANSI -> ANSI
aqFile.ctUnicode -> Unicode
aqFile.ctUTF8 -> UTF-8
aqFile.ctUTF8B -> UTF-8 BOM
aqFile.ctUTF16 -> UTF-16
aqFile.ctUTF16BB -> UTF-16 BE BOM
aqFile.ctUTF16LB -> UTF-16 LE BOM

 

to get around this issue I am currently saving in the incorrect format each (ANSI) to get my UT8 encoding.

 

 

------------------

 

TestComplete Support has provided an interim solution if anyone is interested

 

(each files take approximately 4 seconds to create
300 files now take 20 minutes, saving it in ANSI (incorrect format) takes less than 1 minute)

 

You can remove BOM using the following script.


[JavaScript/JScript]


function bomInUtf8Test()
{
 function getIsContainsBom(bytes)
 {
  var utf8Bom = [0xEF, 0xBB, 0xBF];
  for (var i = 0; i < 3; i++)
  {
   if (bytes[i] != utf8Bom[i])
   {
    return false;
   }
  }
  return true;
 }


 var fileName = "d:\\test.txt";
 aqFile.WriteToTextFile(fileName, "TEST", aqFile.ctUTF8, true);
 var file = aqFile.OpenBinaryFile(fileName, aqFile.faRead);
 var bytes = [];
 // Reads bytes from the file and posts them to the test log  
 while(!file.IsEndOfFile())
 {
  bytes.push(file.ReadByte());
  if (bytes.length == 3 // UTF-8 BOM is 3 bytes long
   && !getIsContainsBom(bytes))
  { // No BOM found in the file, no changes required
   file.Close();
   return;
  }
 }
 file.Close();
  
 var file = aqFile.OpenBinaryFile(fileName, aqFile.faWrite, true);
 // Remove the three bytes of BOM
 bytes = bytes.slice(3);
 for (var i = 0; i < bytes.length; i++)
 {
  file.WriteByte(bytes[i]);
 }
  
 file.Close();  
}  


As an alternative solution, you can use Notepad++ which can convert UTF-8 files to UTF-8 without BOM.

No CommentsBe the first to comment