sysformt.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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,false) then
  216. Str(qword(Args[DoArg].VInt64^),toadd)
  217. else if CheckArg(vtQWord,true) then
  218. Str(Args[DoArg].VQWord^,toadd);
  219. {$ENDIF}
  220. ;
  221. Width:=Abs(width);
  222. Index:=Prec-Length(ToAdd);
  223. ToAdd:=StringOfChar('0',Index)+ToAdd
  224. end;
  225. 'E' : begin
  226. CheckArg(vtExtended,true);
  227. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3);
  228. end;
  229. 'F' : begin
  230. CheckArg(vtExtended,true);
  231. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec);
  232. end;
  233. 'G' : begin
  234. CheckArg(vtExtended,true);
  235. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3);
  236. end;
  237. 'N' : begin
  238. CheckArg(vtExtended,true);
  239. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec);
  240. end;
  241. 'M' : begin
  242. CheckArg(vtExtended,true);
  243. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffCurrency,9999,Prec);
  244. end;
  245. 'S' : begin
  246. if CheckArg(vtString,false) then
  247. hs:=Args[doarg].VString^
  248. else
  249. if CheckArg(vtChar,false) then
  250. hs:=Args[doarg].VChar
  251. else
  252. if CheckArg(vtPChar,false) then
  253. hs:=Args[doarg].VPChar
  254. else
  255. {$ifndef VER1_0}
  256. if CheckArg(vtPWideChar,false) then
  257. hs:=WideString(Args[doarg].VPWideChar)
  258. else
  259. if CheckArg(vtWideChar,false) then
  260. hs:=WideString(Args[doarg].VWideChar)
  261. else
  262. if CheckArg(vtWidestring,false) then
  263. hs:=WideString(Args[doarg].VWideString)
  264. else
  265. {$endif VER1_0}
  266. if CheckArg(vtAnsiString,true) then
  267. hs:=ansistring(Args[doarg].VAnsiString);
  268. Index:=Length(hs);
  269. If (Prec<>-1) and (Index>Prec) then
  270. Index:=Prec;
  271. ToAdd:=Copy(hs,1,Index);
  272. end;
  273. 'P' : Begin
  274. CheckArg(vtpointer,true);
  275. ToAdd:=HexStr(ptrint(Args[DoArg].VPointer),sizeof(Ptrint)*2);
  276. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  277. // Insert(':',ToAdd,5);
  278. end;
  279. 'X' : begin
  280. {$ifdef ver1_0}
  281. if Checkarg(vtinteger,false) then
  282. begin
  283. vl:=Args[Doarg].VInteger and int64($ffffffff);
  284. index:=16;
  285. end
  286. else
  287. begin
  288. CheckArg(vtInt64,true);
  289. vl:=Args[DoArg].VInt64^;
  290. index:=31;
  291. end;
  292. If Prec>index then
  293. ToAdd:=HexStr(vl,index)
  294. else
  295. begin
  296. // determine minimum needed number of hex digits.
  297. Index:=1;
  298. While (DWord(1 shl (Index*4))<=DWord(Args[DoArg].VInteger)) and (index<8) do
  299. inc(Index);
  300. If Index>Prec then
  301. Prec:=Index;
  302. ToAdd:=HexStr(int64(vl),Prec);
  303. end;
  304. {$else}
  305. if Checkarg(vtinteger,false) then
  306. begin
  307. vq:=Cardinal(Args[Doarg].VInteger);
  308. index:=16;
  309. end
  310. else
  311. begin
  312. CheckArg(vtInt64,true);
  313. vq:=Qword(Args[DoArg].VInt64^);
  314. index:=31;
  315. end;
  316. If Prec>index then
  317. ToAdd:=HexStr(vq,index)
  318. else
  319. begin
  320. // determine minimum needed number of hex digits.
  321. Index:=1;
  322. While (qWord(1) shl (Index*4)<=vq) and (index<16) do
  323. inc(Index);
  324. If Index>Prec then
  325. Prec:=Index;
  326. ToAdd:=HexStr(vq,Prec);
  327. end;
  328. {$endif}
  329. end;
  330. '%': ToAdd:='%';
  331. end;
  332. If Width<>-1 then
  333. If Length(ToAdd)<Width then
  334. If not Left then
  335. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  336. else
  337. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  338. Result:=Result+ToAdd;
  339. end;
  340. inc(chpos);
  341. Oldpos:=chpos;
  342. end;
  343. end;