28 #INCLUDE ONCE "../Gir/GLib-2.0.bi"
108 BYVAL Nam
AS CONST gchar
PTR, _
109 BYVAL AttNams
AS CONST gchar
PTR PTR, _
110 BYVAL AttVals
AS CONST gchar
PTR PTR)
AS CONST gchar
PTR
114 IF *AttNams[i] = *Nam
THEN RETURN AttVals[i]
129 #MACRO _START_PARSER(_N_)
130 SUB start_##_N_
CDECL( _
131 BYVAL ctx
AS GMarkupParseContext
PTR, _
132 BYVAL element_name
AS CONST gchar
PTR, _
133 BYVAL AttNams
AS CONST gchar
PTR PTR, _
134 BYVAL AttVals
AS CONST gchar
PTR PTR, _
135 BYVAL UserData
AS gpointer, _
136 BYVAL error_
AS GError
PTR PTR)
152 #MACRO _END_PARSER(_N_)
156 g_markup_parse_context_push(ctx, @
Skip_parser, UserData)
160 SUB end_##_N_
CDECL( _
161 BYVAL ctx
AS GMarkupParseContext
PTR, _
162 BYVAL element_name
AS CONST gchar
PTR, _
163 BYVAL UserData
AS gpointer, _
164 BYVAL error_
AS GError
PTR PTR)
179 #MACRO _NEW_PARSER(_N_)
181 g_markup_parse_context_pop(ctx)
185 STATIC SHARED AS GMarkupParser _N_##_parser =
TYPE(@start_##_N_, @end_##_N_, NULL, NULL, NULL)
190 STATIC SHARED AS GMarkupParser
Skip_parser =
TYPE(NULL, NULL, NULL, NULL, NULL)
210 g_markup_parse_context_push(ctx, @Func_parser, UserData)
224 g_markup_parse_context_pop(ctx)
243 FUNCTION fb_type(
BYVAL Ud
AS ANY PTR)
AS STRING
245 IF 0 =
LEN(.
TypC)
THEN
247 CASE "utf8",
"filename" :
RETURN "/'!'/gchar PTR"
249 IF 0 =
LEN(.
Typ)
THEN RETURN "/'!'/ any PTR"
253 IF LEFT(.Typc, 9) =
"volatile " THEN .
TypC =
MID(.
TypC, 10)
254 IF .Typc =
"gchar" THEN .
TypC =
"gint8"
257 VAR ptrs = 0, i =
LEN(.
TypC) - 1
258 FOR i = i
TO 0 STEP -1
259 IF .
TypC[i] =
ASC(
"*")
THEN ptrs += 1
ELSE EXIT FOR
266 FOR p
AS INTEGER = 1
TO ptrs
274 BYVAL AS GMarkupParseContext
PTR, _
275 BYVAL AS CONST gchar
PTR, _
276 BYVAL AS CONST gchar
PTR PTR, _
277 BYVAL AS CONST gchar
PTR PTR, _
279 BYVAL AS GError
PTR PTR)
283 BYVAL AS GMarkupParseContext
PTR, _
284 BYVAL AS CONST gchar
PTR, _
286 BYVAL AS GError
PTR PTR)
289 STATIC SHARED AS GMarkupParser _
298 SELECT CASE *element_name
299 CASE "parameter",
"instance-parameter"
304 g_markup_parse_context_push(ctx, @
Type_parser, UserData)
308 SELECT CASE *element_name
309 CASE "parameter",
"instance-parameter"
310 g_markup_parse_context_pop(ctx)
336 SELECT CASE *element_name
338 g_markup_parse_context_push(ctx, @
Type_parser, UserData)
340 g_markup_parse_context_push(ctx, @Para_parser, UserData)
344 SELECT CASE *element_name
348 g_markup_parse_context_pop(ctx)
350 g_markup_parse_context_pop(ctx)
360 SELECT CASE *element_name
363 VAR c =
find_value(
"c:type", AttNams, AttVals)
364 IF c
THEN .
TypC = *c
ELSE .
TypC =
""
366 CASE "GLib.List",
"GLib.SList",
"GLib.Array"
368 g_markup_parse_context_push(ctx, @
Skip_parser, UserData)
376 VAR t =
find_value(
"c:type", AttNams, AttVals)
379 IF t
THEN .
TypC = *t
ELSE .
TypC =
""
381 g_markup_parse_context_push(ctx, @
Skip_parser, UserData)
384 IF 0 = n
THEN n =
find_value(
"fixed-size", AttNams, AttVals)
387 g_markup_parse_context_push(ctx, @
Type_parser, UserData)
393 SELECT CASE *element_name
395 IF .Type_flg =
TYPE_LIST THEN g_markup_parse_context_pop(ctx)
400 g_markup_parse_context_pop(ctx)
404 g_markup_parse_context_pop(ctx)
415 SELECT CASE *element_name
416 CASE "method",
"function",
"constructor"
420 VAR bits =
find_value(
"bits", AttNams, AttVals)
422 g_markup_parse_context_push(ctx, @
Type_parser, UserData)
425 VAR r =
find_value(
"readable", AttNams, AttVals)
426 VAR w =
find_value(
"writable", AttNams, AttVals)
427 .
PropRW =
IIF(r
ANDALSO *r =
"0", 0, 1)
428 .
PropRW +=
IIF(w
ANDALSO *w =
"0", 0, 2)
429 g_markup_parse_context_push(ctx, @
Type_parser, UserData)
430 CASE "virtual-method"
433 g_markup_parse_context_push(ctx, @Func_parser, UserData)
438 SELECT CASE *element_name
447 &
" = DYLIBSYMBOL(" & .
OopDll &
", """ & .
FunDll &
""")"
451 FOR i
AS INTEGER = 2
TO .
ParaCnt
454 .
Raus(0) &= NL
" __Obj = " & dll &
"(" & .
ParaStr &
")"
455 .
Raus(0) &= NL
"END CONSTRUCTOR"
468 g_markup_parse_context_pop(ctx)
469 CASE "virtual-method"
476 CASE "method",
"function"
483 IF p
THEN par &=
MID(.
ParaStr, p + 2)
ELSE par &=
")"
486 FOR i
AS INTEGER = 1
TO .
ParaCnt - 1
494 &
" = DYLIBSYMBOL(" & .
OopDll &
", """ & .
FunDll &
""")"
496 .
Raus(0) &= NL
" " & dll &
"(" & parcall &
")"
497 .
Raus(0) &= NL
"END SUB"
501 &
" = DYLIBSYMBOL(" & .
OopDll &
", """ & .
FunDll &
""")"
503 .
Raus(0) &= NL
" RETURN " & dll &
"(" & parcall &
")"
504 .
Raus(0) &= NL
"END FUNCTION"
507 FOR i
AS INTEGER = 0
TO LEN(.
FunNam) - 1
513 .
Raus(0) &= NL
" DIM AS " & .
Typ &
" __r"
514 .
Raus(0) &= NL
" g_object_get(__Obj, """ & .
FunNam &
""", @__r, NULL)"
515 .
Raus(0) &= NL
" RETURN __r"
516 .
Raus(0) &= NL
"END PROPERTY"
521 .
Raus(0) &= NL
" g_object_set(__Obj, """ & .
FunNam &
""", P1, NULL)"
522 .
Raus(0) &= NL
"END PROPERTY"
524 g_markup_parse_context_pop(ctx)
535 SELECT CASE *element_name
538 VAR c =
find_value(
"c:identifier", AttNams, AttVals)
540 VAR bits =
find_value(
"bits", AttNams, AttVals)
542 g_markup_parse_context_push(ctx, @
Type_parser, UserData)
543 CASE "virtual-method"
544 g_markup_parse_context_push(ctx, @
Skip_parser, UserData)
545 CASE "constructor",
"method",
"function"
551 SELECT CASE *element_name
565 g_markup_parse_context_pop(ctx)
566 CASE "virtual-method"
567 g_markup_parse_context_pop(ctx)
568 CASE "method",
"function",
"constructor"
586 SELECT CASE *element_name
589 VAR c =
find_value(
"c:identifier", AttNams, AttVals)
591 VAR bits =
find_value(
"bits", AttNams, AttVals)
593 g_markup_parse_context_push(ctx, @
Type_parser, UserData)
595 .
Level += 1 :
IF .
Level >
UBOUND(.
Raus)
THEN ?"Raus maximum exeded!"
601 g_markup_parse_context_push(ctx,
IIF(
OOP, @Class_parser, @Udt_parser), UserData)
605 SELECT CASE *element_name
617 g_markup_parse_context_pop(ctx)
622 g_markup_parse_context_pop(ctx)
632 SELECT CASE *element_name
635 VAR value =
find_value(
"value", AttNams, AttVals)
636 PRINT #.
FnrBi, NL
" " & nam;
637 IF value
THEN PRINT #.
FnrBi,
" = " &
CINT(*value);
641 SELECT CASE *element_name
652 SELECT CASE *element_name
654 VAR n =
find_value(
IIF(
OOP, @
"name", @
"c:type"), AttNams, AttVals)
655 IF 0 = n
THEN n =
find_value(
"c:identifier", AttNams, AttVals)
658 g_markup_parse_context_push(ctx, @
Type_parser, UserData)
659 CASE "enumeration",
"bitfield"
662 IF LEN(nam)
THEN PRINT #.
FnrBi, NL
"TYPE AS LONG " & nam;
663 PRINT #.
FnrBi, NL
"ENUM";
666 IF OOP THEN PRINT #.
FnrBi,
" EXPLICIT";
667 g_markup_parse_context_push(ctx, @Enum_parser, UserData)
669 VAR n =
find_value(
IIF(
OOP, @
"name", @
"c:type"), AttNams, AttVals)
670 IF 0 = n
THEN n =
find_value(
"glib:type-name", AttNams, AttVals)
672 g_markup_parse_context_push(ctx, @
Type_parser, UserData)
673 CASE "class",
"record",
"interface"
674 VAR n =
find_value(
IIF(
OOP, @
"name", @
"c:type"), AttNams, AttVals)
675 IF 0 = n
THEN n =
find_value(
"glib:type-name", AttNams, AttVals)
677 PRINT #.
FnrBi, NL
"TYPE AS _" & nam &
" " & nam;
679 PRINT #.
FnrBi, NL
"#INCLUDE ONCE """ & _
680 *
find_value(
"name", AttNams, AttVals) &
"-" & _
681 *
find_value(
"version", AttNams, AttVals) &
".bi""";
683 PRINT #.
FnrBi, NL
"' Repository version " & *
find_value(
"version", AttNams, AttVals);
689 VAR dll =
find_value(
"shared-library", AttNams, AttVals) + 3
690 IF dll > 3
ANDALSO 0 =
LEN(.NamDLL)
THEN .
NamDll =
LEFT(*dll,
INSTR(*dll,
".so") - 1)
692 IF 0 =
OOP THEN EXIT SELECT
695 PRINT #.
FnrBi, NL
"DIM SHARED AS ANY PTR " & .
OopDll;
702 SELECT CASE *element_name
706 CASE "utf8",
"filename"
711 g_markup_parse_context_pop(ctx)
712 CASE "enumeration",
"bitfield"
713 PRINT #.
FnrBi, NL
"END ENUM";
714 g_markup_parse_context_pop(ctx)
718 ELSE PRINT #.
FnrBi, NL
"TYPE AS ANY " &
fb_type(UserData);
719 g_markup_parse_context_pop(ctx)
720 CASE "class",
"record",
"interface"
721 CASE "include",
"repository",
"namespace"
730 SELECT CASE *element_name
731 CASE "repository",
"namespace" :
EXIT SUB
734 VAR n =
find_value(*
IIF(
OOP, @
"name", @
"c:type"), AttNams, AttVals)
736 n =
find_value(
"glib:type-name", AttNams, AttVals)
737 IF 0 = n
THEN n =
find_value(
"name", AttNams, AttVals)
740 IF FIRST.find(n)
THEN
742 g_markup_parse_context_push(ctx, @
Skip_parser, UserData)
748 g_markup_parse_context_push(ctx, @
Skip_parser, UserData)
754 SELECT CASE *element_name
755 CASE "interface",
"class"
757 VAR g =
find_value(
"glib:get-type", AttNams, AttVals)
758 IF g
ANDALSO *g <>
"intern" THEN
759 VAR s =
find_value(
"glib:type-struct", AttNams, AttVals) _
761 , p =
INSTR(*g,
"_") _
762 , t2 =
UCASE(
MID(*g, p + 1,
LEN(*g) - p - 9)) _
763 , t3 = t1 &
"TYPE_" & t2 _
764 , t4 = t3 &
", " & *g _
767 .
Raus(0) &= NL
"DECLARE FUNCTION " & *g &
"() AS GType"
768 .
Raus(0) &= NL
"#DEFINE " & t3 &
" (" & *g &
"())"
769 .
Raus(0) &= NL
"#DEFINE " & t1 & t2 &
"(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), " & t3 &
", " & *n &
"))"
770 .
Raus(0) &= NL
"#DEFINE " & t1 & t2 &
"_CLASS(obj) (G_TYPE_CHECK_CLASS_CAST((obj), " & t3 &
", " & t5 &
"))"
771 .
Raus(0) &= NL
"#DEFINE " & t1 &
"IS_" & t2 &
"(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), " & t3 &
"))"
772 IF element_name[0] =
ASC(
"i")
THEN
773 .
Raus(0) &= NL
"#DEFINE " & t1 & t2 &
"_GET_IFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE((obj), " & t3 &
", " & t5 &
"))"
775 .
Raus(0) &= NL
"#DEFINE " & t1 &
"IS_CLASS_" & t2 &
"(obj) (G_TYPE_CHECK_CLASS_TYPE((obj), " & t3 &
"))"
776 .
Raus(0) &= NL
"#DEFINE " & t1 & t2 &
"_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), " & t3 &
", " & t5 &
"))"
783 g_markup_parse_context_push(ctx,
IIF(
OOP, @Class_parser, @Udt_parser), UserData)
788 g_markup_parse_context_push(ctx,
IIF(
OOP, @Class_parser, @Udt_parser), UserData)
792 g_markup_parse_context_push(ctx, @Unio_parser, UserData)
796 g_markup_parse_context_push(ctx, @Func_parser, UserData)
802 g_markup_parse_context_pop(ctx)
806 SELECT CASE *element_name
807 CASE "record",
"interface",
"class"
808 IF .
Level <> 1
THEN ?"Raus level <> 1"
812 FOR i
AS INTEGER = .
RausMax TO 0 STEP -1
816 g_markup_parse_context_pop(ctx)
818 IF .
Level <> 1
THEN ?"Raus level <> 1"
822 FOR i
AS INTEGER = .
RausMax TO 0 STEP -1
826 g_markup_parse_context_pop(ctx)
833 CASE "repository",
"namespace"
843 SELECT CASE *element_name
848 CASE "repository",
"namespace"
852 SELECT CASE *element_name
862 PRINT #.
FnrBi, NL
"END EXTERN"
863 IF OOP THEN PRINT #.
FnrBi, NL
"END NAMESPACE"
875 VAR ctx = g_markup_parse_context_new(@pass##_N_##_parser, 0, @UDat, NULL)
876 IF g_markup_parse_context_parse(ctx, buffer, length, @errr)
THEN _
877 IF 0 = g_markup_parse_context_end_parse(ctx, @errr)
THEN _
878 g_print(!
"Cannot parse %s (invalid content)\n", filename, NULL)
879 g_markup_parse_context_free(ctx)
885 IF COMMAND(1) =
"-v" orelse COMMAND(1) =
"--version" then ?V_TEXT :
END
887 IF __FB_ARGC__ <> 2
orelse _
888 COMMAND(1) =
"-h" orelse COMMAND(1) =
"--help" then ?H_TEXT :
END
890 VAR filename =
COMMAND(1) _
891 , basename =
MID(filename,
INSTRREV(filename,
ANY "\/") + 1)
893 IF g_str_has_suffix(filename,
".gir") _
894 THEN basename =
LEFT(basename,
LEN(basename) - 4) _
895 ELSE filename &=
".gir"
897 VAR ofile = basename &
".bi" _
898 , ifile = basename &
".GirToBac" _
901 DIM AS GError
PTR errr = NULL
904 DIM AS gchar
PTR buffer
907 IF 0 = g_file_get_contents(filename, @buffer, @length, @errr)
THEN
908 ?"Cannot open " & filename;
910 ?"loading " & filename;
916 IF OPEN(ofile
FOR OUTPUT AS .
FnrBi)
THEN
917 ?NL
"Cannot open file for output!";
919 PRINT #.
FnrBi,
"' FreeBasic header file, auto-generated by"
920 PRINT #.
FnrBi,
"' ### GirToBac ###"
921 PRINT #.
FnrBi,
"' LGPLv2.1 (C) 2013-2015 by Thomas[ dot }Freiherr[ at ]gmx[ dot }net"
922 PRINT #.
FnrBi,
"' Auto-translated from file " & filename
924 DIM AS gchar
PTR buff2
926 IF g_file_get_contents(ifile, @buff2, @length2, @errr)
THEN
927 ?NL
"loading " & ifile;
928 VAR ctx = g_markup_parse_context_new(@G2b_parser, 0, @UDat, NULL)
929 IF g_markup_parse_context_parse(ctx, buff2, length2, @errr)
THEN _
930 IF 0 = g_markup_parse_context_end_parse(ctx, @errr)
THEN _
931 g_print(!
"Cannot parse %s (invalid content)\n", ifile, NULL)
932 g_markup_parse_context_free(ctx)
935 ?NL
"generating " & ofile &
" ... ";
937 PRINT #.
FnrBi,
"#INCLUDE ONCE ""_GirToBac-0.0.bi"""
942 PRINT #.
FnrBi, NL
"EXTERN ""C""";
944 PRINT #.
FnrBi, NL
"EXTERN ""C"" LIB """ & .
NamDll &
"""";
947 PRINT #.
FnrBi, NL
"' P_X"
952 IF xx <>
FIRST.
A THEN CONTINUE WHILE
953 proto &= NL !
"\t" & .
NextElm &
" <first>"
958 PRINT #.
FnrBi, NL
"' P_3"
961 PRINT #.
FnrBi, NL
"' P_4"
975 IF LEN(proto)
THEN ?NL
"Symbols in " & ifile &
", but not in " & filename & !
":\n" & proto;