#!./ginsbasic001ok
include "wxbasicscript/basefunctions.inc"

gtkdocdir = "/mnt/sda7/gtk/gtk/share/gtk-doc/html/"


workingfiles = {
"gtk/gtk-General.html" ,
"gtk/gtk-Clipboards.html" ,
"gtk/gtk-Resource-Files.html" , 
"gtk/GtkLabel.html" , 
"gtk/GtkButton.html"
}

'workingfiles = {
'"gtk/gtk-Resource-Files.html"
'}


allowedargs = {"gint" , "gboolean" , "gdouble" , "const gchar" , "gchar", "gfloat"}


droppedfunctions = {}
droppedfunctionscounter = 0
usedfunctions = {}
usedfunctionscounter = 0

flinecounter = 0
ginsfunctions = {}

for each thefile in workingfiles

thefile = gtkdocdir & thefile

html = readfiletolist(thefile)



c=0
done = 0
for each theline in html

  if instr(theline , "<pre") then
    start = 1
  end if

  if done = 0 then
  if start = 1 then
  if left(theline , 1) != " " then
    //print theline

    checkmore = 0

    if left(theline , 4) = "void" then
      ret = "void"
      checkmore = 1
    end if
    if left(theline , 5) = "guint" then
      ret = "guint"
      checkmore = 1
    end if
    if left(theline , 8) = "gboolean" then
      ret = "gboolean"
      checkmore = 1
    end if
    if left(theline , 12) = "const gchar*" then
      ret = "const gchar*"
      checkmore = 1
    end if
    if left(theline , 7) = "gdouble" then
      ret = "gdouble"
      checkmore = 1
    end if
    if left(theline , 6) = "gchar*" then
      ret = "gchar*"
      checkmore = 1
    end if
    if left(theline , 7) = "gchar**" then
      ret = "gchar**"
      checkmore = 1
    end if

    if checkmore = 1 then

      firstarg = "(obj)"

      f = cutleft(theline , "\">")
      functionname = cutrightfromleft(f , "<")
      widget = cutleft(f , "(" )
      widget = cutleft(widget , ">")
      widget = cutrightfromleft(widget , "<")

      arg1 = ""
      arg2 = ""

      if left(html[c+1] , 1) = " " then

        arg1 = trim(html[c+1]) 

        arg1 = replace(arg1 , "," , ")")
        arg1 = cutright(arg1 , ")")
        arg1 = trim(arg1)

// error in docs, gtk_rc_parse_color
if arg1 = "" then
   print "\n\nERROR: \n" & functionname
   print "ERROR Fix Documentation source!!!!!!!!!!!!!!!\n"
end if

      end if

if arg1 != "" then
      if left(html[c+2] , 1) = " " then
        arg2 = html[c+2] 
        arg2 = replace(arg2 , "," , ")")
        arg2 = cutright(arg2 , ")")
        arg2 = trim(arg2)
      end if
end if

      ret = trim(ret)
      functionname = trim(functionname)
      widget = trim(widget)
      if ucase(left(widget , 3)) = "GTK" then
        widget = ucase(widget)
        widget = replace(widget , "GTK" , "GTK_")
        widget = replace(widget , "GTK_RCSTYLE", "")
      end if

      //-- some functions do not use a widget, but a string or so 
      if widget = "" then
'print functionname
        widget = cutleft(f , "(" )
        widget = replace(widget , "," , ")")
        widget = cutright(widget , ")")
'print widget
'print arg1

        if widget = "void" then
          widget = ""
        end if
        firstarg = widget

      end if

//      print ret, functionname, widget, arg1, arg2


     //-- some argumenttypes are incompatible, so skip those functions
      dobuild = 0

if arg1 = "" then
if arg2 = "" then
      dobuild = 1
end if
end if

if dobuild = 0 then
      dobuild1 = 0
      for each argtype in allowedargs
        if left(arg1 , len(argtype)) = argtype then
          dobuild1 = 1
        end if
      next
      dobuild2 = 0
      if arg2 = "" then
        dobuild2 = 1
      else
        for each argtype in allowedargs
          if left(arg2 , len(argtype)) = argtype then
            dobuild2 = 1
          end if
        next
      end if
end if

      if dobuild1 = 1 then
        if dobuild2 = 1 then
          dobuild = 1
        end if
      end if

      //-- *** glibc detected *** free(): invalid pointer: 0x40295f20 ***
      if ret = "gchar**" then
          dobuild = 0
        end if

     //-- build the final C-Code
      if dobuild = 1 then
        buildfunction()
        usedfunctions[usedfunctionscounter] = "used: " & ret & " " & functionname & " " & widget & " " & arg1 & " " & arg2
        usedfunctionscounter += 1
      else
        droppedfunctions[droppedfunctionscounter] = "dropped: " & ret & " " & functionname & " " & widget & " " & arg1 & " " & arg2
        droppedfunctionscounter += 1
        //print "  DROPPING: " & functionname
        //print
      end if
    end if


  end if
  end if
  end if

  if instr(theline , "</pre>") then
    start = 0
    done = 1
  end if
  c+=1
next
next

for each used in usedfunctions
  print used
next

for each dropped in droppedfunctions
  print dropped
next

ginshead = readfiletolist("gins.c-header")
writelisttofile("gins.c" , ginshead)
ginshead = {}
appendlisttofile("gins.c" , ginsfunctions)
ginsfunctions = {}
ginsfoot = readfiletolist("gins.c-footer")
appendlisttofile("gins.c" , ginsfoot)
ginsfoot = {}

function buildfunction()

  ginsfunctions[flinecounter] = "  if (!strcmp( thefunction , \"" &  functionname & "\")) {"
  flinecounter += 1

  if ret = "void" then

    if firstarg = "(obj)" then
      ginsfunctions[flinecounter] = "    " & functionname & "( " & widget & "(obj)"
    else
      if widget != "" then
        ginsfunctions[flinecounter] = "    " & functionname & "( objid " 
      else
        ginsfunctions[flinecounter] = "    " & functionname & "( "
      end if
    end if
    cleanarguments()

    ginsfunctions[flinecounter] &= " );"
    flinecounter += 1

    ginsfunctions[flinecounter] = "    returnstring(\"Nothing\");"
    flinecounter += 1

  else

    resulttype = "resultbuffer"
    returnstring = "    sprintf(result , \"%s\" , " & resulttype & ");"

    if ret = "gboolean" then
      resulttype = "resultgboolean"
      returnstring = "    sprintf(result , \"%d\" , " & resulttype & ");"
    end if
    if ret = "guint" then
      resulttype = "resultguint"
      returnstring = "    sprintf(result , \"%d\" , " & resulttype & ");"
    end if
    if ret = "gdouble" then
      resulttype = "resultgdouble"
      returnstring = "    sprintf(result , \"%f\" , " & resulttype & ");"
    end if

    //ginsfunctions[flinecounter] = "    " & resulttype & " = " & functionname & "( " & widget & "(obj)"

    if firstarg = "(obj)" then
      ginsfunctions[flinecounter] = "    " & resulttype & " = " & functionname & "( " & widget & "(obj)"
    else
      if widget != "" then
        ginsfunctions[flinecounter] = "    " & resulttype & " = " &functionname & "( objid " 
      else
        ginsfunctions[flinecounter] = "    " &resulttype & " = " & functionname & "( "

      end if
    end if
'print "-------" &  functionname , arg1
    cleanarguments()

    ginsfunctions[flinecounter] &= " );"
    flinecounter += 1

    ginsfunctions[flinecounter] = returnstring
    flinecounter += 1
    ginsfunctions[flinecounter] = "    returnstring(result);"
    flinecounter += 1

  end if

    ginsfunctions[flinecounter] = "    free(resultbuffer);"
    flinecounter += 1
    ginsfunctions[flinecounter] = "    return;"
    flinecounter += 1
    ginsfunctions[flinecounter] = "  }"
    flinecounter += 1


end function

function cleanarguments()

    //-- this function converts some argumenttypes and builds the string for gins.c

    if arg1 != "" then

      convertedarg = 0

      if left(arg1 , 7) = "gdouble" then
        ginsfunctions[flinecounter] &= " , atof(arg1) "
        convertedarg = 1
      end if 

      if left(arg1 , 4) = "gint" then
        ginsfunctions[flinecounter] &= " , atoi(arg1) "
        convertedarg = 1
      end if 

      if left(arg1 , 8) = "gboolean" then
        ginsfunctions[flinecounter] &= " , atoi(arg1) "
        convertedarg = 1
      end if 

      if left(arg1 , 8) != "gfloat *" then   
      if left(arg1 , 6) = "gfloat" then
        ginsfunctions[flinecounter] &= " , atof(arg1) "
        convertedarg = 1
      end if 
      end if


      if convertedarg = 0 then
        ginsfunctions[flinecounter] &= " , arg1 "
      end if

      if arg2 != "" then

        convertedarg = 0

        if left(arg2 , 7) = "gdouble" then
          ginsfunctions[flinecounter] &= " , atof(arg2) "
          convertedarg = 1
        end if 

        if left(arg2 , 4) = "gint" then
          ginsfunctions[flinecounter] &= " , atoi(arg2) "
          convertedarg = 1
        end if 

        if left(arg2 , 8) = "gboolean" then
          ginsfunctions[flinecounter] &= " , atoi(arg2) "
          convertedarg = 1
        end if 

        if left(arg2 , 8) != "gfloat *" then   
        if left(arg2 , 6) = "gfloat" then
          ginsfunctions[flinecounter] &= " , atof(arg2) "
          convertedarg = 1
        end if
        end if


        if convertedarg = 0 then
          ginsfunctions[flinecounter] &= " , arg2 "
        end if

      end if
    end if

end function

