Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by MrSwiss »

As the title indicates, some methods to convert Data, store it and, work with it (aka: use it).
Function overload(ing) is also shown, with a numeric to string conversion (same name, different parameter's).
<edit> updated code to prevent compiler warning with: -w pedantic (compiler switch) </edit>

Code: Select all

' D_TandArr_OFunc.bas -- 2017-11-04, by MrSwiss
' Data-Type, conversion Array's and Overloaded Function's (in a nutshell)"

' compile: -s console (FBC x64-Win, ver.1.05.0, tested, -lang FB)

#Include Once "datetime.bi" ' FB internal .bi file (Date/Time stuff)

' initialized arrays to convert numbers (array index), to String
Dim As String * 9 mnth(1 To 12) = { "January", "February", "March", "April", "May", _
                                    "June", "July", "August", "September", _
                                    "October", "November", "December" }

Dim As String * 9 wday(1 To 7)  = { "Sunday", "Monday", "Tuesday", "Wednesday", _
                                    "Thursday", "Friday", "Saturday" }

Dim As Double DT = Now  ' Prog. start: Date & Time

' because: the Function is used, before it is implemented!
Declare Function LeapYear(ByVal As ULong) As Boolean

Type status
    'As ULong   stp  ' step (currently ??? - not used)
    As ULong   clr  ' color
    As ULong   d    ' day
    As ULong   m    ' month
    As ULong   y    ' year
    As ULong   hr   ' hour
    As ULong   mn   ' minute
    As ULong   sc   ' second
    As ULong   dow  ' day of week
    As ULong   fdow ' first day of week
    As Boolean lyr  ' leap year (yes/no = TRUE/FALSE) 
    As Boolean vis  ' visible
    As Boolean ini  ' initialized
    As Boolean res  ' reserved
End Type

Dim As status stat

With stat
	'.stp  = 0
	.clr  = &hFF000000
	.d    = Day(DT)
	.m    = Month(DT)
	.y    = Year(DT)
	.dow  = Weekday(DT)
	.hr   = Hour(DT)
	.mn   = Minute(DT)
	.sc   = Second(DT)
	.fdow = 2 ' Monday (or Sunday = 1)
	.lyr  = LeapYear(stat.y)
	.vis  = TRUE
	.ini  = TRUE
	.res  = FALSE
End With

' here: only used as: Type initializer (see above)
Function LeapYear(ByVal yea As ULong) As Boolean
	If (yea Mod 4) Then Return FALSE    ' definitely NOT a leap Year
	' below: exception test (a century but, not also clean dividable by 400)
    If Not(yea Mod 100) AndAlso Not(yea Mod 400) Then Return FALSE
    ' below: all tests passed = it is, a leap Year
	Return TRUE
End Function

Function TimeString OverLoad( _
    ByVal DaTi As Double, _         ' DateTime serial
    ByVal h2d  As Boolean = FALSE _ ' show leading zero(hour's only)
    ) As String
    Dim As String   sh, sm, ss, sd = ":"
    Dim As ULong    hr = Hour(DaTi), mn = Minute(DaTi), sc = Second(DaTi)

    If Not(h2d) Then    ' single digit hour (if < 10), default
        sh = Str(hr)
        If mn < 10 Then sm = "0" + Str(mn) Else sm = Str(mn)
        If sc < 10 Then ss = "0" + Str(sc) Else ss = Str(sc)
    Else                ' hour always 2 digits (displays leading "0")
        If hr < 10 Then sh = "0" + Str(hr) Else sh = Str(hr)
        If mn < 10 Then sm = "0" + Str(mn) Else sm = Str(mn)
        If sc < 10 Then ss = "0" + Str(sc) Else ss = Str(sc)
    End If

    Return sh + sd + sm + sd + ss
End Function
Function TimeString OverLoad( _
    ByVal hr  As ULong, _           ' hour
    ByVal mn  As ULong, _           ' minute
    ByVal sc  As ULong, _           ' second
    ByVal h2d As Boolean = FALSE _  ' show leading zero(hour's only)
    ) As String
    Dim As String   sh, sm, ss, sd = ":"

    If Not(h2d) Then    ' single digit hour (if < 10), default
        sh = Str(hr)
        If mn < 10 Then sm = "0" + Str(mn) Else sm = Str(mn)
        If sc < 10 Then ss = "0" + Str(sc) Else ss = Str(sc)
    Else                ' hour always 2 digits (displays leading "0")
        If hr < 10 Then sh = "0" + Str(hr) Else sh = Str(hr)
        If mn < 10 Then sm = "0" + Str(mn) Else sm = Str(mn)
        If sc < 10 Then ss = "0" + Str(sc) Else ss = Str(sc)
    End If

    Return sh + sd + sm + sd + ss
End Function
Function TimeString OverLoad( _
    ByRef stt As Const status, _    ' Type "status"
    ByVal h2d As Boolean = FALSE _  ' show leading zero(hour's only)
    ) As String
    Dim As String   sh, sm, ss, sd = ":"
    With stt
        If Not(h2d) Then    ' single digit hour (if < 10), default
            sh = Str(.hr)
            If .mn < 10 Then sm = "0" + Str(.mn) Else sm = Str(.mn)
            If .sc < 10 Then ss = "0" + Str(.sc) Else ss = Str(.sc)
        Else                ' hour always 2 digits (displays leading "0")
            If .hr < 10 Then sh = "0" + Str(.hr) Else sh = Str(.hr)
            If .mn < 10 Then sm = "0" + Str(.mn) Else sm = Str(.mn)
            If .sc < 10 Then ss = "0" + Str(.sc) Else ss = Str(.sc)
        End If
    End With
    Return sh + sd + sm + sd + ss
End Function

' ===== MAIN =====
Dim As String ttl = "Data-Type, conversion Array's and Overloaded Function's (in a nutshell)"

Width 80, 30

Print ttl : Print String(Len(ttl), "~") : Print
Print "DateTime:  "; DT
Print "Type Size: "; SizeOf(status)
Print
' using the Type
With stat
	'Print "stp: "; Str(.stp)
	Print "clr: "; "hex: "; Hex(.clr, 8); "  dec: "; str(.clr)
	Print "d:   "; Str(.d)
	Print "m:   "; Str(.m)
	Print "y:   "; Str(.y)
	Print "dow: "; Str(.dow)
	Print "fdow:"; Str(.fdow)
	Print "hr:  "; Str(.hr)
	Print "mn:  "; Str(.mn)
	Print "sc:  "; Str(.sc)
	Print "lyr: "; UCase(Str(.lyr))
	Print "vis: "; UCase(Str(.vis))
	Print "ini: "; UCase(Str(.ini))
	Print "res: "; UCase(Str(.res))
End With
Print
Print "Program-Start Time & Date was:"
Print "------------------------------"
' using the Type, again
Print "Date: "; Trim(wday(stat.dow)); ", "; Str(stat.d); _
      ". "; Trim(mnth(stat.m)); " "; Str(stat.y); _
      " (using arrays to get the Strings)" 
Print "Time(1): "; TimeString(DT, TRUE); " OverLoad(ed) Function(DateTime serial)"
Print "Time(2): "; TimeString(Hour(DT), Minute(DT), Second(DT)); " OverLoad(ed) Function(hour, minute, second)"
Print "Time(3): "; TimeString(stat); " OverLoad(ed) Function(Type)"

Print : Print : Print "press a key to quit ... ";

Sleep
' ===== END MAIN ===== ' ----- EOF -----
Last edited by MrSwiss on Nov 04, 2017 15:32, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by fxm »

When compiling with '-w pedantic' option:
  • (106) warning 17(0): The type length is too large, consider passing BYREF, at parameter 1 (stt) of TimeString()
There are 2 thresholds to activate this kind of compiler warning on the parameter(s) declared 'As ByVal', when compiling with '-w pedantic' option (presently not noted in documentation):
  • Parameter length > 16 bytes
    => warning 17: The type length is too large, consider passing BYREF
  • Parameters list length > 256 bytes
    => warning 18: The length of the parameters list is too large, consider passing UDT's BYREF
But in both cases, it's just a warning: obviously 'ByRef' is not imposed by the compiler!

To suppress warning (with '-w pedantic' option) :

Code: Select all

Function TimeString OverLoad( _
    ByRef stt As status, _          ' Type "status"
    ByVal h2d As Boolean = FALSE _  ' show leading zero(hour's only)
    ) As String
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by MrSwiss »

Yes, correct.

But I don't entirely agree with your code, since: the type should be protected from modification,
aka: read only (even when passed ByRef), therefore, I'd do it, as below:

Code: Select all

Function TimeString OverLoad( _
    ByRef stt As Const status, _    ' Type "status"
    ByVal h2d As Boolean = FALSE _  ' show leading zero(hour's only)
    ) As String
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by fxm »

OK, you are right.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by jj2007 »

Works like a charm, except line 130:

Code: Select all

Print String(Len(ttl), "~")
error 57: Type mismatch, at parameter 2

Print String(Len(ttl), "~") ' chokes
Print String(Len(ttl), 126) ' works fine

Btw where is the official FB help file? I found two *.chm files in FreeBasic\FbEdit\Help\ but they cover the editor and WS_ styles only. Google points me to a 11 year old PDF from Clark & Feaga, A Beginner’s Guide to FreeBasic...
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by fxm »

jj2007 wrote:Print String(Len(ttl), "~") ' chokes
Print String(Len(ttl), 126) ' works fine
Weird!
jj2007 wrote:Btw where is the official FB help file? I found two *.chm files in FreeBasic\FbEdit\Help\ but they cover the editor and WS_ styles only. Google points me to a 11 year old PDF from Clark & Feaga, A Beginner’s Guide to FreeBasic...
The official documentation is the one associated with the used release: see in "News" forum the Topic for each release.
Otherwise, there is a daily build at http://users.freebasic-portal.de/stw/builds/
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by jj2007 »

fxm wrote:
jj2007 wrote:Print String(Len(ttl), "~") ' chokes
Print String(Len(ttl), 126) ' works fine
Weird!
Indeed.

Code: Select all

FreeBASIC Compiler - Version 1.05.0 (01-31-2016), built for win32 (32bit)
Copyright (C) 2004-2016 The FreeBASIC development team.
standalone
Thanks for the link to the help file. It might deserve a more prominent place ;-)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by MrSwiss »

@jj2007,

don't exactly understand, the problem with:

Code: Select all

Print String(Len(ttl), "~")  ' ???
Have you tried, to replace the char, with the corresponding ASCII-Number?
Has maybe something to do, with 'Locale' setting in Win?
(my setup: US-EN (language), SG (keyboard), Swiss (locale))
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by jj2007 »

MrSwiss wrote:Have you tried, to replace the char, with the corresponding ASCII-Number?
yes, that works:

Code: Select all

print chr("x")	' error 57, type mismatch
print chr(120)	' ok
print string(8, "x")	' error 57, type mismatch
print string(8, 120)	' ok
PRINT STRING(8, "X")	' error 57, type mismatch
print string(8, 120)	' ok
Has maybe something to do, with 'Locale' setting in Win?
(my setup: US-EN (language), SG (keyboard), Swiss (locale))
Possible. My OS and keyboard are Italian.
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by sancho3 »

What editor are you using?
Try the following without copying it (type it in yourself):

Code: Select all

print asc("""")
I'm guessing it has something to do with the way your system is interpreting the quote character.
But then I guess that would make any print statement with a quote fail, so maybe not.
I agree. Weird.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by dodicat »

I cannot replicate this error at all.
Even with silly things

Code: Select all


function L(s as string) as string
    return s
end function

write String(8,Space(1))
Print String(8,str("X"))
Print String(8,L("x"))

Sleep

 
fbedit cannot, it seems, open a .chm freebasic help file.
I presume the Win32 help would be that huge winapi help file which you can download.
The Window styles and fbedit help .chm files open OK.

I use fbide.
You could download the freebasic .chm help file, store it and make a shortcut to it and use it when required.
With Win 10 you first should unblock it under properties/general.

With fbide, once you have the path to the help file set, you can then set the cursor on a keyword in your code and press F1 -- Bingo!
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by jj2007 »

dodicat wrote:I cannot replicate this error at all.
Mystery solved: It's the UTF-8 BOM. If I instruct my editor not to send it, the code builds without problem. Good to know.

Weird, though, that it chokes only for string(8, "x"), not for any other print "xx" command, for example:

Code: Select all

Print "This prints just fine with the BOM: Добро пожаловать"
Output:

Code: Select all

This prints just fine with the BOM: Добро пожаловать
You could download the freebasic .chm help file
That part works, Win7-64 can open *.chm files.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by fxm »

dodicat wrote:fbedit cannot, it seems, open a .chm freebasic help file.
Yes, it can.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by jj2007 »

MrSwiss wrote:' initialized arrays to convert numbers (array index), to String
Dim As String * 9 mnth(1 To 12) = { "January", "February", "March", "April", "May", _
"June", "July", "August", "September", _
"October", "November", "December" }

Dim As String * 9 wday(1 To 7) = { "Sunday", "Monday", "Tuesday", "Wednesday", _
"Thursday", "Friday", "Saturday" }
For Windows, GetDateFormat is available:

Code: Select all

include \masm32\MasmBasic\MasmBasic.inc
  Init
  Inkey "Right now, ", fDate$(0, "dddd dd MMMM yyyy "), fTime$(0, "HH:mm:ss"), Str$(", we are in ISO week %i\n", IsoWeek())
EndOfCode
Output: Today, Monday 06 November 2017 12:25:44, we are in ISO week 45
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Data-Type, conversion Array's and Overloaded Function's (in a nutshell)

Post by dodicat »

OK fxm.
I copied the .chm into the the fbedit help folder and renamed it FB.chm.
(as per the .ini requirement)
It works now.
Also works with the F1 key.

jj2007
Sorry.
I have little patience these days, probably age related.
The bom, yes I have experimented in the past adding a bom with chr(X,X,X) (forget which X's) to a saved text file with differing results, usually the wrong ones.
Never mind.
Carry on.
Post Reply