Good day. Below is a Powerbuilder script that check the file encoding and get the data from the file. Once the data is assign to a string variable(ls_encoding
) it will be passed to the object function, of_TabDelimited()
(script of this function was also shown below) to replace the pipe delimited to tab delimited.
While inside the object function of_TabDelimited()
, the application got hang-up and I can't figure out the caused of the PB hang-up. The data that was passed is about 2,800 rows. But the function of_TabDelimited()
is working fine if the data is not that in bigger rows(e.g. 100 rows). I can't find any limitation of processing in of_TabDelimited()
because it only performs a loop and replace function while inside the loop.
Anybody can help me on how to find and correct the error? Appreciate any help.
//Get the data from the file
ll_FileNum = FileOpen(ls_sourcepath, StreamMode!, Read!, LockWrite!, Replace!)
ll_FileLength = FileLength(ls_sourcepath)
eRet = FileEncoding(ls_sourcepath)
IF NOT ISNULL(ll_FileLength) OR ll_FileLength > 0 THEN
IF eRet = EncodingANSI! THEN
ll_bytes = FileReadEx(ll_FileNum, lbl_data)
ls_Encoding = String(lbl_data, EncodingUTF8!)
FileClose(ll_FileNum)
END IF
ELSEIF ISNULL(ll_FileLength) OR ll_FileLength = 0 THEN
lb_Return = FALSE
END IF
Integer li_start, li_end
//Convert the data to TabDelimited
ls_Return = of_TabDelimited(ls_Encoding) //Application hang-up on this function.
IF NOT ISNULL(ls_Return) OR LEN(ls_Return) > 0 THEN
//Parse To Table
END IF
Function : of_TabDelimited
Return Type : String
Argument Type : as_encoding
Long ll_start=1
String ls_old, ls_new
ls_old = "|"
ls_new = "~t"
// Find the first occurrence of old_str.
ll_start = POS(as_encoding, ls_old, ll_start)
// Only enter the loop if you find old_str.
DO WHILE ll_Start > 0
// Replace old_str with new_str.
as_encoding = Replace(as_encoding, ll_start, Len(ls_old), ls_new)
// Find the next occurrence of old_str.
ll_start = POS(as_encoding, ls_old, ll_start + Len(ls_new))
LOOP
RETURN as_encoding
Below is a new function that I've added in the application and tested. And it seems this script is working fine and can process large number of rows. Basically this script was taken from the PFC's function of_GlobalReplace(), from the object n_cst_string. Even the script in of_TabDelimited() was taken from of_GlobalReplace() but the difference is the computation of len() in the variable ls_old and ls_new.
String ls_Source, ls_Old, ls_New
Long ll_Start, ll_Oldlen, ll_Newlen
ls_Old = "|"
ls_New = "~t"
//Script taken from n_cst_string - of_GlobalReplace
//Get the string lenghts
ll_OldLen = Len(ls_Old)
ll_NewLen = Len(ls_New)
//Search for the first occurrence of as_Old
ll_Start = Pos(as_source, ls_Old)
Do While ll_Start > 0
// replace as_Old with as_New
as_Source = Replace(as_Source, ll_Start, ll_OldLen, ls_New)
// find the next occurrence of as_Old
ll_Start = Pos(as_source, ls_Old, (ll_Start + ll_NewLen))
Loop
Return as_Source