home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / lib / wxbasicscript / basefunctions2.inc < prev   
Encoding:
Text File  |  2006-04-23  |  11.3 KB  |  462 lines

  1.  
  2. //###############################################
  3. //#
  4. //#   now here are some helpfull functions.
  5. //#   simply copy&paste them to your own script.
  6. //#
  7. //###############################################
  8.  
  9.  //////////////////////////////////////
  10. // get the WorkingDirectory
  11.  
  12. STARTDIR = getstartpath()
  13.  
  14.  
  15.  /////////////////////////////////////////
  16. //
  17.  
  18. function GetWinIDbyName( name )
  19.   DIM mylist =  {}
  20.   DIM i
  21.   DIM winid
  22.   DIM winname
  23.   mylist = xwin_listwindows()
  24.  
  25.   for i = 0 to count(mylist)-1
  26.     winid = cutrightfromleft( mylist[i] , " " )
  27.     winname = cutleft ( mylist[i] , " " )
  28.     'print mylist[i] & "---"
  29.  
  30.     if winname = name then   
  31.       'print "---------------------------------" & name
  32.       'break
  33.       
  34.       return winid
  35.     end if
  36.  
  37.   next
  38.   mylist =  {}
  39.   return 0
  40.  
  41. end function
  42.  
  43.  //////////////////////////////////////////////////////////////////////
  44. //   remove temporary files 
  45.  
  46. sub cleanup()
  47.   removefile( "/tmp/wxbxdmenu.tmp" )
  48.   removefile( "/tmp/gtkwindow.script" )
  49. end sub
  50.  
  51.  
  52.  //////////////////////////////////////////////////////////////////////
  53. //   make it easier to run Xmessage
  54.  
  55. function xmessage( themessage )
  56.   DIM xmresult
  57.   DIM xmchoice
  58.   xmresult = xwin_exec( "xmessage -print " & themessage & "  1>/tmp/wxbxdmenu.tmp" )
  59.   xmchoice = readfiletail( "/tmp/wxbxdmenu.tmp" )
  60.   cleanup()
  61.   //if xmchoice = nothing then xmchoice = "-1"
  62.   return xmchoice
  63.  
  64. end function
  65.  
  66.  //////////////////////////////////////////////////////////////////////
  67. //   make it easier to run Xdialog
  68.  
  69. function xdialog( xdmsg )
  70.   DIM xdresult
  71.   DIM xdchoice
  72.   xdresult = xwin_exec( "Xdialog " & xdmsg & " 2> /tmp/wxbxdmenu.tmp" )
  73.   xdchoice = readfiletail( "/tmp/wxbxdmenu.tmp" )
  74.   cleanup()
  75.   return xdresult , xdchoice
  76.  
  77. end function
  78.  
  79.  
  80.  //////////////////////////////////////////////////////////////////////
  81. //   make it easier to run Gtkdialog
  82.  
  83. function gtkdialog( gtkwindowstring )
  84.  
  85.  
  86.   DIM   gtktitleindex = instr( gtkwindowstring , "=" )
  87.   DIM   gtktitle = left ( gtkwindowstring , gtktitleindex-1 )
  88.   'gtktitle = replace ( gtktitle , "export " , "" )
  89.  
  90.    'gtkwindowstring = "#!/bin/sh\n##!/usr/local/bin/gtkdialog -f\n" >kwindowstring
  91.  
  92.   gtkwindowstring =  "#!/bin/sh\nexport " & gtkwindowstring
  93.   gtkwindowstring &=  "gtkdialog --program " & gtktitle & "\n"
  94.   gtkwindowstring &=  "unset "  & gtktitle & "\n"
  95.  
  96.   writestringtofile ( "/tmp/gtkwindow.script" , gtkwindowstring )
  97.   DIM   gtkresult = xwin_exec( "sh /tmp/gtkwindow.script 1> /tmp/wxbxdmenu.tmp" )
  98.   DIM   gtkchoice = readfiletail( "/tmp/wxbxdmenu.tmp" )
  99.   cleanup()
  100.   return gtkchoice
  101.  
  102. end function
  103.  
  104.  //////////////////////////////////////////////////////////////////////
  105. //   make it easier to run Gtkdialog 0.59.8
  106.  
  107. function gtkdialog2( gtkwindowstring )
  108.  
  109.   DIM   gtktitleindex = instr( gtkwindowstring , "=" )
  110.   DIM   gtktitle = left ( gtkwindowstring , gtktitleindex-1 )
  111.   'gtktitle = replace ( gtktitle , "export " , "" )
  112.  
  113.   if left( gtkwindowstring , 11 ) != "MAIN_DIALOG" then
  114.  
  115.     DIM   gtkoldtitle = cutrightfromleft( gtkwindowstring , "=" )  
  116.     gtkwindowstring = cutleft( gtkwindowstring , "=" )
  117.  
  118.     DIM gtkfirstchar = left( gtkwindowstring , 1 )
  119.     gtkwindowstring = right(gtkwindowstring , len(gtkwindowstring)-1)
  120.     gtkwindowstring = "MAIN_DIALOG=" & gtkfirstchar & "\n<wtitle>" & gtkoldtitle & "</wtitle>\n" & gtkwindowstring
  121.  
  122.   end if
  123.  
  124.    'gtkwindowstring = "#!/bin/sh\n##!/usr/local/bin/gtkdialog -f\n" >kwindowstring
  125.  
  126.   gtkwindowstring =  "#!/bin/sh\nexport " & gtkwindowstring
  127.   gtkwindowstring &=  "gtkdialog2 --program " & "MAIN_DIALOG" & "\n"
  128.   gtkwindowstring &=  "unset "  & "MAIN_DIALOG" & "\n"
  129.  
  130.   writestringtofile ( "/tmp/gtkwindow.script" , gtkwindowstring )
  131.   DIM   gtkresult = xwin_exec( "sh /tmp/gtkwindow.script 1> /tmp/wxbxdmenu.tmp" )
  132.   DIM   gtkchoice = readfiletail( "/tmp/wxbxdmenu.tmp" )
  133.   cleanup()
  134.   return gtkchoice
  135.  
  136. end function
  137.  
  138.  
  139.  //////////////////////////////////////////////////////////////////////
  140. //   save string to a file
  141.  
  142. function writestringtofile( thefile , thestring )
  143.  
  144.   open thefile for output as #1
  145.      print #1 , thestring
  146.   close #1
  147.  
  148. end function
  149.  
  150.  //////////////////////////////////////////////////////////////////////
  151. //   append string to a file
  152.  
  153. function appendstringtofile( thefile , thestring )
  154.  
  155.   open thefile for append as #1
  156.      print #1 , thestring
  157.   close #1
  158.  
  159. end function
  160.  
  161.  
  162.  //////////////////////////////////////////////////////////////////////
  163. //   save list to a file
  164.  
  165. function writelisttofile( thefile , thelist )
  166.   DIM theline
  167.   open thefile for output as #1
  168.      for each theline in  thelist 
  169.        print #1 , theline
  170.      next
  171.   close #1
  172.  
  173. end function
  174.  
  175.  //////////////////////////////////////////////////////////////////////
  176. //   append list to a file
  177.  
  178. function appendlisttofile( thefile , thelist )
  179.   DIM theline
  180.   open thefile for append as #1
  181.      for each theline in  thelist 
  182.        print #1 , theline
  183.      next
  184.   close #1
  185.  
  186. end function
  187.  
  188.  
  189.  //////////////////////////////////////////////////////////////////////
  190. //   read in a file to a string
  191.  
  192. function readfile( thefile )
  193.  
  194.   if fileexists( thefile ) then
  195.      DIM freeNum= freeFile()
  196.      DIM text = ""
  197.      DIM buffer 
  198.      open thefile For Input As #freeNum
  199.         while ! eof( freeNum )
  200.           Line Input #freeNum, buffer
  201.           text &= buffer
  202.        end while
  203.      close #freeNum
  204.   end if
  205.   return( text )
  206. end function
  207.  
  208.  //////////////////////////////////////////////////////////////////////
  209. //   read in a templatefile to a string
  210.  
  211. function readtemplate( thefile )
  212.  
  213.   if fileexists( thefile ) then
  214.      DIM freeNum= freeFile()
  215.      DIM text = ""
  216.      DIM buffer 
  217.      open thefile For Input As #freeNum
  218.         while ! eof( freeNum )
  219.           Line Input #freeNum, buffer
  220.           text &= buffer & "\n"
  221.        end while
  222.      close #freeNum
  223.   end if
  224.   return( text )
  225. end function
  226.  
  227.  //////////////////////////////////////////////////////////////////////
  228. //   read in a file to a list
  229.  
  230. function readfiletolist( thefile )
  231.   DIM   thelist = {}
  232.   if fileexists( thefile ) then
  233.      DIM freeNum= freeFile()
  234.      DIM text = ""
  235.      open thefile For Input As #freeNum
  236.         DIM c = 0
  237.         DIM buffer 
  238.         while ! eof( freeNum )
  239.           Line Input #freeNum, buffer
  240.           thelist[c] = buffer
  241.           c+=1
  242.        end while
  243.      close #freeNum
  244.   end if
  245.   return( thelist )
  246. end function
  247.  
  248.  
  249.  //////////////////////////////////////////////////////////////////////
  250. //   read in last line of a file
  251.  
  252. function readfiletail( thefile )
  253.  
  254.   if fileexists( thefile ) then
  255.      DIM freeNum= freeFile()
  256.      DIM buffer
  257.      open thefile For Input As #freeNum
  258.         while ! eof( freeNum )
  259.           Line Input #freeNum, buffer
  260.        end while
  261.      close #freeNum
  262.   end if
  263.   return( buffer )
  264. end function
  265.  
  266.   ////////////////////////////////////////////////////////////////////////////////////
  267.  // 
  268. //    get the path of this script, so that it will find its resources
  269. //    (e.g. Icons)
  270.  
  271. function getstartpath()
  272.  
  273.    DIM full = command(2)
  274.    if left ( full , 1 ) != "/" then return "./"
  275.    return cutright( full , "/" ) 
  276.  
  277. end function
  278.  
  279.   ////////////////////////////////////////////////////////////////////////////////////
  280.  // 
  281. //    return left part of a string, counting the seperator from right
  282.  
  283. function cutright( thestring , splitter )
  284.  
  285.     DIM x = rinstr( thestring , splitter )
  286.     DIM thepath = left( thestring , x-1 )
  287.  
  288.     return thepath
  289.  
  290. end function
  291.  
  292.   ////////////////////////////////////////////////////////////////////////////////////
  293.  // 
  294. //    return left part of a string, counting the seperator from left
  295.  
  296. function cutrightfromleft( thestring , splitter )
  297.  
  298.     DIM x = instr( thestring , splitter )
  299.     DIM thepath = left( thestring , x-1 )
  300.  
  301.     return thepath
  302.  
  303. end function
  304.  
  305.   ////////////////////////////////////////////////////////////////////////////////////
  306.  // 
  307. //    return right part of a string, counting the seperator from left
  308.  
  309. function cutleft( thestring , splitter )
  310.  
  311.     DIM x = instr( thestring , splitter )
  312.     DIM thepath = right( thestring , len(thestring)-x-len(splitter)+1 )
  313.     'print "---" & thepath
  314.  
  315.     return thepath
  316.  
  317. end function
  318.  
  319.  
  320.  
  321.   //////////////////////////////////////////////////////////////////////
  322.  //   explode separates a string using splitter
  323. //    and returns the substrings in a list
  324. //
  325. //  EXAMPLE: 
  326. //
  327. //  mystring = "dogs,puppys,cats"
  328. //  words = {}
  329. //  words = explode( mystring , ",")
  330. //  print words[2] // lists start count at 0
  331.  
  332.  
  333. function explode(wholestr,splitter)
  334.     dim word={}
  335.     DIM spos=0
  336.     DIM i=1
  337.     DIM n=instr(0,wholestr,splitter)
  338.     word[0]=left(wholestr,n-1)
  339.     n=instr(spos,wholestr,splitter)
  340.     spos=n+length(splitter)
  341.     while instr(spos,wholestr,splitter)
  342.       n=instr(spos,wholestr,splitter)
  343.       word[i]=mid (wholestr,spos,n-spos)
  344.       spos=n+length(splitter)
  345.       i+=1
  346.     end while
  347.     word[i]=mid (wholestr,n+length(splitter),length(wholestr))
  348.     return word
  349. end function
  350.  
  351.  
  352.   //////////////////////////////////////////////////////////////////////
  353.  //   remove/delete a file
  354. //    
  355.  
  356. function removefile( thefile )
  357.  
  358.   if thefile = "" then return 1
  359.   if ! fileexists( thefile ) then return 1
  360.   //result = xwin_exec( "rm -f " & thefile ) // this does not work. why?
  361.   shell( "rm -f " & thefile )
  362.   if fileexists( thefile ) then return 1
  363.  
  364.   return 0
  365.  
  366. end function
  367.  
  368.   //////////////////////////////////////////////////////////////////////
  369.  //   delete all HTML-TAGS from a string
  370. //    
  371. function removehtml( thestring )
  372.  
  373.  
  374.    //--- delete Links ---
  375.    DIM category = replace (  thestring , "<" , "<REMOVE" )
  376.    category = replace (  category , ">" , "<" )
  377.    DIM categorywithlinks = explode( category , "<" )
  378.  
  379.    DIM thenewstring = ""
  380.    DIM part 
  381.    for each part in categorywithlinks
  382.      if left( part , 6 ) != "REMOVE" then
  383.         thenewstring &= part
  384.      end if
  385.    next
  386.   thenewstring = replace(  thenewstring , "∞" , "" )
  387.   return thenewstring
  388.  
  389. end function
  390.  
  391.  
  392.   //////////////////////////////////////////////////////////////////////
  393.  //   rewrite HTML-TAGS to a shorter style
  394. //  
  395. function rewritelinks( thestring )
  396.  
  397.  
  398.    //--- delete Links ---
  399.    thestring = replace (  thestring , "<a href=" , "[" )
  400.    thestring = replace (  thestring , "<a class=\"ext\" href=" , "[" )
  401.    thestring = replace (  thestring , "<a " , "[" )
  402.    thestring = replace (  thestring , " href=" , "" )
  403.    thestring = replace (  thestring , "href=" , "" )
  404.    thestring = replace (  thestring , "class=\"missingpage\"" , "" )
  405.  
  406.    thestring = replace (  thestring , "</a>" , "]" )
  407.  
  408.   return thestring
  409.  
  410. end function
  411.  
  412.   //////////////////////////////////////////////////////////////////////
  413.  //   Bubblesort
  414. //  
  415. function bubblesort( thelist )
  416.  
  417.   DIM i
  418.   DIM inOrderFlag
  419.   DIM j
  420.   For i = thelist.Count() to 1 step -1
  421.      inOrderFlag = true
  422.      For j = 0 to thelist.Count() - 2
  423.           If thelist[j] > thelist[j + 1] then
  424.            inOrderFlag = false
  425.            thelist[j], thelist[j + 1] = thelist[j + 1], thelist[j]
  426.           End If
  427.      End For
  428.      If inOrderFlag then break
  429.   End For 
  430.  
  431.   return thelist
  432.  
  433. end function
  434.  
  435.  
  436. function argvtostring( )
  437.  
  438.   DIM   i=3
  439.   DIM   args=""
  440.   while command(i) !=""
  441.   args &= command(i) & " "
  442.   i+=1
  443.   wend
  444.   args=trim(args)
  445.  
  446. return args
  447. end function
  448.  
  449.   ////////////////////////////////////////////////////////////////////////////////////
  450.  // 
  451. //    return right part of a string, counting the seperator from left
  452.  
  453. function cutleftfromright( thestring , splitter )
  454.  
  455.     DIM x = rinstr( thestring , splitter )
  456.     DIM thepath = right( thestring , len(thestring)-x-len(splitter)+1 )
  457.     'print "---" & thepath
  458.  
  459.     return thepath
  460.  
  461. end function
  462.