Forum > General

Multiline string constants

(1/3) > >>

User137:
Would it be possible to do thing like this STRINGIFY() in pascal?

--- Code: ---#define STRINGIFY(A) #A

const GLchar* source = STRINGIFY(

                                     uniform sampler2D tex0;
                                     uniform sampler2D tex1;

                                     void main() {
                                         vec4 s1 = texture2D(tex0, gl_TexCoord[0].st);
                                         vec4 s2 = texture2D(tex1, gl_TexCoord[0].st + vec2(0.0625, 0.0625));
                                         gl_FragColor = mix(vec4(1, s1.g, s1.b, 0.5), vec4(s2.r, s2.g, 1, 0.5), 0.5);
                                     }

                                     );
--- End code ---

There was some Delphi related discussion there:
http://stackoverflow.com/questions/1872503/how-to-assign-a-multiline-string-value-without-quoting-each-line
But it didn't seem like there's answer.

Basically it would simplify text/code insertion within code, and should do like:

--- Code: ---s := DoSomeStringMagics(
  line1
  line2
);
--- End code ---
convert into:

--- Code: ---s := '  line1#13#10  line2';
--- End code ---
with spaces and line changes included.

edit: yay, 1000th post  :D

eny:
When it's a one off I always use something like notepad++ to quote the lines by use of regular expressions.
And if I need multi line texts that change frequently I either use an external file that is read at runtime, or add it as a resource at compilation time.
You could also use a data module and put TMemo's on it I guess.


--- Quote from: User137 on April 25, 2013, 10:35:16 pm ---edit: yay, 1000th post  :D

--- End quote ---
996 :)

Martin_fr:

--- Quote from: eny on April 26, 2013, 12:07:07 am ---When it's a one off I always use something like notepad++ to quote the lines by use of regular expressions.

--- End quote ---

The Lazarus source editor has reg-ex too. So you can do that in Lazarus.

Even better, if you use 1.1/trunk: You can write (and store) pascal script macros, to quote and unquote the text.

And in 1.1, you can configure that pressing return inside a string, adds closing ' on the line (optional "+LineEnding", and opening ' on the next.

User137:
I was hoping there was compiler directives that would allow this, but i guess not. I also remembered that my code should be compilable with Delphi aswell, so new FPC features wouldn't do. This is coming for game engine, so i do not want to demand these texts be required with executable. Currently they look like this:
https://code.google.com/p/nxpascal/source/browse/trunk/src/nxShaders.pas
Thankgod GLSL doesn't need line changes after ; because that would add another +#13 after each line. It's just not easy to move the shader code to separate file and back, when needed. When you want to for example show the shader to somebody else, and stripping all quotes and + marks away... Especially with 3D shaders (which in my case are still very simplistic), it can get very complicated.

irfanbagus:
for same problem i make my own tool to convert my glsl file to *.inc file with string constants. make me easy to edit glsl and make my pascal source cleaner. only need to call from batch file/shell script.


--- Code: ---program txt2const;

{$mode objfpc}{$H+}

uses
  Classes, sysutils;

var
  Src,Dst: TStringList;
  I: Integer;
begin
  if Paramcount<3 then
  begin
    WriteLn;
    WriteLn(ExtractFileName(ParamStr(0))+' <source> <dest> <const name>');
    Exit;
  end;
  try
    Src:= TStringList.Create;
    Dst:= TStringList.Create;
    Src.LoadFromFile(ParamStr(1));
    Dst.Add(ParamStr(3)+': String = ');
    for I:=0 to Src.Count-2 do
      Dst.Add('  ' + QuotedStr(Src[I]) + '#13#10+');
    if Src.Count>1 then
      Dst.Add('  ' + QuotedStr(Src[Src.Count-1]) + ';');
    Dst.SaveToFile(ParamStr(2));
  finally
    Src.Free;
    Dst.Free;
  end;
end.

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version