From e0198ddb912f646fa93b137133015cb7ab9ff859 Mon Sep 17 00:00:00 2001 From: Merlijn Wajer Date: Mon, 24 May 2010 12:08:23 +0200 Subject: [PATCH] AppendFile. --- Doc/mufasa_ps_handbook.tex | 60 ++++++++++++++++++++++ Units/MMLAddon/PSInc/Wrappers/file.inc | 5 ++ Units/MMLAddon/PSInc/psexportedmethods.inc | 1 + Units/MMLCore/files.pas | 27 ++++++++++ 4 files changed, 93 insertions(+) diff --git a/Doc/mufasa_ps_handbook.tex b/Doc/mufasa_ps_handbook.tex index 7ce6a08..cd902e3 100644 --- a/Doc/mufasa_ps_handbook.tex +++ b/Doc/mufasa_ps_handbook.tex @@ -707,17 +707,77 @@ Simba provides several functions to access files - that is, read from them and write to them. \subsection{CreateFile} + +CreateFile creates a file. It will only create +the file if ti doesn't already exist. + +Events called: +\begin{itemize} +\item WriteFileEvent +\end{itemize} + \subsection{OpenFile} + +Opens a file for reading. + +Events called: +\begin{itemize} +\item ReadFileEvent +\end{itemize} \subsection{RewriteFile} + +Rewrite files opens a file for reading and writing, but will remove all the +contents of a file. To open a file for appending, use AppendFile. + +Events called: +\begin{itemize} +\item WriteFileEvent +\end{itemize} +\subsection{AppendFile} +AppendFile opens a file for reading and writing, and will not remove the +contents of the file. + +Events called: +\begin{itemize} +\item WriteFileEvent +\end{itemize} \subsection{CloseFile} + +CloseFile closes the given file. + \subsection{EndOfFile} + +EndOfFile returns true if the character pointer in the file has reached the end +of the file. + \subsection{FileSize} + +FileSize returns the length of the file in bytes. + \subsection{ReadFileString} + +ReadFileString reads a string from a file. + \subsection{WriteFileString} + +WriteFileString writes a string to a file + \subsection{SetFileCharPointer} + +Set the char pointer to a specific position. + \subsection{FilePointerPos} + +Get the position of the char pointer. + \subsection{DirectoryExists} + +Returns true if the directory exists. + \subsection{FileExists} + +Returns true if the file exists. + \subsection{WriteINI} \subsection{ReadINI} \subsection{DeleteINI} diff --git a/Units/MMLAddon/PSInc/Wrappers/file.inc b/Units/MMLAddon/PSInc/Wrappers/file.inc index 435e833..f2b1589 100644 --- a/Units/MMLAddon/PSInc/Wrappers/file.inc +++ b/Units/MMLAddon/PSInc/Wrappers/file.inc @@ -36,6 +36,11 @@ begin Result := CurrThread.Client.MFiles.RewriteFile(Path, Shared); end; +function ps_AppendFile(const Path: string): Integer; extdecl; +begin + Result := CurrThread.Client.MFiles.AppendFile(Path); +end; + procedure ps_CloseFile(FileNum: Integer); extdecl; begin CurrThread.Client.MFiles.CloseFile(FileNum); diff --git a/Units/MMLAddon/PSInc/psexportedmethods.inc b/Units/MMLAddon/PSInc/psexportedmethods.inc index 21f9e23..d3a0a42 100644 --- a/Units/MMLAddon/PSInc/psexportedmethods.inc +++ b/Units/MMLAddon/PSInc/psexportedmethods.inc @@ -111,6 +111,7 @@ SetCurrSection('Files'); AddFunction(@ps_CreateFile, 'function CreateFile(const Path: string): Integer;'); AddFunction(@ps_OpenFile, 'function OpenFile(const Path: string; Shared: Boolean): Integer;'); AddFunction(@ps_RewriteFile, 'function RewriteFile(const Path: string; Shared: Boolean): Integer;'); +AddFunction(@ps_AppendFile, 'function AppendFile(const Path: string): Integer;'); AddFunction(@ps_CloseFile, 'procedure CloseFile(FileNum: Integer);'); AddFunction(@ps_EndOfFile, 'function EndOfFile(FileNum: Integer): Boolean;'); AddFunction(@ps_FileSize, 'function FileSize(FileNum: Integer): LongInt;'); diff --git a/Units/MMLCore/files.pas b/Units/MMLCore/files.pas index e03ce53..c5e6082 100644 --- a/Units/MMLCore/files.pas +++ b/Units/MMLCore/files.pas @@ -44,6 +44,7 @@ type function CreateFile(Path: string): Integer; function OpenFile(Path: string; Shared: Boolean): Integer; function RewriteFile(Path: string; Shared: Boolean): Integer; + function AppendFile(Path: string): Integer; procedure CloseFile(FileNum: Integer); function EndOfFile(FileNum: Integer): Boolean; function FileSizeMuf(FileNum: Integer): LongInt; @@ -273,6 +274,32 @@ begin Result := AddFileToManagedList(Path, FS, fMode); end; +function TMFiles.AppendFile(Path: string): Integer; +var + FS: TFileStream; + fMode: Integer; + Continue : Boolean; +begin + if Assigned(WriteFileEvent) then + begin + Continue := true; + WriteFileEvent(Self,path,continue); + if not Continue then + exit(File_EventError); + end; + fMode := fmOpenReadWrite; + if not FileExists(Path) then + fMode := fMode or fmCreate; + try + FS := TFileStream.Create(UTF8ToSys(Path), fMode); + FS.Seek(0, fsFromEnd); + Result := AddFileToManagedList(Path, FS, fMode); + except + Result := File_AccesError; + TClient(Client).Writeln(Format('AppendFile - Exception. Could not create file: %s',[path])); + end; +end; + {/\ Opens a file for writing. And deletes the contents. Returns the handle (index) to the File Array.