sysformt.inc 8.5 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. {$IFDEF VIRTUALPASCAL}
  18. var Code: longint;
  19. {$ELSE}
  20. var Code: word;
  21. {$ENDIF}
  22. begin
  23. If Value<>-1 then exit; // Was already read.
  24. OldPos:=chPos;
  25. While (Chpos<=Len) and
  26. (Pos(Fmt[chpos],'1234567890')<>0) do inc(chpos);
  27. If Chpos>len then
  28. DoFormatError(feInvalidFormat);
  29. If Fmt[Chpos]='*' then
  30. begin
  31. If (Chpos>OldPos) or (ArgPos>High(Args))
  32. or (Args[ArgPos].Vtype<>vtInteger) then
  33. DoFormatError(feInvalidFormat);
  34. Value:=Args[ArgPos].VInteger;
  35. Inc(ArgPos);
  36. Inc(chPos);
  37. end
  38. else
  39. begin
  40. If (OldPos<chPos) Then
  41. begin
  42. Val (Copy(Fmt,OldPos,ChPos-OldPos),value,code);
  43. // This should never happen !!
  44. If Code>0 then DoFormatError (feInvalidFormat);
  45. end
  46. else
  47. Value:=-1;
  48. end;
  49. end;
  50. Procedure ReadIndex;
  51. begin
  52. ReadInteger;
  53. If Fmt[ChPos]=':' then
  54. begin
  55. If Value=-1 then DoFormatError(feMissingArgument);
  56. Index:=Value;
  57. Value:=-1;
  58. Inc(Chpos);
  59. end;
  60. {$ifdef fmtdebug}
  61. Log ('Read index');
  62. {$endif}
  63. end;
  64. Procedure ReadLeft;
  65. begin
  66. If Fmt[chpos]='-' then
  67. begin
  68. left:=True;
  69. Inc(chpos);
  70. end
  71. else
  72. Left:=False;
  73. {$ifdef fmtdebug}
  74. Log ('Read Left');
  75. {$endif}
  76. end;
  77. Procedure ReadWidth;
  78. begin
  79. ReadInteger;
  80. If Value<>-1 then
  81. begin
  82. Width:=Value;
  83. Value:=-1;
  84. end;
  85. {$ifdef fmtdebug}
  86. Log ('Read width');
  87. {$endif}
  88. end;
  89. Procedure ReadPrec;
  90. begin
  91. If Fmt[chpos]='.' then
  92. begin
  93. inc(chpos);
  94. ReadInteger;
  95. If Value=-1 then
  96. Value:=0;
  97. prec:=Value;
  98. end;
  99. {$ifdef fmtdebug}
  100. Log ('Read precision');
  101. {$endif}
  102. end;
  103. {$ifdef INWIDEFORMAT}
  104. var
  105. FormatChar : TFormatChar;
  106. {$endif INWIDEFORMAT}
  107. begin
  108. {$ifdef fmtdebug}
  109. Log ('Start format');
  110. {$endif}
  111. Index:=-1;
  112. Width:=-1;
  113. Prec:=-1;
  114. Value:=-1;
  115. inc(chpos);
  116. If Fmt[Chpos]='%' then
  117. begin
  118. Result:='%';
  119. exit; // VP fix
  120. end;
  121. ReadIndex;
  122. ReadLeft;
  123. ReadWidth;
  124. ReadPrec;
  125. {$ifdef INWIDEFORMAT}
  126. FormatChar:=UpCase(Fmt[ChPos])[1];
  127. if word(FormatChar)>255 then
  128. ReadFormat:=#255
  129. else
  130. ReadFormat:=FormatChar;
  131. {$else INWIDEFORMAT}
  132. ReadFormat:=Upcase(Fmt[ChPos]);
  133. {$endif INWIDEFORMAT}
  134. {$ifdef fmtdebug}
  135. Log ('End format');
  136. {$endif}
  137. end;
  138. {$ifdef fmtdebug}
  139. Procedure DumpFormat (C : char);
  140. begin
  141. Write ('Fmt : ',fmt:10);
  142. Write (' Index : ',Index:3);
  143. Write (' Left : ',left:5);
  144. Write (' Width : ',Width:3);
  145. Write (' Prec : ',prec:3);
  146. Writeln (' Type : ',C);
  147. end;
  148. {$endif}
  149. function Checkarg (AT : SizeInt;err:boolean):boolean;
  150. {
  151. Check if argument INDEX is of correct type (AT)
  152. If Index=-1, ArgPos is used, and argpos is augmented with 1
  153. DoArg is set to the argument that must be used.
  154. }
  155. begin
  156. result:=false;
  157. if Index=-1 then
  158. DoArg:=Argpos
  159. else
  160. DoArg:=Index;
  161. ArgPos:=DoArg+1;
  162. If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
  163. begin
  164. if err then
  165. DoFormatError(feInvalidArgindex);
  166. dec(ArgPos);
  167. exit;
  168. end;
  169. result:=true;
  170. end;
  171. Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
  172. begin
  173. Result:='';
  174. Len:=Length(Fmt);
  175. Chpos:=1;
  176. OldPos:=1;
  177. ArgPos:=0;
  178. While chpos<=len do
  179. begin
  180. While (ChPos<=Len) and (Fmt[chpos]<>'%') do
  181. inc(chpos);
  182. If ChPos>OldPos Then
  183. Result:=Result+Copy(Fmt,OldPos,Chpos-Oldpos);
  184. If ChPos<Len then
  185. begin
  186. FChar:=ReadFormat;
  187. {$ifdef fmtdebug}
  188. DumpFormat(FCHar);
  189. {$endif}
  190. Case FChar of
  191. 'D' : begin
  192. if Checkarg(vtinteger,false) then
  193. Str(Args[Doarg].VInteger,ToAdd)
  194. {$IFNDEF VIRTUALPASCAL}
  195. else if CheckArg(vtInt64,true) then
  196. Str(Args[DoArg].VInt64^,toadd)
  197. {$ENDIF}
  198. ;
  199. Width:=Abs(width);
  200. Index:=Prec-Length(ToAdd);
  201. If ToAdd[1]<>'-' then
  202. ToAdd:=StringOfChar('0',Index)+ToAdd
  203. else
  204. // + 1 to accomodate for - sign in length !!
  205. Insert(StringOfChar('0',Index+1),toadd,2);
  206. end;
  207. 'U' : begin
  208. if Checkarg(vtinteger,false) then
  209. Str(cardinal(Args[Doarg].VInteger),ToAdd)
  210. {$IFNDEF VIRTUALPASCAL}
  211. else if CheckArg(vtInt64,false) then
  212. Str(qword(Args[DoArg].VInt64^),toadd)
  213. else if CheckArg(vtQWord,true) then
  214. Str(Args[DoArg].VQWord^,toadd);
  215. {$ENDIF}
  216. ;
  217. Width:=Abs(width);
  218. Index:=Prec-Length(ToAdd);
  219. ToAdd:=StringOfChar('0',Index)+ToAdd
  220. end;
  221. 'E' : begin
  222. CheckArg(vtExtended,true);
  223. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3);
  224. end;
  225. 'F' : begin
  226. CheckArg(vtExtended,true);
  227. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec);
  228. end;
  229. 'G' : begin
  230. CheckArg(vtExtended,true);
  231. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3);
  232. end;
  233. 'N' : begin
  234. CheckArg(vtExtended,true);
  235. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec);
  236. end;
  237. 'M' : begin
  238. CheckArg(vtExtended,true);
  239. ToAdd:=FloatToStrF(Args[doarg].VExtended^,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;