[fpc-pascal] Pre-initialising a TStringList
Given one or more lines of text which are known at compilation time, and without the requirement to internationalize (these are, by RFC, US ASCII), what is the best way to get them into a TStringList? -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Pre-initialising a TStringList
On 06/2/11 1:55, Mark Morgan Lloyd wrote: Given one or more lines of text which are known at compilation time, and without the requirement to internationalize (these are, by RFC, US ASCII), what is the best way to get them into a TStringList? Perhaps there are better ways than the straightforward way? const StringsToUse: string = 'Line 1'+LineEnding+ 'Line 2'+LineEnding+ // + ... 'Line n'; var FPreInitialisedSList: TStringList; begin FPreInitialisedSList := TStringList.Create; FPreInitialisedSList.SetText(PChar(StringsToUse)); // ... FPreInitialisedSList.Free; H ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Pre-initialising a TStringList
On 06.02.2011 15:19, Howard Page-Clark wrote: On 06/2/11 1:55, Mark Morgan Lloyd wrote: Given one or more lines of text which are known at compilation time, and without the requirement to internationalize (these are, by RFC, US ASCII), what is the best way to get them into a TStringList? Perhaps there are better ways than the straightforward way? const StringsToUse: string = 'Line 1'+LineEnding+ 'Line 2'+LineEnding+ // + ... 'Line n'; var FPreInitialisedSList: TStringList; begin FPreInitialisedSList := TStringList.Create; FPreInitialisedSList.SetText(PChar(StringsToUse)); // ... FPreInitialisedSList.Free; Why are you using "PChar(StringsToUse)"? The following should be sufficient as well: FPreInitialisedSList.Text := StringsToUse; Note: You can also define StringsToUse as a typeless constant, e.g.: const StringsToUse = 'Line 1' + LineEnding + 'Line 2' + LineEnding + // + ... 'Line n'; Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Pre-initialising a TStringList
Sven Barth kirjoitti sunnuntai 06 helmikuu 2011 16:24:49: > FPreInitialisedSList.Text := StringsToUse; > > Note: You can also define StringsToUse as a typeless constant, e.g.: > > const StringsToUse = 'Line 1' + LineEnding + > 'Line 2' + LineEnding + > // + ... > 'Line n'; The most obvious solution is the best this time. List.Add('Line 1'); List.Add('Line 2'); ... List.Add('Line n'); Setting the Text property (or calling the SetText method) parses the text and splits it by NewLine chars. It makes no sense because you have concatenated the strings with NewLines. Method Add does not need to parse anything. None of these solutions is "preinitialised", compile time solution. You can only initialize a const static arrays at compile time. Juha ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Pre-initialising a TStringList
Juha Manninen wrote: Sven Barth kirjoitti sunnuntai 06 helmikuu 2011 16:24:49: FPreInitialisedSList.Text := StringsToUse; Note: You can also define StringsToUse as a typeless constant, e.g.: const StringsToUse = 'Line 1' + LineEnding + 'Line 2' + LineEnding + // + ... 'Line n'; The most obvious solution is the best this time. List.Add('Line 1'); List.Add('Line 2'); ... List.Add('Line n'); Setting the Text property (or calling the SetText method) parses the text and splits it by NewLine chars. It makes no sense because you have concatenated the strings with NewLines. Method Add does not need to parse anything. None of these solutions is "preinitialised", compile time solution. You can only initialize a const static arrays at compile time. Thanks for that Juha (et al.). I wanted to make sure that I wasn't missing something that I really should have known. Is it possible to set up the text as an attribute (I'm avoiding the word property here for obvious reasons) of the entire class rather than of an instance? Should I be looking at resources here and if so could they be kept in the main source file? This is for a highly-experimental SNMP MIB to Pascal translator that I'm tinkering with so- as an example- there will be a single class describing an HP Ethernet switch which may be instantiated but will have common descriptive text. -- Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues] ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] wkhtmltopdf - trying use h2pas...
On Sat, Feb 5, 2011 at 8:59 AM, ik wrote: > > Webkit already translated to Pascal, and you use the API, and the amount of > time to use the API is shorter then the amount of time to translate the > header files imho. Well... I didn't know that... > You need to load the page to the browser and use the printer api to print it > to pdf (you can also control how exactly it is done), and tell it where to > store the pdf. Not. My program receive a HTML file and convert to PDF file. Just it. This program will be used from a site coded in (old) ASP, via an (wrapper) ActiveX. My first option is use the wkhtmltopdf lib (dll) in my ActiveX. The second option is use the wkhtmltopdf binary together at my program (easy way). I got the the lib to Win http://users.telenet.be/Jan.Van.hijfte/qtforfpc/win_bin-qt4pas-V2.1_Qt4.5.3.zip There are many functions in qt4.pas... Could you tell me what functions I can use to do the convertion above? There is a SVN from sources? Who works in this project? Marcos Douglas ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Pre-initialising a TStringList
Mark Morgan Lloyd kirjoitti sunnuntai 06 helmikuu 2011 17:50:05: > Is it possible to set up the text as an attribute (I'm avoiding the word > property here for obvious reasons) of the entire class rather than of an > instance? The const array could be outside of any class. It is global if declared in interface section, or local to one unit if declared in implementation section. Simple but it works well. Juha ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Re: Correct use of var in function calls?
On Sat, 05 Feb 2011 15:51:37 +0100, Florian Klaempfl wrote: >Am 05.02.2011 10:46, schrieb Bo Berglund: >> But that is not what I am doing at all, so I can stick with a simple: >> >> FillArr(Arr: TByteArr) >> >> and be sure that I will not get back a different array, but instead >> get my array filled as requested... >> > >As soon as you call SetLength, this will break havoc. Then a deep copy >is generated and it gets ref. count of 1 and it is destroyed at callee exit. I think I now encountered your warning live. What happened is that the caller has a dynamic array, which is set to length 2 just as a precursor. Then it calls a method and passes the name of the dynamic array as a parameter. The function does some digging of data and then decides how many elements to store, so it applies SetLength in its execution, then fills the array with the new data. But when the call returns the length of the array is still 2... Turns out that this is a case when the declaration of the function should have var tacked on like this: function MyFunction(var Data: TByteArr): boolean; When I added this the length was passed right back and it was possible to read all of the data by the caller. So in summary: If the called method changes the length of teh dynamic array it must be passed as a var, otherwise the length change will be lost when exiting the method. Bo Berglund ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: Correct use of var in function calls?
Am 06.02.2011 18:53, schrieb Bo Berglund: > > So in summary: > If the called method changes the length of teh dynamic array it must > be passed as a var, otherwise the length change will be lost when > exiting the method. I'd even propose that one uses var as soon as he plans to change the array. It makes the code easier to understand. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal