Welcome! Log In Create A New Profile

Advanced

Generating UTF8-encoded files

Posted by Mike.pcs.crosspost 
Mike.pcs.crosspost
Generating UTF8-encoded files
October 28, 2008 03:46PM
Hi all-

one of my customers is using Windev. He needs to generate a UTF8-encoded file to send me data he needs me to process, but he does not know how. Unfortunately I don't use Windev and I cannot give him any advice. I have browsed the net and searched this forum, to no avail.

Is there a simple way for him to do what he wants to do? What should I advise him to use in order to be able to output a utf8-encoded file?

Thanks in advance- Mike

Message forwarded from pcsoft.us.windev
Fabrice Harari
Re: Generating UTF8-encoded files
October 28, 2008 03:53PM
Hello Mike...

as you are not saying which version of windev your customer is using, I'll suppose it's the last one, v12... in that case, he just have to go in the help and type the keyword UTF...

It will lead to 2 functions allowing to transform string in UTF and UTF in string...

That should do the trick

best regards

Fabrice Harari
WinDev, WebDev, WinDev Mobile consulting
Peter Holemans
Re: Generating UTF8-encoded files
October 28, 2008 05:42PM
A long time ago, I created a command executable in WinDev to convert text files to UTF files.

It works like this: (E.g. from command window)
RESULT = EDST_ANSI_TO_UTF8.exe /InputFile=”C:\MyANSIDocs\FileToConvert.xml” /OutputFile=”C:\MyUTF8Docs\ConvertedFile.xml”

The program accepts two parameters:
1 - /InputFile or -InputFile (REQUIRED)
2 - /OutputFile of –OutputFile (OPTIONAL)

I have a manual which is only available in Dutch but here's the source code of the program. As you will notice there's one command that does it all: gstr_UTF8Text = StringToUTF8(gstr_ANSIText,charsetDefault)... Nothing more, nothing less.
<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
Hi,

See answer and source code at [forum.mysnip.de]

Cheers,

Peter

Message forwarded from pcsoft.us.windev
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: