sysformt.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. {$ifdef ver1_0}
  8. vl : int64;
  9. {$else}
  10. vq : qword;
  11. {$endif}
  12. {
  13. ReadFormat reads the format string. It returns the type character in
  14. uppercase, and sets index, Width, Prec to their correct values,
  15. or -1 if not set. It sets Left to true if left alignment was requested.
  16. In case of an error, DoFormatError is called.
  17. }
  18. Function ReadFormat : Char;
  19. Var Value : longint;
  20. Procedure ReadInteger;
  21. {$IFDEF VIRTUALPASCAL}
  22. var Code: longint;
  23. {$ELSE}
  24. var Code: word;
  25. {$ENDIF}
  26. begin
  27. If Value<>-1 then exit; // Was already read.
  28. OldPos:=chPos;
  29. While (Chpos<=Len) and
  30. (Pos(Fmt[chpos],'1234567890')<>0) do inc(chpos);
  31. If Chpos>len then
  32. DoFormatError(feInvalidFormat);
  33. If Fmt[Chpos]='*' then
  34. begin
  35. If (Chpos>OldPos) or (ArgPos>High(Args))
  36. or (Args[ArgPos].Vtype<>vtInteger) then
  37. DoFormatError(feInvalidFormat);
  38. Value:=Args[ArgPos].VInteger;
  39. Inc(ArgPos);
  40. Inc(chPos);
  41. end
  42. else
  43. begin
  44. If (OldPos<chPos) Then
  45. begin
  46. Val (Copy(Fmt,OldPos,ChPos-OldPos),value,code);
  47. // This should never happen !!
  48. If Code>0 then DoFormatError (feInvalidFormat);
  49. end
  50. else
  51. Value:=-1;
  52. end;
  53. end;
  54. Procedure ReadIndex;
  55. begin
  56. ReadInteger;
  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. {$IFNDEF VIRTUALPASCAL}
  199. else if CheckArg(vtInt64,true) then
  200. Str(Args[DoArg].VInt64^,toadd)
  201. {$ENDIF}
  202. ;
  203. Width:=Abs(width);
  204. Index:=Prec-Length(ToAdd);
  205. If ToAdd[1]<>'-' then
  206. ToAdd:=StringOfChar('0',Index)+ToAdd
  207. else
  208. // + 1 to accomodate for - sign in length !!
  209. Insert(StringOfChar('0',Index+1),toadd,2);
  210. end;
  211. 'U' : begin
  212. if Checkarg(vtinteger,false) then
  213. Str(cardinal(Args[Doarg].VInteger),ToAdd)
  214. {$IFNDEF VIRTUALPASCAL}
  215. else if CheckArg(vtInt64,true) then
  216. Str(qword(Args[DoArg].VInt64^),toadd)
  217. {$ENDIF}
  218. ;
  219. Width:=Abs(width);
  220. Index:=Prec-Length(ToAdd);
  221. ToAdd:=StringOfChar('0',Index)+ToAdd
  222. end;
  223. 'E' : begin
  224. CheckArg(vtExtended,true);
  225. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3);
  226. end;
  227. 'F' : begin
  228. CheckArg(vtExtended,true);
  229. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec);
  230. end;
  231. 'G' : begin
  232. CheckArg(vtExtended,true);
  233. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3);
  234. end;
  235. 'N' : begin
  236. CheckArg(vtExtended,true);
  237. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec);
  238. end;
  239. 'M' : begin
  240. CheckArg(vtExtended,true);
  241. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffCurrency,9999,Prec);
  242. end;
  243. 'S' : begin
  244. if CheckArg(vtString,false) then
  245. hs:=Args[doarg].VString^
  246. else
  247. if CheckArg(vtChar,false) then
  248. hs:=Args[doarg].VChar
  249. else
  250. if CheckArg(vtPChar,false) then
  251. hs:=Args[doarg].VPChar
  252. else
  253. {$ifndef VER1_0}
  254. if CheckArg(vtPWideChar,false) then
  255. hs:=WideString(Args[doarg].VPWideChar)
  256. else
  257. if CheckArg(vtWideChar,false) then
  258. hs:=WideString(Args[doarg].VWideChar)
  259. else
  260. if CheckArg(vtWidestring,false) then
  261. hs:=WideString(Args[doarg].VWideString)
  262. else
  263. {$endif VER1_0}
  264. if CheckArg(vtAnsiString,true) then
  265. hs:=ansistring(Args[doarg].VAnsiString);
  266. Index:=Length(hs);
  267. If (Prec<>-1) and (Index>Prec) then
  268. Index:=Prec;
  269. ToAdd:=Copy(hs,1,Index);
  270. end;
  271. 'P' : Begin
  272. CheckArg(vtpointer,true);
  273. ToAdd:=HexStr(ptrint(Args[DoArg].VPointer),sizeof(Ptrint)*2);
  274. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  275. // Insert(':',ToAdd,5);
  276. end;
  277. 'X' : begin
  278. {$ifdef ver1_0}
  279. if Checkarg(vtinteger,false) then
  280. begin
  281. vl:=Args[Doarg].VInteger and int64($ffffffff);
  282. index:=16;
  283. end
  284. else
  285. begin
  286. CheckArg(vtInt64,true);
  287. vl:=Args[DoArg].VInt64^;
  288. index:=31;
  289. end;
  290. If Prec>index then
  291. ToAdd:=HexStr(vl,index)
  292. else
  293. begin
  294. // determine minimum needed number of hex digits.
  295. Index:=1;
  296. While (DWord(1 shl (Index*4))<=DWord(Args[DoArg].VInteger)) and (index<8) do
  297. inc(Index);
  298. If Index>Prec then
  299. Prec:=Index;
  300. ToAdd:=HexStr(int64(vl),Prec);
  301. end;
  302. {$else}
  303. if Checkarg(vtinteger,false) then
  304. begin
  305. vq:=Cardinal(Args[Doarg].VInteger);
  306. index:=16;
  307. end
  308. else
  309. begin
  310. CheckArg(vtInt64,true);
  311. vq:=Qword(Args[DoArg].VInt64^);
  312. index:=31;
  313. end;
  314. If Prec>index then
  315. ToAdd:=HexStr(vq,index)
  316. else
  317. begin
  318. // determine minimum needed number of hex digits.
  319. Index:=1;
  320. While (qWord(1) shl (Index*4)<=vq) and (index<16) do
  321. inc(Index);
  322. If Index>Prec then
  323. Prec:=Index;
  324. ToAdd:=HexStr(vq,Prec);
  325. end;
  326. {$endif}
  327. end;
  328. '%': ToAdd:='%';
  329. end;
  330. If Width<>-1 then
  331. If Length(ToAdd)<Width then
  332. If not Left then
  333. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  334. else
  335. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  336. Result:=Result+ToAdd;
  337. end;
  338. inc(chpos);
  339. Oldpos:=chpos;
  340. end;
  341. end;