Author Topic: Characters zero in the string  (Read 649 times)

Pietro54

  • Newbie
  • *
  • Posts: 17
    • View Profile
Characters zero in the string
« on: April 30, 2023, 09:30:09 AM »
HI

I was trying to build a function to activate the dialog file for use with the Chameleon Basic compiler (a 32-bit NASM assembler converting compiler similar to Brian C. Becker's original Kool Basic, adapted and repurposed by Chris Morningstar) when I noticed the following thing:
when passing parameters to the GetOpenFileName() function there were errors in passing the string with file filters.

In the C function this string uses the zero character to separate the file types in an array of characters.

The basic construction of the string was as follows:
MyFilter$ = "All Files" + chr$(0) + ”*.*” + chr$(0) + “Text Files(.txt)” + chr$(0) + ”*.txt” + chr$( 0) + ”Dbase Files(.dbf)” + chr$(0) + “*.dbf” + chr$(0) + chr$(0)
but when passed as a parameter in basic the string stops at the first chr$(0).

After several attempts I discovered that almost all compilers correctly evaluate the length of a string that contains the zero character inside. For BCX, Chameleon and KoolBasic, however, no: the zero character is simply ignored; other characters such as chr$(1), chr$(2) and chr$(3) and more are considered valid.

I enclose a zip with the tests carried out with 16, 32 and 64 bit compilers and the test table.

For BCX is constructive choice or is it a mistake?

I apologize for my bad English.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1966
    • View Profile
Re: Characters zero in the string
« Reply #1 on: April 30, 2023, 09:53:44 AM »
BCX uses C-style, null-terminated strings.

If all you want is to write a function in BCX that launches an OPEN or SAVE file dialog,
check out this built-in BCX function which eliminates the null-character problem that
you are having.

https://bcxbasiccoders.com/webhelp/html/getfilename$function.htm

Pietro54

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Characters zero in the string
« Reply #2 on: April 30, 2023, 11:13:31 AM »
Thanks for your quick reply.
Obviously I built a DLL with BCX GETFILENAME$ that works fine.
Mine was just curiosity about the difference in behavior compared to other compilers.
Best regards.

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 1966
    • View Profile
Re: Characters zero in the string
« Reply #3 on: April 30, 2023, 11:36:25 AM »
It is possible to embed nulls inside a BCX string but not in the manner that your samples use.
My point is that it is not a bug. 

Reference: BCX Help

BCX uses ASCIIZ strings which are terminated with an ASCII NULL terminator character to mark the end of the string.  A string must be sized large enough to include this terminator. For example if a string contains 15 characters then it must be sized to at least 16 bytes.




Robert

  • Hero Member
  • *****
  • Posts: 1163
    • View Profile
Re: Characters zero in the string
« Reply #4 on: April 30, 2023, 03:37:35 PM »
Hi Pietro54:

As MrBCX wrote, the 'C' language terminating NULL character is used to mark the end of a standard BCX string. Some other BASIC dialects use BSTR as their standard and, in fact, some of BCX's COM functions use BSTR. Microsoft BSTR details here.

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/automat/bstr

Here is a way to use MACRO to embed NULL in a BCX string.

Code: [Select]

$IPRINT_OFF
  MACRO Filter = "Icons(*.ico)\0*.ICO\0All files(*.*)\0*.*"
$IPRINT_ON

It can be used in some BCX functions but do not try and process it with any BCX functions that use 'C' strlen.

The code snippets below can be used to embed NULL in the initialization of the lpstrFilter member in the OPENFILENAME structure used by the Win32 API GetOpenFileName and GetSaveFileName dialog box functions.

Code: [Select]

$IPRINT_OFF
 MACRO szBASFilter$   = "BAS Files (*.bas)\0*.bas\0\0"
 MACRO szRCFilter$    = "RC Files (*.rc)\0*.rc\0\0"
 MACRO szDLGFilter$   = "DLG Files (*.dlg)\0*.dlg\0\0"
 MACRO szDLGRCFilter$ = "DLG Files (*.dlg)\0*.dlg\0RC Files (*.rc)\0*.rc\0\0"
 MACRO szCFilter$     = "C Files (*.c)\0*.c\0\0"
 MACRO szHFilter$     = "H Files (*.h)\0*.h\0\0"
 MACRO szEXEFilter$   = "EXE Files (*.exe)\0*.exe\0\0"
 MACRO szCHMFilter$   = "CHM Files (*.chm)\0*.chm\0\0"
 MACRO szHLPFilter$   = "HLP Files (*.hlp)\0*.hlp\0\0"
$IPRINT_ON

Code: [Select]

$IPRINT_OFF
 MACRO szFilter$     = "EXE Files (*.exe)\0*.exe\0BAS Files (*.bas)\0*.bas\0All Files (*.*)\0*.*\0\0"
 MACRO szFilterBas$  = "BAS Files (*.bas)\0*.bas\0All Files (*.*)\0*.*\0\0"
 MACRO szFilterObj$  = "LIB/OBJ Files (*.lib)\0*.lib\0OBJ Files (*.obj)\0*.obj\0All Files (*.*)\0*.*\0\0"
$IPRINT_ON



Pietro54

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Characters zero in the string
« Reply #5 on: May 03, 2023, 07:00:07 AM »
Thanks everyone for the explanation.
I thought it was enough to have a little basic knowledge to be able to make any program.
Evidently each version of basic has its own characteristics that I need to know thoroughly.
I think I'll have to re-read the BCX manual carefully!
Greetings

Vortex

  • Full Member
  • ***
  • Posts: 108
    • View Profile
Re: Characters zero in the string
« Reply #6 on: May 04, 2023, 01:59:14 PM »
Hi Pietro54,

Is it possible to visit the website of the Chameleon Basic compiler? Could you provide a link? Thanks.

Pietro54

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Characters zero in the string
« Reply #7 on: May 08, 2023, 07:55:13 AM »
Hi Vortex
There are no more working links of Chameleon Basic!
You can download KoolBasic 16.0 from Brian C. Beker's website at:
http://www.briancbecker.com/blog/projects/compiler-tutorial/
If you send me your email address to pierino.moro@live.it I can share on OneDrive the original Chameleon.rar file and a mirror copy of the one I use with libraries organized differently.
If the message "Could not allocate memory" appears during compilation, change the original GoLink.exe file to the latest version.
Greetings