|
Post by Pjot on Aug 30, 2017 21:23:39 GMT 1
Hi Joe, Your casting works fine, but you can also force the PRINT statement to print a string by using FORMAT, as follows: PRINT Book1.title FORMAT "%s\n"
When using a string variable without the '$' suffix (this is a string by reference), BaCon still will print this variable as long as it has been declared properly. For example: DECLARE a TYPE STRING
a = "Hello cruel world"
PRINT a
HTH Peter
|
|
|
Post by bigbass on Aug 31, 2017 7:57:31 GMT 1
Thanks Peter
The FORMAT way will do fine most of the time
but I thought to add DEF FN a macro when in c++
' a new macro without formatting or casting or c++ DEF FN PRINTC(x) = std::cout << (x)
PRAGMA INCLUDE <iostream> PRAGMA LDFLAGS PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -fpermissive PRAGMA BACONLIB OPTION PARSE FALSE
' a new macro without formatting or casting or c++ DEF FN PRINTC(x) = std::cout << (x)
PRINTC( "Hello BaCon c++ World") PRINT
struct Books { const char* title; const char* author; const char* subject; int book_id;} (Book1); 'Declare Book1 of type Books
Book1.title = "C++ no GUI"; Book1.author = "bacon user"; Book1.subject = "C++ bacon"; Book1.book_id = 234575;
' using a cast (char*) convertprint$ = (char*)Book1.title PRINT convertprint$ PRINT
' using the official bacon way PRINT Book1.author FORMAT "%s\n" PRINT
' using a DEF FN macro for the lazy people PRINTC(Book1.subject) PRINT
PRINTC(Book1.book_id) PRINT
|
|
|
Post by Pjot on Aug 31, 2017 16:47:02 GMT 1
Hi bigbass, Your 'DEF FN' also works, of course Regards Peter
|
|
|
Post by bigbass on Sept 4, 2017 16:48:24 GMT 1
Hey Guys
I noticed Peter the new HASH table header looks cool and also Alex and vovchik adding gtk to c++
here is a clean demo that uses gtk and compiles in gcc
that gets keystrokes in the terminal
P.S would be nice for it to compile in c++ but not happening anytime soon for me just testing what the c++ compiler will allow also had to fix other code in this thread adding < > for the headers to be seen
Joe
PRAGMA LDFLAGS `pkg-config --libs gtk+-2.0` PRAGMA OPTIONS `pkg-config --cflags gtk+-2.0` PRAGMA INCLUDE <gtk-2.0/gtk/gtk.h> PRAGMA INCLUDE <gtk-2.0/gdk/gdk.h>
'press any key to get the key value from the gtk widget struct
'--- GTK PROTO gtk_init PROTO gtk_main PROTO gtk_widget_show PROTO gtk_window_new PROTO gtk_exit PROTO gtk_window_set_title
'---extras PROTO g_signal_connect_data
'--- GDK PROTO gdk_keyval_name PROTO gdk_keyval_to_unicode
'--- Declare variables DECLARE *window TYPE GtkWidget
'========================================================= FUNCTION key_event(GtkWidget *widget, GdkEventKey *event) '=========================================================
LOCAL keypressed LOCAL $keypressed LOCAL SPECIAL_KEYS keypressed = gdk_keyval_to_unicode(event->keyval) $keypressed = gdk_keyval_name(event->keyval) SPECIAL_KEYS = gdk_unicode_to_keyval(event->keyval) PRINT "KEYPRESSED = ", $keypressed ," UNICODE VALUE= ",keypressed ," SPECIAL KEY= " ,SPECIAL_KEYS HARDWARE_CODE = (event->hardware_keycode) PRINT "event hardware_keycode as a variable ", HARDWARE_CODE PRINT "event hardware_keycode directly using print ",(event->hardware_keycode) gtk_window_set_title(GTK_WINDOW(window), $keypressed ) PRINT "-----------------------------------------------------------------" PRINT RETURN FALSE
END FUNCTION
'--- Callback for closing window '================================ SUB exit_prog '================================ PRINT "QUIT" gtk_exit(0) END SUB
gtk_init (0, 0)
window = gtk_window_new (GTK_WINDOW_TOPLEVEL) gtk_window_set_title(GTK_WINDOW(window), "get key press")
'--- Use as callback the defined SUB/FUNCTIONS g_signal_connect_data(window, "delete-event", exit_prog, 0, 0, 0) g_signal_connect_data(window, "key-release-event", G_CALLBACK(key_event) ,0, 0, 0)
gtk_widget_show (window) gtk_main
|
|
|
Post by alexfish on Sept 4, 2017 17:47:38 GMT 1
Hi Joe
Would this Tempt U
BR Alex
PRAGMA LDFLAGS `pkg-config --libs gtk+-2.0` PRAGMA OPTIONS `pkg-config --cflags gtk+-2.0` PRAGMA COMPILER g++ PRAGMA LDFLAGS -L. PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith OPTION PARSE FALSE
USEH #define SHOW gtk_widget_show_all #define CALLBACK(arg1,arg2,arg3,arg4) g_signal_connect_data(arg1, (char*)arg2,(void*)arg3, arg4, 0, 0) #define DeleteEvent(arg1,arg2) g_signal_connect_data(arg1, (char*)"delete-event",(void*)arg2, 0, 0, 0)
extern "C" { extern char* gtk_window_get_title(long); extern void gtk_window_move(long,int,int); extern long gtk_window_new(int); extern void gtk_window_set_position(long,int); extern void gtk_window_set_resizable(long,int); extern void gtk_window_set_title(long,const char*); extern void gtk_window_set_icon_name(long,char*); extern void g_signal_connect_data(long,char*,void*,long,long,int); extern void gtk_init(int*,void*); extern void gtk_widget_show_all(long); extern void gtk_main(); extern void gtk_widget_set_size_request(long,int,int); } END USEH
SUB QUIT END
END SUB
gtk_init(0,0)
win = gtk_window_new(0) gtk_widget_set_size_request(win,600,400) DeleteEvent(win,QUIT) gtk_window_set_title(win, "get key press") gtk_widget_show_all(win) gtk_main()
|
|
|
Post by bigbass on Sept 5, 2017 15:48:40 GMT 1
Hey Alex
That's perfect for demo code short and to the point! and anyone can add or build onto that example
It compiled and ran as is for me and a good test of BaCon's new power to use the c++ compiler when we have to port some code
Thanks Joe
|
|
|
Post by bigbass on Nov 8, 2017 18:05:10 GMT 1
Hey Guys
Alex did an excellent port of gtk to use the g++ compiler in an above post
I wanted to add a new command USECPLUS like the way we use USEC
and it seems we don't need "extern" but would have to test more
I simplified it more for a demo and added a simple callback for testing purposes the only problem I see is the placement of the USEH it should not be an outer block if you move the END USEH it wont work it should read
USEH END USEH
USECPLUS ENDCPLUS
PRAGMA LDFLAGS `pkg-config --libs gtk+-2.0` PRAGMA OPTIONS `pkg-config --cflags gtk+-2.0` PRAGMA COMPILER g++ PRAGMA LDFLAGS -L. PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith OPTION PARSE FALSE PRAGMA BACONLIB
'--- note that USECPLUS shortens code looks a little more like bacon usec USEH #define USECPLUS extern "C" { #define ENDCPLUS }
USECPLUS void gtk_init(int*,void*); long gtk_window_new(int); void g_signal_connect_data(long,char*,long,long,long,int); void gtk_widget_show_all(long); void gtk_main(); void gtk_main_quit(); ENDCPLUS END USEH
gtk_init(0,0) window = gtk_window_new(0) gtk_widget_show_all(window) '---Connecting the clicked signal to the callback function g_signal_connect_data (window, "destroy",(long) gtk_main_quit,0,0,0) gtk_main()
|
|
|
Post by bigbass on Dec 11, 2017 21:15:44 GMT 1
reference link www.tutorialspoint.com/cprogramming/c_structures.htmThis demo code works below the get the info like this printf( "Book title : %s\n", Book1.title) but having trouble with the typedef Book1 when its a pointer (*Book1) to get the output using -> printf( "Book title : %s\n", book->title); printf( "Book author : %s\n", book->author); printf( "Book subject : %s\n", book->subject); printf( "Book book_id : %d\n", book->book_id); a lot of fltk has classes which I am working on converting and this will help to make things easier later in fltk PRAGMA INCLUDE <string.h> <stdio.h> <iostream> PRAGMA LDFLAGS PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -fpermissive PRAGMA BACONLIB OPTION PARSE FALSE
'--- the undocumented bacon syntax for making a typedef dont use char* with arrays struct Books { \ char title[50]; \ char author[50]; \ char subject[50]; \ int book_id; \ } (Book1)
'--- book 1 specification this will fail with a char* and difficult to debug strcpy(Book1.title, "C++ no GUI") strcpy( Book1.author, "bacon user") strcpy( Book1.subject, "C++ bacon") Book1.book_id = 234575
printf( "Book title : %s\n", Book1.title) printf( "Book author : %s\n", Book1.author) printf( "Book subject : %s\n", Book1.subject) printf( "Book book_id : %d\n", Book1.book_id)
|
|
|
Post by vovchik on Dec 12, 2017 0:01:05 GMT 1
Dear Joe,
I know you are preparing for future things, but char* seems to work OK in that struct typedef (at least in this example):
PRAGMA INCLUDE <string.h> <stdio.h> <iostream> PRAGMA LDFLAGS PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -fpermissive PRAGMA LDFLAGS -L. OPTION PARSE FALSE
struct Books { \ char* title; \ char* author; \ char* subject; \ int book_id; \ } (Book1)
Book1.title = "C++ no GUI" Book1.author = "bacon user" Book1.subject = "C++ bacon" Book1.book_id = 234575 b1$ = (char*)Book1.title b2$ = (char*)Book1.author b3$ = (char*)Book1.subject PRINT b1$, NL$, b2$, NL$, b3$, NL$, Book1.book_id, NL$ printf( "Book title : %s\n", Book1.title) printf( "Book author : %s\n", Book1.author) printf( "Book subject : %s\n", Book1.subject) printf( "Book book_id : %d\n", Book1.book_id)
And we can actually create arrays of that structure (which is convenient) this way, without actually using a typedef:
PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -fpermissive PRAGMA LDFLAGS -L. OPTION PARSE FALSE
struct Books { \ char* title; \ char* author; \ char* subject; \ int book_id; \ } (Book1[3])
Book1[0].title = "C++ no GUI" Book1[0].author = "bacon user" Book1[0].subject = "C++ bacon" Book1[0].book_id = 234575 PRINT ALIGN$("Book title :", 20, 0), ALIGN$(Book1[0].title, 20, 0) PRINT ALIGN$("Book author :", 20, 0), ALIGN$(Book1[0].author, 20, 0) PRINT ALIGN$("Book subject :", 20, 0), ALIGN$(Book1[0].subject, 20, 0) PRINT ALIGN$("Book book_id :", 20, 0), Book1[0].book_id
With kind regards, vovchik
|
|
|
Post by bigbass on Dec 14, 2017 17:00:38 GMT 1
Hey vovchik
Thanks for more example code how to go about it another way and showing the bacon syntax needed
We still have a problem with pointers
a simple demo that will test if all the new fixes for c++ are complete when using pointers if you get this to work all will be ok for many other code demos
this compiles cleanly
#include <stdio.h> #include <string.h> struct Books { char title[50]; char author[50]; char subject[100]; int book_id; };
/* function declaration */ void printBook( struct Books *book ); int main( ) {
struct Books Book1; /* Declare Book1 of type Book */ /* book 1 specification */ strcpy( Book1.title, "C Programming"); strcpy( Book1.author, "Nuha Ali"); strcpy( Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407;
/* print Book1 info by passing address of Book1 */ printBook( &Book1 );
return 0; }
void printBook( struct Books *book ) {
printf( "Book title : %s\n", book->title); printf( "Book author : %s\n", book->author); printf( "Book subject : %s\n", book->subject); printf( "Book book_id : %d\n", book->book_id); }
|
|
|
Post by vovchik on Dec 14, 2017 18:50:05 GMT 1
Dear Joe, Have a look over here: pointersPeter has solved the pointers problem....and here is your code, now BaConized: PROTO printf, strcpy
RECORD Books LOCAL title[50] TYPE char LOCAL author[50] TYPE char LOCAL subject[100] TYPE char LOCAL book_id TYPE int END RECORD
DECLARE Book1 TYPE Books_type
' ------------------ SUB PRINT_BOOK(Books_type* book) ' ------------------ printf("Book title : %s\n", book->title) printf("Book author : %s\n", book->author) printf("Book subject : %s\n", book->subject) printf("Book book_id : %d\n", book->book_id) END SUB
strcpy(Book1.title, "C Programming") strcpy(Book1.author, "Nuha Ali") strcpy(Book1.subject, "C Programming Tutorial") Book1.book_id = 6495407 PRINT_BOOK(&Book1) With kind regards, vovchik
|
|
|
Post by bigbass on Dec 15, 2017 6:55:45 GMT 1
Hey vovchik I am really amazed that compiles without any fixes using bacon 3.6 and thank you for doing it because I tried everything and nothing worked for me and I am puzzled by this syntax DECLARE Book1 TYPE Books_type' ------------------ SUB PRINT_BOOK( Books_type* book) This wont work without the word _type or _TYPE added onto the "TYPE" seems like a parsing problem looking for a keyword type it should work with a DECLARE Book1 TYPE Booksfrom a struct I am just happy that it works and if I am wrong about the syntax then I am wrong and will scratch my head wondering for awhile more Joe
|
|
|
Post by vovchik on Dec 15, 2017 8:47:07 GMT 1
Dear Joe,
Great that the example worked in 3.6. Peter describes that _type business in the manual as follows:
And RECORDs are structures. If you want to use *x = xxx->yyy (pointers) and get no errors, please use the latest 3.7 beta, where Peter has provided some enhancement in pointer casting, so that that construction compiles without errors or warnings. Alex's buffer example had that type of assignment, and it now works fine in BaCon.
With kind regards, vovchik
|
|
|
Post by bigbass on Dec 16, 2017 4:27:01 GMT 1
Hey vovchik
Thanks again if thats the only way it will work when the typedef is a pointer
then we must use that syntax
when the typedef is a struct non pointer we can use the c or c++ syntax
at least during the testing stage before full porting to bacon
have to rethink now how to get at the the events in fltk /fluid with this syntax
Joe
|
|
|
Post by bigbass on Dec 16, 2017 5:20:36 GMT 1
well just to show it in g++ with a minor twist of naming the typedef and only declaring in the sub
this works with bacon 3.6 without any fixes
I can except this way also along with the others
Joe
PRAGMA INCLUDE <string.h> <stdio.h> <iostream> PRAGMA LDFLAGS PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -fpermissive PRAGMA BACONLIB OPTION PARSE FALSE
'--- start with the typedef name you want to use and forget about struct RECORD Book1 LOCAL title[50] TYPE char LOCAL author[50] TYPE char LOCAL subject[100] TYPE char LOCAL book_id TYPE int END RECORD
'--- must add the magic keyword "_type" to DECLARE the typedef when using a pointer ' ------------------ SUB PRINT_BOOK(Book1_type *book) ' ------------------ printf("Book title : %s\n", book->title) printf("Book author : %s\n", book->author) printf("Book subject : %s\n", book->subject) printf("Book book_id : %d\n", book->book_id) END SUB
strcpy(Book1.title, "C Programming") strcpy(Book1.author, "Nuha Ali") strcpy(Book1.subject, "C Programming Tutorial") Book1.book_id = 6495407 PRINT_BOOK(&Book1)
|
|