Algorithms
File
 previous   up   next 

Check if a file exists

The "osfiles.s7i" library defines the function fileType which can be used to determine if a file exists.

if fileType("/foo/bar") <> FILE_ABSENT then
  writeln("file exists");
end if;

Check if a file is a directory

The "osfiles.s7i" library defines the function fileType which can be used to determine the type of a file.

if fileType("/foo/bar") = FILE_DIR then
  writeln("file is directory");
end if;

Check if a file is a regular file

The "osfiles.s7i" library defines the function fileType which can be used to determine the type of a file.

if fileType("/foo/bar") = FILE_REGULAR then
  writeln("file is regular file");
end if;

Copy a directory and all its files to a new directory

The "osfiles.s7i" library defines the function copyFile, which copies whole directory hierarchies.

$ include "seed7_05.s7i";

const proc: main is func
  begin
    copyFile(argv(PROGRAM)[1], argv(PROGRAM)[2]);
  end func;

Move or rename a file

The "osfiles.s7i" library defines the function moveFile, which moves or renames a file. Since a directory is also a file this function can also move or rename directories.

$ include "seed7_05.s7i";

const proc: main is func
  begin
    moveFile(argv(PROGRAM)[1], argv(PROGRAM)[2]);
  end func;

Convert files from windows to unix format

This is done by replacing CR LF with LF.

$ include "seed7_05.s7i";
  include "getf.s7i";

const proc: main is func
  local
    var string: file_name is "";
  begin
    for file_name range argv(PROGRAM) do
      putf(file_name, replace(getf(file_name, "\r\n", "\n")));
    end for;
  end func;

Convert files from unix to windows format

This is done by replacing LF with CR LF.

$ include "seed7_05.s7i";
  include "getf.s7i";

const proc: main is func
  local
    var string: file_name is "";
  begin
    for file_name range argv(PROGRAM) do
      putf(file_name, replace(getf(file_name, "\n", "\r\n")));
    end for;
  end func;

Read a 16 bit little endian integer from a file

The function getUInt16Le is part of the "bytedata.s7i" library.

const func integer: getUInt16Le (inout file: in_file) is func
  result
    var integer: number is 0;
  local
    var string: stri is "";
  begin
    stri := gets(in_file, 2);
    number := ord(stri[1]) +
              ord(stri[2]) * 256;
  end func;

Read a 32 bit little endian integer from a file

The function getUInt32Le is part of the "bytedata.s7i" library.

const func integer: getUInt32Le (inout file: in_file) is func
  result
    var integer: number is 0;
  local
    var string: stri is "";
  begin
    stri := gets(in_file, 4);
    number := ord(stri[1]) +
              ord(stri[2]) * 256 +
              ord(stri[3]) * 65536 +
              ord(stri[4]) * 16777216;
  end func;

Read a 16 bit big endian integer from a file

The function getUInt16Be is part of the "bytedata.s7i" library.

const func integer: getUInt16Be (inout file: in_file) is func
  result
    var integer: number is 0;
  local
    var string: stri is "";
  begin
    stri := gets(in_file, 2);
    number := ord(stri[1]) * 256 +
              ord(stri[2]);
  end func;

Read a 32 bit big endian integer from a file

The function getUInt32Be is part of the "bytedata.s7i" library.

const func integer: getUInt32Be (inout file: in_file) is func
  result
    var integer: number is 0;
  local
    var string: stri is "";
  begin
    stri := gets(in_file, 4);
    number := ord(stri[1]) * 16777216 +
              ord(stri[2]) * 65536 +
              ord(stri[3]) * 256 +
              ord(stri[4]);
  end func;

Program that encodes and decodes with rot13

This rot13 program reads from standard input and writes to standard output.

$ include "seed7_05.s7i";

const proc: main is func
  local
    var char: ch is ' ';
  begin
    ch := getc(IN);
    while not eof(IN) do
      if (ch >= 'a' and ch <= 'm') or (ch >= 'A' and ch <= 'M') then
        ch := chr(ord(ch) + 13);
      elsif (ch >= 'n' and ch <= 'z') or (ch >= 'N' and ch <= 'Z') then
        ch := chr(ord(ch) - 13);
      end if;
      write(ch);
      ch := getc(IN);
    end while;
  end func;

Remove empty lines

The program copies from standard input to standard output, except for empty lines, which are ommitted from the output.

$ include "seed7_05.s7i";

const proc: main is func
  local
    var char: ch is ' ';
    var char: lastChar is '\n';
  begin
    ch := getc(IN);
    while ch <> EOF do
      if ch <> '\n' or lastChar <> '\n' then
        write(OUT, ch);
      end if;
      lastChar := ch;
      ch := getc(IN);
    end while;
  end func;

 previous   up   next