Board index » delphi » Length of a declared pChar Constant..

Length of a declared pChar Constant..

In a previous article, Ed Derzawiec <ederz...@FMIS02.NSC.COM> wrote:

Quote
>Does anyone know of a simple way to declare a null terminated string
>constant which exceeds the 256 char line limit of Turbo Pascal/Windows?

>ie, I want to define messages in in my code which are greater than 256
>characters.  However, using the code below is truncates the
>string.  My normal work around is to read the messages in from an
>external file at run time, or to concatinate short messages into longer
>ones.  Isn't there an easier way?

>const
>  Message1:pChar='This message is'+
>                 'Truncated to 256 characters..';
> or
>  Message2:Array[0..1024]='This one also gets truncated';

>-Ed

I have a program with 38 KB of PChars in an .obj file that I link in
at run-time.  There are about 250 PChars in the file, with the shortest
being about 100 chars and the longest being about 1700.  

What I do is:
1.)  Type the PChars into a plain text file in the format:

FirstConstantName
Actual text for the PChar.

SecondConstantName
Text for the Second PChar.

2.) Process the text file with a little program that produces two
    new files:
    - A binary file with the PChars laid end to end.

    - a text file listing ...
       const
          FirstConstantName = 0;
          SecondConstantName = L1 + 1;
          ThirdConstantName  = L1 + L2 + 2;
          N_th__Name         = L1 + L2 + .... + L(N-1) + (N-1);  

      ie.,  The value of each constant is the location within
            the binary file of the first byte of each PChar.

3.) Use {$I ConstantFileName} to include the file of constants into
    a little unit ...

4.) Process the binary file with BinObj and use {$L ObjFileName} to
    link it into the unit.

5.) Put one little procedure into the unit to return a PChar when
    you pass it one of the constant names.

Unit uPChar;

uses Strings;

Interface

{$I PChars.Con}

function GetPChar(PCharName: Word): PChar;

Implementation

procedure PCharBlock; external; {$L PChars.Obj}

function GetPChar(PCharName: Word): PChar;
var P: Pointer;
begin
  LongInt(P) := LongInt(@PCharBlock) + PCharName;
  GetPChar := StrNew(P);  
end;

end.

That is the simple version of it.  In practice, my PChars have special
formatting chars in them (for line breaks, tabs, indents, colour changes,
centering, etc.) and I have a descendant of TurboVisions TStaticText
that I use to display the PChars.

Originally I just put the PChars into a Tcollection, wrote the collection
to a stream, linked the stream into the a unit used by the main program ....  
but eventually the program got so large and I needed ways to reduce size
so I went with the technique I outlined above.

 

Re:Length of a declared pChar Constant..


Does anyone know of a simple way to declare a null terminated string
constant which exceeds the 256 char line limit of Turbo Pascal/Windows?

ie, I want to define messages in in my code which are greater than 256
characters.  However, using the code below is truncates the
string.  My normal work around is to read the messages in from an
external file at run time, or to concatinate short messages into longer
ones.  Isn't there an easier way?

const
  Message1:pChar='This message is'+
                 'Truncated to 256 characters..';
 or
  Message2:Array[0..1024]='This one also gets truncated';

-Ed

Other Threads