Mocka utils

While in the process of getting one thing done, you need to develop procedures that can be reused by other programs. This section is about thast kind of procedures. In time, a MODULE Utils.mod will be created. Topics covered here are:

FileSize

Sometimes you just need to know the length of a file. Mocka does not supply a standard function call in an existing module for that. And neither does Linux. So this time I created my own. It works as follows:

Now, that doesn't sound too complicated, does it? Here is the source:
PROCEDURE FileSize (FileName : Strings.String) : CARDINAL;

CONST	Chunk		= 1024;

VAR	n, size		: CARDINAL;
	m		: INTEGER;
	inF		: BasicIO.File;
	tmpPool		: MemPools.MemPool;
	tmpPtr		: POINTER TO CHAR;

BEGIN
  n := 0;	m := 0;
  BasicIO.OpenInput (inF, FileName);
  IF  NOT BasicIO.DONE  THEN  RETURN 0  END;
  MemPools.NewPool (tmpPool);
  MemPools.PoolAllocate (tmpPool, tmpPtr, Chunk);
  REPEAT
    BasicIO.Read (inF, tmpPtr, Chunk, m);	(* m = bytes read *)
    INC (n, Chunk)
  UNTIL m # Chunk;
  DEC (n, Chunk);
  size := n + CARDINAL (m);
  MemPools.KillPool (tmpPool);
  BasicIO.Close (inF);
  RETURN size
END FileSize;
   
The function is tested and in my own tests, it works fine.

Page created April 15, 2012 and