I have modified my Document to text wrapper that functions like Receive packet.
Not sure if anyone is really interested in this but since I did post the
original method I thought it best for me to post the modified version.
The new version has 2 uses..
1. To replace Receive packet as it was used in a loop to receive the
contents of a document 1 row at a time.
2. To load the contents of a document with a specified EOL character
regardless of platform, with or without an existing header row
Also changed is the requirement to pass the break mode parameter (EOL) when
using as a replacement for Receive Packet. Only the first 2 parameters are now
passed.
I hope someone finds this useful.
John
// Method: ReceivePacket_DocToText
(->pathToDocumentVariable;->VariableToHoldDocContents{;BreakMode}{;RemoveTopRows})->
rowText | {header text | document text}
// ----------------------------------------------------
// Created by: John Baughman
// Date and time: 34/23/20, 1:05 PM
// ----------------------------------------------------
// Description
// Replacement for Receive Packet using Document To Text
// Parameters
C_POINTER($1;$pathPtr) //$1 is a pointer to the variable holding the path to
the document.
//In a repeat loop you can put an empty string in the variable to allow the
user to pick the document using Document Select.
//The variable will be updated to contain the chosen path which will prevent
the document from being loaded again
//If the user cancels the Select Document, $rowText:="", and
$documentTextPtr->:=""
C_POINTER($2;$documentTextPtr) //Pass an empty text variable in the first call
and it will be loaded from the document with Document to text.
//subsequent calls will not reload the document as the variable will no
longer be empty. Instead the wrapper will use this variable as passed in ($2)
//The variable will be loaded with the document contents minus the row being
returned.
C_TEXT($0;$rowText) //$RowText will hold the first row in the $DocumentTextPtr
variable and returned in $0.
//The first row in the $DocumentTextPtr variable wil be removed.
//If you pass more than 2 parameters $0 will be the document contents or
removed top rows as explained in the following parameters
//The following parameters are used only if you just want the contents of the
document. DO NOT USE IN A LOOP
C_LONGINT($3;$breakMode) //Document to Text will convert the EOL character in
the document to the passed desired break mode constants...
//Document with CR
//Document with CRLF
//Document with LF
//Document with native format
C_LONGINT($4;$removeTopRowscount) //The number of top rows to be removed from
the returned contents.
//The document contents with or without top rows removed will be returned in
the $documentTextPtr variable ($2)
//The removed lines will be returned in $0
//If you want the whole document pass 0 in $4. In the case both $0 and the
$documentTextPtr variable ($2) will contain the whole document
//The EOL character(s) will be the requested characters as passed in $3
If (False) //Example Calls
//With regard to EOL characters, the following examples will work on
both Mac and Windows platforms
//-----------
//To replace Recieve Packet getting each row one at a time in a loop
C_TEXT($TextValue;$path)
$TextValue:=""
$path:=""
Repeat
$row:=ReceivePacket_DocToText (->$path;->$TextValue)
//handle the row
Until (ok=0)
//-----------
//To get the whole document contents with CRs as the EOL characters
C_TEXT($TextValue;$path)
$DocumentText:=""
$path:=""
$DocumentText:=ReceivePacket_DocToText
(->$path;->$DocumentText;Document with CR;0) //Get the whole document
//$DocumentText = whole document with CRs including a header row if
it exists
//Note: In this context $2 can be a nill pointer
//-----------
//To get the document contents WITHOUT the header row, with CRs as
the EOL characters
C_TEXT($TextValue;$path)
$DocumentText:=""
$path:=""
$removedRows:=ReceivePacket_DocToText (->$path;->$DocumentText;Document
with CR;1) //Get the whole document without the header row. To remove 1 top
rows, pass 1, for 2 rows pass 2, etc.
//$romovedRows is the header row and $DocumentText contains the
document content without the header row, both with CRs
End if
// ----------------------------------------------------
$pathPtr:=$1
$DocumentTextPtr:=$2
If (Is nil pointer($2))
$documentText:=""
$DocumentTextPtr:=->$documentText
End if
If (Count parameters=2)
$breakMode:=Document with CR //The wrapper will always use CR for
Recieve packet functionality
Else
//Document text with EOL characters is being requested
$breakMode:=$3
$removeTopRowscount:=$4
End if
$rowText:=""
If ($pathPtr->="")
//They want to select the document
ARRAY TEXT($aSelected;0)
$path:=Select document("";"*";"";0;$aSelected)
If (ok=1)
$pathPtr->:=$aSelected{1}
End if
End if
If (ok=1)
If ($DocumentTextPtr->="") | (Count parameters=4)
//Document has not yet been loaded
If (Is Windows)
$documentTextPtr->:=Document to
text($pathPtr->;"ANSI_X3.4-1986";$breakMode)
$stopCharacter:="\r\n"
Else
$documentTextPtr->:=Document to
text($pathPtr->;"MacRoman";$breakMode)
$stopCharacter:="\r"
End if
End if
Case of
: ($breakMode=Document with CR)
$stopCharacter:="\r"
: ($breakMode=Document with LF)
$stopCharacter:="\n"
: ($breakMode=Document with CRLF)
$stopCharacter:="\r\n"
//else
//native format... $stopCharacter was set above
following document to text
End case
If (Count parameters=2)
If (Position($stopCharacter;$DocumentTextPtr->)=0)
//Assumes that this is the last row without a EOL
character. Otherwise it is an error
$rowText:=$documentTextPtr-> //return what is left
ok:=0 //we are done. Recieve packet sets ok to 0 when
done in a repeat loop so I am doing the same here
$documentTextPtr->:=""
Else
$rowText:=Substring($documentTextPtr->;1;Position($stopCharacter;$documentTextPtr->)-1)
//return the first row without the stop character
$documentTextPtr->:=Replace
string($documentTextPtr->;$rowText+$stopCharacter;"")
End if
$0:=$rowText
Else
If ($removeTopRowscount=0)
$0:=$documentTextPtr->
Else
For ($i;1;$removeTopRowscount)
If
(Position($stopCharacter;$documentTextPtr->)#0)
$rowToRemove:=Substring($documentTextPtr->;1;Position($stopCharacter;$documentTextPtr->))
$rowText:=$rowText+$rowToRemove
$documentTextPtr->:=Replace
string($documentTextPtr->;$rowToRemove;"")
End if
End for
$0:=$rowText
End if
End if
Else
$DocumentTextPtr->:=""
$0:=$rowText
End if
**********************************************************************
4D Internet Users Group (4D iNUG)
Archive: http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub: mailto:[email protected]
**********************************************************************