KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cpmake > Printf


1 /*
2  * Copyright (c) 2004, Brian Hawkins
3  * Permission is granted to use this code without restriction as long
4  * as this copyright notice appears in all source files.
5  */

6  
7 package cpmake;
8  
9 import java.util.*;
10
11
12 class Printf
13     {
14     private static String JavaDoc s_knownFlags = "dfxXs"; //Flags are in no particular order
15

16     public static void main(String JavaDoc[] commandline)
17         {
18         Object JavaDoc[] args = new Object JavaDoc[1];
19         
20         args[0] = new Double JavaDoc(1234.1234);
21         System.out.println(Printf.print("double = %2.2f", args));
22         }
23         
24     public static String JavaDoc print(String JavaDoc pattern, Object JavaDoc[] args)
25         {
26         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
27         String JavaDoc[] parts = split(pattern);
28         int paramCount = 0;
29         
30         for (int I = 0; I < parts.length; I++)
31             {
32             if (parts[I].startsWith("%"))
33                 {
34                 result.append(processArg(parts[I], args[paramCount]));
35                 paramCount++;
36                 }
37             else
38                 result.append(parts[I]);
39             }
40         
41         return (result.toString());
42         }
43
44 //-------------------------------------------------------------------
45
private static String JavaDoc processArg(String JavaDoc tag, Object JavaDoc arg)
46         {
47         char[] ctag = tag.toCharArray();
48         String JavaDoc ret = null;
49         
50         switch (ctag[ctag.length-1])
51             {
52             case 'd':
53             case 'f':
54             case 'l':
55                 ret = arg.toString();
56                 if (ctag.length > 2)
57                     ret = formatNumber(ret, new String JavaDoc(ctag, 1, ctag.length-2));
58                 break;
59             case 'x':
60                 ret = Long.toHexString(((Number JavaDoc)arg).longValue()).toLowerCase();
61                 if (ctag.length > 2)
62                     ret = formatNumber(ret, new String JavaDoc(ctag, 1, ctag.length-2));
63                 break;
64             case 'X':
65                 ret = Long.toHexString(((Number JavaDoc)arg).longValue()).toUpperCase();
66                 if (ctag.length > 2)
67                     ret = formatNumber(ret, new String JavaDoc(ctag, 1, ctag.length-2));
68                 break;
69             case 's':
70                 ret = arg.toString();
71                 break;
72             default:
73                 ret = arg.toString();
74             }
75             
76         return (ret);
77         }
78         
79 //-------------------------------------------------------------------
80
private static String JavaDoc formatNumber(String JavaDoc number, String JavaDoc format)
81         {
82         boolean leftJustify = false;
83         boolean zeroPad = false;
84         int startPos = 0;
85         int width = 0;
86         int numberWidth = 0;
87         int precision = 0;
88         boolean setPrecision = false;
89         int precisionIndex = 0;
90         
91         
92         if (format.startsWith("-"))
93             {
94             leftJustify = true;
95             startPos = 1;
96             }
97         else if (format.startsWith("0"))
98             {
99             zeroPad = true;
100             startPos = 1;
101             }
102             
103         if ((precisionIndex = format.indexOf('.')) != -1)
104             {
105             setPrecision = true;
106             precision = Integer.parseInt(format.substring(precisionIndex+1, format.length()));
107             }
108         else
109             precisionIndex = format.length();
110             
111         if (precisionIndex != startPos)
112             width = Integer.parseInt(format.substring(startPos, precisionIndex));
113         
114         if (setPrecision)
115             {
116             int pind;
117             
118             if ((pind = number.indexOf('.')) != -1)
119                 {
120                 number = String.valueOf(round(Double.parseDouble(number), precision));
121                 pind = number.indexOf('.');
122                 }
123             else
124                 pind = number.length();
125             
126             char[] newnum;
127             if ((pind + precision + 1) > number.length())
128                 newnum = new char[pind + precision + 1];
129             else
130                 newnum = new char[number.length()];
131             Arrays.fill(newnum, '0');
132             newnum[pind] = '.';
133             number = new String JavaDoc(copyArray(newnum, number.toCharArray()));
134             }
135         
136         if (number.length() < width)
137             {
138             char[] newnum = new char[width];
139             
140             if (zeroPad)
141                 Arrays.fill(newnum, '0');
142             else
143                 Arrays.fill(newnum, ' ');
144                 
145             if (leftJustify)
146                 number = new String JavaDoc(copyArray(newnum, number.toCharArray()));
147             else
148                 number = new String JavaDoc(copyArray(newnum, number.toCharArray(), width - number.length()));
149             }
150         
151         return (number);
152         }
153         
154 //-------------------------------------------------------------------
155
private static char[] copyArray(char[] dest, char[] src)
156         {
157         return (copyArray(dest, src, 0));
158         }
159
160 //-------------------------------------------------------------------
161
private static char[] copyArray(char[] dest, char[] src, int startOffset)
162         {
163         if (dest.length < src.length)
164             {
165             System.out.println("Source size = " + src.length);
166             System.out.println("Dest size = " + dest.length);
167             System.exit(1);
168             }
169             
170         for (int I = 0; I < src.length; I++)
171             dest[I+startOffset] = src[I];
172         
173         return (dest);
174         }
175         
176 //-------------------------------------------------------------------
177
private static double round(double num, int precision)
178         {
179         long round;
180         
181         round = Math.round(num * Math.pow(10, precision));
182         return (round / Math.pow(10, precision));
183         }
184         
185 //-------------------------------------------------------------------
186
private static String JavaDoc[] split(String JavaDoc pattern)
187         {
188         char[] pat = pattern.toCharArray();
189         Vector ret = new Vector();
190         int lastIndex = 0;
191         
192         //There will never be a single '%' at the end so -1
193
for (int I = 0; I < (pat.length-1); I++)
194             {
195             if (pat[I] == '%')
196                 {
197                 if (pat[I+1] != '%')
198                     {
199                     ret.add(new String JavaDoc(pat, lastIndex, (I - lastIndex)));
200                     lastIndex = I;
201                     for (int J = I; J < pat.length; J++)
202                         {
203                         if (s_knownFlags.indexOf(pat[J]) != -1)
204                             {
205                             ret.add(new String JavaDoc(pat, lastIndex, (J - lastIndex)+1));
206                             lastIndex = J+1;
207                             I = J;
208                             break;
209                             }
210                         }
211                     }
212                 else
213                     {
214                     I++;
215                     ret.add(new String JavaDoc(pat, lastIndex, (I - lastIndex)));
216                     lastIndex = I+1;
217                     }
218                 }
219             }
220         
221         if (lastIndex < pat.length)
222             ret.add(new String JavaDoc(pat, lastIndex, (pat.length - lastIndex)));
223             
224         return ((String JavaDoc[])ret.toArray(new String JavaDoc[1]));
225         }
226         
227     }
228     
229
Popular Tags