sysformt.inc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. Var ChPos,OldPos,ArgPos,DoArg,Len : SizeInt;
  2. Hs,ToAdd : TFormatString;
  3. Index : SizeInt;
  4. Width,Prec : Longint;
  5. Left : Boolean;
  6. Fchar : char;
  7. vq : qword;
  8. {
  9. ReadFormat reads the format string. It returns the type character in
  10. uppercase, and sets index, Width, Prec to their correct values,
  11. or -1 if not set. It sets Left to true if left alignment was requested.
  12. In case of an error, DoFormatError is called.
  13. }
  14. Function ReadFormat : Char;
  15. Var Value : longint;
  16. Procedure ReadInteger;
  17. var Code: Word;
  18. begin
  19. If Value<>-1 then exit; // Was already read.
  20. OldPos:=ChPos;
  21. While (ChPos<=Len) and
  22. (Fmt[ChPos]<='9') and (Fmt[ChPos]>='0') do inc(ChPos);
  23. If ChPos>len then
  24. DoFormatError(feInvalidFormat);
  25. If Fmt[ChPos]='*' then
  26. begin
  27. If (ChPos>OldPos) or (ArgPos>High(Args)) then
  28. DoFormatError(feInvalidFormat);
  29. case Args[ArgPos].Vtype of
  30. vtInteger: Value := Args[ArgPos].VInteger;
  31. vtInt64: Value := Args[ArgPos].VInt64^;
  32. vtQWord: Value := Args[ArgPos].VQWord^;
  33. else
  34. DoFormatError(feInvalidFormat);
  35. end;
  36. Inc(ArgPos);
  37. Inc(ChPos);
  38. end
  39. else
  40. begin
  41. If (OldPos<ChPos) Then
  42. begin
  43. Val (Copy(Fmt,OldPos,ChPos-OldPos),value,code);
  44. // This should never happen !!
  45. If Code>0 then DoFormatError (feInvalidFormat);
  46. end
  47. else
  48. Value:=-1;
  49. end;
  50. end;
  51. Procedure ReadIndex;
  52. begin
  53. If Fmt[ChPos]<>':' then
  54. ReadInteger
  55. else
  56. value:=0; // Delphi undocumented behaviour, assume 0, #11099
  57. If Fmt[ChPos]=':' then
  58. begin
  59. If Value=-1 then DoFormatError(feMissingArgument);
  60. Index:=Value;
  61. Value:=-1;
  62. Inc(ChPos);
  63. end;
  64. {$ifdef fmtdebug}
  65. Log ('Read index');
  66. {$endif}
  67. end;
  68. Procedure ReadLeft;
  69. begin
  70. If Fmt[ChPos]='-' then
  71. begin
  72. left:=True;
  73. Inc(ChPos);
  74. end
  75. else
  76. Left:=False;
  77. {$ifdef fmtdebug}
  78. Log ('Read Left');
  79. {$endif}
  80. end;
  81. Procedure ReadWidth;
  82. begin
  83. ReadInteger;
  84. If Value<>-1 then
  85. begin
  86. Width:=Value;
  87. Value:=-1;
  88. end;
  89. {$ifdef fmtdebug}
  90. Log ('Read width');
  91. {$endif}
  92. end;
  93. Procedure ReadPrec;
  94. begin
  95. If Fmt[ChPos]='.' then
  96. begin
  97. inc(ChPos);
  98. ReadInteger;
  99. If Value=-1 then
  100. Value:=0;
  101. prec:=Value;
  102. end;
  103. {$ifdef fmtdebug}
  104. Log ('Read precision');
  105. {$endif}
  106. end;
  107. {$ifdef INWIDEFORMAT}
  108. var
  109. FormatChar : TFormatChar;
  110. {$endif INWIDEFORMAT}
  111. begin
  112. {$ifdef fmtdebug}
  113. Log ('Start format');
  114. {$endif}
  115. Index:=-1;
  116. Width:=-1;
  117. Prec:=-1;
  118. Value:=-1;
  119. inc(ChPos);
  120. If Fmt[ChPos]='%' then
  121. begin
  122. Result:='%';
  123. exit; // VP fix
  124. end;
  125. ReadIndex;
  126. ReadLeft;
  127. ReadWidth;
  128. ReadPrec;
  129. {$ifdef INWIDEFORMAT}
  130. {$ifdef VER2_2}
  131. FormatChar:=UpCase(Fmt[ChPos])[1];
  132. {$else VER2_2}
  133. FormatChar:=UpCase(UnicodeChar(Fmt[ChPos]));
  134. {$endif VER2_2}
  135. if word(FormatChar)>255 then
  136. ReadFormat:=#255
  137. else
  138. ReadFormat:=FormatChar;
  139. {$else INWIDEFORMAT}
  140. ReadFormat:=Upcase(Fmt[ChPos]);
  141. {$endif INWIDEFORMAT}
  142. {$ifdef fmtdebug}
  143. Log ('End format');
  144. {$endif}
  145. end;
  146. {$ifdef fmtdebug}
  147. Procedure DumpFormat (C : char);
  148. begin
  149. Write ('Fmt : ',fmt:10);
  150. Write (' Index : ',Index:3);
  151. Write (' Left : ',left:5);
  152. Write (' Width : ',Width:3);
  153. Write (' Prec : ',prec:3);
  154. Writeln (' Type : ',C);
  155. end;
  156. {$endif}
  157. function Checkarg (AT : SizeInt;err:boolean):boolean;
  158. {
  159. Check if argument INDEX is of correct type (AT)
  160. If Index=-1, ArgPos is used, and argpos is augmented with 1
  161. DoArg is set to the argument that must be used.
  162. }
  163. begin
  164. result:=false;
  165. if Index=-1 then
  166. DoArg:=Argpos
  167. else
  168. DoArg:=Index;
  169. ArgPos:=DoArg+1;
  170. If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
  171. begin
  172. if err then
  173. DoFormatError(feInvalidArgindex);
  174. dec(ArgPos);
  175. exit;
  176. end;
  177. result:=true;
  178. end;
  179. Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
  180. begin
  181. Result:='';
  182. Len:=Length(Fmt);
  183. ChPos:=1;
  184. OldPos:=1;
  185. ArgPos:=0;
  186. While ChPos<=len do
  187. begin
  188. While (ChPos<=Len) and (Fmt[ChPos]<>'%') do
  189. inc(ChPos);
  190. If ChPos>OldPos Then
  191. Result:=Result+Copy(Fmt,OldPos,ChPos-Oldpos);
  192. If ChPos<Len then
  193. begin
  194. FChar:=ReadFormat;
  195. {$ifdef fmtdebug}
  196. DumpFormat(FCHar);
  197. {$endif}
  198. Case FChar of
  199. 'D' : begin
  200. if Checkarg(vtinteger,false) then
  201. Str(Args[Doarg].VInteger,ToAdd)
  202. else if CheckArg(vtInt64,false) then
  203. Str(Args[DoArg].VInt64^,toadd)
  204. else if CheckArg(vtQWord,true) then
  205. Str(int64(Args[DoArg].VQWord^),toadd);
  206. Width:=Abs(width);
  207. Index:=Prec-Length(ToAdd);
  208. If ToAdd[1]<>'-' then
  209. ToAdd:=StringOfChar('0',Index)+ToAdd
  210. else
  211. // + 1 to accomodate for - sign in length !!
  212. Insert(StringOfChar('0',Index+1),toadd,2);
  213. end;
  214. 'U' : begin
  215. if Checkarg(vtinteger,false) then
  216. Str(cardinal(Args[Doarg].VInteger),ToAdd)
  217. else if CheckArg(vtInt64,false) then
  218. Str(qword(Args[DoArg].VInt64^),toadd)
  219. else if CheckArg(vtQWord,true) then
  220. Str(Args[DoArg].VQWord^,toadd);
  221. Width:=Abs(width);
  222. Index:=Prec-Length(ToAdd);
  223. ToAdd:=StringOfChar('0',Index)+ToAdd
  224. end;
  225. {$ifndef FPUNONE}
  226. 'E' : begin
  227. if CheckArg(vtCurrency,false) then
  228. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffexponent,Prec,3,FormatSettings)
  229. else if CheckArg(vtExtended,true) then
  230. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3,FormatSettings);
  231. end;
  232. 'F' : begin
  233. if CheckArg(vtCurrency,false) then
  234. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffFixed,9999,Prec,FormatSettings)
  235. else if CheckArg(vtExtended,true) then
  236. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec,FormatSettings);
  237. end;
  238. 'G' : begin
  239. if CheckArg(vtCurrency,false) then
  240. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffGeneral,Prec,3,FormatSettings)
  241. else if CheckArg(vtExtended,true) then
  242. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3,FormatSettings);
  243. end;
  244. 'N' : begin
  245. if CheckArg(vtCurrency,false) then
  246. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffNumber,9999,Prec,FormatSettings)
  247. else if CheckArg(vtExtended,true) then
  248. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec,FormatSettings);
  249. end;
  250. 'M' : begin
  251. if CheckArg(vtExtended,false) then
  252. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffCurrency,9999,Prec,FormatSettings)
  253. else if CheckArg(vtCurrency,true) then
  254. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffCurrency,9999,Prec,FormatSettings);
  255. end;
  256. {$else}
  257. 'E','F','G','N','M':
  258. RunError(207);
  259. {$endif}
  260. 'S' : begin
  261. if CheckArg(vtString,false) then
  262. hs:=Args[doarg].VString^
  263. else
  264. if CheckArg(vtChar,false) then
  265. hs:=Args[doarg].VChar
  266. else
  267. if CheckArg(vtPChar,false) then
  268. hs:=Args[doarg].VPChar
  269. else
  270. if CheckArg(vtPWideChar,false) then
  271. hs:=WideString(Args[doarg].VPWideChar)
  272. else
  273. if CheckArg(vtWideChar,false) then
  274. hs:=WideString(Args[doarg].VWideChar)
  275. else
  276. if CheckArg(vtWidestring,false) then
  277. hs:=WideString(Args[doarg].VWideString)
  278. else
  279. if CheckArg(vtAnsiString,true) then
  280. hs:=ansistring(Args[doarg].VAnsiString);
  281. Index:=Length(hs);
  282. If (Prec<>-1) and (Index>Prec) then
  283. Index:=Prec;
  284. ToAdd:=Copy(hs,1,Index);
  285. end;
  286. 'P' : Begin
  287. CheckArg(vtpointer,true);
  288. ToAdd:=HexStr(ptruint(Args[DoArg].VPointer),sizeof(Ptruint)*2);
  289. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  290. // Insert(':',ToAdd,5);
  291. end;
  292. 'X' : begin
  293. if Checkarg(vtinteger,false) then
  294. begin
  295. vq:=Cardinal(Args[Doarg].VInteger);
  296. index:=16;
  297. end
  298. else
  299. if CheckArg(vtQWord, false) then
  300. begin
  301. vq:=Qword(Args[DoArg].VQWord^);
  302. index:=31;
  303. end
  304. else
  305. begin
  306. CheckArg(vtInt64,true);
  307. vq:=Qword(Args[DoArg].VInt64^);
  308. index:=31;
  309. end;
  310. If Prec>index then
  311. ToAdd:=HexStr(int64(vq),index)
  312. else
  313. begin
  314. // determine minimum needed number of hex digits.
  315. Index:=1;
  316. While (qWord(1) shl (Index*4)<=vq) and (index<16) do
  317. inc(Index);
  318. If Index>Prec then
  319. Prec:=Index;
  320. ToAdd:=HexStr(int64(vq),Prec);
  321. end;
  322. end;
  323. '%': ToAdd:='%';
  324. end;
  325. If Width<>-1 then
  326. If Length(ToAdd)<Width then
  327. If not Left then
  328. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  329. else
  330. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  331. Result:=Result+ToAdd;
  332. end;
  333. inc(ChPos);
  334. Oldpos:=ChPos;
  335. end;
  336. end;