unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids
, LAZFileUtils
, fphttpclient
;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
CheckBox1: TCheckBox;
Memo1: TMemo;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
private
Procedure GetInpAPIcsv;
Function InpAPIstream:TMemoryStream;
procedure WriteMemStream(FFileName:String);
public
end;
var
Form1: TForm1;
FFileName : string; // if read from file
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
GetInpAPIcsv;
with StringGrid1 do
begin
Memo1.Append( Cells[14, 1]); // col / Row
Memo1.Append( Cells[15, 1]);
end
end;
Procedure TForm1.GetInpAPIcsv;
var
FileStream: TFileStream; // if read from file
begin
FFileName := 'MyFile.CSV';
try
StringGrid1.BeginUpdate;
StringGrid1.Clear; // Reset the grid
if FileExistsUTF8(FFileName) then // May read from file
begin
FileStream := TFileStream.Create(FFileName, fmOpenRead+fmShareDenyWrite);
StringGrid1.LoadFromCSVStream(FileStream, ';', true, 0, true);
FileStream.Free;
end
else
begin // Read with a memorystream
InpAPIstream.Position:=0;
StringGrid1.LoadFromCSVStream(InpAPIstream, ';', true, 0, true);
if CheckBox1.Checked then WriteMemStream(FFileName); // We may want to inspect the file
InpAPIstream.Free;
end;
finally
StringGrid1.EndUpdate;
end;
end;
Function TForm1.InpAPIstream: TMemoryStream; // The function as a Memorystream
var HTTPS: TFPHTTPClient;
URL:String;
begin
Result := TMemoryStream.Create;
URL:= 'MyUrl.....';
HTTPS := TFPHTTPClient.Create(nil);
try
try
HTTPS.Get(URL, Result); {<------- HTTPS.Get(URL)----------}
Result.Position:=0;
except
on E: Exception do
begin
Memo1.Append(' Error in HTTPS.Get ' + E.Message);
end;
end;
finally
HTTPS.RequestBody.Free;
HTTPS.Free;
end;
end;
// We may want to save the file:
procedure TForm1.WriteMemStream(FFileName:String);
begin
try
InpAPIstream.Position:=0;
InpAPIstream.SaveToFile(FFileName);
except
// on E: Exception do ...
end;
end;
(*
function ReadStream(FFileName: string): string; // Read a stream:
var
Strm: TFileStream;
n: longint;
txt: string;
begin
txt := '';
Strm := TFileStream.Create(FFileName, fmOpenRead or fmShareDenyWrite);
try
n := Strm.Size;
SetLength(txt, n);
Strm.Read(txt[1], n);
finally
Strm.Free;
end;
Result := txt;
end;
// Read values from a csv string
function TForm2.ReadCSVCell(iText: String; ARow, ACol: Integer): String;
// Uses csvdocument //ReadCSVCell
var
CSVDoc:TCSVDocument;
begin
CSVDoc := TCSVDocument.Create;
try
CSVDoc.Delimiter := ';';
CSVDoc.LoadFromFile('Myfile.CSV');
CSVDoc.CSVText:=iText;
Result := CSVDoc.Cells[ACol, ARow];
finally
CSVDoc.Free;
end;
end;
*)
end.