Re: [fpc-pascal] Read JSON from file

2015-08-06 Thread Michael Van Canneyt



On Fri, 7 Aug 2015, Michael Van Canneyt wrote:




On Fri, 7 Aug 2015, Felipe Monteiro de Carvalho wrote:


On Thu, Aug 6, 2015 at 10:37 PM, Chris Moody
 wrote:

For my current project, I download a file from a server that contains JSON
code. I'm not sure how to read it into something that GetJSON is able to
handle.

My first thought was using TStrings, however not sure how to convert a
TString into TStream.


I do it like this, with TStringStream:

 lStrings := TStringList.Create;
 try
   lStrings.LoadFromFile(AFile);

   // Parse JSON data
   lStream := TStringStream.Create(lStrings.Text);
   lParser := TJSONParser.Create(lStream);
   try
 lParser.Strict := False;
 lData := lParser.Parse;
   finally
 lParser.Free;
 lStream.Free;
   end;

Well, in my particular case I use TStringList because I pre-process
the data, removing comments which are not allowed in JSON.


As it happens, I have yesterday committed an extension which allows you to 
specify that the JSON supports comments.


s/supports/contains/  ,  obviously.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Read JSON from file

2015-08-06 Thread Michael Van Canneyt



On Fri, 7 Aug 2015, Felipe Monteiro de Carvalho wrote:


On Thu, Aug 6, 2015 at 10:37 PM, Chris Moody
 wrote:

For my current project, I download a file from a server that contains JSON
code. I'm not sure how to read it into something that GetJSON is able to
handle.

My first thought was using TStrings, however not sure how to convert a
TString into TStream.


I do it like this, with TStringStream:

 lStrings := TStringList.Create;
 try
   lStrings.LoadFromFile(AFile);

   // Parse JSON data
   lStream := TStringStream.Create(lStrings.Text);
   lParser := TJSONParser.Create(lStream);
   try
 lParser.Strict := False;
 lData := lParser.Parse;
   finally
 lParser.Free;
 lStream.Free;
   end;

Well, in my particular case I use TStringList because I pre-process
the data, removing comments which are not allowed in JSON.


As it happens, I have yesterday committed an extension which allows you to 
specify that the JSON supports comments.
Both // and /* */ are detected and discarded.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] alignment of records

2015-08-06 Thread Jonas Maebe

Xiangrong Fang wrote:


​OK, as a matter of fact, the stuff was copied from output of h2pas
conversion program. :-)


Then it seems that h2pas is pretty broken.


However, another issue: if I use this :

​{$CODEALIGN RECORDMIN=4}

It worked.


No, it didn't. The record definition was still wrong.


i.e. output size same as C version.


That's coincidence.

I wonder what's the

difference between $CODEALIGN and $A/$PACKRECORDS?


Packrecords sets the maximum field alignment. As the name implies, 
{$codealign recordmin} sets the minimum field alignment.



Also, if I use "packed records", alignment will never take place,
regardless of $A settings, right?


Indeed.


Jonas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Read JSON from file

2015-08-06 Thread Michael Van Canneyt



On Thu, 6 Aug 2015, Chris Moody wrote:


Hi all,

For my current project, I download a file from a server that contains JSON 
code. I'm not sure how to read it into something that GetJSON is able to 
handle.


My first thought was using TStrings, however not sure how to convert a 
TString into TStream.


Any assistance would be much appreciated.


The following will read JSON and write all keys it finds

uses jsonparser, fpjson, sysutils, classes;

Var
  F : TFileStream;
  D : TJSONData;
  E : TJSONEnum;

begin
  D:=Nil;
  F:=TFileStream.Create('myfile.json',fmOpenRead or fmShareDenywrite);
  try
D:=GetJSON(F);
for E in D do
  Writeln(E.Key, ' : ',E.Value.AsJSON);
  Finally
D.free;
F.Free;
  end;
end.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] alignment of records

2015-08-06 Thread Xiangrong Fang
>
> By using the correct types for the fields:
> http://www.freepascal.org/docs-html/rtl/ctypes/index-3.html
>
> E.g., "unsigned long" is not (always) the same as "dword".
> ​
>
> ​OK, as a matter of fact, the stuff was copied from output of h2pas
conversion program. :-)  I will use ctypes to test again.

However, another issue: if I use this :

​{$CODEALIGN RECORDMIN=4}

It worked. i.e. output size same as C version.   I wonder what's the
difference between $CODEALIGN and $A/$PACKRECORDS?

Also, if I use "packed records", alignment will never take place,
regardless of $A settings, right?

Thanks!
​Xiangrong​




> Jonas
> ___
> fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] alignment of records

2015-08-06 Thread Jonas Maebe

Xiangrong Fang wrote:

It seems that $packrecord does not work at all.


It works fine.


I did some research and
found this document:

http://www.freepascal.org/docs-html/ref/refsu19.html


The documentation seems to be outdated regarding the alignment of 
arrays. Arrays probably used to be aligned based on the size of the 
entire array at one point (it's before recorded history, i.e. before 
September 2000), or maybe the record field insertion code used to 
contain some special (broken) logic for arrays that ignored their 
alignment and instead used their size.


In any case, the alignment requirement of an array is and has been for 
quite some time the alignment requirement of its individual elements, 
and the alignment of a field's type is what's used to determine its 
offset in a record (with a maximum of the current packrecords setting).



Jonas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] alignment of records

2015-08-06 Thread Jonas Maebe

Xiangrong Fang wrote:


program test;
{$mode objfpc}{$PACKRECORDS C}
type
   ifmap = record
 mem_start: dword;
 mem_end: dword;
 base_addr: word;
 irq: byte;
 dma: byte;
 port: byte;
   end;
begin
   WriteLn('ifmap=', SizeOf(ifmap));
end.

The C struct's size is 24, but pascal record is 16.The ifmap struct
is defined as:

struct ifmap {
 unsigned long mem_start;
 unsigned long mem_end;
 unsigned short base_addr;
 unsigned char irq;
 unsigned char dma;
 unsigned char port;
 /* 3 bytes spare */
};

How can I instruct FPC to align records exactly same as GCC?


By using the correct types for the fields: 
http://www.freepascal.org/docs-html/rtl/ctypes/index-3.html


E.g., "unsigned long" is not (always) the same as "dword".


Jonas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Read JSON from file

2015-08-06 Thread Felipe Monteiro de Carvalho
On Thu, Aug 6, 2015 at 10:37 PM, Chris Moody
 wrote:
> For my current project, I download a file from a server that contains JSON
> code. I'm not sure how to read it into something that GetJSON is able to
> handle.
>
> My first thought was using TStrings, however not sure how to convert a
> TString into TStream.

I do it like this, with TStringStream:

  lStrings := TStringList.Create;
  try
lStrings.LoadFromFile(AFile);

// Parse JSON data
lStream := TStringStream.Create(lStrings.Text);
lParser := TJSONParser.Create(lStream);
try
  lParser.Strict := False;
  lData := lParser.Parse;
finally
  lParser.Free;
  lStream.Free;
end;

Well, in my particular case I use TStringList because I pre-process
the data, removing comments which are not allowed in JSON.

-- 
Felipe Monteiro de Carvalho
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] alignment of records

2015-08-06 Thread Xiangrong Fang
It seems that $packrecord does not work at all.  I did some research and
found this document:

http://www.freepascal.org/docs-html/ref/refsu19.html

compiled and ran the example on that page, I got:

​​Size Trec1 : 4 Offset B : 2
Size Trec2 : 3 Offset B : 1
Size Trec3 : 2 Offset B : 1
Size Trec4 : 2 Offset B : 1
Size Trec5 : 5 Offset B : 1 Offset C : 4
Size Trec6 : 5 Offset B : 1 Offset C : 4
Size Trec7 : 9 Offset B : 1 Offset C : 8
Size Trec8 : 9 Offset B : 1 Offset C : 8

While the expected output, as stated in the document above, should be:

Size Trec1 : 4 Offset B : 2
Size Trec2 : 3 Offset B : 1
Size Trec3 : 2 Offset B : 1
Size Trec4 : 2 Offset B : 1
Size Trec5 : 8 Offset B : 4 Offset C : 7
Size Trec6 : 8 Offset B : 4 Offset C : 7
Size Trec7 : 12 Offset B : 4 Offset C : 11
Size Trec8 : 16 Offset B : 8 Offset C : 15



2015-08-07 10:48 GMT+08:00 Xiangrong Fang :

> Hi All,
>
> I try to port a program from C to Pascal, see the following example:
>
> #include 
> #include 
> #include 
> void main() {
> printf("ifmap=%ld\n", sizeof(struct ifmap));
> }
>
> I converted it to:
>
> est.c  test.pas
>
>  X
> program test;
> {$mode objfpc}{$PACKRECORDS C}
> type
>   ifmap = record
> mem_start: dword;
> mem_end: dword;
> base_addr: word;
> irq: byte;
> dma: byte;
> port: byte;
>   end;
> begin
>   WriteLn('ifmap=', SizeOf(ifmap));
> end.
>
> The C struct's size is 24, but pascal record is 16.The ifmap struct is
> defined as:
>
> struct ifmap {
> unsigned long mem_start;
> unsigned long mem_end;
> unsigned short base_addr;
> unsigned char irq;
> unsigned char dma;
> unsigned char port;
> /* 3 bytes spare */
> };
>
> How can I instruct FPC to align records exactly same as GCC?
>
> Thanks!
>
>
>
>
>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] alignment of records

2015-08-06 Thread Xiangrong Fang
Hi All,

I try to port a program from C to Pascal, see the following example:

#include 
#include 
#include 
void main() {
printf("ifmap=%ld\n", sizeof(struct ifmap));
}

I converted it to:

est.c  test.pas

   X
program test;
{$mode objfpc}{$PACKRECORDS C}
type
  ifmap = record
mem_start: dword;
mem_end: dword;
base_addr: word;
irq: byte;
dma: byte;
port: byte;
  end;
begin
  WriteLn('ifmap=', SizeOf(ifmap));
end.

The C struct's size is 24, but pascal record is 16.The ifmap struct is
defined as:

struct ifmap {
unsigned long mem_start;
unsigned long mem_end;
unsigned short base_addr;
unsigned char irq;
unsigned char dma;
unsigned char port;
/* 3 bytes spare */
};

How can I instruct FPC to align records exactly same as GCC?

Thanks!
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] Read JSON from file

2015-08-06 Thread leledumbo
> I download a file from a server that contains JSON code. I'm not sure how
to read it into something that GetJSON is able to handle.

Can't you figure out from the function interface:
http://www.freepascal.org/docs-html/fcl/fpjson/getjson.html




--
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/Read-JSON-from-file-tp5722198p5722199.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/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Read JSON from file

2015-08-06 Thread Chris Moody

Hi all,

For my current project, I download a file from a server that contains 
JSON code. I'm not sure how to read it into something that GetJSON is 
able to handle.


My first thought was using TStrings, however not sure how to convert a 
TString into TStream.


Any assistance would be much appreciated.

Chris
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] which GUI (noob)

2015-08-06 Thread waldo kitty

On 08/06/2015 12:30 PM, Graeme Geldenhuys wrote:

On 2015-08-06 15:43, Graeme Geldenhuys wrote:

  1. Run Lazarus and open the fpgui_toolkit.lpk package found in the
fpGUI code: /src/corelib/[x11|gdi]/fpgui_toolkit.lpk
Click "Compile".


It was brought to my attention that the above is somewhat ambiguous.

I meant for the text [x11|gdi] to mean "x11" or "gdi" depending on your
platform.


i found that out ;)

thanks for the clarification!

--
 NOTE: No off-list assistance is given without prior approval.
   *Please keep mailing list traffic on the list* unless
   private contact is specifically requested and granted.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] which GUI (noob)

2015-08-06 Thread Graeme Geldenhuys
On 2015-08-06 15:43, Graeme Geldenhuys wrote:
>  1. Run Lazarus and open the fpgui_toolkit.lpk package found in the
>fpGUI code: /src/corelib/[x11|gdi]/fpgui_toolkit.lpk
>Click "Compile".

It was brought to my attention that the above is somewhat ambiguous.

I meant for the text [x11|gdi] to mean "x11" or "gdi" depending on your
platform.

So if you run Windows, open the following package:
  \src\corelib\gdi\fpgui_toolkit.lpk

And if you run any X11 or OSX system, open this packages instead:
  /src/corelib/x11/fpgui_toolkit.lpk


Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] blowfish in fpc

2015-08-06 Thread Graeme Geldenhuys
On 2015-08-06 16:33, Xiangrong Fang wrote:
> I would like to know if the FPC implementation of BlowFish is considered
> secure or not?

I personally have no idea.

But for completeness, there is also the open source project called
DCPCrypt [1] which contains another Blowfish implementation, which many
others too.

Maybe there is some comparison you can do, or test case you can run them
against.


[1] http://wiki.freepascal.org/DCPcrypt#Source_Code

Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] blowfish in fpc

2015-08-06 Thread Xiangrong Fang
Hi All,

I would like to know if the FPC implementation of BlowFish is considered
secure or not? Especially:

1) Does it has the bug mentioned here:

https://www.schneier.com/blowfish-bug.txt

2) Does it operate in ECB mode or CBC mode (or any other mode)?

I am sorry that by just looking at the code I cannot answer any of the
questions above.

Thanks in advance for any comments!
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] which GUI (noob)

2015-08-06 Thread Graeme Geldenhuys
On 2015-08-06 15:26, Peter wrote:
> I was unsure how to
> get Lazarus to use fpgui in the first place. Anyway, its here, for
> anyone interested.

Correction. Those instructions mentioned in the URL you posted is to
create a LCL-fpGUI application. The LCL-fpGUI widgetset is not feature
complete, and not nearly ready for real usage.

It is recommended that you create fpGUI applications using fpGUI
directly (no LCL involved). For that you follow these simple steps:

Lets configure Lazarus to have a new project type:
--
 1. Run Lazarus and open the fpgui_toolkit.lpk package found in the
   fpGUI code: /src/corelib/[x11|gdi]/fpgui_toolkit.lpk
   Click "Compile".

 2. Now open the IDE add-on package, compile and install.
Open /extras/lazarus_ide/fpgui_ide.lpk
   Click "Compile" & "Install".
   Lazarus will rebuild and restart itself.


Now to create a fpGUI application:
--
 1. Run Lazarus

 2. Select "Project -> New Project..." and select "fpGUI Toolkit
Application".


All done, you have create your first fpGUI Toolkit application using
Lazarus IDE as your editor! :)

I also recommend you integrate the fpGUI UIDesigner (forms designer) and
DocView as external tool items in Lazarus IDE. It is just a convenience,
not a requirement. For that you can follow the instructions found in
this page. The page describes the process for docview, but the same
process is used for the UIDesigner.

  http://fpgui.sourceforge.net/docview_ide_integration.shtml


For more available options see the INSTALL.txt file found in
/docs/ directory.

Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] which GUI (noob)

2015-08-06 Thread Peter
On 04/08/15 16:20, Graeme Geldenhuys wrote:
> On 2015-08-04 16:11, Peter wrote:
>> I have not used Lazarus for a while, but I think you can use it as an
>> IDE, while using fpGUI as a component set. Perhaps someone can confirm?
> 
> I believe I answered that in an earlier reply.
> 
>   http://lists.freepascal.org/pipermail/fpc-pascal/2015-August/044795.html
> 
> 
> Regards,
>   - Graeme -
> 

Hi Graeme,

Your earlier reply contains a link about docview. I was unsure how to
get Lazarus to use fpgui in the first place. Anyway, its here, for
anyone interested.

http://wiki.freepascal.org/fpGUI_Interface#Creating_your_first_LCL_application_with_fpGUI


Regards,
Peter
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] another xml question

2015-08-06 Thread Marc Santhoff
On Do, 2015-08-06 at 01:11 -0700, Chris Moody wrote:
> Hi Marc,
> 
> Thanks for the feedback. One more question and I think I'm done for now 
> and able to finish the program I'm writing:
> 
> If FindNode does not locate a node with the given name, what does it 
> return?

I don't know. It looks like it would be the first child of the subnode
you're working at. I'm not sure if that first child can be a nil
pointer, so you need to check yourself or someone else has to answer
this question.

But it is easy to jump to the implementation of FindNode() yourself:

1. Put the cursor on the function call to .FindNode()
2. klick the right mouse key
3. in the popped up menu select "Find Declaration of FindNode"
4. hit the keys Shift+down (arrow)

After Step 3 you're shown the class declaration, then after the fourth
step the cursor is sitting in the implementation of the method in
question. Then you can read the code.

Have fun,
Marc


> Thanks,
> 
> Chris
> 
> On 07/24/2015 01:34 PM, Marc Santhoff wrote:
> > On Fr, 2015-07-24 at 11:57 -0700, Chris Moody wrote:
> >
> >> Would it be easiest to introduce a fourth element to the XML that says
> >> how many children the 3rd element has? Or is there an easy way already
> >> to get this and just run a for next loop to read each child?
> > Maybe sth. like this would do:
> >
> > 
> > 1>/child>
> > 2>/child>
> > 3>/child>
> > 
> >
> > or similar. Besides making the count an attribute of the root node of
> > counted elements you could simply use a standard pascal loop:
> >
> > n: XMLNode;
> >
> > { this is only pseude code ... }
> > while (xmldoc.hasMoreChilds()) do begin
> > n := getChild();
> > ... process n ...
> > end:
> >
> > For real code http://wiki.freepascal.org/XML_Tutorial mwill help.
> >
> > HTH,
> > Marc
> >
> 
> ___
> fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

-- 
Marc Santhoff 

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] another xml question

2015-08-06 Thread Chris Moody

Hi Marc,

Thanks for the feedback. One more question and I think I'm done for now 
and able to finish the program I'm writing:


If FindNode does not locate a node with the given name, what does it 
return?


Thanks,

Chris

On 07/24/2015 01:34 PM, Marc Santhoff wrote:

On Fr, 2015-07-24 at 11:57 -0700, Chris Moody wrote:


Would it be easiest to introduce a fourth element to the XML that says
how many children the 3rd element has? Or is there an easy way already
to get this and just run a for next loop to read each child?

Maybe sth. like this would do:


1>/child>
2>/child>
3>/child>


or similar. Besides making the count an attribute of the root node of
counted elements you could simply use a standard pascal loop:

n: XMLNode;

{ this is only pseude code ... }
while (xmldoc.hasMoreChilds()) do begin
n := getChild();
... process n ...
end:

For real code http://wiki.freepascal.org/XML_Tutorial mwill help.

HTH,
Marc



___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal