General: Files behave very
much like string objects. The key difference is that the string object data
resides in memory and the file data resides on some external physical media.
Once you create an instance of a file object and save it in a variable, you can
operate on that variable just as if it were a string object. In fact, it is a
string object. Currently, Glee reads the entire file into a
string object when it is opened and then services file requests through its
string object buffer. This will probably always be a feature of read only files
in Glee. This technique is not practical for very large files
like databases. Glee will eventually serve those needs as well.
For now, there is a limit on the size of Glee file objects.
Purge Directory: These
examples need to have a known environment in which to work. The file system
will currently only write to mountable media and to the c:\glee\user
path. As you will see, Glee files have very powerful features. To
prevent damaging your system I choose to limit the exposure to the
Glee corral at this time. The .purge method of the the
File Context object will remove all files and directories from a path. In this
tutorial all my work will be done in the c:\glee\user\test path.
Before attempting the purge, I clear the namespace ( #ns => @).
This assures me that all files opened by Glee are closed. Purge
can't delete open files. It will delete all the files that it can and reports
as a result a sequence of files and directories that can't be destroyed and
purged.
Illegal Purge: The previous
gave two examples of valid directory purges. This examples shows the behavior
in an attempt to perform an invalid purge.
Purge Behavior: This
example displays the behavior of the :purge method of the File Context
object when it is unable to complete the work. I create two files. I close one
(notice the Hnd property is 0) and leave the other open (the
Hnd property contains the current file handle). I then try to purge.
It reports the open file as an exception not being purged. Everything that can
be purged will be purged and is not reported. This sequence of unpurged objects
gives you what you need to report the deficiency and mitigate the situation.
Creating and writing a whole
file: This example illustrates assignment into a null index to replace fill
a file (i.e. replace its total contents.)
Creating and appending to
a file: When a file is created or opened, the write cursor is positioned at
its end. It is ready for data to be appended. Just like streaming to a string,
you can append to a file using the ->> operator. Notice
that this example illustrates conversion to a string with the
%* operator before appending. If this wasn't done,
Glee would append a sequence in the second append. That is an
advanced concept.
Example: This example creates
an instance of a file object. It then uses streaming and indexing techniques to
display the first occurrence of the word "file" and its relative
surroundings.