/* Dynamic Import Utility Author: Denis Tatarik Description: PROGRESS misses an IMPORT method for BUFFER object handle. This utility helps to add such functionality to the language. Sample calls: 1. def temp-table ttCustomer like customer. run dynamicImport (temp-table ttCustomer:handle, "customerdata.d"). for each ttCustomer: disp ttCustomer with 2 col. end. 2. define variable hBuffer as handle no-undo. create buffer hBuffer for table "tablename". run dynamicImport (hBuffer, "datafilename.d"). */ /*dynamicImport - imports data from given file to the handle of the table specified Parameters: hTable - Handle to the table to load the data to cFileName - Full path to the file where the data resides */ procedure dynamicImport: define input parameter hTable as handle no-undo. define input parameter cFileName as character no-undo. def var hBuffer as handle no-undo. def var hField as handle no-undo. def var i as integer no-undo. def var k as integer no-undo. def var str as character no-undo. def var cTmp as character no-undo. hBuffer = hTable:default-buffer-handle. input from value(cFileName). repeat on error undo, leave: import unformatted str. if str <> "" then do: hBuffer:buffer-create. do k = 1 to hBuffer:num-fields on error undo, retry: /*in case of an error - do something here*/ if retry then do: message "ERROR:" error-status:get-message(1) view-as alert-box. return. end. hField = hBuffer:buffer-field(k). /*character fields are exported with quotes, so we need to call different routines to handle it*/ if hField:data-type = "character" then do: /*for extent fields need to scroll thru all of them*/ if hField:extent = 0 then do: run importChar(input-output str, output cTmp). hField:buffer-value = cTmp. end. else do: do i = 1 to hField:extent: run importChar(input-output str, output cTmp). hField:buffer-value[i] = ctmp. end. end. end. else do: /*for extent fields need to scroll thru all of them*/ if hField:extent = 0 then do: run importNonChar(input-output str, output cTmp). /*let progress convert the data on its own*/ hField:buffer-value = cTmp. end. else do: do i = 1 to hField:extent: run importNonChar(input-output str, output cTmp). /*let progress convert the data on its own*/ hField:buffer-value[i] = ctmp. end. end. end. end. end. end. input close. end procedure. /*importNonChar - reads non-character data field and returns its value while removing it from input string Parameters: str - input-output - input string cValue - output - Return value */ procedure importNonChar private: define input-output parameter str as character no-undo. define output parameter cValue as character no-undo. def var iPos as integer no-undo. /*check for ?*/ if substr(str, 1, 1) = "?" then do: cValue = ?. str = substr(str, 3). return. end. /*find next space and return the data if no space found then we are at the last field in list*/ iPos = index(str, " "). if iPos = 0 then do: cValue = str. str = "". end. else do: cValue = trim(substr(str, 1, iPos)). str = substr(str, iPos + 1). end. end procedure. /*importChar - reads character data field and returns its value while removing it from input string. This procedure handles dblquotes in character by recursively call itself Parameters: str - input-output - input string cValue - output - Return value */ procedure importChar private: define input-output parameter str as character no-undo. define output parameter cValue as character no-undo. def var iPos as integer no-undo. def var cTmp as character no-undo. /*check for ?*/ if substr(str, 1, 1) = "?" then do: cValue = ?. str = substr(str, 3). return. end. /*if current field doesnt start from a quote we got an error might be a good idea to put an error handling here, but should never happen if the file came from the good source*/ iPos = index(str, '"'). if iPos <> 1 then do: cValue = ?. return. end. iPos = index(str, '"', 2). if iPos > 0 then do: /*check if it is 2 and next char is space if yes we have an empty string*/ if iPos = 2 and (length(str) = 2 or substr(str, 3, 1) = " ") then do: cValue = "". if length(str) = 2 then str = "". else str = trim(substr(str, 3)). return. end. /*check if next char is dblquote*/ if substr(str, iPos + 1, 1) = '"' then do: /*we have an escape quote*/ cValue = substr(str, 2, iPos - 2) + '"'. str = '"' + substr(str, iPos + 2). /*now do recursive checks for dbl dblquotes*/ run importChar(input-output str, output cTmp). cValue = cValue + cTmp. return. end. /*now just get the value*/ cValue = substr(str, 2, iPos - 2). if iPos = length(str) then str = "". else str = substr(str, iPos + 2). end. else do: /*something is wrong here - may be a good place for an error handling, but really should never happen if it came from the good source*/ cValue = ?. return. end. end procedure.