![]() |
||
| Home | SwiftForth Archive | SwiftX Archive | |

Perhaps this isn't what you want either, but I use the following code to
read a text file into a scrolling window for viewing log files. I also use
it to implement a simple help option. Almost all of the code was written by
RVN.
Note that you can scroll around and view the text in the window even while
items are being appended to the file. Of course, the updates will not be
visible until you exit and re-view the file.
Attach VIEW-DAILY to a menu item in the usual way. This code assumes a
turnkeyed application. DailyFile$ is a Zstring containing the name of the
file to be displayed (this can be filled with a string returned from a file
selector dialog).
{
---------------------------------------------------------------------------
DISPLAY DAILY LOG
---------------------------------------------------------------------------
}
CREATE DailyFile$ ,Z" DailyFile.txt" \ test example
\ ===== Read file to memory
0 VALUE |FILE| \ holds last file size: valid only immediately after
GET-FILE?
: GET-FILE? ( adr len -- adr flag) \ flag is true for a good read
2DUP R/O OPEN-FILE IF
DROP 100 ALLOCATE DROP >R
S" File " R@ ZPLACE R@ ZAPPEND
S" not found." R@ ZAPPEND R> FALSE EXIT
THEN ( handle) 0 0 LOCALS| buf n fid | 2DROP
fid FILE-SIZE 2DROP DUP TO n TO |FILE|
n 4 + ALLOCATE DROP TO buf 0 buf n + C!
buf n fid READ-FILE 2DROP
fid CLOSE-FILE DROP buf TRUE ;
\ ===== Setup file viewer
0 WS_CHILD OR WS_VISIBLE OR WS_VSCROLL OR
WS_BORDER OR ES_LEFT OR ES_MULTILINE OR
ES_AUTOVSCROLL OR ES_READONLY OR
CONSTANT DLSTYLE
: DL-HEDIT ( -- hwnd zstr ) HWND Z" Daily Log Viewer" ;
: /DL-VIEWER ( - ior )
0 Z" edit" 0 DLSTYLE 0 0 0 0 HWND 1 HINST 0 CreateWindowEx
DL-HEDIT ROT SetProp 0= ;
: /DL-CONTENTS ( -- )
DL-HEDIT GetProp LPARAM @ SetWindowText DROP
LPARAM @ FREE DROP ;
[SWITCH DL-MESSAGES DEFWINPROC ( msg -- res )
WM_CREATE RUN:
/DL-VIEWER ?DUP ?EXIT /DL-CONTENTS 0 ;
WM_SETFOCUS RUN:
DL-HEDIT GetProp SetFocus DROP 0 ;
WM_SIZE RUN:
DL-HEDIT GetProp 0 0 LPARAM LOHI 1 MoveWindow DROP 0 ;
WM_DESTROY RUN:
DL-HEDIT RemoveProp DROP 0 ;
SWITCH]
:NONAME ( -- res ) MSG LOWORD DL-MESSAGES ; 4 CB: DL-CALLBACK
\ ===== Show logfile in separate window
: (show-daily) ( adr -) \ show logfile
( buffer_address -- ) >R Z" Log Viewer" DL-CALLBACK DefaultClass DROP
0 Z" Log Viewer" DUP WS_OVERLAPPEDWINDOW CW_USEDEFAULT DUP 2DUP
0 0 HINST R> CreateWindowEx ?DUP IF
DUP SW_SHOW ShowWindow DROP UpdateWindow DROP
ELSE
0 Z" Failed to start daily log viewer." Z" Daily Log Viewer"
MB_OK MessageBox DROP
THEN ;
: daily-viewer ( adr len -)
GET-FILE? IF ( file ok and buffer allocated, buffer adr on stack)
(show-daily)
ELSE ( error Zstring on stack)
DUP
0 SWAP Z" Daily Log Viewer" MB_OK MessageBox DROP
FREE DROP ( free memory containing error string)
THEN ;
: VIEW-DAILY ( - res) DailyFile$ ZCOUNT daily-viewer 0 ;
-----Original Message-----
From: Damir [mailto:damir.vrduka_at_os.tel.hr]
Sent: Saturday, March 30, 2002 10:33 AM
To: sftalk_at_forth.com
Subject: [sftalk] Re: File to memory and memory to file allot + mem to
swiftforth
Hello,
I am newbie to Forth.
Please tell me how to display filename.ext in simple
Windows window.
Thank you.
Damir
On Mon, 3 Dec 2001 10:51:02 -0800, you wrote:
>
>An example of file access and memory allocation:
>
>\ Use S" filename.ext" FILE>MEM
>: FILE>MEM ( filename count -- memAddr filesize )
> R/O OPEN-FILE THROW >R
> R@ FILE-SIZE THROW D>S ( size )
> DUP ALLOCATE THROW ( Allocate a Memory Block )
> DUP ROT R@ READ-FILE THROW ( bytes-read )
> R> CLOSE-FILE THROW ;
>
>: TEST ( -- ) \ Display file contents
> S" myfile.txt" FILE>MEM ( MemAddress FileSize )
> CR 2DUP TYPES ( Display File Contents )
> DROP ( discard size )
> FREE THROW ( Release Memory - we must do this! ) ;
>
>
>Note that TYPES is a version of TYPE that handles line feeds etc.
>
>Mike
>
>
>-----Original Message-----
>From: sftalk-bounce_at_forth.com [mailto:sftalk-bounce_at_forth.com]On Behalf
>Of Schmitt Louis Jean-Pierre
>Sent: Monday, December 03, 2001 9:44 AM
>To: sftalk_at_forth.com
>Subject: [sftalk] File to memory and memory to file allot + mem to
>swiftforth
>
>
>
>
>y tank you Mike for help
>
>my next problems
>
> a) Wat is the command to permit extend memory area
> b) to load an file to available memory
> c) to store an memory area to an file
>
>Tanks
>
>Louis Jean-Pierre
>----------------------------------------------------------------------
>sftalk_at_forth.com The SwiftForth programming discussion email list
>To unsubscribe, send subject "unsubscribe sftalk" to listar_at_forth.com
>For help with listar commands, send subject "help" to listar_at_forth.com
>Archives are located at http://www.forth.com/sftalk -- check them out!
>Search the archives! Visit http://www.forth.com/search for details.
>
>----------------------------------------------------------------------
>sftalk_at_forth.com The SwiftForth programming discussion email list
>To unsubscribe, send subject "unsubscribe sftalk" to listar_at_forth.com
>For help with listar commands, send subject "help" to listar_at_forth.com
>Archives are located at http://www.forth.com/sftalk -- check them out!
>Search the archives! Visit http://www.forth.com/search for details.
----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe sftalk" to listar_at_forth.com
For help with listar commands, send subject "help" to listar_at_forth.com
Archives are located at http://www.forth.com/sftalk -- check them out!
Search the archives! Visit http://www.forth.com/search for details.
----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe sftalk" to listar_at_forth.com
For help with listar commands, send subject "help" to listar_at_forth.com
Archives are located at http://www.forth.com/sftalk -- check them out!
Search the archives! Visit http://www.forth.com/search for details.
Received on Tue Apr 02 2002 - 07:11:02 PST
This archive was generated by hypermail 2.2.0 : Fri Nov 21 2008 - 03:04:22 PST