sysformt.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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)) then
  28. DoFormatError(feInvalidFormat);
  29. case Args[ArgPos].Vtype of
  30. vtInteger: Value := Args[ArgPos].VInteger;
  31. vtInt64: Value := Args[ArgPos].VInt64^;
  32. vtQWord: Value := Args[ArgPos].VQWord^;
  33. else
  34. DoFormatError(feInvalidFormat);
  35. end;
  36. Inc(ArgPos);
  37. Inc(chPos);
  38. end
  39. else
  40. begin
  41. If (OldPos<chPos) Then
  42. begin
  43. Val (Copy(Fmt,OldPos,ChPos-OldPos),value,code);
  44. // This should never happen !!
  45. If Code>0 then DoFormatError (feInvalidFormat);
  46. end
  47. else
  48. Value:=-1;
  49. end;
  50. end;
  51. Procedure ReadIndex;
  52. begin
  53. ReadInteger;
  54. If Fmt[ChPos]=':' then
  55. begin
  56. If Value=-1 then DoFormatError(feMissingArgument);
  57. Index:=Value;
  58. Value:=-1;
  59. Inc(Chpos);
  60. end;
  61. {$ifdef fmtdebug}
  62. Log ('Read index');
  63. {$endif}
  64. end;
  65. Procedure ReadLeft;
  66. begin
  67. If Fmt[chpos]='-' then
  68. begin
  69. left:=True;
  70. Inc(chpos);
  71. end
  72. else
  73. Left:=False;
  74. {$ifdef fmtdebug}
  75. Log ('Read Left');
  76. {$endif}
  77. end;
  78. Procedure ReadWidth;
  79. begin
  80. ReadInteger;
  81. If Value<>-1 then
  82. begin
  83. Width:=Value;
  84. Value:=-1;
  85. end;
  86. {$ifdef fmtdebug}
  87. Log ('Read width');
  88. {$endif}
  89. end;
  90. Procedure ReadPrec;
  91. begin
  92. If Fmt[chpos]='.' then
  93. begin
  94. inc(chpos);
  95. ReadInteger;
  96. If Value=-1 then
  97. Value:=0;
  98. prec:=Value;
  99. end;
  100. {$ifdef fmtdebug}
  101. Log ('Read precision');
  102. {$endif}
  103. end;
  104. {$ifdef INWIDEFORMAT}
  105. var
  106. FormatChar : TFormatChar;
  107. {$endif INWIDEFORMAT}
  108. begin
  109. {$ifdef fmtdebug}
  110. Log ('Start format');
  111. {$endif}
  112. Index:=-1;
  113. Width:=-1;
  114. Prec:=-1;
  115. Value:=-1;
  116. inc(chpos);
  117. If Fmt[Chpos]='%' then
  118. begin
  119. Result:='%';
  120. exit; // VP fix
  121. end;
  122. ReadIndex;
  123. ReadLeft;
  124. ReadWidth;
  125. ReadPrec;
  126. {$ifdef INWIDEFORMAT}
  127. FormatChar:=UpCase(Fmt[ChPos])[1];
  128. if word(FormatChar)>255 then
  129. ReadFormat:=#255
  130. else
  131. ReadFormat:=FormatChar;
  132. {$else INWIDEFORMAT}
  133. ReadFormat:=Upcase(Fmt[ChPos]);
  134. {$endif INWIDEFORMAT}
  135. {$ifdef fmtdebug}
  136. Log ('End format');
  137. {$endif}
  138. end;
  139. {$ifdef fmtdebug}
  140. Procedure DumpFormat (C : char);
  141. begin
  142. Write ('Fmt : ',fmt:10);
  143. Write (' Index : ',Index:3);
  144. Write (' Left : ',left:5);
  145. Write (' Width : ',Width:3);
  146. Write (' Prec : ',prec:3);
  147. Writeln (' Type : ',C);
  148. end;
  149. {$endif}
  150. function Checkarg (AT : SizeInt;err:boolean):boolean;
  151. {
  152. Check if argument INDEX is of correct type (AT)
  153. If Index=-1, ArgPos is used, and argpos is augmented with 1
  154. DoArg is set to the argument that must be used.
  155. }
  156. begin
  157. result:=false;
  158. if Index=-1 then
  159. DoArg:=Argpos
  160. else
  161. DoArg:=Index;
  162. ArgPos:=DoArg+1;
  163. If (Doarg>High(Args)) or (Args[Doarg].Vtype<>AT) then
  164. begin
  165. if err then
  166. DoFormatError(feInvalidArgindex);
  167. dec(ArgPos);
  168. exit;
  169. end;
  170. result:=true;
  171. end;
  172. Const Zero = '000000000000000000000000000000000000000000000000000000000000000';
  173. begin
  174. Result:='';
  175. Len:=Length(Fmt);
  176. Chpos:=1;
  177. OldPos:=1;
  178. ArgPos:=0;
  179. While chpos<=len do
  180. begin
  181. While (ChPos<=Len) and (Fmt[chpos]<>'%') do
  182. inc(chpos);
  183. If ChPos>OldPos Then
  184. Result:=Result+Copy(Fmt,OldPos,Chpos-Oldpos);
  185. If ChPos<Len then
  186. begin
  187. FChar:=ReadFormat;
  188. {$ifdef fmtdebug}
  189. DumpFormat(FCHar);
  190. {$endif}
  191. Case FChar of
  192. 'D' : begin
  193. if Checkarg(vtinteger,false) then
  194. Str(Args[Doarg].VInteger,ToAdd)
  195. else if CheckArg(vtInt64,false) then
  196. Str(Args[DoArg].VInt64^,toadd)
  197. else if CheckArg(vtQWord,true) then
  198. Str(int64(Args[DoArg].VQWord^),toadd);
  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. else if CheckArg(vtInt64,false) then
  211. Str(qword(Args[DoArg].VInt64^),toadd)
  212. else if CheckArg(vtQWord,true) then
  213. Str(Args[DoArg].VQWord^,toadd);
  214. Width:=Abs(width);
  215. Index:=Prec-Length(ToAdd);
  216. ToAdd:=StringOfChar('0',Index)+ToAdd
  217. end;
  218. 'E' : begin
  219. if CheckArg(vtCurrency,false) then
  220. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffexponent,Prec,3,FormatSettings)
  221. else if CheckArg(vtExtended,true) then
  222. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffexponent,Prec,3,FormatSettings);
  223. end;
  224. 'F' : begin
  225. if CheckArg(vtCurrency,false) then
  226. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffFixed,9999,Prec,FormatSettings)
  227. else if CheckArg(vtExtended,true) then
  228. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffFixed,9999,Prec,FormatSettings);
  229. end;
  230. 'G' : begin
  231. if CheckArg(vtCurrency,false) then
  232. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffGeneral,Prec,3,FormatSettings)
  233. else if CheckArg(vtExtended,true) then
  234. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffGeneral,Prec,3,FormatSettings);
  235. end;
  236. 'N' : begin
  237. if CheckArg(vtCurrency,false) then
  238. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffNumber,9999,Prec,FormatSettings)
  239. else if CheckArg(vtExtended,true) then
  240. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffNumber,9999,Prec,FormatSettings);
  241. end;
  242. 'M' : begin
  243. if CheckArg(vtExtended,false) then
  244. ToAdd:=FloatToStrF(Args[doarg].VExtended^,ffCurrency,9999,Prec,FormatSettings)
  245. else if CheckArg(vtCurrency,true) then
  246. ToAdd:=FloatToStrF(Args[doarg].VCurrency^,ffCurrency,9999,Prec,FormatSettings);
  247. end;
  248. 'S' : begin
  249. if CheckArg(vtString,false) then
  250. hs:=Args[doarg].VString^
  251. else
  252. if CheckArg(vtChar,false) then
  253. hs:=Args[doarg].VChar
  254. else
  255. if CheckArg(vtPChar,false) then
  256. hs:=Args[doarg].VPChar
  257. else
  258. if CheckArg(vtPWideChar,false) then
  259. hs:=WideString(Args[doarg].VPWideChar)
  260. else
  261. if CheckArg(vtWideChar,false) then
  262. hs:=WideString(Args[doarg].VWideChar)
  263. else
  264. if CheckArg(vtWidestring,false) then
  265. hs:=WideString(Args[doarg].VWideString)
  266. else
  267. if CheckArg(vtAnsiString,true) then
  268. hs:=ansistring(Args[doarg].VAnsiString);
  269. Index:=Length(hs);
  270. If (Prec<>-1) and (Index>Prec) then
  271. Index:=Prec;
  272. ToAdd:=Copy(hs,1,Index);
  273. end;
  274. 'P' : Begin
  275. CheckArg(vtpointer,true);
  276. ToAdd:=HexStr(ptrint(Args[DoArg].VPointer),sizeof(Ptrint)*2);
  277. // Insert ':'. Is this needed in 32 bit ? No it isn't.
  278. // Insert(':',ToAdd,5);
  279. end;
  280. 'X' : begin
  281. if Checkarg(vtinteger,false) then
  282. begin
  283. vq:=Cardinal(Args[Doarg].VInteger);
  284. index:=16;
  285. end
  286. else
  287. if CheckArg(vtQWord, false) then
  288. begin
  289. vq:=Qword(Args[DoArg].VQWord^);
  290. index:=31;
  291. end
  292. else
  293. begin
  294. CheckArg(vtInt64,true);
  295. vq:=Qword(Args[DoArg].VInt64^);
  296. index:=31;
  297. end;
  298. If Prec>index then
  299. ToAdd:=HexStr(vq,index)
  300. else
  301. begin
  302. // determine minimum needed number of hex digits.
  303. Index:=1;
  304. While (qWord(1) shl (Index*4)<=vq) and (index<16) do
  305. inc(Index);
  306. If Index>Prec then
  307. Prec:=Index;
  308. ToAdd:=HexStr(vq,Prec);
  309. end;
  310. end;
  311. '%': ToAdd:='%';
  312. end;
  313. If Width<>-1 then
  314. If Length(ToAdd)<Width then
  315. If not Left then
  316. ToAdd:=Space(Width-Length(ToAdd))+ToAdd
  317. else
  318. ToAdd:=ToAdd+space(Width-Length(ToAdd));
  319. Result:=Result+ToAdd;
  320. end;
  321. inc(chpos);
  322. Oldpos:=chpos;
  323. end;
  324. end;