Mike.pcs.crosspost
Generating UTF8-encoded files October 28, 2008 03:46PM |
Fabrice Harari
Re: Generating UTF8-encoded files October 28, 2008 03:53PM |
Peter Holemans
Re: Generating UTF8-encoded files October 28, 2008 05:42PM |
<p><u><b>INIT CODE OF THE PROJECT</b></u></p> GLOBAL gstr_InputFile is string gint_InputFileHandle is int gstr_OutputFile is string gint_OutputFileHandle is int gstr_LogFile is string gint_LogFileHandle is int gstr_LogText is string gstr_ANSITextLine is string gstr_ANSIText is string gstr_UTF8Text is string gbool_ReplaceOriginalFile is boolean = False gbool_GoAhead is boolean = True //###################################################### //Manage Parameter Passed: Retrieve the passed filename //###################################################### gstr_InputFile = CommandLine("InputFile","") gstr_OutputFile = CommandLine("OutputFile","") IF NOT gstr_InputFile ~= "" THEN //###################################################### //Manage Input File: Check if the file exists //###################################################### IF fFileExist(gstr_InputFile) THEN gint_InputFileHandle = fOpen(gstr_InputFile,foRead) IF gint_InputFileHandle > 0 THEN //###################################################### //Manage Output File: Create Unicode file //###################################################### SWITCH gstr_OutputFile CASE ~= "", ~= gstr_InputFile //Use temporary rename gstr_OutputFile = fExtractPath(gstr_InputFile,fDrive+fDirectory+fFileName)+"_UTF8"+fExtractPath(gstr_InputFile,fExtension) gbool_ReplaceOriginalFile = True OTHER CASE //Check if dir exists, otherwise create IF NOT fDirectoryExist(fExtractPath(gstr_OutputFile,fDrive+fDirectory)) THEN IF NOT fMakeDir(fExtractPath(gstr_OutputFile,fDrive+fDirectory)) THEN //Write error to log file gstr_LogText += DateToString(DateSys(),"YYYYMMDD")+"-"+TimeSys()+" | "+"Unable to create output directory for: "+gstr_OutputFile+CR gbool_GoAhead = False END END END //###################################################### //Valid input and output file? //###################################################### IF gbool_GoAhead THEN //Create target file and lock for other processes gint_OutputFileHandle = fOpen(gstr_OutputFile,foCreate+foReadLock) IF gint_OutputFileHandle > 0 THEN //###################################################### //Read inputfile in string variable //###################################################### gstr_ANSITextLine = fReadLine(gint_InputFileHandle) WHILE gstr_ANSITextLine <> EOT gstr_ANSIText += gstr_ANSITextLine+CR gstr_ANSITextLine = fReadLine(gint_InputFileHandle) END //###################################################### //Manage File Conversion: Remove trailing Carriage returns //###################################################### WHILE Right(gstr_ANSIText,1) = CR gstr_ANSIText = Left(gstr_ANSIText,Length(gstr_ANSIText)-1) END //###################################################### //Manage File Conversion: Convert and write to file //###################################################### gstr_UTF8Text = StringToUTF8(gstr_ANSIText,charsetDefault) IF NOT fWrite(gint_OutputFileHandle,gstr_UTF8Text) THEN //Write error to log file gstr_LogText += DateToString(DateSys(),"YYYYMMDD")+"-"+TimeSys()+" | "+"Unable to write to outputfile: "+gstr_OutputFile+CR END //###################################################### //Close Opened Files //###################################################### fClose(gint_InputFileHandle) fClose(gint_OutputFileHandle) //###################################################### //New file or replace original file? //###################################################### IF gbool_ReplaceOriginalFile THEN IF fDelete(gstr_InputFile,frToRecycleBin) THEN IF NOT fRename(gstr_OutputFile,gstr_InputFile) THEN gstr_LogText += DateToString(DateSys(),"YYYYMMDD")+"-"+TimeSys()+" | "+StringBuild("Unable to rename original file %1 into %2.",gstr_OutputFile,gstr_InputFile)+CR END ELSE gstr_LogText += DateToString(DateSys(),"YYYYMMDD")+"-"+TimeSys()+" | "+StringBuild("Unable to move original file %1 to the recycle bin.",gstr_InputFile)+CR END END ELSE //Close open files fClose(gint_InputFileHandle) //Write error to log file gstr_LogText += DateToString(DateSys(),"YYYYMMDD")+"-"+TimeSys()+" | "+"Unable to create outputfile: "+gstr_OutputFile+CR END END ELSE //Write error to log file gstr_LogText += DateToString(DateSys(),"YYYYMMDD")+"-"+TimeSys()+" | "+"Unable to open inputfile: "+gstr_InputFile+CR END ELSE //Write error to log file gstr_LogText += DateToString(DateSys(),"YYYYMMDD")+"-"+TimeSys()+" | "+StringBuild("Inputfile %1 does not exist",gstr_InputFile)+CR END ELSE //Write error to log file gstr_LogText += DateToString(DateSys(),"YYYYMMDD")+"-"+TimeSys()+" | "+"No inputfile has been passed as a parameter to the program. Use '/InputFile=' or '-InputFile=' to pass the input file to the executable program. E.g.: C:\MyProgram\EDST_ANSI_TO_UTF8.exe /InputFile=""C:\MyDirectory\MyANSIFile.xml"""+CR END //###################################################### //Manage Log File: If required //###################################################### IF NOT gstr_LogText ~= "" THEN IF fFileExist(gstr_InputFile) THEN gstr_LogFile = fExtractPath(gstr_InputFile,fDrive+fDirectory+fFileName)+".log" ELSE IF gstr_InputFile ~= "" THEN gstr_LogFile = CompleteDir(ExeInfo(exeDirectory))+DateToString(DateSys(),"YYYYMMDD")+"-"+TimeSys()+".log" ELSE gstr_LogFile = CompleteDir(ExeInfo(exeDirectory))+fExtractPath(gstr_InputFile,fFileName)+".log" END END gint_LogFileHandle = fOpen(gstr_LogFile,foCreate+foReadLock) IF gint_LogFileHandle > 0 THEN fWrite(gint_LogFileHandle,gstr_LogText) fClose(gint_LogFileHandle) END END <u><b>ENDING CODE OF THE PROJECT</b></u> //###################################################### //Return 0 or 1 based on successful conversion //###################################################### IF gstr_LogText ~= "" THEN RESULT 1 ELSE RESULT 0 END
Peter H..pcs.crosspost
Re: Generating UTF8-encoded files October 29, 2008 11:11AM |