sysformt.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. CheckArg(vtExtended,true);
  213. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3);
  214. end;
  215. 'F' : begin
  216. CheckArg(vtExtended,true);
  217. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec);
  218. end;
  219. 'G' : begin
  220. CheckArg(vtExtended,true);
  221. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3);
  222. end;
  223. 'N' : begin
  224. CheckArg(vtExtended,true);
  225. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec);
  226. end;
  227. 'M' : begin
  228. CheckArg(vtExtended,true);
  229. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffCurrency,9999,Prec);
  230. end;
  231. 'S' : begin
  232. if CheckArg(vtString,false) then
  233. hs:=Args[doarg].VString^
  234. else
  235. if CheckArg(vtChar,false) then
  236. hs:=Args[doarg].VChar
  237. else
  238. if CheckArg(vtPChar,false) then
  239. hs:=Args[doarg].VPChar
  240. else
  241. if CheckArg(vtPWideChar,false) then
  242. hs:=WideString(Args[doarg].VPWideChar)
  243. else
  244. if CheckArg(vtWideChar,false) then
  245. hs:=WideString(Args[doarg].VWideChar)
  246. else
  247. if CheckArg(vtWidestring,false) then
  248. hs:=WideString(Args[doarg].VWideString)
  249. else
  250. if CheckArg(vtAnsiString,true) then
  251. hs:=ansistring(Args[doarg].VAnsiString);
  252. Index:=Length(hs);
  253. If (Prec<>-1) and (Index>Prec) then
  254. Index:=Prec;
  255. ToAdd:=Copy(hs,1,Index);
  256. end;
  257. 'P' : Begin
  258. CheckArg(vtpointer,true);
  259. ToAdd:=HexStr(ptrint(Args[DoArg].VPointer),sizeof(Ptrint)*2);
  260. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  261. // Insert(':',ToAdd,5);
  262. end;
  263. 'X' : begin
  264. if Checkarg(vtinteger,false) then
  265. begin
  266. vq:=Cardinal(Args[Doarg].VInteger);
  267. index:=16;
  268. end
  269. else
  270. begin
  271. CheckArg(vtInt64,true);
  272. vq:=Qword(Args[DoArg].VInt64^);
  273. index:=31;
  274. end;
  275. If Prec>index then
  276. ToAdd:=HexStr(vq,index)
  277. else
  278. begin
  279. // determine minimum needed number of hex digits.
  280. Index:=1;
  281. While (qWord(1) shl (Index*4)<=vq) and (index<16) do
  282. inc(Index);
  283. If Index>Prec then
  284. Prec:=Index;
  285. ToAdd:=HexStr(vq,Prec);
  286. end;
  287. end;
  288. '%': ToAdd:='%';
  289. end;
  290. If Width<>-1 then
  291. If Length(ToAdd)<Width then
  292. If not Left then
  293. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  294. else
  295. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  296. Result:=Result+ToAdd;
  297. end;
  298. inc(chpos);
  299. Oldpos:=chpos;
  300. end;
  301. end;