[fpc-pascal] Sample unit code

2009-04-04 Thread Francisco Reyes

Could anyone share sample code for a unit?
Looking at the ref.pdf I don't really follow it.
The examples included are not meaningful to this pascal newbie. :-(

Alternative any unit package on the site that may be a good example would be 
just as great.


As I work on a small program I find parts of code which I would like to 
reuse and thought a unit may be a good way. Also in the future some of these 
functionality may be good enough to share with others so I figure it is a 
good time to learn how to make a unit.


Tried to look for the standard units to see if they could be an example, but 
only found the compiled versions. 
___

fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sample unit code

2009-04-04 Thread Michael Van Canneyt


On Sat, 4 Apr 2009, Francisco Reyes wrote:

 Could anyone share sample code for a unit?
 Looking at the ref.pdf I don't really follow it.
 The examples included are not meaningful to this pascal newbie. :-(

What is not clear ?

 
 Alternative any unit package on the site that may be a good example would be
 just as great.

Look in the 'Contributed units' section of the website (on the right, in 
section 'Coding'). 
There are lots of units there.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sample unit code

2009-04-04 Thread Francisco Reyes

Michael Van Canneyt writes:


What is not clear ?


For starters I don't really follow the diagrams in the ref document. It 
probably is some type of formal definition, but I am neither familiar with 
it, nor seem to get it. Even when I look at a simpler case like procedure 
declaration I can't follow those blocks.


I see some simpler declarations on top of the ref document so I will look at 
those and try to get more familiar with that style of documentation. 

The sample code in the ref document doesn't document the simplest case, but 
goes over what I think is a more complex example of a unit that uses another 
unit.  

It also doesn't have any actual code to do anything, so it is kind of an 
abstract example . I was looking on the other docs to see if there were any 
examples, but did not find any. 

Would be great to have something simmilar to what we have for the procedure 
declaration on page 100. Something complete, yet trivial enough that a total 
newbie can follow. 

Look in the 'Contributed units' section of the website (on the right, in section 'Coding'). 
There are lots of units there.


I started to look at those and the first one I tried was not even 
accessible. Then tried sourceforge. The second I tried didn't even have a 
readme document. :-(


I guess I could try one unit at a time until I find a simple one, but I 
figure if anyone knows of a simple unit that may be easy to read, that may 
be a better starting point.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sample unit code

2009-04-04 Thread Michael Van Canneyt


On Sat, 4 Apr 2009, Francisco Reyes wrote:

 Michael Van Canneyt writes:
 
  What is not clear ?
 
 For starters I don't really follow the diagrams in the ref document. It
 probably is some type of formal definition, but I am neither familiar with it,
 nor seem to get it. Even when I look at a simpler case like procedure
 declaration I can't follow those blocks.
 
 I see some simpler declarations on top of the ref document so I will look at
 those and try to get more familiar with that style of documentation. 
 The sample code in the ref document doesn't document the simplest case, but
 goes over what I think is a more complex example of a unit that uses another
 unit.  
 It also doesn't have any actual code to do anything, so it is kind of an
 abstract example . I was looking on the other docs to see if there were any
 examples, but did not find any. 

I'll add more examples.

 Would be great to have something simmilar to what we have for the procedure
 declaration on page 100. Something complete, yet trivial enough that a total
 newbie can follow. 
  Look in the 'Contributed units' section of the website (on the right, in
  section 'Coding'). There are lots of units there.
 
 I started to look at those and the first one I tried was not even accessible.
 Then tried sourceforge. The second I tried didn't even have a readme document.
 :-(
 
 I guess I could try one unit at a time until I find a simple one, but I figure
 if anyone knows of a simple unit that may be easy to read, that may be a
 better starting point.

All Free Pascal sources are made up of units. That should yield something ? :-)

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sample unit code

2009-04-04 Thread Francisco Reyes

Michael Van Canneyt writes:


I'll add more examples.


Thanks.
I will also go over the existing unit documentation and send you some 
thoughts and suggestions from a newbie's point of view. Often what is clear 
to an experienced developer is not so clear for a newcomer. 


All Free Pascal sources are made up of units. That should yield something ? :-)


I am going to have to download the sources from the site. Both my OpenSuse 
and FreeBSD installs only have the binaries for the units. That was the very 
first thing I tried.


Still going over the contributed units. Even though I am finding many broken 
links sooner or later I will bump into one that should help me understand 
the basic structure of a unit.   
___

fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sample unit code

2009-04-04 Thread Mattias Gaertner
On Sat, 04 Apr 2009 12:56:51 -0400
Francisco Reyes li...@stringsutils.com wrote:

 Michael Van Canneyt writes:
 
  I'll add more examples.
 
 Thanks.
 I will also go over the existing unit documentation and send you some 
 thoughts and suggestions from a newbie's point of view. Often what is
 clear to an experienced developer is not so clear for a newcomer. 

For newbies there are lots of tutorials:
http://wiki.lazarus.freepascal.org/Lazarus_Documentation

 
[...]

Mattias
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Sample unit code

2009-04-04 Thread Pete Cervasio
On Saturday 04 April 2009 11:40:03 Francisco Reyes wrote:

 I guess I could try one unit at a time until I find a simple one, but I
 figure if anyone knows of a simple unit that may be easy to read, that may
 be a better starting point.

Here's a simple unit that may help.  I have occasion to add time values in my 
programs.  I don't care that 249 seconds isn't proper time and in the form 
of 4 minutes and 9 seconds, I just want it encoded into a TDateTime value so 
I can add it to another TDateTime value.

Save this as my_unit.pp:

unit my_unit;

interface

{ Everything out here in the interface section is seen by programs
  that use this unit }

function EncodeTimeValue (hr, min, sec, msec: Integer): TDateTime;

implementation

{ Everything down here is hidden to the outside }

const
  HoursPerDay = 24;
  MinutesPerDay = 60 * HoursPerDay;
  SecondsPerDay = 60 * MinutesPerDay;
  MilliSecsPerDay = 1000 * SecondsPerDay;

function EncodeTimeValue (hr, min, sec, msec: Integer): TDateTime;
begin
  Result := hr / HoursPerDay + 
min / MinutesPerDay +
sec / SecondsPerDay +
msec / MilliSecsPerDay;
end;

end.

There you go, one complete unit.  In a main program (or even in another unit), 
all you have to do is use that unit and you can then call the function all 
you want:

program test_my_unit;

uses
  my_unit;

begin
   Writeln ('24 hours and 720 minutes is 1.5 days: ',
 EncodeTimeValue (24, 720, 0, 0));
   { Uncomment this next to see an error, because HoursPerDay is private }
   { Writeln ('Hours per day is: ', HoursPerDay); }
end.

To add a new routine, put it down in the implementation section, and copy 
the 'procedure foo(blahblah)' or 'function bar(blahblah): blah' part up to 
the interface section.

There are also 'initialization' and 'finalization' sections too, but you can 
probably ignore those until you understand units better.  

I hope this is helpful.  The code above is released to the public domain, if 
anyone cares to use it.

Best regards,
Pete C.

-- 
=
Cliches are a dime a dozen; that's why I avoid them like the plague.
=
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal