Vincent Snijders wrote:
Hi,

I want to covert a string to qword, but strtoqword seems to be missing?

Attached is a patch to implement StrToQWord and related functions and a test 
program.

Vincent.
Index: objpas/sysutils/sysstr.inc
===================================================================
--- objpas/sysutils/sysstr.inc  (revision 2116)
+++ objpas/sysutils/sysstr.inc  (working copy)
@@ -773,7 +773,6 @@
 
 function StrToInt64(const S: string): int64;
 var Error: word;
-
 begin
   Val(S, result, Error);
   if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
@@ -788,28 +787,51 @@
 end;
 
 
+function StrToQWord(const s: string): QWord;
+var Error: word;
+begin
+  Val(S, result, Error);
+  if Error <> 0 then raise EConvertError.createfmt(SInvalidInteger,[S]);
+end;
 
+
+function TryStrQWord(const s: string; var i: QWord): boolean;
+var Error : word;
+begin
+  Val(s, i, Error);
+  TryStrQWord:=Error=0
+end;
+
 {   StrToIntDef converts the string S to an integer value,
     Default is returned in case S does not represent a valid integer value  }
 
 function StrToIntDef(const S: string; Default: integer): integer;
 var Error: word;
 begin
-Val(S, result, Error);
-if Error <> 0 then result := Default;
+  Val(S, result, Error);
+  if Error <> 0 then result := Default;
 end ;
 
-{   StrToIntDef converts the string S to an integer value,
-    Default is returned in case S does not represent a valid integer value  }
+{   StrToInt64Def converts the string S to an int64 value,
+    Default is returned in case S does not represent a valid int64 value  }
 
 function StrToInt64Def(const S: string; Default: int64): int64;
 var Error: word;
 begin
-Val(S, result, Error);
-if Error <> 0 then result := Default;
+  Val(S, result, Error);
+  if Error <> 0 then result := Default;
 end ;
 
+{   StrToQWordDef converts the string S to an QWord value,
+    Default is returned in case S does not represent a valid QWord value  }
 
+function StrToQWordDef(const S: string; Default: QWord): QWord;
+var Error: word;
+begin
+  Val(S, result, Error);
+  if Error <> 0 then result := Default;
+end;
+
 {   LoadStr returns the string resource Ident.   }
 
 function LoadStr(Ident: integer): string;
Index: objpas/sysutils/sysstrh.inc
===================================================================
--- objpas/sysutils/sysstrh.inc (revision 2116)
+++ objpas/sysutils/sysstrh.inc (working copy)
@@ -107,8 +107,11 @@
 function TryStrToInt(const s: string; var i : integer) : boolean;
 function StrToInt64(const s: string): int64;
 function TryStrToInt64(const s: string; var i : int64) : boolean;
+function StrToQWord(const s: string): QWord;
+function TryStrQWord(const s: string; var i : QWord) : boolean;
 function StrToIntDef(const S: string; Default: integer): integer;
 function StrToInt64Def(const S: string; Default: int64): int64;
+function StrToQWordDef(const S: string; Default: QWord): QWord;
 function LoadStr(Ident: integer): string;
 // function FmtLoadStr(Ident: integer; const Args: array of const): string;
 Function Format (Const Fmt : String; const Args : Array of const) : String;
program TestStrToQWord;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils
  { add your units here };
var
  s: string;
  q: qword;
  error: integer;
  
procedure print(const s: string; valid: boolean);
var
  error: integer;
  q: qword;
begin
  writeln('S: ', s);
  if valid then
    writeln(StrToQWord(s));
  writeln('StrToQWordDef: ', StrToQWordDef(s,0));
  if TryStrQWord(s,q) then
    writeln('TryStrQWord: ', q)
  else
    writeln('Invalid qword');
end;

begin
  print(IntToStr(High(QWord)), true);
  writeln;
  print('-10', false);
  readln;
end.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to