> On Mar 25, 2020, at 8:21 PM, Keisuke Miyako via 4D_Tech 
> <4d_tech@lists.4d.com> wrote:
> 
> sometimes, it might just be easier to read the entire text with
> 
> Document to text (which can normalise the EOL character)

I took Keisuke’s advice to heart and looked closer at Document to text. I did 
not realize that it provided a means to handle any EOL confusion between Mac 
and Windows. For me this makes the use of Document to text a much better way to 
read the contents of a document by row than Receive packet. 

 In an effort to duplicate Receive packet’s functionality when used in a repeat 
loop to get rows of a tab delimited text document, I created a wrapper method 
as a replacement for Receive packet.

I am posting this method here in the event someone on on the NUG might find it 
useful. More importantly for me, however, perhaps to get some feedback with 
regard to anything I may have overlooked or anything I may need to do to clean 
it up.

It was written in a v14 database so does not use any of the newer features in 
v17 and later like Is Windows and/or objects.

 Thanks for any feedback.

John


------------------------------

  // Method: ReceivePacket_DocToText 
(->pathToDocumentVariable;->VariableToHoldDocContents;BreakMode)
  // ----------------------------------------------------
  // Created by: John Baughman
  // ----------------------------------------------------
  // 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 for subsequent 
calls for rows in the loop. 
  //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.
  //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.

C_LONGINT($3;$breakMode)  //Document to Text will convert the EOL character to 
the following desired break mode constants...
  //Document with CR
  //Document with CRLF
  //Document with LF
  //Document with native format

  //Example Call
If (False)
  C_TEXT($TextValue;$path)
  $TextValue:=""
  $path:=""
  Repeat 
    $rowText:=ReceivePacket_DocToText (->$path;->$TextValue;Document with CR)
         //If TextValue is “” then the text will be loaded from the document 
and returned minus the first row in TextValue
         //Subsequent calls will return first row in $rowText and return the 
text minus the first row in TextValue.
      If (rowText#””)
         //handle the row
      End if
  Until (TextValue =“")   //note: I do not use the ok variable as I found that 
the ok variable may get set incorrectly by 4D if the last row does not have CR.

End if 

  // ----------------------------------------------------

$pathPtr:=$1
$DocumentTextPtr:=$2
$breakMode:=$3
$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->="")
                  //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 

           //else document has been loaded use text in DocumentTextPtr
                
        End if 
        
        $found:=False
        
        Case of 
                        
                : ($breakMode=Document with CR)
                        $stopCharacter:="\r"
                        
                : ($breakMode=Document with LF)
                        $stopCharacter:="\n"
                        
                : ($breakMode=Document with CRLF)
                        $stopCharacter:="\r\n"
                        
                          //else
                          //$stopCharacter was set to the default for the 
platform above following document to text
                        
        End case 
        
        
        If (Position($stopCharacter;$DocumentTextPtr->)=0)
                  //Assumes that this is the last row without an 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. I have found the ok variable to be 
unreliable in this method
                $documentTextPtr->:=“”    //nothing left
                
        Else 
                
$rowText:=Substring($documentTextPtr->;1;Position($stopCharacter;$documentTextPtr->)-1)
  //put the first row without the stop character in $rowText
                $documentTextPtr->:=Replace 
string($documentTextPtr->;$rowText+$stopCharacter;””).   //return the document 
text minus the first row in documentTextPtr
                
        End if 
        
Else 
        $DocumentTextPtr->:=""
        
End if 

$0:=$rowText.  //return the first row
**********************************************************************
4D Internet Users Group (4D iNUG)
Archive:  http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:4d_tech-unsubscr...@lists.4d.com
**********************************************************************

Reply via email to