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