FPC uses AVL_TREE - pascal

I'm using Ubuntu 11.04 and Free Pascal Compiler version 2.4.0-2ubuntu3.1 [2011/06/17] for x86_64 Copyright (c) 1993-2009 by Florian Klaempfl
when I'm trying to compile this code:
program test;
uses AVL_Tree;
const N = 10;
type Number = ^Integer;
var all : TAVLTree;
actual :TAVLTreeNode;
a,i :Integer;
t : array[1..N] of Integer;
x :Number;
function compare(a,b:Pointer):LongInt;
begin
compare := Number(a)^ - Number(b)^;
end;
begin
randomize;
for i := 1 to N do
t[i] := i;
all := TAVLTree.Create(#compare);
for i := N downto 1 do
begin
a:=1+random(i);
new(x);
x^:=t[a];
all.add(x);
t[a]:=t[i];
end;
actual := all.findLowest;
while actual <> nil do
begin
Writeln(Number(actual.data)^);
dispose(Number(actual.data));
actual := all.FindSuccessor(actual);
end;
all.Free;
end.
it gives me:
Compiling avl.pas
Fatal: Can't find unit AVL_Tree used by test
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be compiled)
I don't know what is going on.. in my school this code worked fine.. Any ideas what should I do?

Related

Is it possible to open a Text File in Pascal several times in the same program?

I am trying to make a program that allow me to read a text file and then print it in the terminal.
I just put the simplified parts below so that you see how I think it should work.
My problem is that if for example I open the file a.txt then b.txt it works.
But when I want to open a.txt again, the program stops with an error 217. Same if I want to open another file name c.txt for example. I've spent days on this problem but I do not know where it comes from. I looked on the internet and erorr 217 seems to be related to a non-existing file ? but it is not the case for me...
The error seems to occur on the 'assign' function.
To clarify :
'key' is a Char,
'map' is a two dimension dynamic array of a Record Type.
Repeat
readln(key);
name := key +'.txt';
fileLoading(name, map, maxX, maxY);
Until key = 'l';
...
procedure fileLoading (name : String; var map : PPObjet; var maxX,maxY : Integer);
var
fichier : Text;
i, j : Integer;
chaine : String;
begin
if (FileExists(name)) then
begin
assign(fichier, name);
reset(fichier);
read(fichier,maxX);
readln(fichier,maxY);
if (maxX < 1) or (maxX > MAX) or (maxY < 1) or (maxY > MAX) then
begin
writeln('Tailles invalides');
halt();
end;
allocationTab(maxX, maxY, map);
while (not eof(fichier)) do
begin
for j := 1 to maxY do
begin
readln(fichier,chaine);
for i := 1 to maxX do
begin
case chaine[i] of
'0' : begin
map[j][i].solide := false;
map[j][i].nature := 'v';
map[j][i].valeur := chaine[i];
end;
'1' : begin
map[j][i].solide := true;
map[j][i].nature := 'm';
map[j][i].valeur := chaine[i];
end;
'2'..'9' : begin
map[j][i].solide := false;
map[j][i].nature := 's';
map[j][i].valeur := chaine[i];
end;
end;
end;
end;
end;
end
else
begin
writeln('Erreur le fichier n''existe pas');
halt();
end;
close(fichier);
end;
...
This is the first time I ask a question on stack overflow and I'm not really familiar with it, so I hope my problem is clear enough, as well as my english.
Thanks in advance for all the help you may bring.
try setting
filemode:=0;
before your assign/reset

Not overloading operator

Good day, I'm doing some Codeforces exercises in my free time, and I had a problem to test if the user was a boy or a girl, well, my problem isn't that, i have just demonstrated the code.
While compiling my code in my computer ( I'm using version 3.0.4 for i386 ) i get no error, but codeforces gives me this error
program.pas(15,16) Error: Operator is not overloaded: "freq(Char;AnsiString):LongInt;" + "ShortInt"
program.pas(46,4) Fatal: There were 1 errors compiling module, stopping
The error wasn't clear enough to me, as the same script was perfectly compiled with my version.
The platform is using ( version 3.0.2 i386-Win32 ).
program A236;
uses wincrt, sysutils;
var
username : String;
function freq(char: char; username : String): Integer;
var
i: Integer;
begin
freq:= 0;
for i:= 1 to length(username) do
if char = username[i] then
freq:= freq + 1;
//writeln(freq);
end;
function OddUserName(username : String): Boolean;
var
i, counter: Integer;
begin
OddUserName:= false; // even
counter:= 0;
for i:= 1 to length(username) do
if freq(username[i], username) <> 1 then
delete(username, i, 1)
else
counter:= counter + 1;
if counter mod 2 <> 0 then
OddUserName:= true; // odd
//writeln(counter);
//writeln(OddUserName);
end;
begin
readln(username);
if not OddUserName(username) then
writeln('CHAT WITH HER!')
else
writeln('IGNORE HIM!');
//readkey();
end.
The error is supposed to be at this line probably :
function freq(character: char; username : String): Integer;
Thanks for everyone who helps.
Inside of a function, the function's name can be used as a substitute for using an explicit local variable or Result. freq() and OddUserName() are both doing that, but only freq() is using the function name as an operand on the right-hand side of an assignment. freq := freq + 1; should be a legal statement in modern Pascal compilers, see Why i can use function name in pascal as variable name without definition?.
However, it would seem the error message is suggesting that the failing compiler is treating freq in the statement freg + 1 as a function type and not as a local variable. That would explain why it is complaining about not being able to add a ShortInt with a function type.
So, you will have to use an explicit local variable instead, (or the special Result variable, if your compiler provides that), eg:
function freq(charToFind: char; username : String): Integer;
var
i, f: Integer;
begin
f := 0;
for i := 1 to Length(username) do
if charToFind = username[i] then
f := f + 1;
//writeln(f);
freq := f;
end;
function freq(charToFind: char; username : String): Integer;
var
i: Integer;
begin
Result := 0;
for i := 1 to Length(username) do
if charToFind = username[i] then
Result := Result + 1;
//writeln(f);
end;

How to get versions of files (exe's and dll's) which are included to Inno Setup installer exe file?

As in topic, is it possible? And, I want to display them on one page of installer if parameter (e.g. parameter passed to exe file) is set to true.
I know how to display some page:
if dev then
PageWersjePlikow :=
CreateOutputMsgMemoPage(
1, 'Wersje plików zawarte w instalatorze',
'Lista plików niewidoczna dla klienta',
'Pliki:', 'TU WPISAĆ WERSJE PLIKÓW');
I have some ideas, but every idea is based on .txt file built while compiling exe installer and then read from it...
Use GetVersionNumbers or GetVersionNumbersString support functions.
The GetVersionNumbersString returns version in format Major.Minor.Rev.Build.
If you need a different format, you need to use GetVersionNumbers and format the version components, the way you need (like Major.Minor.Rev):
function MyGetVersionNumbersString(
const Filename: String; var Version: String): Boolean;
var
MS, LS: Cardinal;
Major, Minor, Rev, Build: Cardinal;
begin
Result := GetVersionNumbers(Filename, MS, LS);
if Result then
begin
Major := MS shr 16;
Minor := MS and $FFFF;
Rev := LS shr 16;
Build := LS and $FFFF;
Version := Format('%d.%d.%d', [Major, Minor, Rev]);
end
end;
Thank you! I have found solution for checking cmd parameter:
function GetParam: Boolean;
var
param: string;
i: integer;
begin
Result := False;
for i:= 0 to ParamCount do
begin
param := ParamStr(i);
if (param = '-p') then
begin
Result := True;
break;
end;
end;
end;
With my function I can just run my installer with '-p' parameter and it will show my page containing information which I want :-)

Pascal CloseFile not found

I have been making a program in Delphi and what I am trying to do is set up me game with a 'save file'. I have been doing this in Delphi and not when I bring the code home I am just using a pascal compiler and I cannot seem to run my program as I get the following errors
Free Pascal Compiler version 2.6.2-8 [2014/01/22] for x86_64
Copyright (c) 1993-2012 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling control.p
control.p(44,12) Error: Identifier not found "CloseFile"
control.p(116,14) Error: Identifier not found "closeFile"
control.p(127,13) Error: Identifier not found "assignFile"
control.p(143,4) Fatal: There were 3 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be compiled)
Sorry if this is a stupid question but I am new to files and I really want this to work. Below is all of my current code just in case you need it, sorry if its confusing its a draft and thanks for helping.
program Task3;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
Type
gameRec = record
name: string[30];
skill: integer;
str: integer;
modif: integer;
sskill: string[3];
sstr: string[3];
smodif: string[3];
sf: integer;
ssf : string[1];
end;
var
gameFile : file of gameRec;
p1, p2 : gameRec;
procedure showStats;
begin
FileMode := fmOpenRead;
Reset(gameFile);
read(gameFile, p1);
read(gameFile, p2);
writeln;
writeln(p1.name, '''s stats');
writeln('Skill: ', p1.skill);
writeln('Strenght: ', p1.str);
writeln('Modifier: ', p1.modif);
writeln;
writeln(p2.name, '''s stats');
writeln('Skill: ', p2.skill);
writeln('Strenght: ', p2.str);
writeln('Modifier: ', p2.modif);
writeln;
CloseFile(gameFile);
end;
procedure resetsf;
var
ran12, ran4, namelen: integer;
namepass: boolean;
name: string;
begin
writeln('No save file detected, generating new stats');
namelen := 0;
namepass := false;
repeat
write('What is player 1''s name: ');
readln(name);
namelen := length(name);
if (namelen > 2) and (namelen < 30) then
begin
p1.name := name;
namepass := true;
end
else
writeln('You name must be between 3 and 30 characters');
until namepass = true;
namepass := false;
repeat
write('What is player 2''s name: ');
readln(name);
namelen := length(name);
if (namelen > 2) and (namelen < 30) then
begin
p2.name := name;
namepass := true;
end
else
writeln('You name must be between 3 and 30 characters');
until namepass = true;
ran12 := random(12) + 1;
ran4 := random(4) + 1;
p1.skill := 10 + (ran12 div ran4);
ran12 := random(12) + 1;
ran4 := random(4) + 1;
p1.str := 10 + (ran12 div ran4);
ran12 := random(12) + 1;
ran4 := random(4) + 1;
p2.skill := 10 + (ran12 div ran4);
ran12 := random(12) + 1;
ran4 := random(4) + 1;
p2.str := 10 + (ran12 div ran4);
reWrite(gameFile);
p1.sskill := inttostr(p1.skill); //debug
p1.sstr := inttostr(p1.str);
p1.smodif := inttostr(p1.modif);
//write(gameFile,p1);
p2.sskill := inttostr(p2.skill);
p2.sstr := inttostr(p2.str);
p2.smodif := inttostr(p2.modif); //debug
write(gameFile,p2);
p1.sf := 1;
p1.ssf := inttostr(p1.sf);
write(gameFile,p1); //debug
closeFile(gameFile);
FileMode := fmOpenRead;
Reset(gameFile);
read(gameFile, p1);
read(gameFile, p2);
end;
begin
assignFile(gameFile, 'N:\gamerec.dat');
randomize;
writeln('Game :)');
writeln('By Sam Collins');
writeln;
FileMode := fmOpenRead;
Reset(gameFile);
read(gameFile, p1);
writeln(p1.sf);
if p1.sf = 0 then
resetsf
else
writeln('Save file detected using old stats');
showStats;
readln;
end.
If you want delphi compatibility, put the compiler in Delphi mode, either by compiling with -Sd or adding {$mode Delphi} to the source (somewhere at the top, e.g. near the $apptype).
Then closefile() and assignfile() will be accepted. The default dialect is turbo pascal. Lazarus puts FPC in objfpc (which is also delphi alike) by default.
Closefile is in an unit (objpas) with system unit enhancements that is only in scope in Delphi or objfpc modi.
Using namespaces (SYSTEM.sysutils instead of sysutils) might be dangerous too. Better simplify to sysutils. Namespaces is an Delphi extension that only got significant use with Delphi XE2.
I tested, and removing the {$R *.res}, the removal of system. before sysutils and -Sd makes the code compile
If I understood the question correctly, you want to port a piece of code from Delphi to Free Pascal and you have problems with file operations.
In Free (and Turbo) Pascal, file handling is much more easier than in Delphi: in Pascal we have Assign instead of AssignFile and Close instead of CloseFile . The syntax for these two procedures can be found in the help system.

How to convert a string version value to a numerical value in Inno Setup Scripts?

I want to develop a setup package for conditionally upgrading an existing package. I want to check the existing software version against to-be-installed version. In order to do that, I have to compare the version strings.
How can I convert the string value to a numerical value in a Inno setup script?
RegQueryStringValue(HKEY_LOCAL_MACHINE, 'Software\Blah blah', 'Version', version)
version = 'V1.R2.12';
numVersion := ??string_to_numerical_value??(version);
This is a little more tricky, as you would want to handle versions like 'V1.R2.12' and 'V0.R15.42' correctly - with the simple conversion in the other answer you would get 1212 and 1542, which would not compare the way you would expect.
You need to decide how big each part of the version number can be, and multiply the parts by that value to get a correct end number. Something like this:
[Code]
function string_to_numerical_value(AString: string; AMaxVersion: LongWord): LongWord;
var
InsidePart: boolean;
NewPart: LongWord;
CharIndex: integer;
c: char;
begin
Result := 0;
InsidePart := FALSE;
// this assumes decimal version numbers !!!
for CharIndex := 1 to Length(AString) do begin
c := AString[CharIndex];
if (c >= '0') and (c <= '9') then begin
// new digit found
if not InsidePart then begin
Result := Result * AMaxVersion + NewPart;
NewPart := 0;
InsidePart := TRUE;
end;
NewPart := NewPart * 10 + Ord(c) - Ord('0');
end else
InsidePart := FALSE;
end;
// if last char was a digit the last part hasn't been added yet
if InsidePart then
Result := Result * AMaxVersion + NewPart;
end;
You can test this with the following code:
function InitializeSetup(): Boolean;
begin
if string_to_numerical_value('V1.R2.12', 1) < string_to_numerical_value('V0.R15.42', 1) then
MsgBox('Version ''V1.R2.12'' is not as recent as version ''V0.R15.42'' (false)', mbConfirmation, MB_OK);
if string_to_numerical_value('V1.R2.12', 100) > string_to_numerical_value('V0.R15.42', 100) then
MsgBox('Version ''V1.R2.12'' is more recent than version ''V0.R15.42'' (true)', mbConfirmation, MB_OK);
Result := FALSE;
end;
Whether you pass 10, 100 or 1000 for AMaxVersion depends on the number and range of your version number parts. Note that you must not overflow the LongWord result variable, which has a maximum value of 2^32 - 1.
I haven't tried that (and my Pascal knowledge is a bit rusty), but something like the following should work:
function NumericVersion(s: String): Integer;
var
i: Integer;
s1: String;
begin
s1 := '';
for i := 0 to Length(s)-1 do
if (s[i] >= '0') and (s[i] <= '9') then
s1 := s1 + s[i];
Result := StrToIntDef(s1, 0);
end;
Please not that you'll have to play with the start and end value for i as I'm not sure whether it is zero-based or not (s[0] may contain the length of the string if it is a "Pascal String").
I've implemented two version strings (actually one string and one dword value) in the registry to overcome complexity.
displayversion="v1.r1.0"
version="10100" (=1*10^4 + 1*10^2 + 0*10^0)
That's simple. Though not an answer to this question, however one might think the other way around when faced with complexity, which could be avoided in a simpler way.

Resources