sysformt.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. var
  108. FormatChar : TFormatChar;
  109. begin
  110. {$ifdef fmtdebug}
  111. Log ('Start format');
  112. {$endif}
  113. Index:=-1;
  114. Width:=-1;
  115. Prec:=-1;
  116. Value:=-1;
  117. inc(chpos);
  118. If Fmt[Chpos]='%' then
  119. begin
  120. Result:='%';
  121. exit; // VP fix
  122. end;
  123. ReadIndex;
  124. ReadLeft;
  125. ReadWidth;
  126. ReadPrec;
  127. {$ifdef INWIDEFORMAT}
  128. FormatChar:=UpCase(Fmt[ChPos])[1];
  129. if word(FormatChar)>255 then
  130. ReadFormat:=#255
  131. else
  132. ReadFormat:=FormatChar;
  133. {$else INWIDEFORMAT}
  134. ReadFormat:=Upcase(Fmt[ChPos]);
  135. {$endif INWIDEFORMAT}
  136. {$ifdef fmtdebug}
  137. Log ('End format');
  138. {$endif}
  139. end;
  140. {$ifdef fmtdebug}
  141. Procedure DumpFormat (C : char);
  142. begin
  143. Write ('Fmt : ',fmt:10);
  144. Write (' Index : ',Index:3);
  145. Write (' Left : ',left:5);
  146. Write (' Width : ',Width:3);
  147. Write (' Prec : ',prec:3);
  148. Writeln (' Type : ',C);
  149. end;
  150. {$endif}
  151. function Checkarg (AT : SizeInt;err:boolean):boolean;
  152. {
  153. Check if argument INDEX is of correct type (AT)
  154. If Index=-1, ArgPos is used, and argpos is augmented with 1
  155. DoArg is set to the argument that must be used.
  156. }
  157. begin
  158. result:=false;
  159. if Index=-1 then
  160. DoArg:=Argpos
  161. else
  162. DoArg:=Index;
  163. ArgPos:=DoArg+1;
  164. If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
  165. begin
  166. if err then
  167. DoFormatError(feInvalidArgindex);
  168. dec(ArgPos);
  169. exit;
  170. end;
  171. result:=true;
  172. end;
  173. Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
  174. begin
  175. Result:='';
  176. Len:=Length(Fmt);
  177. Chpos:=1;
  178. OldPos:=1;
  179. ArgPos:=0;
  180. While chpos<=len do
  181. begin
  182. While (ChPos<=Len) and (Fmt[chpos]<>'%') do
  183. inc(chpos);
  184. If ChPos>OldPos Then
  185. Result:=Result+Copy(Fmt,OldPos,Chpos-Oldpos);
  186. If ChPos<Len then
  187. begin
  188. FChar:=ReadFormat;
  189. {$ifdef fmtdebug}
  190. DumpFormat(FCHar);
  191. {$endif}
  192. Case FChar of
  193. 'D' : begin
  194. if Checkarg(vtinteger,false) then
  195. Str(Args[Doarg].VInteger,ToAdd)
  196. {$IFNDEF VIRTUALPASCAL}
  197. else if CheckArg(vtInt64,true) then
  198. Str(Args[DoArg].VInt64^,toadd)
  199. {$ENDIF}
  200. ;
  201. Width:=Abs(width);
  202. Index:=Prec-Length(ToAdd);
  203. If ToAdd[1]<>'-' then
  204. ToAdd:=StringOfChar('0',Index)+ToAdd
  205. else
  206. // + 1 to accomodate for - sign in length !!
  207. Insert(StringOfChar('0',Index+1),toadd,2);
  208. end;
  209. 'U' : begin
  210. if Checkarg(vtinteger,false) then
  211. Str(cardinal(Args[Doarg].VInteger),ToAdd)
  212. {$IFNDEF VIRTUALPASCAL}
  213. else if CheckArg(vtInt64,true) then
  214. Str(qword(Args[DoArg].VInt64^),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. {$ifndef VER1_0}
  252. if CheckArg(vtPWideChar,false) then
  253. hs:=WideString(Args[doarg].VPWideChar)
  254. else
  255. if CheckArg(vtWideChar,false) then
  256. hs:=WideString(Args[doarg].VWideChar)
  257. else
  258. if CheckArg(vtWidestring,false) then
  259. hs:=WideString(Args[doarg].VWideString)
  260. else
  261. {$endif VER1_0}
  262. if CheckArg(vtAnsiString,true) then
  263. hs:=ansistring(Args[doarg].VAnsiString);
  264. Index:=Length(hs);
  265. If (Prec<>-1) and (Index>Prec) then
  266. Index:=Prec;
  267. ToAdd:=Copy(hs,1,Index);
  268. end;
  269. 'P' : Begin
  270. CheckArg(vtpointer,true);
  271. ToAdd:=HexStr(ptrint(Args[DoArg].VPointer),sizeof(Ptrint)*2);
  272. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  273. // Insert(':',ToAdd,5);
  274. end;
  275. 'X' : begin
  276. {$ifdef ver1_0}
  277. if Checkarg(vtinteger,false) then
  278. begin
  279. vl:=Args[Doarg].VInteger and int64($ffffffff);
  280. index:=16;
  281. end
  282. else
  283. begin
  284. CheckArg(vtInt64,true);
  285. vl:=Args[DoArg].VInt64^;
  286. index:=31;
  287. end;
  288. If Prec>index then
  289. ToAdd:=HexStr(vl,index)
  290. else
  291. begin
  292. // determine minimum needed number of hex digits.
  293. Index:=1;
  294. While (DWord(1 shl (Index*4))<=DWord(Args[DoArg].VInteger)) and (index<8) do
  295. inc(Index);
  296. If Index>Prec then
  297. Prec:=Index;
  298. ToAdd:=HexStr(int64(vl),Prec);
  299. end;
  300. {$else}
  301. if Checkarg(vtinteger,false) then
  302. begin
  303. vq:=Cardinal(Args[Doarg].VInteger);
  304. index:=16;
  305. end
  306. else
  307. begin
  308. CheckArg(vtInt64,true);
  309. vq:=Qword(Args[DoArg].VInt64^);
  310. index:=31;
  311. end;
  312. If Prec>index then
  313. ToAdd:=HexStr(vq,index)
  314. else
  315. begin
  316. // determine minimum needed number of hex digits.
  317. Index:=1;
  318. While (qWord(1) shl (Index*4)<=vq) and (index<16) do
  319. inc(Index);
  320. If Index>Prec then
  321. Prec:=Index;
  322. ToAdd:=HexStr(vq,Prec);
  323. end;
  324. {$endif}
  325. end;
  326. '%': ToAdd:='%';
  327. end;
  328. If Width<>-1 then
  329. If Length(ToAdd)<Width then
  330. If not Left then
  331. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  332. else
  333. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  334. Result:=Result+ToAdd;
  335. end;
  336. inc(chpos);
  337. Oldpos:=chpos;
  338. end;
  339. end;