Browse Source

* Small fixes and demo for TStrings.LoadFromURL

michael 5 years ago
parent
commit
a0a0016f99

+ 1 - 1
demo/rtl/demoloadstreamfromurl.html

@@ -3,7 +3,7 @@
 <head>
 <head>
   <meta http-equiv="Content-type" content="text/html; charset=utf-8">
   <meta http-equiv="Content-type" content="text/html; charset=utf-8">
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <meta name="viewport" content="width=device-width, initial-scale=1">
-  <title>scratch</title>
+  <title>Load stream from URL demo</title>
   <script src="demoloadstreamfromurl.js"></script>
   <script src="demoloadstreamfromurl.js"></script>
 </head>
 </head>
 <body>
 <body>

+ 11 - 19
demo/rtl/demoloadstreamfromurl.lpr

@@ -6,7 +6,7 @@ uses
   browserconsole, Classes;
   browserconsole, Classes;
 
 
 Var
 Var
-  SS : TStringStream;
+  SS,SS2,SS3 : TStringStream;
 
 
 begin
 begin
   Writeln('Loading synchronously');
   Writeln('Loading synchronously');
@@ -18,30 +18,22 @@ begin
     SS.Free;
     SS.Free;
   end;
   end;
   Writeln('Loading asynchronously');
   Writeln('Loading asynchronously');
-  SS:=TStringStream.Create('');
-  try
-    SS.LoadFromURL('bytes.txt',False,procedure(Sender: tobject)
+  SS2:=TStringStream.Create('');
+  SS2.LoadFromURL('bytes.txt',True,procedure(Sender: tobject)
     begin
     begin
-    Writeln('Loaded 2: ',SS.DataString);
+      Writeln('Loaded 2: ',SS2.DataString);
     end
     end
-    )
-  finally
-    SS.Free;
-  end;
+    );
   Writeln('Loading non-existing file');
   Writeln('Loading non-existing file');
-  SS:=TStringStream.Create('');
-  try
-    SS.LoadFromURL('bytesnonexist.txt',False,procedure(Sender: tobject)
+  SS3:=TStringStream.Create('');
+  SS3.LoadFromURL('bytesnonexist.txt',True,procedure(Sender: tobject)
     begin
     begin
-    Writeln('Loaded 3: ',SS.DataString);
+      Writeln('Loaded 3: ',SS3.DataString);
     end
     end
-    ,
+  ,
     procedure(Sender: tobject; Const aError : string)
     procedure(Sender: tobject; Const aError : string)
     begin
     begin
-    Writeln('Load error: ',aError);
+      Writeln('Load error: ',aError);
     end
     end
-    )
-  finally
-    SS.Free;
-  end;
+  );
 end.
 end.

+ 19 - 0
demo/rtl/demoloadstringsfromurl.html

@@ -0,0 +1,19 @@
+<!doctype html>
+<html lang="en">
+<head>
+  <meta http-equiv="Content-type" content="text/html; charset=utf-8">
+  <meta name="viewport" content="width=device-width, initial-scale=1">
+  <title>Demo load strings from URL</title>
+  <script src="demoloadstringsfromurl.js"></script>
+</head>
+<body>
+  <h1>Demo loading file from URL to TStringList</h1>
+  <div id="pasjsconsole">
+  </div>
+  <script>
+  rtl.run();
+
+  </script>
+  
+</body>
+</html>

+ 88 - 0
demo/rtl/demoloadstringsfromurl.lpi

@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+  <ProjectOptions>
+    <Version Value="12"/>
+    <General>
+      <Flags>
+        <MainUnitHasCreateFormStatements Value="False"/>
+        <MainUnitHasTitleStatement Value="False"/>
+        <MainUnitHasScaledStatement Value="False"/>
+        <Runnable Value="False"/>
+      </Flags>
+      <SessionStorage Value="InProjectDir"/>
+      <Title Value="demoloadstringsfromurl"/>
+      <UseAppBundle Value="False"/>
+      <ResourceType Value="res"/>
+    </General>
+    <CustomData Count="2">
+      <Item0 Name="MaintainHTML" Value="1"/>
+      <Item1 Name="PasJSWebBrowserProject" Value="1"/>
+    </CustomData>
+    <BuildModes>
+      <Item Name="Default" Default="True"/>
+    </BuildModes>
+    <PublishOptions>
+      <Version Value="2"/>
+      <UseFileFilters Value="True"/>
+    </PublishOptions>
+    <RunParams>
+      <FormatVersion Value="2"/>
+    </RunParams>
+    <Units>
+      <Unit>
+        <Filename Value="demoloadstringsfromurl.lpr"/>
+        <IsPartOfProject Value="True"/>
+      </Unit>
+      <Unit>
+        <Filename Value="demoloadstringsfromurl.html"/>
+        <IsPartOfProject Value="True"/>
+        <CustomData Count="1">
+          <Item0 Name="PasJSIsProjectHTMLFile" Value="1"/>
+        </CustomData>
+      </Unit>
+    </Units>
+  </ProjectOptions>
+  <CompilerOptions>
+    <Version Value="11"/>
+    <Target FileExt=".js">
+      <Filename Value="demoloadstringsfromurl"/>
+    </Target>
+    <SearchPaths>
+      <IncludeFiles Value="$(ProjOutDir)"/>
+      <UnitOutputDirectory Value="js"/>
+    </SearchPaths>
+    <Parsing>
+      <SyntaxOptions>
+        <AllowLabel Value="False"/>
+        <CPPInline Value="False"/>
+        <UseAnsiStrings Value="False"/>
+      </SyntaxOptions>
+    </Parsing>
+    <CodeGeneration>
+      <TargetOS Value="browser"/>
+    </CodeGeneration>
+    <Linking>
+      <Debugging>
+        <GenerateDebugInfo Value="False"/>
+        <UseLineInfoUnit Value="False"/>
+      </Debugging>
+    </Linking>
+    <Other>
+      <CustomOptions Value="-Jeutf-8 -Jirtl.js -Jc -Jminclude"/>
+      <CompilerPath Value="$(pas2js)"/>
+    </Other>
+  </CompilerOptions>
+  <Debugging>
+    <Exceptions>
+      <Item>
+        <Name Value="EAbort"/>
+      </Item>
+      <Item>
+        <Name Value="ECodetoolError"/>
+      </Item>
+      <Item>
+        <Name Value="EFOpenError"/>
+      </Item>
+    </Exceptions>
+  </Debugging>
+</CONFIG>

+ 53 - 0
demo/rtl/demoloadstringsfromurl.lpr

@@ -0,0 +1,53 @@
+program demoloadstringsfromurl;
+
+{$mode objfpc}
+
+uses
+  browserconsole, Classes;
+
+Procedure DumpList(s : Tstrings);
+
+Var
+  I : Integer;
+
+begin
+  Writeln('----------');
+  For I:=0 to S.Count-1 do
+    Writeln('Line[',I,'] : ',S[I]);
+  Writeln('----------');
+end;
+
+Var
+  Lst,lst2,lst3 : TStrings;
+
+begin
+  Writeln('Loading synchronously');
+  Lst:=TStringList.Create;
+  try
+    Lst.LoadFromFile('bytes.txt');
+    DumpList(Lst);
+  finally
+    Lst.Free;
+  end;
+  Writeln('Loading asynchronously');
+  // We can't free the stringlist, because of the async nature
+  Lst2:=TStringList.Create;
+  Lst2.LoadFromURL('bytes.txt',True,procedure(Sender: tobject)
+    begin
+    DumpList(Lst2);
+    end
+  );
+  Writeln('Loading non-existing file');
+  // We can't free the stringlist, because of the async nature
+  Lst3:=TStringList.Create;
+  Lst3.LoadFromURL('bytesnonexist.txt',True,procedure(Sender: tobject)
+  begin
+    DumpList(Lst3);
+  end
+  ,
+  procedure(Sender: tobject; Const aError : string)
+  begin
+  Writeln('Load error: ',aError);
+  end
+  );
+end.