rejetto / hfs2

web based file server
https://rejetto.com/hfs
GNU General Public License v3.0
601 stars 131 forks source link

♻️ scriptLib.pas - macrosLog, | Suggestions for Improvements and Optimization #39

Open filhotecmail opened 1 year ago

filhotecmail commented 1 year ago

The condition "if sizeOfFile(MACROS_LOG_FILE) = 0 then" is checking if the log file is empty before appending the new record. This can lead to an additional read of the file to check the size, which can be inefficient for large files. An alternative would be to keep a separate counter or use a global variable to check if this is the first time a record is being added and then add the log header if so.


const
  HtmlEncodeChars: array[0..4] of record
    Char: Char;
    Code: string;
  end = (
    (Char: '<'; Code: '&lt;'),
    (Char: '>'; Code: '&gt;'),
    (Char: '&'; Code: '&amp;'),
    (Char: '"'; Code: '&quot;'),
    (Char: ''''; Code: '&apos;')
  );

function htmlEncode(const s: string): string;
var
  i, j, len: integer;
begin
  len := Length(s);
  SetLength(Result, len * 6); // máximo tamanho possível após a conversão
  j := 1;

  for i := 1 to len do
  begin
    if s[i] < ' ' then // caracteres de controle
    begin
      Result[j] := '?';
      Inc(j);
    end
    else
    begin
      case s[i] of
        '<', '>', '&', '"', '''':
          begin
            Move(PChar(HtmlEncodeChars[s[i] = '<']).^, PChar(@Result[j])^, Length(HtmlEncodeChars[s[i] = '<']) * SizeOf(HtmlEncodeChars[0]));
            Inc(j, Length(HtmlEncodeChars[s[i] = '<']));
          end;
      else
        Result[j] := s[i];
        Inc(j);
      end;
    end;
  end;

  SetLength(Result, j - 1);
end;

Create a constant called HtmlEncodeChars that contains a list of HTML characters and their HTML encoded equivalents.

I've defined an htmlEncode function that iterates through the characters in the input string, checks whether each character needs to be HTML-encoded, and then adds the encoded character to the result. This is done using the Move function to copy the corresponding HTML code into the result.

If the macrosLog method is called frequently over a short period of time, it can be useful to group multiple log entries into a single write to disk.

This can be done by adding a global variable that stores the last write time to disk and then checking that a sufficient amount of time has passed since the last disk write before writing the new log entry.

rejetto commented 1 year ago

hey Carlos, I forgot to write on the first page that this project is not actively developed anymore. I started a completely new project an another repository. https://github.com/rejetto/hfs