Page 1 of 1

snprintf - Bug and workaround

Posted: Fri Jul 20, 2018 12:28 pm
by mk-soft
wilbert wrote:
skywalk wrote:This function call works on PB v562 x64.
You are right but I still believe there's a bug as this doesn't work

Code: Select all

ImportC ""
  _snprintf(*sRes.p-Ascii, nBytes.i, sFmt.p-Ascii, NumToFmt.d)
EndImport
Global.i nBytes = #MAX_PATH
Global sRes.s = Space(nBytes)
_snprintf(@sRes, nBytes, "%6.6e", -#PI*2000e6)
Debug PeekS(@sRes, -1, #PB_Ascii)
skywalk wrote:Yes, the bug is confusing :evil:

Code: Select all

ImportC ""
  _snprintf(*sRes.p-Ascii, nBytes.i, sFmt.p-Ascii, sTxt.p-Ascii, a2.d)
  _snprintf1(*sRes.p-Ascii, nBytes.i, sFmt.p-Ascii, a2.d) As "_snprintf"
EndImport
Global.i nBytes = #MAX_PATH
Global sRes.s = Space(nBytes)
_snprintf(@sRes, nBytes, "%s %6.6e", "Result = ", -#PI*2000e6)
Debug PeekS(@sRes, -1, #PB_Ascii)
_snprintf1(@sRes, nBytes, "%6.6e", -#PI*2000e6)
Debug PeekS(@sRes, -1, #PB_Ascii)

Re: snprintf - Bug and workaround

Posted: Fri Jul 20, 2018 12:29 pm
by mk-soft
Here my solution with Macro

Update v1.02
- Bugfix x86

Update v1.04
- All OS

Code: Select all

;-TOP

; By mk-soft
; v1.04
; Update 20.07.2018

EnableExplicit

DeclareModule MyPrintf
  ; Dummy Variables (EnableExplizit)
  Global _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8
  Threaded *__Result
  *__Result = AllocateMemory(4096)
  
  Macro Printf(Result, sFormat, _arg1, _arg2=_arg2, _arg3=_arg3, _arg4=_arg4, _arg5=_arg5, _arg6=_arg6, _arg7=_arg7, _arg8=_arg8)
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Windows
        ImportC ""
          CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
            __Printf_#MacroExpandedCount(*pResult, nBytes, sFMT.p-UTF8, sBugfix.s, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8) As "_snprintf"
          CompilerElse
            __Printf_#MacroExpandedCount(*pResult, nBytes, sFMT.p-UTF8, sBugfix.s, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8) As "__snprintf"
          CompilerEndIf  
        EndImport
      CompilerCase #PB_OS_MacOS
        ImportC ""
          __Printf_#MacroExpandedCount(*pResult, nBytes, sFMT.p-UTF8, sBugfix.s, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8) As "_snprintf"
        EndImport
      CompilerCase #PB_OS_Linux
        ImportC ""
          __Printf_#MacroExpandedCount(*pResult, nBytes, sFMT.p-UTF8, sBugfix.s, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8) As "snprintf"
        EndImport
      CompilerEndSelect
    __Printf_#MacroExpandedCount(*__Result, MemorySize(*__Result) - 1, "%s" + sFormat, #Empty$, _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8)
    Result = PeekS(*__Result, -1, #PB_UTF8)
  EndMacro
  
EndDeclareModule

Module MyPrintf
  ; Nothing  
EndModule

UseModule MyPrintf

Define t1.s = "Hello World! (Only Windows)"
Define t2 = UTF8("Hello World!")

Define i1.i = 1234567
Define d1.d = #PI
Define d2.d = 12345.6789
Define Result.s

Debug "Only Window support '%ls'"
Printf(Result, "Result: %ls / i1 = %i / d1 = %f / d2 = %f", t1, i1, d1.d, d2.d)
Debug Result

Debug "MacOS support UTF8"
Printf(Result, "Result: %s / d1 = %f / i1 = %i / d2 = %f", t2, d1.d, i1, d2.d)
Debug Result

FreeMemory(t2)

Debug "Float Not support, only double on x86 And x64"
Define f1.f = 123.45
Printf(Result, "Result: f1 = %f", f1.f)
Debug Result

Re: snprintf - Bug and workaround

Posted: Fri Jul 20, 2018 12:48 pm
by wilbert
I posted some information about what is causing the problem on x64.
viewtopic.php?f=7&t=71079

As for the workaround, what PB versions did you test for ?
When I try on PB Windows x86 is get a linker error (Windows 10).
On Windows x64 it does compile without problems.

Re: snprintf - Bug and workaround

Posted: Fri Jul 20, 2018 1:19 pm
by mk-soft
I have a bug with import for x86. Is fixed... :wink:

Update v1.02
- Bugfix x86

Re: snprintf - Bug and workaround

Posted: Fri Jul 20, 2018 6:28 pm
by mk-soft
Update v1.04
- All OS

Its working, but i like my function "Format(...)" better! :wink:

Re: snprintf - Bug and workaround

Posted: Fri Jul 20, 2018 6:51 pm
by wilbert
mk-soft wrote:Update v1.04
- All OS
Great that you added support for all OS.
I believe for MacOS and Linux the empty string workaround isn't required but it also doesn't hurt.

Passing a .f variable works fine as long as you use .d for the import instruction.
Don't know if it would be possible with a macro to replace .f with .d for the import only.

Re: snprintf - Bug and workaround

Posted: Fri Jul 20, 2018 9:49 pm
by mk-soft
I couldn't get it with a Macro. I tried to replace".s" with".p-ascii".

Re: snprintf - Bug and workaround

Posted: Sun Jul 22, 2018 4:25 pm
by Kwai chang caine
This is my result on W64 X64 / v5.61 X86
Only Window support '%ls'
Result: Hello World! (Only Windows) / i1 = 1234567 / d1 = 3.141593 / d2 = 12345.678900
MacOS support UTF8
Result: Hello World! / d1 = 3.141593 / i1 = 1234567 / d2 = 12345.678900
Float Not support, only double on x86 And x64
Result: f1 = 0.000000
Thanks 8)