Native FormatString (sprintf)
Native FormatString (sprintf)
Times had written this code a long time ago.
Add For better performans with dynamic number of parameters maybe "c sprintf" as native command.
Link: http://www.purebasic.fr/english/viewtop ... 12&t=32026
Add For better performans with dynamic number of parameters maybe "c sprintf" as native command.
Link: http://www.purebasic.fr/english/viewtop ... 12&t=32026
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Native FormatString (sprintf)
Pushup
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
-
- Addict
- Posts: 1443
- Joined: Wed Nov 12, 2008 5:01 pm
- Location: Russia
Re: Native FormatString (sprintf)
Code: Select all
s.s{1024}
OpenLibrary(0, "MSVCRT.dll")
CallCFunction(0, "swprintf", @s, @"Param %i", 10)
Debug s
Re: Native FormatString (sprintf)
What's wrong with floats?
Code: Select all
ImportC "MSVCRT.LIB"
swPrintf.l(out$, fmt$, Xd.d) As "swprintf"
EndImport
Define.s r$ = StrD(#PI,4) + "ZZZ"
x.d = #PI
Debug r$
Debug x
ShowMemoryViewer(@r$, 32)
Debug swPrintf(r$, "%e", x)
ShowMemoryViewer(@r$, 32)
Debug r$
Last edited by skywalk on Sat Jul 22, 2017 3:50 pm, edited 1 time in total.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
-
- Always Here
- Posts: 6425
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Native FormatString (sprintf)
sprintf() would be an excellent addition to PB.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
-
- Addict
- Posts: 4527
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Native FormatString (sprintf)
Works on Windows, but not on Linux (and probably not on Mac, too).User_Russian wrote:Code: Select all
s.s{1024} OpenLibrary(0, "MSVCRT.dll") CallCFunction(0, "swprintf", @s, @"Param %i", 10) Debug s
I would appreciate to have a native cross-platform sprintf() function built-in into PureBasic.
For me, this is especially useful when I write programs that support multiple languages.
I then put all strings for one language in a file, and the strings contain placeholders such as %s etc., which will be replaced by their respective current values during runtime of the program. This way, all language dependent strings are easy to handle, and are completely separated from the rest of the program.
So +1 from me for this request.
Re: Native FormatString (sprintf)
Linux has the glibc.
But would about Import with variable parameter?
Code: Select all
ImportC ""
snprintf(a, b, c.p-utf8 ,d)
EndImport
iBufferSize = 256
sOut.s = Space(iBufferSize)
snprintf(@sOut, iBufferSize, "Param %i", 10)
sOut = PeekS(@sOut, -1, #PB_UTF8)
Debug sOut
Re: Native FormatString (sprintf)
As I mentioned, the sXXprintf functions are halfway useless without float support.
Code: Select all
ImportC "MSVCRT.lib"
snprintf(a, b, c.p-utf8, d.f) As "_snprintf"
EndImport
iBufferSize = 256
sOut.s = Space(iBufferSize)
snprintf(@sOut, iBufferSize, "Param %f", 10.2) ;<-- "Param 0.000000"
sOut = PeekS(@sOut, -1, #PB_UTF8)
Debug sOut
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Native FormatString (sprintf)
The import of sprintf don´t work with dynamics's parameters and different datatypes (String, float, double, ...)
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Native FormatString (sprintf)
You could try vsprintf instead but of course a native solution is better.mk-soft wrote:The import of sprintf don´t work with dynamics's parameters and different datatypes (String, float, double, ...)
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Native FormatString (sprintf)
Yes, but how to create va_list with purebasic
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Native FormatString (sprintf)
ImportC sprintf() used to work in earlier PB versions.
But, I lost track after I wrote my own Engineering/Scientific formatter.
Maybe Fred could compile MSVCRT.lib to work with floats instead of going to the trouble of vsprintf()?
But, I lost track after I wrote my own Engineering/Scientific formatter.
Maybe Fred could compile MSVCRT.lib to work with floats instead of going to the trouble of vsprintf()?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Re: Native FormatString (sprintf)
I looked into it but didn't realize that would be so complicatedmk-soft wrote:Yes, but how to create va_list with purebasic
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)