Open a file with a unicode-encoded name

New to FreeBASIC? Post your questions here.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Open a file with a unicode-encoded name

Post by jj2007 »

Josep Roca wrote:All that is needed is to put the files in a subfolder called Afx. However, for the compiler to found them, you need to indicate the path whe re it has to search it.
You need to "indicate" by setting the environment variable PATH, I guess? Of course, it works also if the source sits right above the Afx folder...

@OP:
1. Go to this Github page
2. Click on the green Clone or Download button
3. Open the archive, and select the Afx folder
4. Extract it to your main FB folder, e.g. C:\MyStuff\FreeBasic
5. Test the installation by extracting a source from the WinFBE-master\Examples subfolders to your main FB folder
6. Try to build the source from there

It works with setting the PATH in a batch file used to build the source, for example:

Code: Select all

@echo off
set options=-gen gcc -t 2000 -Wc -O2 -s console
echo ** compiling %~nx1 with %options% **
set path=%~p0
fbc.exe %options% %1
This batch file needs to be in the folder where fbc.exe sits. Good luck, it seems to be a good library.
Last edited by jj2007 on Jul 04, 2018 17:40, edited 2 times in total.
PaulSquires
Posts: 1002
Joined: Jul 14, 2005 23:41

Re: Open a file with a unicode-encoded name

Post by PaulSquires »

Josep Roca wrote: All that is needed is to put the files in a subfolder called Afx. However, for the compiler to found them, you need to indicate the path whe re it has to search it. In my computer, I have a folder called Programs, a subfolder called WinFBX and a subfolder of WinFBX called Afx, and in the WinFBE editor I have C:\Programs\WinFBX in the "WinFBX Library Path" field. But you can install it anywhere, for example as a subfolder of the FreeBasic "inc" subfolder.

The easiest way is to install the WinFBE suite: https://github.com/PaulSquires/WinFBE/releases
Note that in the latest WinFBE Editor the "WinFBX Library Path" is/will be removed from the "Compiler Options". The best place to install Jose's WinFBX library is in a "Afx" subfolder off of your "\inc" folder for your FB installation. As Jose also points out, the "WinFBE Suite" download in the releases contains everything to get started quickly.... editor, compiler, includes, WinFBX, etc.
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Open a file with a unicode-encoded name

Post by Josep Roca »

> The best place to install Jose's WinFBX library is in a "Afx" subfolder off of your "\inc" folder for your FB installation.

I don't agree. Doing this, you have to copy the Afx folder in the \inc folder of both the 32 and 64 bit compilers.

> Note that in the latest WinFBE Editor the "WinFBX Library Path" is/will be removed from the "Compiler Options".

This is very bad news for me. I suggested the addition of this field because this way I only need to have the includes in one place. Before it, each time that I changed something in the include files I had to copy the changes to two folders to be able to test them with both 32 and 64 bit.
PaulSquires
Posts: 1002
Joined: Jul 14, 2005 23:41

Re: Open a file with a unicode-encoded name

Post by PaulSquires »

Let's talk about it over on the planetsquires forum :)
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Open a file with a unicode-encoded name

Post by Josep Roca »

Agreed. We are off topic.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Open a file with a unicode-encoded name

Post by dodicat »

Anyway, now the football is out of the way, I tried this unicode stuff with the C runtime
Qriginal request:

Code: Select all



#include "crt.bi"
#include "file.bi"

function _Remove(byval Text As String,Char As String) as string
    Var index = 0,asci=Asc(char)
    For i As Integer = 0 To Len(Text) - 1
        If Text[i] <> ASCi Then Text[index] = Text[i] : index =index+ 1
    Next 
    return Left(Text,index)
End function

Sub save overload (filename As wString,p As String)
     dim as wstring * 10 tempname="VT.txt"
    Dim As Integer n
    n=Freefile
    If Open (tempname For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename
    End If
    if len(filename) then
   _wrename(@tempname,@filename)
else
   print "Filename error"
   end if
    if fileexists(tempname) then kill tempname  
End Sub

sub save overload(filename as wstring,content as wstring)
     dim as wstring * 10 tempname="VT.txt"
     Dim As Long n=Freefile
    Open tempname For Output Encoding "utf16" As #n
    Print #n,content
    Close #n
    _wrename(@tempname,@filename)
    if fileexists(tempname) then kill tempname
end sub

sub load overload(filename as wstring, text as string)
     dim as wstring * 10 tempname="VT.txt"
     _wrename(@filename,@tempname)
   var  f=freefile
    Open tempname For Binary Access Read As #f
    If Lof(1) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
     _wrename(@tempname,@filename)
     if fileexists(tempname) then kill tempname
end sub

sub load overload(filename as wstring,content as wstring)
     dim as wstring * 20 r="rt+,ccs=UNICODE"'"r"
     dim as wstring * 1000 t
     dim as long flag
   dim as file ptr fp = _wfopen(@filename,@r)
   if fp=0 then print "Unable to load ";filename:sleep:exit sub
   while 1
       flag=0
    if (fgetws (@t,1000,fp)= 0)then exit while
   content+=t+wchr(10)
   rtrim(content,wchr(10))
wend
fclose(fp)
end sub


dim as wstring * 15 filename="unic.txt"
dim as wstring * 15 utext="<Kc.txt>"
save(filename,utext)'save wide

dim as string  file1
 
   Dim as integer posL, posR
   dim as string tmp
   load("unic.txt",file1)'load ascii 
   print "unic.txt:"
   print file1
  
   posL=Instr(file1, "<") 
   posR=Instr(file1, ">") 
   Print "L=";posL;", Rx=";posR
  
   file1=Mid(file1, posL+1, posR-posL-1)
   file1=_remove(file1,chr(0))
 
  print "New filename ";file1
  dim as string msg
dim as string binaryfile="Hello World,"+chr(0)+chr(0)+chr(0)+" how are you?" +chr(13,10)
for n as long=1 to 2
    if n=2 then msg=" Goodbye " else msg=""
binaryfile+=binaryfile+msg
next n

binaryfile +="  END"
print "---- actual file -----"
print binaryfile
print "----"


save(file1,binaryfile)       'save ascii

dim as string  s
load(file1,s) 'load asci
print "loaded from file:"
print s
print "Done press a key"
sleep

dim as wstring * 50 z= "   My filename is "+ wchr(&h0414, &h043e, &h0431)

dim as wstring * 50 ret
save(wchr(&h0414, &h043e, &h0431)+".txt",z) 'save wide
load(wchr(&h0414, &h043e, &h0431)+".txt",ret)'loaad wide

print "Content of file with unicode name ";wchr(&h0414, &h043e, &h0431); ret
 sleep
 kill "Kc.txt"
 kill "unic.txt"  
Please note i have deleted "unic.txt" (optional)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Open a file with a unicode-encoded name

Post by jj2007 »

Cute idea ;-)

_wrename(@tempname,@filename)
Post Reply