1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| program CommentFormatter5;
(* Pascal Comment Formatter *)
{$APPTYPE CONSOLE}
{$MODE DELPHI}
{$MACRO ON}
uses
SysUtils,
Windows,
Strings,
LazUTF8 in 'F:\Lazarus\components\lazutils\lazutf8.pas';
{$DEFINE SPACE:=#32}
{$DEFINE EOL:=#13#10}
{$DEFINE CMT_OPEN:='(*'}
{$DEFINE CMT_CLOSE:='*)'}
{-$DEFINE CMT_FILL:='*'}
{-$DEFINE CMT_OPEN:='//'}
{-$DEFINE CMT_CLOSE:='//'}
{-$DEFINE CMT_FILL:='/'}
{-$DEFINE CMT_OPEN:=#123}
{-$DEFINE CMT_CLOSE:=#125}
{$DEFINE CMT_FILL:='-'}
const
L = 80;
var
cmt: string;
MemAddress: Pointer;
MemHandle: tHandle;
ajust: integer;
procedure Add(s: string);
var
i: integer;
begin
if s = '' then
begin
ajust := Length(CMT_OPEN + CMT_CLOSE);
cmt := Concat(
cmt,
CMT_OPEN,
StringOfChar(CMT_FILL, L - ajust),
CMT_CLOSE,
EOL
);
end else
begin
ajust := Length(CMT_OPEN + SPACE + SPACE + CMT_CLOSE);
while UTF8Length(s) > L - ajust do
begin
i := L - ajust;
while (i > 1) and (s[i] <> SPACE) do
Dec(i);
if i = 1 then
begin
i := L - ajust;
cmt := Concat(
cmt,
CMT_OPEN,
SPACE,
UTF8Copy(s, 1, i),
SPACE,
CMT_CLOSE,
EOL
);
Delete(s, 1, i);
end else
begin
cmt := Concat(
cmt,
CMT_OPEN,
SPACE,
UTF8Copy(s, 1, i - 1),
StringOfChar(SPACE, L - ajust - i + 1),
SPACE,
CMT_CLOSE,
EOL
);
Delete(s, 1, i);
end;
end;
cmt := Concat(
cmt,
CMT_OPEN,
SPACE,
s,
StringOfChar(SPACE, L - ajust - UTF8Length(s)),
SPACE,
CMT_CLOSE,
EOL
);
end;
end;
(*
Environment variables:
http://www.freepascal.org/docs-html/prog/progsu41.html
Macros:
http://www.freepascal.org/docs-html/prog/progse5.html
*)
begin
cmt := '';
Add('');
Add('Program: Pascal Comment Formatter');
Add('File: ' + {$I %FILE%});
Add('Purpose: Helps for writing formatted Pascal comment.');
Add('Last modification: ' + DateTimeToStr(Now));
Add('Compilation: ' + {$I %DATE%} + ' ' + {$I %TIME%});
Add('Compiler: Free Pascal ' + {$I %FPCVERSION%});
Add('Compiler: Free Pascal ' + IntToStr(FPC_VERSION));
Add('Compiler: Free Pascal ' + IntToStr(FPC_FULLVERSION));
Add('OS: ' + SysUtils.GetEnvironmentVariable('OS'));
Add('OS: ' + {$I %OS%});
Add('FPC Target OS: ' + {$I %FPCTARGETOS%});
Add('LongStringWithoutSpaceCharacterLongStringWithoutSpaceCharacterLongStringWithoutSpaceCharacter');
Add('Long String With Space Characters Long String With Space Characters Long String With Space Characters');
Add('LongueCha¯neAccentuÚeSansEspaceLongueCha¯neAccentuÚeSansEspaceLongueCha¯neAccentuÚeSansEspace');
Add('Longue Cha¯ne AccentuÚe Avec Espace Longue Cha¯ne AccentuÚe Avec Espace Longue Cha¯ne AccentuÚe Avec Espace');
Add('CaractÞres accentuÚs');
Add('');
(* Opening the clipboard *)
if OpenClipboard(0)
then
begin
(* Flushing the clipboard *)
if EmptyClipboard
then
begin
(* Copying text to a global memory block *)
MemHandle := GlobalAlloc(0,Length(cmt));
if MemHandle <> 0
then
begin
MemAddress := GlobalLock(MemHandle);
if MemAddress <> Nil
then
begin
StrCopy(pChar(MemAddress),pChar(cmt));
GlobalUnlock(MemHandle);
(* Storing the memory block into the clipboard *)
if SetClipboardData(CF_TEXT,MemHandle) = 0
then
WriteLn('Error: Couldn''t store output to clipboard');
end
else
WriteLn('Error: Couldn''t copy output to memory block');
end
else
WriteLn('Error: Couldn''t allocate memory for output');
end
else
WriteLn('Error: Couldn''t empty clipboard');
(* Don't forget to release the clipboard ! *)
CloseClipboard;
end
else
WriteLn('Error: Couldn''t write output to clipboard');
end. |
Partager