Re: [fpc-pascal] fpc has trouble with array types
On Tue, Oct 18, 2011 at 5:47 AM, Andrew Pennebaker andrew.penneba...@gmail.com wrote: function XlatPrime () : array of byte; begin XlatPrime := ( $64, $73, $66, $64, $3b, $6b, $66, $6f, I think that a syntax similar to this is available only for constant initialization. Actually I remember it from constant initialization for records. Maybe it works for arrays too. The usual way to set a dynamic array is: function XlatPrime () : array of byte; begin SetLength(XlatPrime, 20); XlatPrime[0] := $64; XlatPrime[1] := $73; etc But you can also try this (maybe it works): function XlatPrime () : array of byte; const ConstXlatPrime: array[0..19] of Byte = ( $64, $73, $66, $64, $3b, $6b, $66, $6f,); begin Result := ConstXlatPrime; end; -- Felipe Monteiro de Carvalho ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How can I implement thrice in Free Pascal?
Andrew Pennebaker wrote: thrice :: a - [a] thrice x = [x, x, x] I know the answer involves generics, but the docs don't offer examples using Free Pascal's built-in generic types. The solution would require generic functions, these are implemented in FPC trunk only. In the latest release (2.4.4) the result you want can be achieved with overloading and some copy-paste (that can be avoided using include files). See attached code for an example. However note that it does not work properly for class instances. Since they are basically pointers the output would be three pointers to the same class instance. AFAIK there is no way in FPC (and Delphi) to deep-copy arbitrary class instance because TObject does not force us to define copy constructors. program project1; uses Classes; {$mode delphi} type TIntegerArray = array of Integer; TStringArray = array of String; function Thrice(AValue: Integer): TIntegerArray; overload; var I: Integer; begin SetLength(Result, 3); for I := 0 to 2 do Result[I] := AValue; end; function Thrice(const AValue: String): TStringArray; overload; var I: Integer; begin SetLength(Result, 3); for I := 0 to 2 do Result[I] := AValue; end; var I: Integer; Thrice5: TIntegerArray; ThriceHello: TStringArray; ThriceWorld: TStringList; begin Thrice5 := Thrice(5); for I := 0 to High(Thrice5) do Write(Thrice5[I], ' '); WriteLn(); ThriceHello := Thrice('hello'); for I := 0 to High(ThriceHello) do Write(ThriceHello[I], ' '); end. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Delphi's anonymous functions in Free Pascal
Am 17.10.2011 22:53, schrieb Andrew Pennebaker: Does Free Pascal have anonymous functions that you can pass around, e.g. to a sort(compare : function, arr : array) function? If not, does anyone know any hacks to accomplish this? No, FPC does not support this currently and I know no one who plans to support it in the near future. A similiar feature available in the upcoming 2.6 and in trunk are nested function references. I wrote about that some days ago in this mail: http://www.hu.freepascal.org/lists/fpc-pascal/2011-October/030460.html Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Re: Delphi's anonymous functions in Free Pascal
On 17/10/2011 21:53, Andrew Pennebaker wrote: Does Free Pascal have anonymous functions that you can pass around, e.g. to a sort(compare : function, arr : array) function? If not, does anyone know any hacks to accomplish this? Cheers, Andrew Pennebaker www.yellosoft.us http://www.yellosoft.us Yes: //you can declare a type : TMyFunction: function(AMyParam : TType): TOtherType; // then implement: function MyFunctionWithArbitraryName(AMyParam: TType): TOtherType; begin {do something with AMyParam} end; //then (or before, if the TMyFunction is in type declaration section) function AFunctionUsingTMyFunctionAsArgument(AFunction : TMyFunction):TWhateverType; var OtherReturn: TOtherType; AParameterToAFunction : TType; begin {some code} OtherReturn := AFunction(AParameter); {other code} end; // and finally even const MYFUNCTIONCOUNT = 1 ArrayOfTMyFunction = array[0..MYFUNCTIONCOUNT-1] of TMyFunction = (MyFunctionWithArbitraryName); {it's a static array, so match the number of elemets} begin for {declared somewhere global} i := 0 to MYFUNCTIONCOUNT-1 do WhateverReturn := AFunctionUsingTMyFunctionAsArgument(ArrayOfTMyFunction[i](MyParam)); end. (Terms and conditions : this is invoked from /dev/mem, some syntax discrepancies may occur as my attention moved on from this as it obviously worked) I have written a dumb CLI interpreter this way ;) recently. (the function table contains command name as string in a record together with the function and the main procedure looks for the name and executes the arbitrary function when found) TMyFunctionRecord = record CLIName: string; MyFunction : TMyFunction end; const ArrayOfCLIFunctions = array[0..CLIFUNCCOUNT-1] of TMyFunctionRecord = ({...}); var CLIFunctionToExecute : TMyFunction; WhateverReturn : TWhateverType; begin for i := 0 to CLIFUNCCOUNT-1 do if ArrayOfCLIFunctions[i].CLIName = paramstr[1] then begin CLIFunctionToExecute := ArrayOfCLIFunctions[i].MyFunction; break; end; WhateverReturn := AFunctionUsingTMyFunctionAsArgument(CLIFunctionToExecute(MyParam)); end. Also the 'anonymous' functions can be implemented in a separate unit which only exports the /relevant/ ones in its interface section. The obvious limitation / safeguard is : you must use the function of declared type to pass into the function AFunctionUsingTMyFunctionAsArgument(AFunction : TMyFunction) and no other type; (An obvious workaround to that is to use varargs ;) but I did not try that so I can't tell whether that would work) It's not much OOP in action (and may have {obvious for some //not me} performance penalties but oh well. ;) L. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] fpc has trouble with array types
Am 18.10.2011 05:47, schrieb Andrew Pennebaker: But, but, the docs imply that this is the syntax for a function that returns an array of bytes. [snip] function XlatPrime () : array of byte; begin XlatPrime := ( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); end; Free Pascal (and Delphi and TP) does not support such syntax. I suggest you that you define your array as a const instead of a function and just use that. E.g. const XlatPrime: array[0..52] of Byte = ( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); It is necessary to specify the element count correctly. Note: In trunk you should be able to do the following: type TByteArray = array of Byte; function XlatPrime(): TByteArray; begin XlatPrime := TByteArray.Create( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); end; This feature was introduced because of Delphi compatibilty (and I still have the opinion that it would be nice if that would be extended for unnamed array types as well...) Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How can I implement thrice in Free Pascal?
Am 18.10.2011 10:52, schrieb Vladimir Zhirov: The solution would require generic functions, these are implemented in FPC trunk only. Generic functions are NOT implemented in trunk (at least as far as I know...). Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] fpc has trouble with array types
Am 18.10.2011 11:12, schrieb Sven Barth: type TByteArray = array of Byte; function XlatPrime(): TByteArray; begin XlatPrime := TByteArray.Create( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); end; Before I forget it: If you use this solution (not the const one) or you use SetLength, you need to free the array using SetLength(0) or YourArrayVariable := Nil, otherwise you'll have a memory leak. You must not do this if you use the const solution. Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] fpc has trouble with array types
2011/10/18 Sven Barth pascaldra...@googlemail.com: Am 18.10.2011 11:12, schrieb Sven Barth: type TByteArray = array of Byte; function XlatPrime(): TByteArray; begin XlatPrime := TByteArray.Create( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); end; Before I forget it: If you use this solution (not the const one) or you use SetLength, you need to free the array using SetLength(0) or YourArrayVariable := Nil, otherwise you'll have a memory leak. You must not do this if you use the const solution. That is unexpected. Until now, SetLength was used for ansistrings and dynamic arrays, which didn't have this requirement (unless you messed with the internals using Move). Vincent ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How can I implement thrice in Free Pascal?
Sven Barth wrote: Generic functions are NOT implemented in trunk (at least as far as I know...). Ouch, sorry. I read about generic procedural types at New features trunk wiki page and thought it was what OP need. Andrew, I apologize for misinformation and thanks Sven for correcting me. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Re: Delphi's anonymous functions in Free Pascal
[...] (facepalm) I did not try 'anonymous' function as you asked, oh. Need to learn to read ;) L. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] fpc has trouble with array types
Am 18.10.2011 11:19, schrieb Vincent Snijders: 2011/10/18 Sven Barthpascaldra...@googlemail.com: Am 18.10.2011 11:12, schrieb Sven Barth: type TByteArray = array of Byte; function XlatPrime(): TByteArray; begin XlatPrime := TByteArray.Create( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); end; Before I forget it: If you use this solution (not the const one) or you use SetLength, you need to free the array using SetLength(0) or YourArrayVariable := Nil, otherwise you'll have a memory leak. You must not do this if you use the const solution. That is unexpected. Until now, SetLength was used for ansistrings and dynamic arrays, which didn't have this requirement (unless you messed with the internals using Move). Could be that I missed the reference counting of arrays. At least I always try to free my arrays by hand ^^ Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Re: fpweb and reading the contents of a request
Try using Indy or Synapse as HttpClient to send data content using HTTP POST with the parameters as you mean. my FPWeb success to do it using PHP via CURL. I guess this is not a limitation of HTTP, just not the standard for web scripting - - -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/fpweb-and-reading-the-contents-of-a-request-tp4909915p4913056.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] fpc has trouble with array types
Am 18.10.2011 11:33, schrieb Sven Barth: Am 18.10.2011 11:19, schrieb Vincent Snijders: 2011/10/18 Sven Barthpascaldra...@googlemail.com: Am 18.10.2011 11:12, schrieb Sven Barth: type TByteArray = array of Byte; function XlatPrime(): TByteArray; begin XlatPrime := TByteArray.Create( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); end; Before I forget it: If you use this solution (not the const one) or you use SetLength, you need to free the array using SetLength(0) or YourArrayVariable := Nil, otherwise you'll have a memory leak. You must not do this if you use the const solution. That is unexpected. Until now, SetLength was used for ansistrings and dynamic arrays, which didn't have this requirement (unless you messed with the internals using Move). Could be that I missed the reference counting of arrays. At least I always try to free my arrays by hand ^^ Yes, seems like I missed the ref counting. See http://www.freepascal.org/docs-html/ref/refsu15.html#x39-430003.3.1 As remarked earlier, dynamic arrays are reference counted: if in one of the previous examples A goes out of scope and B does not, then the array is not yet disposed of: the reference count of A (and B) is decreased with 1. As soon as the reference count reaches zero the memory, allocated for the contents of the array, is disposed of. Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: fpweb and reading the contents of a request
Am 18.10.2011 11:35, schrieb herux: Try using Indy or Synapse as HttpClient to send data content using HTTP POST with the parameters as you mean. my FPWeb success to do it using PHP via CURL. I guess this is not a limitation of HTTP, just not the standard for web scripting One should at least consider the following statement from http://en.wikipedia.org/wiki/HTTP#Safe_methods : Some methods (for example, HEAD, GET, OPTIONS and TRACE) are defined as safe, which means they are intended only for information retrieval and should not change the state of the server. In other words, they should not have side effects, beyond relatively harmless effects such as logging, caching, the serving of banner advertisements or incrementing a web counter. Making arbitrary GET requests without regard to the context of the application's state should therefore be considered safe. Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: fpweb and reading the contents of a request
On Tue, Oct 18, 2011 at 11:35 AM, herux her...@gmail.com wrote: Try using Indy or Synapse as HttpClient to send data content using HTTP POST with the parameters as you mean. my FPWeb success to do it using PHP via CURL. !? My problem was sending data from JavaScript to FPWeb, I fail to see how any of this could apply. -- Felipe Monteiro de Carvalho ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] CGI under Freepascal
Hello listers, I am trying to use CGI with Pascal. I don't have background in web development, but principles of CGI seems to be simple. The first way could be FPWeb. However, I prefer another solution even if it is the best bet. It requires complex compilations and configurations since it's not available in a deb package. So I want an alternative working with FPC 2.4.4. The second attempt was custcgi. Except for the source code and an single example sent to a friend, no more documentation I found. I tried also Ezcgi, but the lack of documentation and examples are big obstacles. What I want to create something simple with a minimum support of CGI facilities, but without onus of instalations, compilations and configurations. I don't matter if the solution is modern or old, if only a set of some few resources are available, if the method is not appropriate to a large quantity o of simultaneous users, for me, it's important the solution only works. With a tStringList.loadfromfile, I can load a HTML file and show it iterating on list elements. If this is not my option is because the difficulty to process variables received from a form. So a minimum of specialized routines should be available. The variety of projects under Freepascal is so big that I will be not surprised with other solutions for my problem. What do you suggest for me? Regards, Luciano ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] CGI under Freepascal
I think that fpweb is the best option, since most people seam to be using it. But the wiki has an example about how to do a more simple CGI app: http://wiki.lazarus.freepascal.org/CGI_Web_Programming The provided minimal example teaches all the basics. -- Felipe Monteiro de Carvalho ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] CGI under Freepascal
On Tue, 18 Oct 2011, Luciano de Souza wrote: Hello listers, I am trying to use CGI with Pascal. I don't have background in web development, but principles of CGI seems to be simple. [snip] The variety of projects under Freepascal is so big that I will be not surprised with other solutions for my problem. What do you suggest for me? You can always search for powtils, but as far as I know it is unmaintained. (and also not well-documented IMHO) Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Getting Hardware information in Linux
Hello list, I'm trying to figure out how to get hardware information about the machine i'm running at in Linux OS. For example: hard-drive size, manufacture etc... BIOS information, screen information (regardless of X, that is the hardware itself), cards that are assigned and the whole information about such cards. Disks of any kind etc... CPU Information (can use the /proc/cpuinfo) Does anyone know or can point me on how to do it ? Thanks, Ido LINESIP - Opening the source for communication http://www.linesip.com http://www.linesip.co.il ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] fpc has trouble with array types
RRUZhttp://stackoverflow.com/questions/7802319/whats-the-syntax-for-literal-arrays-in-free-pascalhas the answer: const XLAT_SIZE = 53; xlat : Array[0..XLAT_SIZE-1] of Integer = ( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); Works for me! Cheers, Andrew Pennebaker www.yellosoft.us On Tue, Oct 18, 2011 at 5:36 AM, Sven Barth pascaldra...@googlemail.comwrote: Am 18.10.2011 11:33, schrieb Sven Barth: Am 18.10.2011 11:19, schrieb Vincent Snijders: 2011/10/18 Sven Barthpascaldragon@googlemail.**compascaldra...@googlemail.com : Am 18.10.2011 11:12, schrieb Sven Barth: type TByteArray = array of Byte; function XlatPrime(): TByteArray; begin XlatPrime := TByteArray.Create( $64, $73, $66, $64, $3b, $6b, $66, $6f, $41, $2c, $2e, $69, $79, $65, $77, $72, $6b, $6c, $64, $4a, $4b, $44, $48, $53, $55, $42, $73, $67, $76, $63, $61, $36, $39, $38, $33, $34, $6e, $63, $78, $76, $39, $38, $37, $33, $32, $35, $34, $6b, $3b, $66, $67, $38, $37 ); end; Before I forget it: If you use this solution (not the const one) or you use SetLength, you need to free the array using SetLength(0) or YourArrayVariable := Nil, otherwise you'll have a memory leak. You must not do this if you use the const solution. That is unexpected. Until now, SetLength was used for ansistrings and dynamic arrays, which didn't have this requirement (unless you messed with the internals using Move). Could be that I missed the reference counting of arrays. At least I always try to free my arrays by hand ^^ Yes, seems like I missed the ref counting. See http://www.freepascal.org/* *docs-html/ref/refsu15.html#**x39-430003.3.1http://www.freepascal.org/docs-html/ref/refsu15.html#x39-430003.3.1 As remarked earlier, dynamic arrays are reference counted: if in one of the previous examples A goes out of scope and B does not, then the array is not yet disposed of: the reference count of A (and B) is decreased with 1. As soon as the reference count reaches zero the memory, allocated for the contents of the array, is disposed of. Regards, Sven __**_ fpc-pascal maillist - fpc-pascal@lists.freepascal.**orgfpc-pascal@lists.freepascal.org http://lists.freepascal.org/**mailman/listinfo/fpc-pascalhttp://lists.freepascal.org/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How can I implement thrice in Free Pascal?
Thrice is designed to work for *any* array type, e.g. arrays of arrays of arrays of bytes. Even if I hardcoded several thousand possible types, Thrice wouldn't work for custom user types. If you do find a way to make this work, please let me know. Cheers, Andrew Pennebaker www.yellosoft.us On Tue, Oct 18, 2011 at 5:39 AM, Vladimir Zhirov vvzh.li...@gmail.comwrote: Sven Barth wrote: Generic functions are NOT implemented in trunk (at least as far as I know...). Ouch, sorry. I read about generic procedural types at New features trunk wiki page and thought it was what OP need. Andrew, I apologize for misinformation and thanks Sven for correcting me. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How can I implement thrice in Free Pascal?
In particular, if anyone knows a way to implement a general concatenation function Concat(Arr1, Arr2), let me know. Cheers, Andrew Pennebaker www.yellosoft.us On Tue, Oct 18, 2011 at 1:59 PM, Andrew Pennebaker andrew.penneba...@gmail.com wrote: Thrice is designed to work for *any* array type, e.g. arrays of arrays of arrays of bytes. Even if I hardcoded several thousand possible types, Thrice wouldn't work for custom user types. If you do find a way to make this work, please let me know. Cheers, Andrew Pennebaker www.yellosoft.us On Tue, Oct 18, 2011 at 5:39 AM, Vladimir Zhirov vvzh.li...@gmail.comwrote: Sven Barth wrote: Generic functions are NOT implemented in trunk (at least as far as I know...). Ouch, sorry. I read about generic procedural types at New features trunk wiki page and thought it was what OP need. Andrew, I apologize for misinformation and thanks Sven for correcting me. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: Delphi's anonymous functions in Free Pascal
Sokol, I'm writing a function GenArray(generator) that returns a random array populated by calling the generator function. So the return type of GenArray matches array of the return type of the generator function, for any generator function. E.g., GenArray(GenChar) would return a random string. (Except I don't know the syntax for passing function pointers.) Maybe you could fork my code and use templates to achieve this? GitHub https://github.com/mcandre/paycheck Cheers, Andrew Pennebaker www.yellosoft.us On Tue, Oct 18, 2011 at 4:57 AM, Lukasz Sokol el.es...@gmail.com wrote: On 17/10/2011 21:53, Andrew Pennebaker wrote: Does Free Pascal have anonymous functions that you can pass around, e.g. to a sort(compare : function, arr : array) function? If not, does anyone know any hacks to accomplish this? Cheers, Andrew Pennebaker www.yellosoft.us http://www.yellosoft.us Yes: //you can declare a type : TMyFunction: function(AMyParam : TType): TOtherType; // then implement: function MyFunctionWithArbitraryName(AMyParam: TType): TOtherType; begin {do something with AMyParam} end; //then (or before, if the TMyFunction is in type declaration section) function AFunctionUsingTMyFunctionAsArgument(AFunction : TMyFunction):TWhateverType; var OtherReturn: TOtherType; AParameterToAFunction : TType; begin {some code} OtherReturn := AFunction(AParameter); {other code} end; // and finally even const MYFUNCTIONCOUNT = 1 ArrayOfTMyFunction = array[0..MYFUNCTIONCOUNT-1] of TMyFunction = (MyFunctionWithArbitraryName); {it's a static array, so match the number of elemets} begin for {declared somewhere global} i := 0 to MYFUNCTIONCOUNT-1 do WhateverReturn := AFunctionUsingTMyFunctionAsArgument(ArrayOfTMyFunction[i](MyParam)); end. (Terms and conditions : this is invoked from /dev/mem, some syntax discrepancies may occur as my attention moved on from this as it obviously worked) I have written a dumb CLI interpreter this way ;) recently. (the function table contains command name as string in a record together with the function and the main procedure looks for the name and executes the arbitrary function when found) TMyFunctionRecord = record CLIName: string; MyFunction : TMyFunction end; const ArrayOfCLIFunctions = array[0..CLIFUNCCOUNT-1] of TMyFunctionRecord = ({...}); var CLIFunctionToExecute : TMyFunction; WhateverReturn : TWhateverType; begin for i := 0 to CLIFUNCCOUNT-1 do if ArrayOfCLIFunctions[i].CLIName = paramstr[1] then begin CLIFunctionToExecute := ArrayOfCLIFunctions[i].MyFunction; break; end; WhateverReturn := AFunctionUsingTMyFunctionAsArgument(CLIFunctionToExecute(MyParam)); end. Also the 'anonymous' functions can be implemented in a separate unit which only exports the /relevant/ ones in its interface section. The obvious limitation / safeguard is : you must use the function of declared type to pass into the function AFunctionUsingTMyFunctionAsArgument(AFunction : TMyFunction) and no other type; (An obvious workaround to that is to use varargs ;) but I did not try that so I can't tell whether that would work) It's not much OOP in action (and may have {obvious for some //not me} performance penalties but oh well. ;) L. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: Delphi's anonymous functions in Free Pascal
On 10/18/2011 8:10 PM, Andrew Pennebaker wrote: Sokol, I'm writing a function GenArray(generator) that returns a random array populated by calling the generator function. So the return type of GenArray matches array of the return type of the generator function, for any generator function. E.g., GenArray(GenChar) would return a random string. (Except I don't know the syntax for passing function pointers.) Sven pointed out yesterday that you cannot achieve in FPC what you want to achieve. (I haven't used Delphi in a long time and don't know what exactly anonymous functions are in Delphi, but I strongly tend to trust his word.) You can't get anonymous but only typed function pointers (cf. also Lukasz mail, second to last paragraph, plus his short mail). Since you were asking for hacks... By way of satire(!): To bypass the need to explicitly define a function type for each data type, you could give up on types and write a lot of functions like function GenChar : Pointer; function GenString : Pointer; The type of all of those can be given as type TGen = function : Pointer; Generator would have to return an array of Pointer in this scenario. Then, you could hack your way on from there. Please don't mention my name, though. However, if you're just asking about the syntax for procedural types, the Language reference guide for FPC 2.4.4, section 3.6 (p. 45) has all the details: http://www.freepascal.org/docs.var Maybe you could fork my code Out of curiosity: Is that the impersonal you? and use templates to achieve this? Generics are currently for classes only, as chapter 8 (p. 87) of the aforementioned reference guide explains. Best, Roland On Tue, Oct 18, 2011 at 4:57 AM, Lukasz Sokol el.es...@gmail.com wrote: On 17/10/2011 21:53, Andrew Pennebaker wrote: Does Free Pascal have anonymous functions that you can pass around, e.g. to a sort(compare : function, arr : array) function? If not, does anyone know any hacks to accomplish this? Cheers, Andrew Pennebaker www.yellosoft.us http://www.yellosoft.us Yes: //you can declare a type : TMyFunction: function(AMyParam : TType): TOtherType; // then implement: function MyFunctionWithArbitraryName(AMyParam: TType): TOtherType; begin {do something with AMyParam} end; //then (or before, if the TMyFunction is in type declaration section) function AFunctionUsingTMyFunctionAsArgument(AFunction : TMyFunction):TWhateverType; var OtherReturn: TOtherType; AParameterToAFunction : TType; begin {some code} OtherReturn := AFunction(AParameter); {other code} end; // and finally even const MYFUNCTIONCOUNT = 1 ArrayOfTMyFunction = array[0..MYFUNCTIONCOUNT-1] of TMyFunction = (MyFunctionWithArbitraryName); {it's a static array, so match the number of elemets} begin for {declared somewhere global} i := 0 to MYFUNCTIONCOUNT-1 do WhateverReturn := AFunctionUsingTMyFunctionAsArgument(ArrayOfTMyFunction[i](MyParam)); end. (Terms and conditions : this is invoked from /dev/mem, some syntax discrepancies may occur as my attention moved on from this as it obviously worked) I have written a dumb CLI interpreter this way ;) recently. (the function table contains command name as string in a record together with the function and the main procedure looks for the name and executes the arbitrary function when found) TMyFunctionRecord = record CLIName: string; MyFunction : TMyFunction end; const ArrayOfCLIFunctions = array[0..CLIFUNCCOUNT-1] of TMyFunctionRecord = ({...}); var CLIFunctionToExecute : TMyFunction; WhateverReturn : TWhateverType; begin for i := 0 to CLIFUNCCOUNT-1 do if ArrayOfCLIFunctions[i].CLIName = paramstr[1] then begin CLIFunctionToExecute := ArrayOfCLIFunctions[i].MyFunction; break; end; WhateverReturn := AFunctionUsingTMyFunctionAsArgument(CLIFunctionToExecute(MyParam)); end. Also the 'anonymous' functions can be implemented in a separate unit which only exports the /relevant/ ones in its interface section. The obvious limitation / safeguard is : you must use the function of declared type to pass into the function AFunctionUsingTMyFunctionAsArgument(AFunction : TMyFunction) and no other type; (An obvious workaround to that is to use varargs ;) but I did not try that so I can't tell whether that would work) It's not much OOP in action (and may have {obvious for some //not me} performance penalties but oh well. ;) L. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How can I implement thrice in Free Pascal?
On 18 Oct 2011, at 20:03, Andrew Pennebaker wrote: In particular, if anyone knows a way to implement a general concatenation function Concat(Arr1, Arr2), let me know. I'm under the impression that you are trying to program in a statically typed language the same way as you'd use a dynamically typed language. Even with generic functions (which, as mentioned before, are not yet supported by FPC) you'd have to explicitly instantiate such a function for every type you'd want to do this for. More generally, concatenating arrays is an operation that is seldom done, because a) it's slow, especially once the arrays get to a certain size (lots of data copying, memory allocation operations) b) it leads to memory fragmentation (freeing the old arrays, allocating a new one) Pascal has a separate string type to optimize one common case where concatenating arrays is often required. In other cases, people generally use some form of list structure (generic or not) in case lots of insertions/deletions/concatenations are required. Jonas___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: Delphi's anonymous functions in Free Pascal
anonimous types, thrice, generic array concatenation, interpreting Pascal, anonymous functions... I'm guessing what language you come from. Then, the second question arises: why Pascal, now. Anyway, I think that immersing yourself in some classes and pointer logic could help you find some solutions to your tasks. (I still, anyway, suggest the quick and dirty C way with its macros and includes...) Cheers, A. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Getting Hardware information in Linux
On Tue, 18 Oct 2011, ik wrote: Hello list, I'm trying to figure out how to get hardware information about the machine i'm running at in Linux OS. For example: hard-drive size, manufacture etc... BIOS information, screen information (regardless of X, that is the hardware itself), cards that are assigned and the whole information about such cards. Disks of any kind etc... CPU Information (can use the /proc/cpuinfo) Does anyone know or can point me on how to do it ? use DBUS to query HAL. Normally you should get most of the info. I wrote an article on how to do this in FPC. if you want, I can send it to you. Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Getting Hardware information in Linux
On Tue, Oct 18, 2011 at 21:44, Michael Van Canneyt mich...@freepascal.orgwrote: On Tue, 18 Oct 2011, ik wrote: Hello list, I'm trying to figure out how to get hardware information about the machine i'm running at in Linux OS. For example: hard-drive size, manufacture etc... BIOS information, screen information (regardless of X, that is the hardware itself), cards that are assigned and the whole information about such cards. Disks of any kind etc... CPU Information (can use the /proc/cpuinfo) Does anyone know or can point me on how to do it ? use DBUS to query HAL. Normally you should get most of the info. HAL is deprecated :( I wrote an article on how to do this in FPC. if you want, I can send it to you. Sure, it can help me, thanks. Michael. __**_ fpc-pascal maillist - fpc-pascal@lists.freepascal.**orgfpc-pascal@lists.freepascal.org http://lists.freepascal.org/**mailman/listinfo/fpc-pascalhttp://lists.freepascal.org/mailman/listinfo/fpc-pascal Ido ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] How can I implement thrice in Free Pascal?
On 18.10.2011 21:30, Jonas Maebe wrote: On 18 Oct 2011, at 20:03, Andrew Pennebaker wrote: In particular, if anyone knows a way to implement a general concatenation function Concat(Arr1, Arr2), let me know. I'm under the impression that you are trying to program in a statically typed language the same way as you'd use a dynamically typed language. Even with generic functions (which, as mentioned before, are not yet supported by FPC) you'd have to explicitly instantiate such a function for every type you'd want to do this for. At least in theory it should work with generic functions (and using the Delphi compatible generic syntax): === source begin === type TGenArrayT = array of T; // this should work in trunk already function ConcatT(Arr1, Arr2: TGenArrayT): TGenArrayT; begin SetLength(Result, Length(aArray1) + Length(aArray2)); if Length(aArray1) 0 then Move(aArray1[0], Result[0], Length(aArray1) * SizeOf(T)); if Length(aArray2) 0 then Move(aArray2[0], Result[Length(aArray1)], Length(aArray2) * SizeOf(T)); end; var arr1, arr2, res: array of Integer; begin // init arr1 ... // init arr2 ... res := ConcatInteger(arr1, arr2); ... end. === source end === (tested using a non generic integer version) Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Getting Hardware information in Linux
On 18.10.2011 21:50, ik wrote: On Tue, Oct 18, 2011 at 21:44, Michael Van Canneyt mich...@freepascal.org mailto:mich...@freepascal.org wrote: On Tue, 18 Oct 2011, ik wrote: Hello list, I'm trying to figure out how to get hardware information about the machine i'm running at in Linux OS. For example: hard-drive size, manufacture etc... BIOS information, screen information (regardless of X, that is the hardware itself), cards that are assigned and the whole information about such cards. Disks of any kind etc... CPU Information (can use the /proc/cpuinfo) Does anyone know or can point me on how to do it ? use DBUS to query HAL. Normally you should get most of the info. HAL is deprecated :( You can then take a look at its successor, DeviceKit (see here: http://en.wikipedia.org/wiki/DeviceKit ). According to the wiki entry you might also try to take a look at libudev. I've found a guide here: http://www.signal11.us/oss/udev/ (I haven't read it and thus can't say whether it's good or not). Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: Delphi's anonymous functions in Free Pascal
Schäfer, thanks, that's a lot of practical information. Have you used Haskell QuickCheckhttp://www.haskell.org/haskellwiki/Introduction_to_QuickCheck? It's amazing that such a strictly typed language can do these sorts of things. Yes, pointers are probably the only way I can implement this, for now. If at all possible, I'd like to use more idiomatic Pascal code. The only way I managed to write the C port https://github.com/mcandre/qc was void* tricks :P but there's only so much you can do without lambdas. If I decide to use pointers, what's the syntax for accepting a function pointer (unknown type, unknown arity) and calling the function? In C, you have to know the entire function signature and explicitly cast the pointer to that before you can call it. Does Pascal also require this, or can I just accept a pointer and call it as if it were a normal function call? Finally, does Pascal have syntax for something like Lisp's (apply f args), or Smalltalk's Block valueWithArguments? Once I get forAll to accept function pointers, and call the functions, I need a way to pass the values to another function (again, unknown types, unknown arity). Cheers, Andrew Pennebaker www.yellosoft.us 2011/10/18 Roland Schäfer roland.schae...@fu-berlin.de On 10/18/2011 8:10 PM, Andrew Pennebaker wrote: Sokol, I'm writing a function GenArray(generator) that returns a random array populated by calling the generator function. So the return type of GenArray matches array of the return type of the generator function, for any generator function. E.g., GenArray(GenChar) would return a random string. (Except I don't know the syntax for passing function pointers.) Sven pointed out yesterday that you cannot achieve in FPC what you want to achieve. (I haven't used Delphi in a long time and don't know what exactly anonymous functions are in Delphi, but I strongly tend to trust his word.) You can't get anonymous but only typed function pointers (cf. also Lukasz mail, second to last paragraph, plus his short mail). Since you were asking for hacks... By way of satire(!): To bypass the need to explicitly define a function type for each data type, you could give up on types and write a lot of functions like function GenChar : Pointer; function GenString : Pointer; The type of all of those can be given as type TGen = function : Pointer; Generator would have to return an array of Pointer in this scenario. Then, you could hack your way on from there. Please don't mention my name, though. However, if you're just asking about the syntax for procedural types, the Language reference guide for FPC 2.4.4, section 3.6 (p. 45) has all the details: http://www.freepascal.org/docs.var Maybe you could fork my code Out of curiosity: Is that the impersonal you? and use templates to achieve this? Generics are currently for classes only, as chapter 8 (p. 87) of the aforementioned reference guide explains. Best, Roland On Tue, Oct 18, 2011 at 4:57 AM, Lukasz Sokol el.es...@gmail.com wrote: On 17/10/2011 21:53, Andrew Pennebaker wrote: Does Free Pascal have anonymous functions that you can pass around, e.g. to a sort(compare : function, arr : array) function? If not, does anyone know any hacks to accomplish this? Cheers, Andrew Pennebaker www.yellosoft.us http://www.yellosoft.us Yes: //you can declare a type : TMyFunction: function(AMyParam : TType): TOtherType; // then implement: function MyFunctionWithArbitraryName(AMyParam: TType): TOtherType; begin {do something with AMyParam} end; //then (or before, if the TMyFunction is in type declaration section) function AFunctionUsingTMyFunctionAsArgument(AFunction : TMyFunction):TWhateverType; var OtherReturn: TOtherType; AParameterToAFunction : TType; begin {some code} OtherReturn := AFunction(AParameter); {other code} end; // and finally even const MYFUNCTIONCOUNT = 1 ArrayOfTMyFunction = array[0..MYFUNCTIONCOUNT-1] of TMyFunction = (MyFunctionWithArbitraryName); {it's a static array, so match the number of elemets} begin for {declared somewhere global} i := 0 to MYFUNCTIONCOUNT-1 do WhateverReturn := AFunctionUsingTMyFunctionAsArgument(ArrayOfTMyFunction[i](MyParam)); end. (Terms and conditions : this is invoked from /dev/mem, some syntax discrepancies may occur as my attention moved on from this as it obviously worked) I have written a dumb CLI interpreter this way ;) recently. (the function table contains command name as string in a record together with the function and the main procedure looks for the name and executes the arbitrary function when found) TMyFunctionRecord = record CLIName: string; MyFunction : TMyFunction end; const ArrayOfCLIFunctions = array[0..CLIFUNCCOUNT-1] of TMyFunctionRecord = ({...}); var CLIFunctionToExecute :
Re: [fpc-pascal] How can I implement thrice in Free Pascal?
Barth, something's not quite right. I've compiled and installed the trunk version of fpc, but it won't recognize this syntax. paycheck.pas: unit Paycheck; interface type TArrayT = array of T; ... Trace: fpc example.pas Compiling example.pas Compiling paycheck.pas paycheck.pas(4,8) Fatal: Syntax error, = expected but found Fatal: Compilation aborted Either my syntax is wrong, or trunk doesn't have the syntax, or I'm having trouble getting the trunk version. Cheers, Andrew Pennebaker www.yellosoft.us On Tue, Oct 18, 2011 at 3:55 PM, Sven Barth pascaldra...@googlemail.comwrote: On 18.10.2011 21:30, Jonas Maebe wrote: On 18 Oct 2011, at 20:03, Andrew Pennebaker wrote: In particular, if anyone knows a way to implement a general concatenation function Concat(Arr1, Arr2), let me know. I'm under the impression that you are trying to program in a statically typed language the same way as you'd use a dynamically typed language. Even with generic functions (which, as mentioned before, are not yet supported by FPC) you'd have to explicitly instantiate such a function for every type you'd want to do this for. At least in theory it should work with generic functions (and using the Delphi compatible generic syntax): === source begin === type TGenArrayT = array of T; // this should work in trunk already function ConcatT(Arr1, Arr2: TGenArrayT): TGenArrayT; begin SetLength(Result, Length(aArray1) + Length(aArray2)); if Length(aArray1) 0 then Move(aArray1[0], Result[0], Length(aArray1) * SizeOf(T)); if Length(aArray2) 0 then Move(aArray2[0], Result[Length(aArray1)], Length(aArray2) * SizeOf(T)); end; var arr1, arr2, res: array of Integer; begin // init arr1 ... // init arr2 ... res := ConcatInteger(arr1, arr2); ... end. === source end === (tested using a non generic integer version) Regards, Sven __**_ fpc-pascal maillist - fpc-pascal@lists.freepascal.**orgfpc-pascal@lists.freepascal.org http://lists.freepascal.org/**mailman/listinfo/fpc-pascalhttp://lists.freepascal.org/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Getting Hardware information in Linux
On Tuesday 18 October 2011 19:36:48 ik wrote: Does anyone know or can point me on how to do it ? read the output of lshw or read its source on how to do it yourself. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Getting Hardware information in Linux
I think all this info can be found on /proc directory... 2011/10/18 ik ido...@gmail.com: Hello list, I'm trying to figure out how to get hardware information about the machine i'm running at in Linux OS. For example: hard-drive size, manufacture etc... BIOS information, screen information (regardless of X, that is the hardware itself), cards that are assigned and the whole information about such cards. Disks of any kind etc... CPU Information (can use the /proc/cpuinfo) Does anyone know or can point me on how to do it ? Thanks, Ido LINESIP - Opening the source for communication http://www.linesip.com http://www.linesip.co.il ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal