snprintf - Bug and workaround

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

snprintf - Bug and workaround

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: snprintf - Bug and workaround

Post 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
Last edited by mk-soft on Fri Jul 20, 2018 6:26 pm, edited 3 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: snprintf - Bug and workaround

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: snprintf - Bug and workaround

Post by mk-soft »

I have a bug with import for x86. Is fixed... :wink:

Update v1.02
- Bugfix x86
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: snprintf - Bug and workaround

Post by mk-soft »

Update v1.04
- All OS

Its working, but i like my function "Format(...)" better! :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: snprintf - Bug and workaround

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: snprintf - Bug and workaround

Post by mk-soft »

I couldn't get it with a Macro. I tried to replace".s" with".p-ascii".
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: snprintf - Bug and workaround

Post 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)
ImageThe happiness is a road...
Not a destination
Post Reply