Post by kokenge on Jun 5, 2009 7:48:42 GMT -5
I was trying to convert some PHP code to RunBasic using a editor.
I found it very time consuming.
You have to move the $ from the beginning to the end of each field. This tries to put it on the end.
WHILE, IF, and FOREACH statements use the { and } for begin and end and } else { for else.
This tries to set the begin and end statements for these.
PHP uses "_" to separate field names. This gives you the option to use a dot or use caps for the next character.
This converts [] to ().
Anyway I wrote a quick RB program to help in the conversion.
It is not fancy, but it takes care of the bulk of the tedious stuff. As you know some things, you simply have to look at to determine what needs to be done.
This creates the converted bas program in the public directory..
I found it very time consuming.
You have to move the $ from the beginning to the end of each field. This tries to put it on the end.
WHILE, IF, and FOREACH statements use the { and } for begin and end and } else { for else.
This tries to set the begin and end statements for these.
PHP uses "_" to separate field names. This gives you the option to use a dot or use caps for the next character.
This converts [] to ().
Anyway I wrote a quick RB program to help in the conversion.
It is not fancy, but it takes care of the bulk of the tedious stuff. As you know some things, you simply have to look at to determine what needs to be done.
This creates the converted bas program in the public directory..
' ---------------------------------------
' PHP to RB conversion
' php2rb.bas
' ---------------------------------------
html "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR>"
html "<TD BGCOLOR=steelblue ALIGN=CENTER><FONT COLOR=#FFCC00 FACE=Arial>PHP to RB conversion</TD>"
html "</TR></TABLE>"
html "<center><TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0><TR><TD nowrap><P>Convert '_' to"
radiogroup #cd, "Dots, Caps", "Caps"
#cd horizontal(1)
html "<P></TD></TR><TR><TD align=center>"
upload "Select PHP file"; file$
html "</tr></table>"
if file$ = "" then end
html "</center>"
cd$ = #cd selection$()
cr$ = chr$(13)
lf$ = chr$(10)
tab$ = chr$(9)
sqliteconnect #mem, ":memory:"
mem$ = "CREATE TABLE php (
seqNum int,
code text,
cmt int
)"
#mem execute(mem$)
seqNum = 0
'get contents of uploaded file into string variable
open file$ for binary as #f
a$ = input$(#f, LOF(#f))
close #f
ln = len(a$)
cr = instr(a$,cr$)
lf = instr(a$,lf$)
a$ = strRep$(a$,lf$,cr$) ' lf to cr
a$ = lower$(a$)
seqNum = 0
i1 = 1
[loop]
i = instr(a$,cr$,i1)
if i > 0 then
a1$ = " ";strip$(mid$(a$,i1,i - i1));" "
a1$ = strRep$(a1$,";","")
if INSTR(a1$,tab$) > 0 then a1$ = strRep$(a1$,tab$," ") ' tabs to space
if INSTR(a1$," ") > 0 then a1$ = strRep$(a1$," "," ") ' single space
if INSTR(a1$,"&&") > 0 then a1$ = strRep$(a1$,"&&","AND") ' single space
if INSTR(a1$,"echo") > 0 then a1$ = strRep$(a1$,"echo","HTML")
if INSTR(a1$," . ") > 0 then a1$ = strRep$(a1$," . "," + ")
if INSTR(a1$,"[") > 0 then a1$ = strRep$(a1$,"[","(")
if INSTR(a1$,"]") > 0 then a1$ = strRep$(a1$,"]",")")
if INSTR(a1$,"else") > 0 then
a1$ = strRep$(a1$,"{","")
a1$ = strRep$(a1$,"}","")
end if
if cd$ <> "Caps" then
a1$ = strRep$(a1$,"_",".")
else
[loop1]
j = INSTR(a1$,"_")
if j > 0 then
a1$ = left$(a1$,j - 1) + upper$(mid$(a1$,j + 1,1)) + mid$(a1$,j + 2)
goto [loop1]
end if
end if
' ------------------------------
' move $ to end of variable
' ------------------------------
j1 = 0
[loop2]
j = INSTR(a1$,"$", j1)
if j > 0 then
j1 = INSTR(a1$," ",j)
if j1 = 0 then j1 = len(a1$)
j1a = INSTR(a1$,"(",j)
if j1a > 0 then j1 = MIN(j1,j1a)
j1a = INSTR(a1$,")",j)
if j1a > 0 then j1 = MIN(j1,j1a)
j1a = INSTR(a1$,",",j)
if j1a > 0 then j1 = MIN(j1,j1a)
j2 = (j1 - j) - 1
if j > 0 and j1 > 0 and j2 > 0 then
a1$ = left$(a1$,j - 1) + mid$(a1$,j+1, j2) + "$" + mid$(a1$,j1)
goto [loop2]
end if
end if
[cmtOn]
if cmtOn > 0 then
a1$ = "'' " + a1$
if INSTR(a1$,"*/") > 0 then ' check for end of comments
cmtOn = 0
'print "============ COMMENT OFF ============"
aa1$ = "''" + word$(a1$,1,"*/")
seqNum = seqNum + 1
cmt = 1
mem$ = "INSERT INTO php VALUES(";seqNum;",'";aa1$;"',";cmt;")"
#mem execute(mem$)
'print mem$
a1$ = word$(a1$,2,"*/")
else
goto [skipCmt]
end if
end if
cmt = INSTR(a1$,"//")
if cmtOn = 0 then cmtOn = INSTR(a1$,"/*") ' Turn comments on
if cmtOn > 0 then
'print "============ COMMENT ON =============";a1$
aa1$ = trim$(word$(a1$,1,"/*"))
if aa1$ <> "" then
seqNum = seqNum + 1
cmt = INSTR(aa1$,"//")
mem$ = "INSERT INTO php VALUES(";seqNum;",'";aa1$;"',";cmt;")"
#mem execute(mem$)
'print mem$
end if
a1$ = word$(a1$,2,"/*")
goto [cmtOn]
end if
[skipCmt]
seqNum = seqNum + 1
mem$ = "INSERT INTO php VALUES(";seqNum;",'";a1$;"',";cmt;")"
#mem execute(mem$)
'print mem$
[skip]
i1 = i + 1
goto [loop]
end if
s = 1
[nxtForeach]
mem$ = "SELECT * FROM php WHERE code LIKE '% foreach %' AND seqNum >= ";s;" ORDER BY seqNum LIMIT 1"
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code$,"'","''")
cmt = #row cmt()
'print using("########",seqNum);" ";code$
i = INSTR(code$," foreach ")
i1 = INSTR(code$,"{",i)
if i1 > 0 then
code$ = left$(code$,i1 - 1) + mid$(code$,i1 + 1)
code$ = strRep$(code$,"foreach","FOR")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
#mem execute(mem$)
'print mem$
else
mem$ = "SELECT * FROM php WHERE code LIKE '%{%' AND seqNum > ";seqNum;" ORDER BY seqNum LIMIT 1"
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code$,"'","''")
cmt = #row cmt()
code$ = strRep$(code$,"{","")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
#mem execute(mem$)
'print mem$
end if
end if
mem$ = "SELECT * FROM php WHERE code LIKE '%}%' AND seqNum >= ";seqNum;" ORDER BY seqNum LIMIT 1"
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code$,"'","''")
cmt = #row cmt()
code$ = strRep$(code$,"}","NEXT")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
#mem execute(mem$)
'print mem$
end if
s = seqNum + 1
goto [nxtForeach]
end if
s = 1
[nxtIf]
mem$ = "SELECT * FROM php WHERE code LIKE '% if %' AND seqNum >= ";s;" ORDER BY seqNum LIMIT 1"
'print mem$
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code$,"'","''")
cmt = #row cmt()
'print using("########",seqNum);" ";code$
'input x
i = INSTR(code$," if ")
i1 = INSTR(code$,"{",i)
if i1 > 0 then
code$ = left$(code$,i1 - 1) + " THEN " + mid$(code$,i1 + 1)
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
else
mem$ = "SELECT * FROM php WHERE code LIKE '%{%' AND seqNum > ";seqNum;" ORDER BY seqNum LIMIT 1"
'print mem$
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code$,"'","''")
cmt = #row cmt()
code$ = strRep$(code$,"{","THEN")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
end if
end if
mem$ = "SELECT * FROM php WHERE code LIKE '%}%' AND seqNum >= ";seqNum;" ORDER BY seqNum LIMIT 1"
'print mem$
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code$,"'","''")
cmt = #row cmt()
code$ = strRep$(code$,"}","END IF")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
end if
s = seqNum + 1
goto [nxtIf]
end if
s = 1
[nxtWhile]
mem$ = "SELECT * FROM php WHERE code LIKE '% while %' AND seqNum >= ";s;" ORDER BY seqNum LIMIT 1"
'print mem$
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code$,"'","''")
cmt = #row cmt()
'print using("########",seqNum);" ";code$
i = INSTR(code$," while ")
i1 = INSTR(code$,"{",i)
if i1 > 0 then
code$ = left$(code$,i1 - 1) + mid$(code$,i1 + 1)
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
else
mem$ = "SELECT * FROM php WHERE code LIKE '%{%' AND seqNum > ";seqNum;" ORDER BY seqNum LIMIT 1"
'print mem$
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code$,"'","''")
cmt = #row cmt()
code$ = strRep$(code$,"{","")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
end if
end if
mem$ = "SELECT * FROM php WHERE code LIKE '%}%' AND seqNum >= ";seqNum;" ORDER BY seqNum LIMIT 1"
'print mem$
#mem execute(mem$)
rows = #mem ROWCOUNT()
if rows > 0 then
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
code$ = strRep$(code$,"'","''")
cmt = #row cmt()
code$ = strRep$(code$,"}","WEND")
mem$ = "UPDATE php SET code = '";code$;"' WHERE seqNum = ";seqNum
'print mem$
#mem execute(mem$)
end if
s = seqNum + 1
goto [nxtWhile]
end if
' -------------------------------
' file out in \public dir
' -------------------------------
fileOut$ = DefaultDir$ + "\public\" + file$
print "File Out:";fileOut$
OPEN fileOut$ for OUTPUT as #fo
mem$ = "SELECT * FROM php ORDER BY seqNum"
#mem execute(mem$)
WHILE #mem hasanswer()
#row = #mem #nextrow()
seqNum = #row seqNum()
code$ = #row code$()
cmt = #row cmt()
if cmt > 0 then code$ = strRep$(code$,"//","'")
if INSTR(code$,"\n") > 0 then code$ = strRep$(code$,"\n","chr$(13)")
print using("########",seqNum);" |";code$
print #fo,code$
WEND
close #fo
wait
' --------------------------------
' string replace rep str with
' --------------------------------
FUNCTION strRep$(str$,rep$,with$)
ln = len(rep$)
ln1 = ln - 1
i = 1
while i <= len(str$)
if mid$(str$,i,ln) = rep$ then
strRep$ = strRep$ + with$
i = i + ln1
else
strRep$ = strRep$ + mid$(str$,i,1)
end if
i = i + 1
WEND
END FUNCTION
' -----------------------------------------
' strip junk
' -----------------------------------------
FUNCTION strip$(str$)
strip$ = ""
for i = 1 to len(str$)
a$ = MID$(str$,i,1)
a = ASC(a$)
if a > 31 then
if a < 127 then
if a$ <> "'" then
if a$ <> """" then
strip$ = strip$ + a$
end if
end if
end if
end if
next i
END FUNCTION