sysformt.inc 9.0 KB

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