|

How to write an editor - Chapter 3 - by Aiwendil
Harddrives, no matter how fast, are always slow compared to RAM.
Fileoperations, operations with files, are nice if you at some point would like to save your work. This is a part that can cause really big problems for a programmer since there are too many different ways to store information in a file. Since this editor is indended for multiple platforms and to write files to use with fpc I will make the example code use hard CrLf.
Units we use are buffers and dos.
First we need a function to determine if there is a file or not in place.
FUNCTION IsFile(FileName:STRING):Integer;
{Will return 0 if file is found}
VAR TSearchRec : SearchRec;
BEGIN
FindFirst(FileName,$3F,TSearchRec);
IsFile:=DOSError;
END;
That one is pretty straightforward, for more info take a look at DOSError in any good documentation about Pascal or DOS.
And we do also need a procedure (no errorchecking is implemented in it anyway since it would only be extra code to write) to get a line from a file and put it into a buffer.
FUNCTION LoadFile(FileName:STRING):tBufferList;
{And as always this performs no error-checking, you should make sure of
that no error can occur before you call this one}
VAR BuffList : tBufferList;
Buffer : pBuffer;
F : Text;
S : STRING;
BEGIN
InitBufferList(BuffList);
Assign(F,FileName);
Reset(F);
WHILE NOT EoF(F) DO BEGIN
ReadLn(F,S);
AllockBuffer(Buffer);
AllocBufferData(Buffer,S);
AddBufferToList(BuffList,Buffer);
END;
Close(F);
LoadFile:=BuffList;
END;
And a savefile is also needed, by now you should have gotten the hang of how I do things.. and as always, check for the file first, this procedure will overwrite the file if it exist.
PROCEDURE SaveFile(FileName:STRING;BufferList:tBufferList);
VAR F : Text;
S : STRING;
tempBuffer : pBuffer;
BEGIN
IF BufferList.FirstBuffer=NIL THEN Exit;
Assign(F,FileName);
tempBuffer:=BufferList.FirstBuffer;
Rewrite(F);
WHILE tempBuffer<>NIL DO BEGIN
S:=GetBufferData(tempBuffer);
WriteLn(F,S);
tempBuffer:=tempBuffer^.Next;
END;
Close(F);
END;
That seems to be what is needed in this unit.. so we call it
End of part3
And also that is the end of the actual tutorial.. now maybe you are thinking, "Hey, but this isn't an working editor, all I got was some lousy procedures, functions and units and nothing more". Well, that is true but hey, if you wanted me to write the entire program I would have written an GPL:ed editor, this shows one (and there are several more ways) so solve many of the problems that occur and can make you stuck for months untilyou give up completly.
"But in part1 you also stated that you would split it up into keyboard.pas and types.pas" yes, I did. keyboard.pas is straightforward and what should be in it and some may even prefer to have it as their main-part of the program, and types.pas is more meant for the programs global vars that should be accessible when you expand the units.
You must by yourself figure out how to forge all of this together, and that is actually easier than one might think..
Hope it helps..
Back to previous page
|