Foxtools – Editor _EDGETSTR

Version: 01.10.10 - last Update: Tuesday, October 13, 2009, 12:45:00

Previous ChapterFoxtools Home (Alphabetical TOC)Foxtools Home (Grouped TOC)Next Chapter See also:_EdGetChar | _EdGetLNum | _EdGetLPos | _EdGetPos | _EdPosInVi | _EdSkipLin


Foxtools Editor Support Functions

_EDGETSTR

_EdGetStr() Returns all characters starting from a specified offset until second offset is reached in the editor session passed in <nWHAND> parameter.

VFP Syntax

cString = _EdGetStr(nWHAND, nOffset1, nOffset2)

Parameters

[in] nWHAND

Fox-window handle of the editor session in question. If nWHANDL is no valid editor-session handle, an error “API call caused an exception” (Error 2028) is raised.

[in] nOffset1

Point in file from where text retrieval starts. This start offset value is zero-based counted from begin of file (BOF).

[in] nOffset2

Point in file where text retrieval ends. This end offset value is zero-based counted from begin of file (BOF).

Returns

String – all characters between start- and end- offset in the file.

Remarks

_EdGetStr() retrieves all characters between both offset positions including the two characters at both end points.

Note: If nOffset2 (the end point) is larger than the file size, an empty string is returned. You can get the file size (max value for <nOffset2> from 2nd element of your editor session’s environment array. _EdGetEnv() has more information on that.

Note: Using negative values for <nOffset> shows some “interesting” outcome. IMHO there is no internal boundary checking. Thus, we can “read over” the left text buffer boundary – cool? Just try something like this:
nWHAND =_EDOPENFIL("TEST.TXT", 1)
FOR lnLOOP = 1 TO 4000
   ??_EdGetStr(m.nWHAND, - m.lnloop, 0)
NEXT

Note: Issuing  _EdGetStr(m.nWHAND,0 , 0) returns the first character in the file.

Note: There is another oddity I ran into: my testing showed that _EdGetStr() sometimes isn’t aware of modification that were made to an editor’s text buffer! See 2nd code example below!

VFP Example(s)

Opening an editor session and returning first 2 lines:

CLEAR
*\\ Open editor session
*\\   use file with at least 3 lines of text
nWHAND =_EDOPENFIL("TEST.TXT", 1)
*\\ get offset to BOL of last line
lnLines = _EdGetLNum(m.nWHAND, _EdGetLPos(m.nWHAND, -1))+1
IF lnLines = 2
  ? "That's too easy!"
ELSE
  IF lnLines > 2 && one-based numbering here
    *\\ BOL offset of 3rd line minus CR, minus position caret;
    *\\     before last character on line#2 >>> this is the;
    *\\     position of last character of line #2
    nOffset2 = _EdGetLPos(m.nWHAND, 2) – 2
    *\\ "--" are just to proof that there are no CRs retrieved
    ??"--"
    ? _EdGetStr(m.nWHAND,0 , m.nOffset2)
    ? "--"
    *//
  ELSE
    ? "not enough lines in file"
  ENDIF
ENDIF
Play with following lines to test if _EdGetStr() always returns the right text:
*\\ Use a file with let’s say 10 lines of text.
*\\ Open such a file in editor
nWHAND =_EDOPENFIL("TEST.TXT", 1)
nOffset1 = _EdGetLPos(m.nWHAND, 1)    && from beginning of 2nd line
nOffset2 = _EdGetLPos(m.nWHAND, 5) -2 && to end of 5th line (w/o CR)
CLEAR
??"--"
? _EdGetStr(m.nWHAND, m.nOffset1, m.nOffset2)
? ""
*\\ Now, change/delete editor content BUT DO NOT SAVE it, then re-run
*\\ Add empty lines then add a single character to those lines, in between
*\\ re-run the code and observe the outcome. Then save file and re-run

Previous ChapterFoxtools Home (Alphabetical TOC)Foxtools Home (Grouped TOC)Next Chapter