sysformt.inc 9.8 KB

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