Question on "As Const" construct

General FreeBASIC programming questions.
Post Reply
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Question on "As Const" construct

Post by wallyg »

I have a GTK function defined as follows:

DECLARE FUNCTION gtk_entry_get_text(BYVAL AS GtkEntry PTR) AS CONST gchar PTR

However if I do the following

Dim strData as gchar ptr
strData = gtk_entry_get_text(gtkentry)
i = strlen(strData)

then I get an invalid assignment on line 2

If I change the dim statement to

Dim strData as const gchar ptr

line two compiles ok, but line three objects "Invalid assignment/conversion, at parameter 1 of STRLEN()"

If I try

dim strData as gchar ptr
strData = Cast(gchar ptr,gtk_entry_get_text(gtkentry))

It still objects to the assignment.

If I try

Union uKludge
Dim strConstData As Const gchar Ptr
Dim strData As gchar Ptr
End Union
Dim Kludge As uKludge
Kludge.strConstData = gtk_entry_get_text(gtkentry)
i = strlen(Kludge.strData)

It now works, but it is UGLY UGLY UGLY and makes the code difficult to read by someone else. Is there any simpler way to get around the CONST declaration in the function definition?
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Question on "As Const" construct

Post by TJF »

What about this?

Code: Select all

VAR strData = gtk_entry_get_text(gtkentry)
VAR i = LEN(strData)
Don't care about the types. Let the compiler do this job!
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Question on "As Const" construct

Post by wallyg »

This does not work. The VAR still declares strData as a Const gchar ptr and the use of strData still causes the strlen(strData) statement to generate the error.

I think the problem comes from the GTK definition. I do not think the function should be declared with CONST result. The returned value is not what is susposed to be constant, but the string it points to should not be changed. It is not the value of the pointer that should not be changed.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Question on "As Const" construct

Post by TJF »

Here's the original declaration from the gtkentry.h header (version 3.4.4);

Code: Select all

/* returns a reference to the text */
const gchar* gtk_entry_get_text        (GtkEntry      *entry);
The FB header is OK. Which compiler version do you use on which OS? (My code compiles here on Ubuntu-12.04 with fbc-0.24)

Heureka, this is post # 2222!
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Question on "As Const" construct

Post by wallyg »

I am working on an old project and it has problems (have not checked them out yet) with 0.24 and the 3.x GTK binaries did not work either. I was too busy to try and find out why.

So I am using fbc 0.23 with the last 2.24 gtk stuff/

Oh yeah I have the most up to date windows 7 system.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Question on "As Const" construct

Post by TJF »

Here's the C source from gtkentry.h (version 2.24.6)

Code: Select all

/* returns a reference to the text */
const gchar* gtk_entry_get_text                 (GtkEntry      *entry);
Nothing changed, the FB headers are OK.

As a workaround: you can use
  • i = gtk_entry_get_text_length(gtkentry)
to get the length. (Note: the length is in characters, not bytes.)
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Question on "As Const" construct

Post by wallyg »

Thanks this works for the length. But later on I need to compare this string to a stored string using strcmp and the error still occurs.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Question on "As Const" construct

Post by TJF »

Yes, that's right. -- When you use the original FB header.

The problem is: SWIG (the first tool for FB header translation) does not support the CONST keyword, so it's missing in a bunch of old headers while new headers translated with the more complete tools like fb-frog or h_2_bi are correct. From my point of view it's better to fix the old onces than working around this issue in new headers. (Even if it means changing some FB code bas.)

I made some updates for old headers and some of them I uploaded. But it's too much work for me to make a complete upload package and describe the changes and ... I asked several times for a better platform to do header translation, ... No support from the community. -- OK

Anyway, here's the workaround I use for crt/string.bi (WSTRING function aren't fixed):

Code: Select all

''
''
'' string -- header translated with help of SWIG FB wrapper
''
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
''         be included in other distributions without authorization.
''
'' modified by TJF 120923
''
#ifndef __crt_string_bi__
#define __crt_string_bi__

#include once "crt/stddef.bi"

extern "c"

DECLARE FUNCTION memcpy(BYVAL AS ANY PTR, BYVAL AS CONST ANY PTR, BYVAL AS size_t) AS ANY PTR
DECLARE FUNCTION memmove(BYVAL AS ANY PTR, BYVAL AS CONST ANY PTR, BYVAL AS size_t) AS ANY PTR
DECLARE FUNCTION memset(BYVAL AS ANY PTR, BYVAL AS INTEGER, BYVAL AS size_t) AS ANY PTR
DECLARE FUNCTION memcmp(BYVAL AS CONST ANY PTR, BYVAL AS CONST ANY PTR, BYVAL AS size_t) AS INTEGER
DECLARE FUNCTION memchr(BYVAL AS CONST ANY PTR, BYVAL AS INTEGER, BYVAL AS size_t) AS ANY PTR
DECLARE FUNCTION strcpy(BYVAL AS ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR
DECLARE FUNCTION strncpy(BYVAL AS ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS size_t) AS ZSTRING PTR
DECLARE FUNCTION strcat(BYVAL AS ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR
DECLARE FUNCTION strncat(BYVAL AS ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS size_t) AS ZSTRING PTR
DECLARE FUNCTION strcmp(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS INTEGER
DECLARE FUNCTION strncmp(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS size_t) AS INTEGER
DECLARE FUNCTION strcoll(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS INTEGER
DECLARE FUNCTION strxfrm(BYVAL AS ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS size_t) AS size_t
DECLARE FUNCTION strchr(BYVAL AS CONST ZSTRING PTR, BYVAL AS INTEGER) AS ZSTRING PTR
DECLARE FUNCTION strrchr(BYVAL AS CONST ZSTRING PTR, BYVAL AS INTEGER) AS ZSTRING PTR
DECLARE FUNCTION strcspn(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS size_t
DECLARE FUNCTION strspn(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS size_t
DECLARE FUNCTION strpbrk(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR
DECLARE FUNCTION strstr(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR
DECLARE FUNCTION strtok(BYVAL AS ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR
DECLARE FUNCTION strlen(BYVAL AS CONST ZSTRING PTR) AS size_t
DECLARE FUNCTION strerror(BYVAL AS INTEGER) AS ZSTRING PTR

declare function wcscat (byval as wchar_t ptr, byval as wchar_t ptr) as wchar_t ptr
declare function wcschr (byval as wchar_t ptr, byval as wchar_t) as wchar_t ptr
declare function wcscmp (byval as wchar_t ptr, byval as wchar_t ptr) as integer
declare function wcscoll (byval as wchar_t ptr, byval as wchar_t ptr) as integer
declare function wcscpy (byval as wchar_t ptr, byval as wchar_t ptr) as wchar_t ptr
declare function wcscspn (byval as wchar_t ptr, byval as wchar_t ptr) as size_t
declare function wcslen (byval as wchar_t ptr) as size_t
declare function wcsncat (byval as wchar_t ptr, byval as wchar_t ptr, byval as size_t) as wchar_t ptr
declare function wcsncmp (byval as wchar_t ptr, byval as wchar_t ptr, byval as size_t) as integer
declare function wcsncpy (byval as wchar_t ptr, byval as wchar_t ptr, byval as size_t) as wchar_t ptr
declare function wcspbrk (byval as wchar_t ptr, byval as wchar_t ptr) as wchar_t ptr
declare function wcsrchr (byval as wchar_t ptr, byval as wchar_t) as wchar_t ptr
declare function wcsspn (byval as wchar_t ptr, byval as wchar_t ptr) as size_t
declare function wcsstr (byval as wchar_t ptr, byval as wchar_t ptr) as wchar_t ptr
declare function wcstok (byval as wchar_t ptr, byval as wchar_t ptr) as wchar_t ptr
declare function wcsxfrm (byval as wchar_t ptr, byval as wchar_t ptr, byval as size_t) as size_t

#ifdef __FB_WIN32__
declare function _strerror (byval as zstring ptr) as zstring ptr
declare function _memccpy (byval as any ptr, byval as any ptr, byval as integer, byval as size_t) as any ptr
declare function _memicmp (byval as any ptr, byval as any ptr, byval as size_t) as integer
declare function _strdup (byval as zstring ptr) as zstring ptr
declare function _strcmpi (byval as zstring ptr, byval as zstring ptr) as integer
declare function _stricmp (byval as zstring ptr, byval as zstring ptr) as integer
declare function _stricoll (byval as zstring ptr, byval as zstring ptr) as integer
declare function _strlwr (byval as zstring ptr) as zstring ptr
declare function _strnicmp (byval as zstring ptr, byval as zstring ptr, byval as size_t) as integer
declare function _strnset (byval as zstring ptr, byval as integer, byval as size_t) as zstring ptr
declare function _strrev (byval as zstring ptr) as zstring ptr
declare function _strset (byval as zstring ptr, byval as integer) as zstring ptr
declare function _strupr (byval as zstring ptr) as zstring ptr
declare sub _swab (byval as zstring ptr, byval as zstring ptr, byval as size_t)
declare function _strncoll (byval as zstring ptr, byval as zstring ptr, byval as size_t) as integer
declare function _strnicoll (byval as zstring ptr, byval as zstring ptr, byval as size_t) as integer
declare function _wcsdup (byval as wchar_t ptr) as wchar_t ptr
declare function _wcsicmp (byval as wchar_t ptr, byval as wchar_t ptr) as integer
declare function _wcsicoll (byval as wchar_t ptr, byval as wchar_t ptr) as integer
declare function _wcslwr (byval as wchar_t ptr) as wchar_t ptr
declare function _wcsnicmp (byval as wchar_t ptr, byval as wchar_t ptr, byval as size_t) as integer
declare function _wcsnset (byval as wchar_t ptr, byval as wchar_t, byval as size_t) as wchar_t ptr
declare function _wcsrev (byval as wchar_t ptr) as wchar_t ptr
declare function _wcsset (byval as wchar_t ptr, byval as wchar_t) as wchar_t ptr
declare function _wcsupr (byval as wchar_t ptr) as wchar_t ptr
declare function _wcsncoll (byval as wchar_t ptr, byval as wchar_t ptr, byval as size_t) as integer
declare function _wcsnicoll (byval as wchar_t ptr, byval as wchar_t ptr, byval as size_t) as integer
#endif

end extern

#endif
wallyg
Posts: 267
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Question on "As Const" construct

Post by wallyg »

Thank you. This should take care of the problem. I will put this into my system for future use. I appreciate the help.
Post Reply