Jump to content

Read entire file: Difference between revisions

(→‎{{header|Prolog}}: Adding Prolog)
Line 837:
 
=={{header|FutureBasic}}==
Note: This code goes beyond simply specifying the file to open. It includes a dialog window (openpanel) that allows the user to select a text file to read. Depending on system memory, as many as 4.2 billion characters can be read. The file contents are placed in a convenient console window with automatic save as, copy and paste, select all and undoscrolling commandstextview. (Did I mention that FutureBasic -- or FB as developers prefer to call it -- is handy for Macintosh development!) Of course, the programmer is free to code his own window and menu options.
<lang futurebasic>_window = 1
begin enum 1
_scrollView
_textView
end ifenum
 
void local fn BuildWindow
Note: This code goes beyond simply specifying the file to open. It includes a dialog window that allows the user to select a text file to read. Depending on system memory, as many as 4.2 billion characters can be read. The file contents are placed in a convenient console window with automatic save as, copy and paste, select all and undo commands. (Did I mention that FutureBasic -- or FB as developers prefer to call it -- is handy for Macintosh development!) Of course, the programmer is free to code his own window and menu options.
CGRect r = {0,0,550,400}
<lang futurebasic>
window _window, @"Read Entire File", r
include "ConsoleWindow"
scrollview _scrollView, r
ViewSetAutoresizingMask( _scrollView, NSViewWidthSizable + NSViewHeightSizable )
textview _textView,, _scrollView
end fn
 
local fn ReadTextFile
CFStringRef string
dim as CFURLRef fileRef
CFURLRef url = openpanel 1, @"Select text file..."
dim as Handle h
if ( hurl )
dim as CFStringRef cfStr : cfStr = NULL
string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
dim as long fileLen
TextSetString( _textView, string )
 
else
if ( files$( _CFURLRefOpen, "TEXT", "Select text file...", @fileRef ) )
open "i",// 2,user fileRefcancelled
end if
fileLen = lof( 2, 1 )
h = fn NewHandleClear( fileLen )
if ( h )
read file 2, [h], fileLen
close #2
cfStr = fn CFStringCreateWithBytes( _kCFAllocatorDefault, #[h], fn GetHandleSize(h), _kCFStringEncodingMacRoman, _false )
fn DisposeH( h )
end if
else
// User canceled
end if
 
fn HIViewSetText( sConsoleHITextView, cfStr )
CFRelease( cfStr )
end fn
 
fn BuildWindow
fn ReadTextFile
 
</lang>
HandleEvents</lang>
This can be shortened considerably by wrapping Objective-C code:
<pre>
416

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.