Author Topic: How to use the stdio.h standard ANSI C library with QB64?  (Read 4719 times)

0 Members and 1 Guest are viewing this topic.

Offline Fifi

  • Forum Regular
  • Posts: 181
    • View Profile
    • My small QB64 contribution
How to use the stdio.h standard ANSI C library with QB64?
« on: August 15, 2018, 11:19:03 am »
Hello,

QB64 is supposed to be able to use external libraries.

However, I didn't find any sample to do it.

So, the subject of this post is simple: how to use the stdio.h standard ANSI C library in order to use some of its functions such as prinf()?

A smal piece of code showing such a use would be very appreciated. ;)

Best regards.
It's better to look like an idiot for a short time while asking something obvious to an expert than pretending to be smart all your life. (C) Me.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: How to use the stdio.h standard ANSI C library with QB64?
« Reply #1 on: August 15, 2018, 11:59:40 am »
Please take a look into my Libraries collection (see signature), especially the QB-StdLibs folder. There's also an example in it to showcase the usage.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Fifi

  • Forum Regular
  • Posts: 181
    • View Profile
    • My small QB64 contribution
Re: How to use the stdio.h standard ANSI C library with QB64 x64?
« Reply #2 on: August 15, 2018, 01:21:13 pm »
Hi RhoSigma,

Please take a look into my Libraries collection (see signature), especially the QB-StdLibs folder. There's also an example in it to showcase the usage.

Thanx for the link.

Just downloaded your QB64Library and the first thing I did was to read the QB64Library.txt file.

So, unfortunately, the library I'd like to use (qbstdio.h/.bi/.bm) isn't usable with QB64 x64 that is the sole QB64 release I use and exclusively on Linux x64.

So my question still is the same: how to use the standard stdio.h ANSI C library from QB64 v1.2 x64.

Any short piece of code showing how to use the printf() function would be truly appreciated Vs. my poor, inelegant and inefficiant code below to simulate it.

Code: QB64: [Select]
  1. '
  2. ' program name: Print_Using.bas
  3. '
  4. ' This small program formats a float with two separate blocks.
  5. ' The first block is the interger part of the float separated
  6. ' in groups of three didits by a comma, the second group is
  7. ' the decimal part of the float rounded to 2 digits after the
  8. ' decimal point.
  9. ' The main purpose of this program is to format a float without
  10. ' using the PRINT USING command with a mask that places spaces
  11. ' in front of the mask when the float is smaller than the mask
  12. ' and that puts the % character when the float is larger than
  13. ' the mask.
  14. ' A better solution would be:
  15. ' 1) to modifiy the PRINT USING command in the way ANSI C or PHP
  16. '    printf() commands behave such as: printf("%.2f", float).
  17. ' 2) or to use the standard stdio.h library of the C ANSI to be
  18. '    able to use the printf() and sprintf() and other commands
  19. '    of the library.
  20. '
  21. ' title of the windows when used in a console
  22. '
  23. _TITLE "Print_Using"
  24. '
  25. ' variable initialisation
  26. '
  27. Number# = 1.00
  28. '
  29. ' main program
  30. '
  31. WHILE Number# <> 0
  32.     PRINT "Please enter a float: ";
  33.     INPUT (Number#)
  34.     PRINT "Using this function will print:" + Print_Using$(Number#)
  35.     PRINT "PRINT USING command will print:";
  36.     PRINT USING "#,###,###,###.##"; Number#
  37. '
  38. ' function to format the integer part of a float and round its decimal part at 2 digits.
  39. '
  40. FUNCTION Print_Using$ (nb#)
  41.     Print_Using$ = intPart(nb#) + decPart(nb#)
  42. '
  43. ' function to format and round the decimal part of a float at 2 digits.
  44. '
  45. FUNCTION decPart$ (nb#)
  46.     dot$ = "."
  47.     dotPos% = INSTR(LTRIM$(STR$(nb#)), dot$)
  48.     IF dotPos% = 0 THEN
  49.         partdec$ = ".00"
  50.     ELSE
  51.         partdec$ = MID$(LTRIM$(STR$(nb#)), dotPos%)
  52.         strlen% = LEN(partdec$)
  53.     END IF
  54.     IF strlen% = 1 THEN
  55.         partdec$ = ".00"
  56.     ELSE
  57.         SELECT CASE strlen%
  58.             CASE 2
  59.                 partdec$ = partdec$ + "0"
  60.             CASE IS > 3
  61.                 IF VAL(MID$(partdec$, 4, 1)) >= 5 THEN
  62.                     MID$(partdec$, 3, 1) = LTRIM$(STR$(VAL(MID$(partdec$, 3, 1)) + 1))
  63.                 END IF
  64.                 partdec$ = LEFT$(partdec$, 3)
  65.         END SELECT
  66.     END IF
  67.     decPart$ = partdec$
  68. '
  69. ' function to format the integer part of a float with groups of 3 digits.
  70. '
  71. FUNCTION intPart$ (nb#)
  72.     i$ = LTRIM$(RTRIM$(STR$(FIX(nb#))))
  73.     IF nb# < 0 THEN
  74.         i$ = MID$(i$, 2)
  75.     END IF
  76.     lenI = LEN(i$)
  77.     IF lenI > 3 THEN
  78.         m3 = lenI MOD 3
  79.         IF m3 <> 0 THEN
  80.             i$ = LEFT$(i$, m3) + "," + MID$(i$, m3 + 1)
  81.         END IF
  82.         idx = m3 + 4 + (m3 = 0)
  83.         WHILE idx < LEN(i$)
  84.             i$ = LEFT$(i$, idx) + "," + MID$(i$, idx + 1)
  85.             idx = idx + 4
  86.         WEND
  87.     ELSEIF lenI = 0 THEN
  88.         i$ = "0"
  89.     END IF
  90.     IF nb# < 0 AND lenI > 0 THEN
  91.         i$ = "-" + i$
  92.     END IF
  93.     intPart$ = i$
  94.  

Best regards.
It's better to look like an idiot for a short time while asking something obvious to an expert than pretending to be smart all your life. (C) Me.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: How to use the stdio.h standard ANSI C library with QB64 x64?
« Reply #3 on: August 15, 2018, 03:30:43 pm »
So, unfortunately, the library I'd like to use (qbstdio.h/.bi/.bm) isn't usable with QB64 x64 that is the sole QB64 release I use and exclusively on Linux x64.

No problem, its just a pointer size thing, which can be fixed quickly. It's already on my TODO list for a while, but as I use mainly the Win x32 version it was no high priority, but now there's a reason to do it, just give me a day.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Fifi

  • Forum Regular
  • Posts: 181
    • View Profile
    • My small QB64 contribution
Re: How to use the stdio.h standard ANSI C library with QB64 x64?
« Reply #4 on: August 16, 2018, 09:02:35 am »
Hi RhoSigma,

No problem, its just a pointer size thing, which can be fixed quickly. It's already on my TODO list for a while, but as I use mainly the Win x32 version it was no high priority, but now there's a reason to do it, just give me a day.

Thanx again for your proposal and for your time.

Meanwhile, I would like to learn how to use the ANSI C standard stdio.h library since there are many other functions I'd like to use.

So, my request still is how to use this library from QB64 since I could not find any sample on the net.

Then, a short piece of code implementaing this library with the exemple of the call of sprinf() or fprintf() or any other function of such a library would be very usefull for many others than me.

TIA for your advice.

Best regards.
It's better to look like an idiot for a short time while asking something obvious to an expert than pretending to be smart all your life. (C) Me.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: How to use the stdio.h standard ANSI C library with QB64?
« Reply #5 on: August 16, 2018, 09:33:30 am »
Hi Fifi,

just completed the revision of my library to make it work with x64. Need another couple hours to test anything and put together some info for your stdio.h usage request. Stay tuned, i'll let you know when the "Libraries Collection" download in my signature is updated with the new version.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Fifi

  • Forum Regular
  • Posts: 181
    • View Profile
    • My small QB64 contribution
Re: How to use the stdio.h standard ANSI C library with QB64?
« Reply #6 on: August 16, 2018, 10:14:03 am »
Hi RhoSigma,

Hi Fifi,
just completed the revision of my library to make it work with x64. Need another couple hours to test anything and put together some info for your stdio.h usage request. Stay tuned, i'll let you know when the "Libraries Collection" download in my signature is updated with the new version.

Don't worry, there is no urgency on my side.

I just need to learn how to do that.

Anyhow, thanx again for your time.

Kind regards.
It's better to look like an idiot for a short time while asking something obvious to an expert than pretending to be smart all your life. (C) Me.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: How to use the stdio.h standard ANSI C library with QB64?
« Reply #7 on: August 17, 2018, 07:17:29 am »
Hy guys
I find your activities on QB64.ORG engaging...
'---------------------------------------------------------------
'main
Hi Fifi
WHILE Rho go down to code by own some function,
               you can see at these wiki's pages
                              http://qb64.org/wiki/C_Libraries
                              http://qb64.org/wiki/Libraries
WEND
IF on [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there] forum there is also an example of external library made in PASCAL  THEN
               maybe that perspective is wider of that I know
ELSE
               you can try by your own some code in that direction
END IF
END
'logic end of program

PS:
Do you know where I can search settings or switches of  C compiler of QB64?
Programming isn't difficult, only it's  consuming time and coffee

FellippeHeitor

  • Guest
Re: How to use the stdio.h standard ANSI C library with QB64?
« Reply #8 on: August 17, 2018, 07:23:09 am »
Hi tempodi.

Just type qb64 /help in the command line.

Aaaaaah, sorry. Misread your request.

You will find the line that is used to compile a program you wrote in internal/temp/recompile.bat
« Last Edit: August 17, 2018, 07:25:19 am by FellippeHeitor »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: How to use the stdio.h standard ANSI C library with QB64?
« Reply #9 on: August 17, 2018, 07:39:38 am »
Hi Fellippe

Thanks
I'll watch into  internal/temp/recompile.bat
Programming isn't difficult, only it's  consuming time and coffee

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: How to use the stdio.h standard ANSI C library with QB64?
« Reply #10 on: August 20, 2018, 07:27:40 pm »
Hi Fifi,

finally I've have updated the "Libraries collection" download in my signature with fixed versions of my "QB-StdLibs", which are now usable with QB64 x32 as well as x64. Please download this new version, unpack and make sure you move the entire "QB64Library" folder into your QB64 home folder (where qb64.exe is in).

As for your request about how to use  C library functions you already got the two wiki links from TempodiBasic, which give you information about the C vs. QB64 datatypes and how to declare the functions in QB64. Ontop of this I'll give you a short example for the vsprintf() function. First note that we not use the regular printf(), fprintf() or sprintf(), as these use variable argument lists (the ... in C/C++). This is not possible in QB64, hence we use the stub functions which expect a pointer to such a list instead. These stub functions usually start with a "v", such as vprintf(), vfprintf() or vsprintf(). So here's the example for the latter one:
Code: QB64: [Select]
  1.     FUNCTION vsprintf& (s$, format$, arg$)
  2.     'function defined according to its C/C++ syntax:
  3.     'int vsprintf (char * s, const char * format, va_list arg );
  4.  
  5. buf$ = SPACE$(256) 'reserve a sufficient output buffer
  6. num& = vsprintf&(buf$, "%.2f" + CHR$(0), MKD$(123.45678)) 'call function
  7. PRINT LEFT$(buf$, num&) 'print result
  8.  
  9.  
However, as this looks very easy, it becomes more complicated as soon as you wanna format more than one number in one vsprint() call, especially if it maybe shall be INTEGER (%) or LONG (&) values, which must be (depending on x32 or x64) padded so that the next entry in the variable args list falls to a x32 (4-Byte) or x64 (8-Byte) boundary again. If you would even like to pass strings as variable arguments (for the %s format token), then the real problems start, you can not simply add the string in between your variable arguments list, but you need to place its pointer in memory into the list. As it is practically impossible to get such a memory-pointer for regular variable length strings in QB64, you can't do it. A solution would be a fixed-length string, get its reference in a _MEM variable and use mem.OFFSET, but once again depending on x32 or x64 we would need to add this pointer as either LONG (32bit) or _INTEGER64 (64bit), which gives us the next problem. You can't convert a OFFSET variable into anything else using eg. a MKL$(mem.OFFSET). Here we need to cheat QB64, I use a simple wrapper function in qbstdarg.h to do that, Steve did another interesting approch here: http://qb64.freeforums.net/thread/52/convert-offset-integer64.

Fortunatly you don't need to deal with all this, as my "QB-StdLibs" do wrap all this internal stuff into some easy to use regular QB64 functions. In qbstdarg.bm you'll find all functions to setup variable argument lists with any number of any type values in it, qbstdio.bm gives you VPrintF, VFPrintF and VSPrintF$ (as QB64 synonymes for the same named C library functions) and if you need, you also find date/time formatting functions in qbtime.bm. So here's the code example from above modified for use of my library:
Code: QB64: [Select]
  1. '$INCLUDE: 'QB64Library\QB-StdLibs\qbstdarg.bi'
  2. '$INCLUDE: 'QB64Library\QB-StdLibs\qbstdio.bi'
  3.  
  4. InitArgs arg$
  5. DoubleArg arg$, 123.456789
  6. VPrintF "%.2f\n", arg$
  7. FreeArgs arg$
  8.  
  9.  
  10. '$INCLUDE: 'QB64Library\QB-StdLibs\qbstdarg.bm'
  11. '$INCLUDE: 'QB64Library\QB-StdLibs\qbstdio.bm'
  12.  
As you can see, you don't need to deal with DECLARE LIBRARY, providing sufficient buffers, adding CHR$(0) or extracting the actually written number of chars out of the buffer. To get a general overview and a better understanding for the dependencies I'd recommend to read through all function documentations in qbstdarg.bm, qbstdio.bm and qbtime.bm at least once and also have a look into the "example" sub-folder. If any questions remain, then don't hesitate to ask.

kind regards
« Last Edit: August 20, 2018, 07:55:55 pm by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Fifi

  • Forum Regular
  • Posts: 181
    • View Profile
    • My small QB64 contribution
Re: How to use the stdio.h standard ANSI C library with QB64?
« Reply #11 on: September 11, 2018, 06:35:38 pm »
Hi RhoSigma,

Thanx for your excellent job.

Cheers.
Fifi
It's better to look like an idiot for a short time while asking something obvious to an expert than pretending to be smart all your life. (C) Me.