sysformt.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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,false) then
  191. Str(Args[DoArg].VInt64^,toadd)
  192. else if CheckArg(vtQWord,true) then
  193. Str(int64(Args[DoArg].VQWord^),toadd);
  194. Width:=Abs(width);
  195. Index:=Prec-Length(ToAdd);
  196. If ToAdd[1]<>'-' then
  197. ToAdd:=StringOfChar('0',Index)+ToAdd
  198. else
  199. // + 1 to accomodate for - sign in length !!
  200. Insert(StringOfChar('0',Index+1),toadd,2);
  201. end;
  202. 'U' : begin
  203. if Checkarg(vtinteger,false) then
  204. Str(cardinal(Args[Doarg].VInteger),ToAdd)
  205. else if CheckArg(vtInt64,false) then
  206. Str(qword(Args[DoArg].VInt64^),toadd)
  207. else if CheckArg(vtQWord,true) then
  208. Str(Args[DoArg].VQWord^,toadd);
  209. Width:=Abs(width);
  210. Index:=Prec-Length(ToAdd);
  211. ToAdd:=StringOfChar('0',Index)+ToAdd
  212. end;
  213. 'E' : begin
  214. if CheckArg(vtCurrency,false) then
  215. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffexponent,Prec,3)
  216. else if CheckArg(vtExtended,true) then
  217. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3);
  218. end;
  219. 'F' : begin
  220. if CheckArg(vtCurrency,false) then
  221. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffFixed,9999,Prec)
  222. else if CheckArg(vtExtended,true) then
  223. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec);
  224. end;
  225. 'G' : begin
  226. if CheckArg(vtCurrency,false) then
  227. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffGeneral,Prec,3)
  228. else if CheckArg(vtExtended,true) then
  229. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3);
  230. end;
  231. 'N' : begin
  232. if CheckArg(vtCurrency,false) then
  233. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffNumber,9999,Prec)
  234. else if CheckArg(vtExtended,true) then
  235. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec);
  236. end;
  237. 'M' : begin
  238. if CheckArg(vtExtended,false) then
  239. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffCurrency,9999,Prec)
  240. else if CheckArg(vtCurrency,true) then
  241. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,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. if CheckArg(vtPWideChar,false) then
  254. hs:=WideString(Args[doarg].VPWideChar)
  255. else
  256. if CheckArg(vtWideChar,false) then
  257. hs:=WideString(Args[doarg].VWideChar)
  258. else
  259. if CheckArg(vtWidestring,false) then
  260. hs:=WideString(Args[doarg].VWideString)
  261. else
  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. if Checkarg(vtinteger,false) then
  277. begin
  278. vq:=Cardinal(Args[Doarg].VInteger);
  279. index:=16;
  280. end
  281. else
  282. if CheckArg(vtQWord, false) then
  283. begin
  284. vq:=Qword(Args[DoArg].VQWord^);
  285. index:=31;
  286. end
  287. else
  288. begin
  289. CheckArg(vtInt64,true);
  290. vq:=Qword(Args[DoArg].VInt64^);
  291. index:=31;
  292. end;
  293. If Prec>index then
  294. ToAdd:=HexStr(vq,index)
  295. else
  296. begin
  297. // determine minimum needed number of hex digits.
  298. Index:=1;
  299. While (qWord(1) shl (Index*4)<=vq) and (index<16) do
  300. inc(Index);
  301. If Index>Prec then
  302. Prec:=Index;
  303. ToAdd:=HexStr(vq,Prec);
  304. end;
  305. end;
  306. '%': ToAdd:='%';
  307. end;
  308. If Width<>-1 then
  309. If Length(ToAdd)<Width then
  310. If not Left then
  311. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  312. else
  313. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  314. Result:=Result+ToAdd;
  315. end;
  316. inc(chpos);
  317. Oldpos:=chpos;
  318. end;
  319. end;