Reading input string from a text file in progress4gl - progress-4gl

I have a .txt file having some text. I want to read this file and store it into a string.
Is there available any function for that?
Or how to do it ? please help

define variable s as longchar no-undo.
copy-lob from file "fileName.txt" to s.

DEF VAR ch-var AS CHARACTER NO-UNDO.
INPUT FROM VALUE(file-name).
REPEAT:
IMPORT UNFORMATTED ch-var.
END.

Related

How to copy one text file to another text file with changes (editing df files)

I am fairly new to progress 4gl. I am trying to copy the contents of one df file into another file where I want to copy everything except the areas of the df file. this is what I have so far.
define stream mystream.
input from "C:\OpenEdge\WRK\df file\sample.df".
do while true on endkey undo, leave:
output to "C:\OpenEdge\WRK\df file\test1.df".
end.
output close.
display "finished".
I would first like to copy all the contents of the first df file into another df file, but right now I'm getting a blank file. Please let me know where I am going wrong.
This is what I do:
grep -v AREA < dbname.df > dbname.df2
If you are stuck with on OS that lacks "grep" or if you just have an urge to do it with 4gl code I'd do something like:
define variable lineIn as character no-undo.
input from "dbname.df".
output to "dbname.df2".
repeat:
import unformatted lineIn. /* read the data a whole line at a time */
if lineIn begins " AREA" then next. /* skip lines that start with " AREA" */
put unformatted lineIn skip. /* spit the input line back out */
if lineIn = "" then put unformatted skip(1). /* yes, this is a weird thing */
end.
To copy all the contents of first df file to another df file, use this code:
DEFINE VARIABLE TEXT-STRING AS CHARACTER FORMAT "X(76)".
INPUT FROM "C:\OPENEDGE\WRK\CMD-LOG.DF".
OUTPUT TO "C:\OPENEDGE\WRK\MY.DF".
DO WHILE TRUE ON ENDKEY UNDO, LEAVE:
IMPORT UNFORMATTED TEXT-STRING.
MESSAGE TEXT-STRING .
END.
INPUT CLOSE.
OUTPUT CLOSE.

Progress OpenEdge simple utility with input parameter

I have a .p file and I run it in unix console. and i love it . because it just a simple utility to run. It is simple and helps me learn.
Now I started to get more fancy. I am wondering if I can do an input parameter to a .p file?
this is how I usually run my .p file.
Now if test.p needs 2 parameter .. how do I do it in the .p? and how do I run it in the console?
This is what I have in the test.p and dOrd and dLocation is the input parameter I want.
output to /usr2/appsrv/test/test.txt.
def var dOrd like Ord.Ord.
def var dLocation like Ord.Ord.
find OrdCSRef no-lock where OrdCSRef.Ord = dOrd and OrdCSRef.Loc = dLocation no-error.
if available OrdCSRef then do:
put unformatted OrdCSRef.CSOrdRef skip.
end.
else
put unformatted "Create CSOrdRef" skip.
end.
output close.
I have tried the following syntax in the unix console. but obviously it will not work.
INPUT parameters is what you are looking for. Change test.p as follows and then run it as "RUN /usr2/appsrv/test/test.p ("ARG1", "ARG2")"
output to /usr2/appsrv/test/test.txt.
def input parameter dOrd like Ord.Ord.
def input parameter dLocation like Ord.Ord.
find OrdCSRef no-lock where OrdCSRef.Ord = dOrd and OrdCSRef.Loc = dLocation no-error.
if available OrdCSRef then do:
put unformatted OrdCSRef.CSOrdRef skip.
end.
else
put unformatted "Create CSOrdRef" skip.
end.
output close.

Python : Convert ascii string to unicode string

I have an ascii string, e.g.
"\u005c\u005c192.150.4.89\u005ctpa_test_python\u005c5.1\u005c\videoquality\u005crel_5.1.1Mx86\u005cblacklevelsetting\u005c\u5e8f\u5217\u5e8f\u5217.xml"
And I want to convert it into unicode and dump into a file, so that it gets dumped like:
"\\192.150.4.89\tpa\tpa_test_python\5.1\videoquality\logs\blacklevelsetting\序列序列.xml"
Please share your thoughts.
Thanks,
Abhishek
Use the unicode_escape codec. Python 3 example:
s=rb'\u005c\u005c192.150.4.89\u005ctpa_test_python\u005c5.1\u005cvideoquality\u005crel_5.1.1Mx86\u005cblacklevelsetting\u005c\u5e8f\u5217\u5e8f\u5217.xml'
s=s.decode('unicode_escape')
with open('out.txt','w',encoding='utf8') as f:
f.write(s)
Output to file:
\\192.150.4.89\tpa_test_python\5.1\videoquality\rel_5.1.1Mx86\blacklevelsetting\序列序列.xml
Note: There was an extra backslash before videoquality that turned the v to a \v character (vertical form feed) that I removed from your example string.

Input a .i in a progress 10.2b

I'm inputting a file that needs to be converted to an xml file but I also want to input a .i with the definition of the temp table used to create the xml. Also the delimiter isn't working (I need a way to convert a variable to something the command can read). Thanks!
define input parameter pInputFile as character no-undo.
define input parameter pDelimiter as character no-undo.
???define input parameter pIncludeFile as character no-undo.???
define output parameter pOutputFile as character no-undo init "/tmp/out..
/* start of .i */
define temp-table ttGeneric no-undo
field cust_id as integer
field name as character
field address as character
field address2 as character
field city as character
field state as character
field zip as character
field cust_key as character
index idx is primary cust_id.
/* end of .i */
input stream sImport from value(pInputFile) no-echo.
repeat:
create ttGeneric.
import stream sImport delimiter pDelimiter ttGeneric.
end.
input stream sImport close.
temp-table ttGeneric:write-xml("LONGCHAR", pOutputFile, yes).
maybe set a pre-processor in the calling program (some how).
Delimiters to IMPORT and EXPORT must be literal strings. You cannot use variables, fields, parameters or anything like that.
I have, on occasion, worked around that with a CASE statement. i.e.:
case pDelimiter:
when "," then import stream sImport delimiter "," ttGeneric.
when "|" then import stream sImport delimiter "|" ttGeneric.
end.
Ugly. But it works.
I think you might be trying to say that you want to pass the name of an include file that contains a TT definition? And somehow associate that definition with a temp-table?
If that is more or less correct then you are probably being too specific about your requirement -- you probably really just want to dynamically create a TT whose definition is external to the program and unknown at compile time.
One way to do that is to use the read-xmlschema() method -- you're already using write-xml() so it's a small step... first convert your .i to a little .p like so:
define temp-table ttGeneric no-undo
field cust_id as integer
field name as character
field address as character
field address2 as character
field city as character
field state as character
field zip as character
field cust_key as character
index idx is primary cust_id.
buffer ttGeneric:write-xmlschema( "file", "ttgeneric.xsd", true, ?, ?, ? ).
return.
(This little stub lets you create the .XSD file. It serves no other purpose. Just run it once to get that file.)
Then when you want to use that temp-table:
define variable tx as handle no-undo.
create temp-table tx.
tx:read-xmlschema( "ttgeneric.xsd", "file", ?, ?, ? ).
(Note: Unlike with delimiters you can use variables & parameters etc. for the xsd name and it could be in a longchar rather than a file...)
The next adventure you will run into is figuring out a replacement for IMPORT that works with dynamic temp-tables. Buffer handles don't have import() and export() methods :(
The following snippets may help:
define variable dummy as character no-undo extent 128.
...
dummy = ?.
import dummy.
...
do i = 1 to 128:
if dummy[i] = ? then leave.
tx:buffer-field( i ):buffer-value() = dummy[i].
end.

How to list text files in a directory in Progress 4GL?

I have a folder in c drive,whicn contain 1000 txt file,i want
to get the list of all these txt file. How can i get this list?
Use the OS-DIR() function.
For example:
DEFINE STREAM dirlist.
DEFINE VARIABLE filename AS CHARACTER FORMAT "x(30)" NO-UNDO.
INPUT STREAM dirlist FROM OS-DIR(".").
REPEAT:
IMPORT STREAM dirlist filename.
DISPLAY filename.
END.
INPUT CLOSE.
For example: ipcPath = "C:\temp\
DEFINE INPUT PARAMETER ipcPath AS CHARACTER NO-UNDO.
DEFINE VARIABLE chFiles AS CHARACTER NO-UNDO.
INPUT FROM OS-DIR(ipcPath).
REPEAT:
IMPORT UNFORMATTED chImport NO-ERROR.
DISPLAY chFiles FORMAT "X(75)".
END.
INPUT CLOSE.
chFiles is a spacedelimeted list and contains the filename, the path, and an 'F' or 'D' tag.
I have a directory-tools program which enable a developer to do all kinds of fun things with file systems. You can get the code here: http://communities.progress.com/pcom/docs/DOC-16578