Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/tbl_web_add_row'
Browse files Browse the repository at this point in the history
  • Loading branch information
salhk committed Feb 16, 2024
2 parents bcfb0da + 96d4e23 commit ea4ba8f
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 32 deletions.
68 changes: 36 additions & 32 deletions tables/tables_web.tbs
Original file line number Diff line number Diff line change
Expand Up @@ -131,43 +131,23 @@ function tbl_web_get_header(table as string) as string
end function

function tbl_web_get_fld_data(index as word, num_of_fld as byte) as string
dim j, k, x, tmp_instr as byte
dim j, k, x as byte
dim fld_data as string
dim tmp_data as string=""
dim tmp_fld_data as string
tbl_record_sg(index,EN_TBL_GET)
tmp_data="\x22"+str(index)+"\x22,"
for j = 0 to num_of_fld - 1
tbl_field_sg(tbl_web_fields(j),fld_data,EN_TBL_GET)
tmp_fld_data=""
tmp_data=tmp_data+"\x22"
k = 1
do
x = instr(k, fld_data, "\r", 1)
tmp_instr = instr(k, fld_data, "\n", 1)
if tmp_instr <> 0 and (tmp_instr < x or x = 0) then
x = tmp_instr
tmp_fld_data = tmp_fld_data + mid(fld_data, k, x - k) + "\x5cn"
else if x <> 0 then
tmp_fld_data = tmp_fld_data + mid(fld_data, k, x - k) + "\x5cr"
end if
fld_data = escape_string(fld_data)

if x = 0 then
x = len(fld_data)
tmp_fld_data = tmp_fld_data + mid(fld_data, k, x - k + 1)
end if

k = x + 1

loop while x <> len(fld_data)
fld_data = tmp_fld_data
k = 1
do
x = instr(k, fld_data, "\x22",1)
x = instr(k, fld_data, "\x5c\x22",1)
if x = 0 then
x = len(fld_data) + 1
end if
tmp_data = tmp_data + mid(fld_data, k, x - k + 1) + "\x22"
tmp_data = tmp_data + mid(fld_data, k, x - k) + "\x22"
k = x + 1
loop while x <> len(fld_data) + 1
if j<num_of_fld - 1 then
Expand Down Expand Up @@ -242,15 +222,27 @@ function tbl_web_add_row(byref table as string,byref row as string) as string
num_of_fld = tbl_get_num_fields(table)
pos1=1
for i = 0 to num_of_fld-1
s=""
tbl_get_field_info(table,i,field_metadata)
' if i>0 then
' pos1=instr(pos1,row,",",i)
' end if
pos2=instr(pos1,row,delimiter,1)
if enclosed_in_quotes=YES and pos2=0 then
pos2 = len(row)
pos2=instr(pos1 + 1,row,delimiter,1)
if enclosed_in_quotes=YES then
while (instr(pos1,row,"\x5c\x22",1) > 0 and instr(pos1,row,"\x5c\x22",1) < instr(pos1 + 1,row,delimiter,1))
or (num_of_fld = 1 and instr(pos1,row,"\x5c\x22",1) > 0)
pos1 = pos1 + 1
pos2=instr(pos1,row,"\x5c\x22",1)
s = s + mid(row,pos1, pos2 - pos1) + "\x22"
pos1 = pos2+1
pos2=instr(pos1 + 1,row,delimiter,1)
wend

if pos2 = 0 then
pos2 = len(row)
end if
end if
s=mid(row,pos1+enclosed_quote_offset,pos2-pos1-enclosed_quote_offset)
s= s + mid(row,pos1+enclosed_quote_offset,pos2-pos1-enclosed_quote_offset)
if s="" then
tbl_web_tbl_status = tbl_get_field_def(table,field_metadata.field_name,s)
if tbl_web_tbl_status <> EN_TBL_STATUS_OK then goto verify
Expand Down Expand Up @@ -364,15 +356,27 @@ function tbl_web_edit_row(byref table as string,byref index as word,byref row as
pos1=1
tbl_record_sg(index,EN_TBL_GET)
for i = 0 to num_of_fld-1
s=""
tbl_get_field_info(table,i,field_metadata)
' if i>0 then
' pos1=instr(pos1,row,",",i)
' end if
pos2=instr(pos1,row,delimiter,1)
if enclosed_in_quotes=YES and pos2=0 then
pos2 = len(row)
pos2=instr(pos1 + 1,row,delimiter,1)
if enclosed_in_quotes=YES then
while (instr(pos1,row,"\x5c\x22",1) > 0 and instr(pos1,row,"\x5c\x22",1) < instr(pos1 + 1,row,delimiter,1))
or (num_of_fld = 1 and instr(pos1,row,"\x5c\x22",1) > 0)
pos1 = pos1 + 1
pos2=instr(pos1,row,"\x5c\x22",1)
s = s + mid(row,pos1, pos2 - pos1) + "\x22"
pos1 = pos2+1
pos2=instr(pos1 + 1,row,delimiter,1)
wend

if pos2 = 0 then
pos2 = len(row)
end if
end if
s=mid(row,pos1+enclosed_quote_offset,pos2-pos1-enclosed_quote_offset)
s= s + mid(row,pos1+enclosed_quote_offset,pos2-pos1-enclosed_quote_offset)
if s="" then
tbl_web_tbl_status = tbl_get_field_def(table,field_metadata.field_name,s)
if tbl_web_tbl_status <> EN_TBL_STATUS_OK then goto verify
Expand Down
3 changes: 3 additions & 0 deletions utils/utils.tbh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ declare function math_pow(base as integer, exp as integer) as real
declare function math_hex_to_float(num as string) as real
declare function ftofixed(r as real, decimals as byte) as string
declare function hex_mac(mac as string) as string
declare function json_get_value(byref key as string, byref data as string) as string
declare function escape_string(data as string) as string
declare function unescape_string(data as string) as string
117 changes: 117 additions & 0 deletions utils/utils.tbs
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,121 @@ function hex_mac(mac as string) as string

hex_mac = left(result,len(result)-1)

end function


function json_get_value(byref key as string, byref data as string) as string
json_get_value=""
dim start_pos as byte=0
dim end_pos as byte=0
dim current_key as string=""
start_pos=instr(1,data,"{",1)
while start_pos<>0
'get current key
start_pos=instr(start_pos,data,"\x22",1)
if start_pos<>0 then
end_pos=instr(start_pos+1,data,"\x22",1)
current_key=mid(data,start_pos+1,end_pos-start_pos-1)

'get value
start_pos=instr(end_pos,data,":",1)
if start_pos<>0 then
end_pos=instr(start_pos,data,",",1)
if end_pos=0 then
end_pos=instr(start_pos,data,"}",1)
end if

end if
if current_key=key then
dim is_string as boolean=instr(start_pos,data,"\x22",1)=start_pos+1
if is_string then
'is string so look for closing quote
dim tmp_end as byte=start_pos+1
while tmp_end<len(data)
tmp_end=instr(tmp_end+1,data,"\x22",1)
if tmp_end=0 then
exit while
end if
if mid(data,tmp_end-1,1)<>"\x5c" then
end_pos=tmp_end+1
exit while
end if
wend
end if
json_get_value=mid(data,start_pos+1,end_pos-start_pos-1)
if is_string then
json_get_value=unescape_string(json_get_value)
end if
if instr(1,json_get_value,"\x22",1)=1 then
json_get_value=mid(json_get_value,2,len(json_get_value)-2)
end if
exit function
end if
start_pos=end_pos+1

end if
wend
end function

function escape_string(data as string) as string
dim x, tmp_instr as byte
dim tmp_str as string=""
dim k as byte=1
dim i as byte=0
escape_string=""

dim special_characters as string="\x22\r\n\x5c\t"
dim escaped_chars as string="\x22rn\x5ct"
while k < len(data) + 1
for i=0 to len(special_characters)-1
dim escape_char as string(1)=mid(special_characters,i+1,1)
dim escaped_char as string(1)=mid(escaped_chars,i+1,1)
dim current_char as string(1)=mid(data,k,1)
if escape_char = current_char then
x=k
dim tmp1 as string=mid(data,1,x-1)
dim tmp2 as string=mid(data,x + 1,len(data)-x)
data=tmp1+"\x5c"+escaped_char+tmp2
k = k + 1
end if
next i
k=k+1
wend
escape_string=data
end function

function unescape_string(data as string) as string
dim x, tmp_instr, offset as byte
dim tmp_str as string=""
dim k as byte=1
dim unescaped_char as string=""
unescape_string=data

while k < len(unescape_string) - 1
dim current_char as string(1)=mid(unescape_string,k,1)
'\
if current_char="\x5c" then
current_char=mid(unescape_string,k + 1,1)
select case current_char
case "r":
unescaped_char="\r"
case "n":
unescaped_char="\n"
case "t":
unescaped_char="\t"
case "\x5c":
unescaped_char="\x5c"
case "\x22":
unescaped_char="\x22"
case else:
unescaped_char=current_char
end select
dim tmp1 as string=mid(unescape_string,1,k-1)
dim tmp2 as string=mid(unescape_string,k + 2,255)
unescape_string=tmp1+unescaped_char+tmp2
end if

k=k+1
wend

end function

0 comments on commit ea4ba8f

Please sign in to comment.