Sample programs

New to FreeBASIC? Post your questions here.
deltarho[1859]
Posts: 1272
Joined: Jan 02, 2017 0:34
Location: UK

Re: Sample programs

Postby deltarho[1859] » Jan 16, 2018 11:28

It is a strange phenomenon that newcomers to FreeBASIC, on arriving at the swimming pool, have a tendency to jump into the deep end. I did and I got involved in stuff which I am not entirely comfortable with now one year after joining. <smile> It does not help when the first folder in the examples folder is compression. That should be renamed as zcompression so that is the last folder to look at <laugh>; unless someone makes a beeline for it because it is about compression.

@JLWest

Is it compression that you are interested in as I have just got zlib working on this example. I increased the test data to 1MB and added a time to the output and got:

Code: Select all

ZLib test - Version 1.2.7
 
Test data size      : 1048576 bytes.
Time 228.7812417512214 ms
Compressed to       : 350590 bytes.
Uncompressed to     : 1048576 bytes.
crc32 checksum      : PASSED
 
Press any key to end . . .

However, that is 32 bit crunching. I don't think that much has been done re 64 bit compression.

dodicat wrote:quicklz.bi seems to be in the 32 bit inc folder.

It is, but I could not find a library for it.

Anyway, I'll not go into detail as it may not be compression that you are interested in and I don't want to spend wasted time.

If you want 32 bit compression then get back to me and I will get you rolling.
JLWest
Posts: 12
Joined: Jan 12, 2018 8:43

Re: Sample programs

Postby JLWest » Jan 16, 2018 16:24

Thanks for your effort. Actually I was looking at Free Basic to write a simple Windows program, well not so simple, as I'm finding out. And I was exploring Free Basic, Free Pascal and .Net. I have ruled out .Net.

The program is for my X-Plane 11 flight simulator. Presently I create at flight plan on a site called SimBrief. After creating the flight plan with starting airport, destination airport and all the way points in between SimBrief will download the plan to your computer Downloads directory. The plans can be a special formatted file, XML or text file depending on the aircraft you selected as part of the plan build.

The plan is also associated with a cycle number call an AIRAC cycle. AIRAC cycles are updated every month. AIRAC cycles come from Navigraph in Sweden. The current Cycle is 1801. You buy a yearly subscription or an occasional cycle. Flying Jet airliners on plans made with out of date AIRAC cycles can be difficult. Missed approaches, bad SID and STARS. Plans can have twenty or thirty way points. They can be entered into the FMC (Flight Data Computed) by hand, tedious work or loaded, much better, from the special built plans.

The program Reuirements:

The download plan is always named KPHXKLAX.flp. (Starting Airport ICO code, Destination airpot ICO code) The extension can vary from .flp, _route, ebr, pln and 5 or six more extensions.

Once the plan is in your downloads directory it will look like this: KPHXKLAX.flp or KPHXKLAX.ebr (Phoenix to LAX) depending on which aircraft the plan was built for.

Next the file must be copied to a special directory. Each plane requires it own Directory. If you are flying an X-plane 11 supplied plane, one that comes with X-Plane the plan need to be copied or moved to C:\X-Plane 11\OUTPUT\FMS Plans\.

FF Boeing767-300ER and 777 have to be moved to C:\X-Plane 11\Aircraft\Boeing767-300ER\plugins\757767Avionics\routes\.

I have 5 payware planes (Not X-Plane 11 base product planes) all requiring flight plans installs in special directories.

Can I use Free Basic to:

1. Locate a flight plan file(s) in the Download directory.
2. Check the system date stamp on the file.
3. Calculate the AIRAC Cycle based on the date the file was placed in Downloads.
3. Rename the file from say KPHXKLAX.fpl to FF757KPHXKLAS1801.flp.
4. Move or copy the file to a MasterFlightPlans directory.
5. Have the user select the flight plan from a list box. List would look like:
FF757 KPHXKLAX 1801
LR BC90 KSDLJSEA 1801
IXEG737 KORDKJFK 1711

6. Copy FF757KPHXKLAS1801.flp. to the required destination.
7. Rename FF757KPHXKLAS1801.flp to KPHXKLAS.flp. (required format for the an FMC ro load).
8. User should be able to delete out of date flight plans in the MasterFlightPkans directory.
9. User should be able to delete flight plan files in the various directories.


I don't know if Free Basic can do this but I am exploring the idea. Any suggestions?
Tourist Trap
Posts: 2381
Joined: Jun 02, 2015 16:24

Re: Sample programs

Postby Tourist Trap » Jan 16, 2018 17:34

JLWest wrote:Can I use Free Basic to:

1. Locate a flight plan file(s) in the Download directory.
2. Check the system date stamp on the file.
3. Calculate the AIRAC Cycle based on the date the file was placed in Downloads.
3. Rename the file from say KPHXKLAX.fpl to FF757KPHXKLAS1801.flp.
4. Move or copy the file to a MasterFlightPlans directory.
5. Have the user select the flight plan from a list box. List would look like:
FF757 KPHXKLAX 1801
LR BC90 KSDLJSEA 1801
IXEG737 KORDKJFK 1711

6. Copy FF757KPHXKLAS1801.flp. to the required destination.
7. Rename FF757KPHXKLAS1801.flp to KPHXKLAS.flp. (required format for the an FMC ro load).
8. User should be able to delete out of date flight plans in the MasterFlightPkans directory.
9. User should be able to delete flight plan files in the various directories.


I don't know if Free Basic can do this but I am exploring the idea. Any suggestions?

Hi JLWest,

YES to all of this!
The most technical stuff will be the access to your website for download. If it doesn't require login and password, I'm pretty sure it will be easy anyway.

As a concrete starting example about this very first step I propose that you check this example here viewtopic.php?f=7&t=24922&p=222949&hilit=big+review#p222949.

Let me explain, this post there at this link above (link to a page of this forum) had be made possible by a download+parse done on this forum material , but it could have applied to any other similar web stuff.

What is interesting in this example is that I was able to do everything with moderate knowledge of Freebasic, with the use of SNC (a free utility made by DJ.Peters and which is provided with its source code), and with hand made parser of mine (a little crap but did the job). The task accomplished is clearly automation on some arbitrary amount of targeted data (here text extracted from html code of webpages). So it's a kind of superset of your problem. I mean , if this was possible and you have the proof by the facts (the job couldn't have been achieved by hand), your download issue should be possible, as well as your parsing issues.

To conclude, the others affairs like file management (rename and so on) is pretty well handled by the freebasic core language.

I hope I gave you a starting point? You'll need to play with SNC first as I see it. Maybe other people will give you better insight however ;)

SNC -> https://www.freebasic.net/forum/viewtop ... =7&t=23421
JLWest
Posts: 12
Joined: Jan 12, 2018 8:43

Re: Sample programs

Postby JLWest » Jan 17, 2018 18:20

Actually II login in to SimBrief and create a Flight Plan, When I'm finished I select the aircraft and hit the download button. The flight plan is then downloaded by Simbrief to my Downloads directory.
Tourist Trap
Posts: 2381
Joined: Jun 02, 2015 16:24

Re: Sample programs

Postby Tourist Trap » Jan 17, 2018 19:26

JLWest wrote:Actually II login in to SimBrief and create a Flight Plan, When I'm finished I select the aircraft and hit the download button. The flight plan is then downloaded by Simbrief to my Downloads directory.

Good JLWest. So you did well until here. You explained the big picture of your project, started to split your question into small parts easier to handle one after the othee, and you give us now a clear shot of what is exactly the task to be completed first.

Task to be completed first:
Get to the internet , get logged with login and password, then read some repository (listing) and download some item pointed on there.

Now first a general advice for best efficiency and also as a common rule of the forum (yet not absolutely mandatory but really generally better): you ll have to start a topic for each question separately. It will catch the eye more efficiently of anyone wishing to share on the very special point treated at the moment. And it helps the search engine to give better results. I must admit that I tried a search with HTTP and DOWNLOAD but they are too general, so those keywords aren't good...

Anyway right now, let me tell you that for today it's not me that will give you an example for the kind of task you want to achieve. I can tell you what I know in short , and show you examples up to these boundaries (until I get more time to investigate). But someone will help you before, just get a topic started for this.

For now, you need to understand this. You will need to ask your computer OS to send a message to a DNS (web address) via the right protocole. In general you won't have to bother so much, a library attached to your program will tell the system what you want to ask in a more simplified way. That's where I recommend SNC to get started.

Roughly what you'll do with SNC is write a series of messages easily formulated that will trigger commands like HTTP-GET to read a web page and so on.

Basically when you send a message to be connected at a web site, it will answer you and tell you if it granted you access or not. The web site will also generally send you the list of messages you must use with a text interface (it can differ from what you might do from a web browser). From then you will just to follow the steps, and you will be able to get your first web pages very quickly.

What I don't know now is how to download a file other than a html web page (=text). But I'm sure it's quite similar of getting html content downloaded.

Something that must be said right now, you should choose carefully your website for testing access repeatedly. Some websites would ban your IP if you send requests to frequently as it can happen when testing a code ;)

I know I'm just filling the page with generalities but right now I wanted to give you those first advices in order to ensure we will complete your project quite fast.

Last little thing, we are not fanatics right here. Maybe a Python program could fulfil your expectations. Anyway, exactly as in freebasic, the first step will be to get a library that will do the communication stuff in the OS format. As said before SNC is available in FB and I showed an example of its capabilities right above. So I recommend you stick at this for now. Maybe you should read the dedicated page if not already done:
viewtopic.php?f=2&t=25618&p=231338&hilit=simple+connection#p231338
MrSwiss
Posts: 2679
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Sample programs

Postby MrSwiss » Jan 17, 2018 20:19

JLWest wrote:Actually II login in to SimBrief and create a Flight Plan, When I'm finished I select the aircraft and hit the download button. The flight plan is then downloaded by Simbrief to my Downloads directory.
That means to me, that you don't need any Networking, in your program.

Therefore, the first thing to look at (before delfing into GUI progamming) would be:
Directory- and File-handling (look at: dir.bi and file.bi), for the respective procedures ...
jj2007
Posts: 650
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Sample programs

Postby jj2007 » Jan 17, 2018 20:32

JLWest wrote:I select the aircraft and hit the download button. The flight plan is then downloaded by Simbrief to my Downloads directory.
Tourist Trap wrote:Roughly what you'll do with SNC is write a series of messages easily formulated that will trigger commands like HTTP-GET to read a web page and so on.

OP clearly states that the file is already in his local download folder. No need to "trigger commands like HTTP-GET to read a web page and so on". Don't make simple things more complicated than they are.
St_W
Posts: 1318
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Sample programs

Postby St_W » Jan 17, 2018 20:39

Most of your tasks should be very easy to implement with FreeBasic. Just the user interface may be a bit more complicated.

But let's get started with the easiest things. For file management tasks see the following overview page in the wiki and the commands referenced there for more details:
https://freebasic.net/wiki/wikka.php?wakka=CatPgOpsys
JLWest
Posts: 12
Joined: Jan 12, 2018 8:43

Re: Sample programs

Postby JLWest » Jan 17, 2018 22:03

St_W wrote:Most of your tasks should be very easy to implement with FreeBasic. Just the user interface may be a bit more complicated.

But let's get started with the easiest things. For file management tasks see the following overview page in the wiki and the commands referenced there for more details:
https://freebasic.net/wiki/wikka.php?wakka=CatPgOpsys


Your right, the program won't have to login to the net. the file (KLAXKORD.fpl) is delivered to my Downloads directory.

It looks like most of the functions for file handling I will need is there.
MKDIR - Make the MasterFlightPlan directory
CURDIR - Get the directory when searching fir flight plans
DIR - get files
KILL - Delete Files
NAME - Rename Files

In DOS which I know some we use to do DIR *.* >List.txt to get a list of the files in the current Dir and put them in a file List.txt
Tourist Trap
Posts: 2381
Joined: Jun 02, 2015 16:24

Re: Sample programs

Postby Tourist Trap » Jan 18, 2018 5:33

jj2007 wrote:OP clearly states that the file is already in his local download folder.

I'm not so fluent in english ;) Sorry for the confusion! I understood clearly something anyway but not the right thing...
jj2007
Posts: 650
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Sample programs

Postby jj2007 » Jan 18, 2018 6:28

JLWest wrote:the file (KLAXKORD.fpl) is delivered to my Downloads directory.
Perhaps you could post a few lines of each of the expected formats, so that we can see how complex the parsing will be. Take a look at INSTR(), for example. FB doesn't have a built-in FileRead$() but somebody may have a working equivalent. The rest is straightforward.

EDIT: Just saw the FileRead$() equivalent as loadfile in dodicat's post below
Last edited by jj2007 on Jan 18, 2018 15:27, edited 2 times in total.
dodicat
Posts: 5020
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Sample programs

Postby dodicat » Jan 18, 2018 13:53

Here are some procedures, if they are of any help.

Code: Select all

#Include "dir.bi"
#include "vbcompat.bi"

Sub savefile(filename As String,p As String)
    Dim As Integer n
    n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename
    End If
End Sub

Function loadfile(file As String) As String
   If Fileexists(file)=0 Then Print file;" not found":Sleep:End
    Var  f=Freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(1) > 0 Then
        text = String(Lof(f), 0)
        Get #f, , text
    End If
    Close #f
    Return text
End Function

Function datestamp(filename As String) As String
    If Fileexists( filename ) Then
        Dim As Double d = Filedatetime( filename )
        Return Format( d, "dd-mm-yyyy   hh:mm:ss " )
    Else
        Return filename +" not found"
    End If
End Function

Sub list_files (Byref filespec As String, Byval attrib As Integer=fbnormal)'(FROM HELP FILES)
    Dim As String filename = Dir(filespec, attrib) ' Start a file search with the specified filespec/attrib *AND* get the first filename.
    Do While Len(filename) > 0 ' If len(filename) is 0, exit the loop: no more filenames are left to be read.
        Print filename
        filename = Dir()
    Loop
End Sub



'=========================
Dim As String path = (Curdir+"\Brand_New_Folder\")

Mkdir (path)        'create the folder

Dim As String newfile="Brand new file.txt"

Dim As String filetext="Hello World!"

savefile(path+newfile,filetext)

Print path;
list_files(path+"*")

Dim As String s=loadfile(path+newfile)

Print
Print "File Contents and date/time stamp:"
Print s
Print datestamp(path+newfile)

Print
Print "Press any key to end"
Sleep


Kill path+newfile
Print Iif(Fileexists(path+newfile),"Delete manually.","File is gone.")
If Rmdir(path) =0 Then Print "Folder is gone" Else Print "Delete folder manually"
Sleep





 
JLWest
Posts: 12
Joined: Jan 12, 2018 8:43

Re: Sample programs

Postby JLWest » Jan 18, 2018 15:12

jj2007 wrote:
JLWest wrote:the file (KLAXKORD.fpl) is delivered to my Downloads directory.
Perhaps you could post a few lines of each of the expected formats, so that we can see how complex the parsing will be. Take a look at INSTR(), for example. FB doesn't have a built-in FileRead$() but somebody may have a working equivalent. The rest is straightforward.


File formats expected in the Downloads directory:

KPHXKLAX01.flp
KPHXKLAX.flp
KPHXKLAX01_route
KPHXKLAX01.ebr
KPHXKLAX.pln
KPHXKLAX.nz2k.fpl
KPHX-KLAX.grp
KPHXKLAX.flp
corte.in
KPHXKLAX.pln
KPHXKLAX._FS9_11Jan18.pln
KPHXKLAX._FSX_11Jan18.pln
KPHXKLAX._FSX_11Jan18.pln


File Naming Spec

5 char . 3 char extension
8 char . 3 char extension
10 char . 3 char extension
10 char _ 5 char extension always route
8 char . 4 char extension always n2zk . 3 char extension
8 Char . _ 3 char always FS9 _ date . 3 car ext


corte.in This an exception. that will always be the file plan
downloaded. So just need to look for this file name in the Downloads directory.
JLWest
Posts: 12
Joined: Jan 12, 2018 8:43

Re: Sample programs

Postby JLWest » Jan 18, 2018 15:15

dodicat - Yes I think I can use those file routines.

Thanks
jj2007
Posts: 650
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Sample programs

Postby jj2007 » Jan 18, 2018 15:30

JLWest wrote:
jj2007 wrote:File formats expected in the Downloads directory
What I meant is a few lines of the content of these files, not the name or extension. We need to know how difficult parsing could be.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests