Task

Load the entire contents of some text file as a single string variable.

If applicable, discuss: encoding selection, the possibility of memory-mapping.

Of course, in practice one should avoid reading an entire file at once
if the file is large and the task can be accomplished incrementally instead
(in which case check File IO);
this is for those cases where having the entire file is actually what is wanted.




_window = 1
begin enum 1
  _scrollView
  _textView
end enum
 
void local fn BuildWindow
  CGRect r = {0,0,550,400}
  window _window, @"Read Entire File", r
  scrollview _scrollView, r
  ViewSetAutoresizingMask( _scrollView, NSViewWidthSizable + NSViewHeightSizable )
  textview _textView,, _scrollView
end fn
 
local fn ReadTextFile
  CFStringRef string
  CFURLRef url = openpanel 1, @"Select text file..."
  if ( url )
    string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
    TextSetString( _textView, string )
  else
    // user cancelled
  end if
end fn
 
fn BuildWindow
fn ReadTextFile
 
HandleEvents