sysformt.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. (Pos(Fmt[chpos],'1234567890')<>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. FormatChar:=UpCase(Fmt[ChPos])[1];
  131. if word(FormatChar)>255 then
  132. ReadFormat:=#255
  133. else
  134. ReadFormat:=FormatChar;
  135. {$else INWIDEFORMAT}
  136. ReadFormat:=Upcase(Fmt[ChPos]);
  137. {$endif INWIDEFORMAT}
  138. {$ifdef fmtdebug}
  139. Log ('End format');
  140. {$endif}
  141. end;
  142. {$ifdef fmtdebug}
  143. Procedure DumpFormat (C : char);
  144. begin
  145. Write ('Fmt : ',fmt:10);
  146. Write (' Index : ',Index:3);
  147. Write (' Left : ',left:5);
  148. Write (' Width : ',Width:3);
  149. Write (' Prec : ',prec:3);
  150. Writeln (' Type : ',C);
  151. end;
  152. {$endif}
  153. function Checkarg (AT : SizeInt;err:boolean):boolean;
  154. {
  155. Check if argument INDEX is of correct type (AT)
  156. If Index=-1, ArgPos is used, and argpos is augmented with 1
  157. DoArg is set to the argument that must be used.
  158. }
  159. begin
  160. result:=false;
  161. if Index=-1 then
  162. DoArg:=Argpos
  163. else
  164. DoArg:=Index;
  165. ArgPos:=DoArg+1;
  166. If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
  167. begin
  168. if err then
  169. DoFormatError(feInvalidArgindex);
  170. dec(ArgPos);
  171. exit;
  172. end;
  173. result:=true;
  174. end;
  175. Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
  176. begin
  177. Result:='';
  178. Len:=Length(Fmt);
  179. Chpos:=1;
  180. OldPos:=1;
  181. ArgPos:=0;
  182. While chpos<=len do
  183. begin
  184. While (ChPos<=Len) and (Fmt[chpos]<>'%') do
  185. inc(chpos);
  186. If ChPos>OldPos Then
  187. Result:=Result+Copy(Fmt,OldPos,Chpos-Oldpos);
  188. If ChPos<Len then
  189. begin
  190. FChar:=ReadFormat;
  191. {$ifdef fmtdebug}
  192. DumpFormat(FCHar);
  193. {$endif}
  194. Case FChar of
  195. 'D' : begin
  196. if Checkarg(vtinteger,false) then
  197. Str(Args[Doarg].VInteger,ToAdd)
  198. else if CheckArg(vtInt64,false) then
  199. Str(Args[DoArg].VInt64^,toadd)
  200. else if CheckArg(vtQWord,true) then
  201. Str(int64(Args[DoArg].VQWord^),toadd);
  202. Width:=Abs(width);
  203. Index:=Prec-Length(ToAdd);
  204. If ToAdd[1]<>'-' then
  205. ToAdd:=StringOfChar('0',Index)+ToAdd
  206. else
  207. // + 1 to accomodate for - sign in length !!
  208. Insert(StringOfChar('0',Index+1),toadd,2);
  209. end;
  210. 'U' : begin
  211. if Checkarg(vtinteger,false) then
  212. Str(cardinal(Args[Doarg].VInteger),ToAdd)
  213. else if CheckArg(vtInt64,false) then
  214. Str(qword(Args[DoArg].VInt64^),toadd)
  215. else if CheckArg(vtQWord,true) then
  216. Str(Args[DoArg].VQWord^,toadd);
  217. Width:=Abs(width);
  218. Index:=Prec-Length(ToAdd);
  219. ToAdd:=StringOfChar('0',Index)+ToAdd
  220. end;
  221. {$ifndef FPUNONE}
  222. 'E' : begin
  223. if CheckArg(vtCurrency,false) then
  224. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffexponent,Prec,3,FormatSettings)
  225. else if CheckArg(vtExtended,true) then
  226. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3,FormatSettings);
  227. end;
  228. 'F' : begin
  229. if CheckArg(vtCurrency,false) then
  230. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffFixed,9999,Prec,FormatSettings)
  231. else if CheckArg(vtExtended,true) then
  232. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec,FormatSettings);
  233. end;
  234. 'G' : begin
  235. if CheckArg(vtCurrency,false) then
  236. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffGeneral,Prec,3,FormatSettings)
  237. else if CheckArg(vtExtended,true) then
  238. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3,FormatSettings);
  239. end;
  240. 'N' : begin
  241. if CheckArg(vtCurrency,false) then
  242. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffNumber,9999,Prec,FormatSettings)
  243. else if CheckArg(vtExtended,true) then
  244. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec,FormatSettings);
  245. end;
  246. 'M' : begin
  247. if CheckArg(vtExtended,false) then
  248. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffCurrency,9999,Prec,FormatSettings)
  249. else if CheckArg(vtCurrency,true) then
  250. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffCurrency,9999,Prec,FormatSettings);
  251. end;
  252. {$else}
  253. 'E','F','G','N','M':
  254. RunError(207);
  255. {$endif}
  256. 'S' : begin
  257. if CheckArg(vtString,false) then
  258. hs:=Args[doarg].VString^
  259. else
  260. if CheckArg(vtChar,false) then
  261. hs:=Args[doarg].VChar
  262. else
  263. if CheckArg(vtPChar,false) then
  264. hs:=Args[doarg].VPChar
  265. else
  266. if CheckArg(vtPWideChar,false) then
  267. hs:=WideString(Args[doarg].VPWideChar)
  268. else
  269. if CheckArg(vtWideChar,false) then
  270. hs:=WideString(Args[doarg].VWideChar)
  271. else
  272. if CheckArg(vtWidestring,false) then
  273. hs:=WideString(Args[doarg].VWideString)
  274. else
  275. if CheckArg(vtAnsiString,true) then
  276. hs:=ansistring(Args[doarg].VAnsiString);
  277. Index:=Length(hs);
  278. If (Prec<>-1) and (Index>Prec) then
  279. Index:=Prec;
  280. ToAdd:=Copy(hs,1,Index);
  281. end;
  282. 'P' : Begin
  283. CheckArg(vtpointer,true);
  284. ToAdd:=HexStr(ptruint(Args[DoArg].VPointer),sizeof(Ptruint)*2);
  285. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  286. // Insert(':',ToAdd,5);
  287. end;
  288. 'X' : begin
  289. if Checkarg(vtinteger,false) then
  290. begin
  291. vq:=Cardinal(Args[Doarg].VInteger);
  292. index:=16;
  293. end
  294. else
  295. if CheckArg(vtQWord, false) then
  296. begin
  297. vq:=Qword(Args[DoArg].VQWord^);
  298. index:=31;
  299. end
  300. else
  301. begin
  302. CheckArg(vtInt64,true);
  303. vq:=Qword(Args[DoArg].VInt64^);
  304. index:=31;
  305. end;
  306. If Prec>index then
  307. ToAdd:=HexStr(int64(vq),index)
  308. else
  309. begin
  310. // determine minimum needed number of hex digits.
  311. Index:=1;
  312. While (qWord(1) shl (Index*4)<=vq) and (index<16) do
  313. inc(Index);
  314. If Index>Prec then
  315. Prec:=Index;
  316. ToAdd:=HexStr(int64(vq),Prec);
  317. end;
  318. end;
  319. '%': ToAdd:='%';
  320. end;
  321. If Width<>-1 then
  322. If Length(ToAdd)<Width then
  323. If not Left then
  324. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  325. else
  326. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  327. Result:=Result+ToAdd;
  328. end;
  329. inc(chpos);
  330. Oldpos:=chpos;
  331. end;
  332. end;