Recent

Recent Posts

Pages: [1] 2 3 ... 10
1
General / compiler error in unit
« Last post by paule32 on Today at 01:36:37 pm »
Hello,
during compile, I stuck on line: 57 (see attached image).
I get the compiler error:
FPC_WinTypes.pas(57) Fatal: Syntax error, "UNIT" expected but "end of file" found

And this is the content:

Code: Pascal  [Select][+][-]
  1. // ---------------------------------------------------------------------------
  2. // File:   FPC_WinTypes.pas
  3. // Author: (c) 2024 Jens Kallup - paule32
  4. // All rights reserved
  5. //
  6. // only for education, and non-profit usage !
  7. // ---------------------------------------------------------------------------
  8. unit FPC_WinTypes;
  9. interface
  10.  
  11. uses fpc_types;
  12. // ---------------------------------------------------------------------------
  13. // win32api constants, and variables ...
  14. // ---------------------------------------------------------------------------
  15. type BOOL      = LongBool;       // true or false
  16.  
  17. type PVOID     = Pointer;
  18. type LPVOID    = ^PVOID;
  19. type LPCVOID   = ^LPVOID;
  20.  
  21. type HANDLE    = PVOID;
  22. type FARPROC   = PVOID;
  23.  
  24. type THANDLE   =    DWORD;      // onject handle
  25. type PHandle   = ^THANDLE;
  26.  
  27. type LCID      = DWord;         // a local identifier
  28. type LANGID    = DWord;         // a language identifier
  29.  
  30. type WPARAM    = DWord;         // 32-bit message parameter
  31. type LPARAM    = DWord;         // 32-bit message parameter
  32.  
  33. type LRESULT   = DWord;         // 32-bit unsigned return value
  34. type HRESULT   = DWord;         // 32-bit signed   return value
  35.  
  36. type HINSTANCE = HANDLE;        // a handle to an instance
  37. type HLOCAL    = HANDLE;        // a handle to a local memory block
  38. type HMODULE   = HINSTANCE;     // a handle to a module (.dll)
  39.  
  40. type HWND      = DWord;         // a handle to a window
  41. type ATOM      = DWord;         // local/global atom index for a string
  42.  
  43. type HGLOBAL   = THandle;       // a globally memory allocated handle
  44.  
  45. type PAnsiString = ^AnsiString;
  46. type LPCSTR      = PAnsiString;
  47. type LPCWSTR     = PAnsiString;
  48.  
  49. //type FARPROC   = Pointer;
  50.  
  51. type LPCTSTR   = LPCSTR;
  52.  
  53. type LONG_PTR  = DWORD;
  54. type PLONC_PTR = ^LONG_PTR;
  55.  
  56. type
  57.     PPoint = ^TPoint;
  58.     TPoint = record
  59.         x: DWORD;
  60.         y: DWORD;
  61.     end;
  62. type
  63.     PMessage = ^TMessage;
  64.     TMessage = record
  65.         hwnd     : HWND;
  66.         message  : DWORD;
  67.         wParam   : WPARAM;
  68.         lParam   : LPARAM;
  69.         time     : DWORD;
  70.         pt       : TPOINT;
  71.         lPrivate : DWORD;
  72.     end;
  73. // ---------------------------------------------------------------------------
  74. // security structures ...
  75. // ---------------------------------------------------------------------------
  76. type
  77.     POverlapped = ^TOverlapped;
  78.     _OVERLAPPED = record
  79.         Internal      : ^DWORD ;
  80.         InternalHigh  : ^DWORD ;
  81.         Offset        :  DWORD ;
  82.         OffsetHigh    :  DWORD ;
  83.         hEvent        : THandle;
  84.     end;
  85.     TOverlapped = _OVERLAPPED;
  86.  
  87.     PSECURITY_ATTRIBUTES  = ^TSECURITYATTRIBUTES;
  88.     LPSECURITY_ATTRIBUTES = ^TSECURITYATTRIBUTES;
  89.     PSecurityAttributes   = ^TSecurityAttributes;
  90.     TSECURITYATTRIBUTES  = record
  91.         nLength              : DWORD  ;
  92.         lpSecurityDescriptor : Pointer;
  93.         bInheritHandle       : BOOL   ;
  94.     end;
  95.  
  96. implementation
  97.  
  98. end.

So, what is going on ?
2
Although I would like smaller files for testing. My drive only has 15 gb free space, so I cannot run the billion line generator

Did you see that the generator has a command line switch "-n" to set the amount of generated lines? This way you can create files for testing of any size.
3
General / Re: Multithreading - synchronize or not?
« Last post by Martin_fr on Today at 01:08:18 pm »
https://en.wikipedia.org/wiki/Memory_barrier

So between modifying the data in "aEven" and setting "EvenFlag" for the other thread you need a barrier. (write barrier).

After checking the flag, and before reading the data, you need a readbarrier.



Assuming ONLY ONE thread each:

Code: Pascal  [Select][+][-]
  1.           if FlagEven = 0 then  // this can be a dirty read, no problem
  2.           begin
  3. //               FlagEven:=1; // not needed
  4. ReadWriteBarrier; // Don't read the below data before FlagEven really is 0
  5.        // Don't write it either....
  6.                for k:=0 to cBigSize-1 do aEven[k]:=EvenVal;
  7. // If I am correct then the other thread won't access them due to FlagEven => so "inc" is ok, no InterlockedIncrement needed)
  8.                inc(EvenVal);
  9.                inc(CountOutData);
  10. WriteBarrier; // Don't write the below, before all the above has been written
  11.                FlagEven:=2; //-- send data
  12.           end;
If you only write to aEven, so you may only need a WriteBarrier on entry into the "if" block. You can check for each block if you read, write, or read/write shared data ... Though the "inc" is a read/write...



You still need InterlockedIncrement for the Counter that is not protected by any flags.
4
General / Re: Multithreading - synchronize or not?
« Last post by Martin_fr on Today at 01:06:32 pm »
"Counter" is member of thread class. It is local to thread, there is no conflict.

aEven, aOdd is global array, that one thread writes and other reads. Flag indicates when to read when to write.

So 2 threads total only?

Then why set               
Code: Pascal  [Select][+][-]
  1. FlagEven:=1;
There is no thread checking for that. The thread that wanted it 0 is in the "if" block, the other one will wait till it is 2. So you could leave it 0?


Quote
Interlocked, does include the needed barriers.

"Interlocked" you mean:
lock cmpxchg [mem], reg
or
lock xadd [mem], reg ?

Both of them include (afaik) all the read/write barriers that you need. Any InterLocked... (or asm lock ...) should do that.
5
General / Re: Multithreading - synchronize or not?
« Last post by mika on Today at 12:55:41 pm »
"Counter" is member of thread class. It is local to thread, there is no conflict.

aEven, aOdd is global array, that one thread writes and other reads. Flag indicates when to read when to write.

If there is ONLY ONE thread of each then... Well, afaik technically you need Read/Write barriers in some cases. But it may work without. Though, I would put a "WriteBarrier;" in front of the assignments setting it to 2 or 0 (before the "end" of the "if"'s begin/end block). 
yes it is  "ONLY ONE thread of each" and ONLY ONE at a time.

Interlocked, does include the needed barriers.

"Interlocked" you mean:
lock cmpxchg [mem], reg
or
lock xadd [mem], reg ?

6
Unix / Re: A fairly simple sound solution? [SOLVED]
« Last post by VisualLab on Today at 12:48:08 pm »
Hi!   8-)

Is there any fairly simple sound solution, one can use at a (rather simple) Pascal program?

Beep and Sound didn't work for me and I found other suggestions, but they're way too old (and irrelevant), for today's Linux standards, or much complicated (eg. using sophisticated C routines etc).

Please keep in mind, that Pascal (amongst many other things) is also an educational tool and I'm in need of a rather simple solution (like the above mentioned-but no working commands Beep and Sound) and not for excellent but sophisticated C or Java like code, which are all good and well, but I'll have a really hard time, trying to demonstrate them to novice learners.

This is an operating system modeled on the old OS designed for mainframe machines. It is constantly being developed mainly to support networks, servers, etc. solutions. And it works well there. Unfortunately, Linux is not suitable for teaching or presenting multimedia solutions (audio, video, animations, fancy graphics, etc.). Yes, there are attempts to use Linux for specific multimedia applications (e.g. video editing -> DaVinci), but they are not suitable for programming education.

Please don't take this as an insult or a malicious comment. This is simply the architecture of Linux and this is how its programmers want to develop it (like many other open source projects). And users can use what is available or they can look for a different solution.

Besides, Windows multimedia solutions are not that simple either (although much better designed and operated). Unfortunately, multimedia support requires knowledge of API and/or external libraries. There are no shortcuts here.
7
General / Re: Multithreading - synchronize or not?
« Last post by Martin_fr on Today at 12:41:07 pm »
Quote
Code: Pascal  [Select][+][-]
  1. FlagEven
That should work...

Well, depends: Is there exactly ONE even thread, and exactly ONE odd thread? => Or are there several of at least one of them.

If there are several, then it is a problem.
If there are several then you need (iirc)
Code: Pascal  [Select][+][-]
  1. InterlockedCompareExchange(FlagEven, 1, 0)
Set it to 1, but only if it really still is 0. (Because, otherwise 2 threads can set it to 1.)
And use the result of that for the "if" condition

If there is ONLY ONE thread of each then... Well, afaik technically you need Read/Write barriers in some cases. But it may work without. Though, I would put a "WriteBarrier;" in front of the assignments setting it to 2 or 0 (before the "end" of the "if"'s begin/end block). 

Interlocked, does include the needed barriers.



--- EDIT:

Well actually, if you have ONLY ONE of each thread. You may need ReadWriteBarrier after entering each if block.
8
Graphics / Re: May be useful to somebody
« Last post by KodeZwerg on Today at 12:38:22 pm »
Good one Handoko, I was not aware about yours and started my own, basics are same I think.
My thingy aint perfect nor ready, just some basics prepared to make it better.
Button1 initiate the action on Panel2.
2 checkboxes and a TEdit is used.
As of right now my way does write only to the width, height/wordwrap is not teached yet but you can call method seperate with proper coordinate.

TODO:
better color managment, currently it is fixed to black background and white text.
boundschecking if written text is inside rect, wordwrap etc

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
  9.   Spin;
  10.  
  11. type
  12.   TTextStyle = (tsNone, tsSuper, tsSub);
  13.   TOneLine = packed record
  14.     X, Y: Integer;
  15.     TextStyle: TTextStyle;
  16.     Line: string;
  17.   end;
  18.   TAllLines = array of TOneLine;
  19.  
  20. type
  21.  
  22.   { TForm1 }
  23.  
  24.   TForm1 = class(TForm)
  25.     Button1: TButton;
  26.     cbSub: TCheckBox;
  27.     cbSuper: TCheckBox;
  28.     Edit1: TEdit;
  29.     Label1: TLabel;
  30.     Panel1: TPanel;
  31.     Panel2: TPanel;
  32.     SpinEdit1: TSpinEdit;
  33.     procedure Button1Click(Sender: TObject);
  34.     procedure cbSuperChange(Sender: TObject);
  35.     procedure cbSubChange(Sender: TObject);
  36.     procedure FormCreate(Sender: TObject);
  37.   strict private
  38.     FBMP: TBitmap;
  39.     Lines: TAllLines;
  40.   strict private
  41.     procedure AddLine(const ABitmap: TBitmap; const AFont: TFont;
  42.       const AText: string; const AStyle: TTextStyle = tsNone;
  43.       const X: Integer = -1; const Y: Integer = -1);
  44.   private
  45.  
  46.   public
  47.  
  48.   end;
  49.  
  50. var
  51.   Form1: TForm1;
  52.  
  53. implementation
  54.  
  55. {$R *.lfm}
  56.  
  57. { TForm1 }
  58.  
  59. procedure TForm1.FormCreate(Sender: TObject);
  60. begin
  61.   FBMP := TBitmap.Create;
  62.   FBMP.SetSize(Panel2.ClientRect.Width, Panel2.ClientRect.Height);
  63. end;
  64.  
  65. procedure TForm1.AddLine(const ABitmap: TBitmap; const AFont: TFont;
  66.   const AText: string; const AStyle: TTextStyle = tsNone;
  67.   const X: Integer = -1; const Y: Integer = -1);
  68. var
  69.   LX, LY: Integer;
  70.   i: Integer;
  71.   FontSize: Integer;
  72. begin
  73.   // sanity check
  74.   if AText = '' then
  75.     Exit;
  76.   ABitmap.Canvas.Brush.Style := bsClear;
  77.   ABitmap.Canvas.Font := AFont;
  78.   ABitmap.Canvas.Font.Color := clWhite;
  79.   // precheck X/Y
  80.   if (X = -1) and (Length(Lines) = 0) then
  81.     LX := 0;
  82.   if (Y = -1) and (Length(Lines) = 0) then
  83.     LY := 0;
  84.   if (X = -1) and (Length(Lines) > 0) then
  85.     LX := Lines[High(Lines)].X + ABitmap.Canvas.TextWidth(Lines[High(Lines)].Line);
  86.   if (Y = -1) and (Length(Lines) > 0) then
  87.     LY := Lines[High(Lines)].Y;
  88.   i := Length(Lines);
  89.   SetLength(Lines, Succ(i));
  90.   Lines[i].Line := AText;
  91.   Lines[i].X := LX;
  92.   Lines[i].Y := LY;
  93.   Lines[i].TextStyle := AStyle;
  94.   case AStyle of
  95.     tsNone:  ABitmap.Canvas.TextOut(Lines[i].X, Lines[i].Y, Lines[i].Line);
  96.     tsSuper: begin
  97.                if ABitmap.Canvas.Font.Size = 0 then
  98.                  FontSize := (9 * 2) div 3
  99.                else
  100.                  FontSize := (ABitmap.Canvas.Font.Size * 2) div 3
  101.                ABitmap.Canvas.Font.Size := FontSize;
  102.                ABitmap.Canvas.TextOut(Lines[i].X, Lines[i].Y - FontSize, Lines[i].Line);
  103.              end;
  104.     tsSub:   begin
  105.                if ABitmap.Canvas.Font.Size = 0 then
  106.                  FontSize := (9 * 2) div 3
  107.                else
  108.                  FontSize := (ABitmap.Canvas.Font.Size * 2) div 3
  109.                ABitmap.Canvas.Font.Size := FontSize;
  110.                ABitmap.Canvas.TextOut(Lines[i].X, Lines[i].Y + FontSize, Lines[i].Line);
  111.              end;
  112.   end;
  113. end;
  114.  
  115. procedure TForm1.Button1Click(Sender: TObject);
  116. begin
  117.   if ((not cbSuper.Checked) and (not cbSub.Checked)) then
  118.     AddLine(FBMP, Self.Font, Edit1.Text);
  119.   if (cbSuper.Checked and (not cbSub.Checked)) then
  120.     AddLine(FBMP, Self.Font, Edit1.Text, tsSuper);
  121.   if ((not cbSuper.Checked) and cbSub.Checked) then
  122.     AddLine(FBMP, Self.Font, Edit1.Text, tsSub);
  123.   Panel2.Canvas.Draw(0, 0, FBMP);
  124. end;
  125.  
  126. procedure TForm1.cbSuperChange(Sender: TObject);
  127. begin
  128.   if cbSuper.Checked then
  129.     cbSub.Checked := False;
  130. end;
  131.  
  132. procedure TForm1.cbSubChange(Sender: TObject);
  133. begin
  134.   if cbSub.Checked then
  135.     cbSuper.Checked := False;
  136. end;
  137.  
  138. end.
9
General / Re: Multithreading - synchronize or not?
« Last post by Martin_fr on Today at 12:31:38 pm »
Well, only looking at the extract in the post...

The global counters have a risk of going wrong. (race condition).

You should at least use
Code: Pascal  [Select][+][-]
  1. InterlockedIncrement(Counter)
And similar for other counters.


I don't know if "aEven" is shared with something?
10
Beginners / Re: Can function be used for change event
« Last post by VisualLab on Today at 12:18:23 pm »
Has anyone ever tried using a function with boolean result for a change event?
Currently I use procedure change_event (sender:tobject);

Sometimes change events get activated when no change actually occurred such as when a tedit loses focus.

Specific events are used to handle such situations. In case of loss of focus, handling the OnExit event helps. There is no need to come up with an unusual solution.
Pages: [1] 2 3 ... 10

TinyPortal © 2005-2018