Rev 382 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
255 | ligi | 1 | /*************************************************************** |
2 | * |
||
3 | * Helper functions for DUBwise |
||
4 | * |
||
5 | * Author: Marcus -LiGi- Bueschleb |
||
6 | * Mailto: LiGi @at@ LiGi DOTT de |
||
7 | * |
||
8 | ***************************************************************/ |
||
9 | |||
242 | ligi | 10 | import java.util.Vector; |
255 | ligi | 11 | import java.io.*; |
12 | import javax.microedition.io.*; |
||
493 | ligi | 13 | import javax.microedition.lcdui.*; |
242 | ligi | 14 | |
223 | ligi | 15 | public final class DUBwiseHelper |
206 | ligi | 16 | { |
223 | ligi | 17 | public final static String ip_digit_zeroes(int digit) |
206 | ligi | 18 | { return "" + digit/100 + "" + (digit/10)%10 + "" + (digit)%10; } |
19 | |||
20 | |||
382 | ligi | 21 | public final static String[] combine_str_arr(String[] arr1 , String[] arr2) |
22 | { |
||
23 | String[] res=new String[arr1.length+arr2.length]; |
||
24 | for (int pos=0;pos<res.length;pos++) |
||
25 | if (pos<arr1.length) |
||
26 | res[pos]=arr1[pos]; |
||
27 | else |
||
28 | res[pos]=arr2[pos-arr1.length]; |
||
29 | return res; |
||
30 | } |
||
31 | |||
252 | ligi | 32 | public final static String pound_progress(int val,int max) |
33 | { |
||
34 | String res="" + (val+1) + "/" + max + " |"; |
||
35 | for (int i=0;i<max;i++) |
||
36 | res+=(i<(val+1))?"#":"_"; |
||
37 | res+="|"; |
||
38 | return res; |
||
39 | } |
||
40 | |||
41 | |||
42 | |||
223 | ligi | 43 | public final static String ip_str(int[] ip,boolean with_zeroes) |
206 | ligi | 44 | { |
45 | if(with_zeroes) |
||
46 | return ip_digit_zeroes(ip[0]) + "." +ip_digit_zeroes(ip[1]) + "."+ip_digit_zeroes(ip[2]) + "."+ip_digit_zeroes(ip[3]) + ":"+ip_digit_zeroes(ip[4]) ; |
||
47 | else |
||
48 | return ip[0]+"."+ip[1]+"."+ip[2]+"."+ip[3]+":"+ip[4]; |
||
49 | |||
50 | } |
||
51 | |||
255 | ligi | 52 | |
53 | |||
54 | |||
55 | |||
56 | public final static String get_http_string(String url) |
||
57 | { |
||
58 | |||
59 | try { |
||
60 | |||
61 | InputStream stream = null; |
||
62 | StringBuffer buff = new StringBuffer(); |
||
63 | StreamConnection conn=null; |
||
64 | |||
65 | System.out.println("starting conn"); |
||
66 | conn = (StreamConnection)Connector.open(url); |
||
67 | stream = conn.openInputStream(); |
||
68 | int ch; |
||
69 | |||
70 | while((ch = stream.read()) != -1) |
||
71 | buff.append((char) ch); |
||
72 | |||
73 | if(stream != null) |
||
74 | stream.close(); |
||
75 | |||
76 | if(conn != null) |
||
77 | conn.close(); |
||
78 | |||
79 | |||
80 | return buff.toString(); |
||
81 | |||
82 | } |
||
83 | catch ( Exception e) |
||
84 | { |
||
85 | return "err"; |
||
86 | } |
||
87 | |||
88 | } |
||
89 | |||
90 | |||
91 | |||
92 | |||
93 | |||
223 | ligi | 94 | public final static int pow(int val,int pow) |
206 | ligi | 95 | { |
96 | int res=1; |
||
97 | |||
98 | for (int p=0;p<pow;p++) |
||
99 | res*=val; |
||
100 | |||
101 | return res; |
||
102 | } |
||
103 | |||
104 | |||
255 | ligi | 105 | |
106 | |||
107 | |||
242 | ligi | 108 | public final static String[] split_str(String str,String sep) |
109 | { |
||
110 | |||
111 | Vector nodes = new Vector(); |
||
112 | |||
113 | // Parse nodes into vector |
||
114 | int index = str.indexOf(sep); |
||
115 | while(index>=0) { |
||
116 | nodes.addElement( str.substring(0, index) ); |
||
117 | str = str.substring(index+sep.length()); |
||
118 | index = str.indexOf(sep); |
||
119 | } |
||
120 | // add last element |
||
121 | nodes.addElement( str ); |
||
122 | |||
123 | // Create splitted string array |
||
124 | String[] result = new String[ nodes.size() ]; |
||
125 | if( nodes.size()>0 ) { |
||
126 | for(int loop=0; loop<nodes.size(); loop++) |
||
127 | { |
||
128 | result[loop] = (String)nodes.elementAt(loop); |
||
129 | System.out.println(result[loop]); |
||
130 | } |
||
131 | |||
132 | } |
||
133 | |||
134 | return result; |
||
135 | } |
||
136 | |||
137 | |||
138 | |||
223 | ligi | 139 | public final static int mod_decimal(int val,int mod_power,int modder,int setter,int clipper) |
206 | ligi | 140 | { |
141 | |||
142 | int res=0; |
||
143 | |||
144 | for (int power=0;power<4;power++) |
||
145 | { |
||
146 | |||
147 | int act_digit=(val/pow(10,power))%10; |
||
148 | |||
149 | int new_digit=act_digit; |
||
150 | if (power==mod_power) |
||
151 | { |
||
152 | if (setter!=-1) |
||
153 | new_digit=setter; |
||
154 | |||
155 | new_digit+=modder; |
||
156 | |||
157 | if(new_digit<0) |
||
158 | new_digit=0; |
||
159 | |||
160 | if(new_digit>clipper) |
||
161 | new_digit=clipper; |
||
162 | |||
163 | } |
||
164 | |||
165 | // new_digit=1; |
||
166 | res+=new_digit*pow(10,power); |
||
167 | } |
||
168 | |||
169 | |||
170 | return res; |
||
171 | |||
172 | |||
173 | } |
||
174 | |||
493 | ligi | 175 | |
176 | public static Image scaleImage(Image original, int newWidth) |
||
177 | // this function is from http://willperone.net/Code/codescaling.php |
||
178 | { |
||
179 | |||
180 | int[] rawInput = new int[original.getHeight() * original.getWidth()]; |
||
181 | original.getRGB(rawInput, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight()); |
||
182 | |||
183 | int newHeight=((original.getHeight()* (newWidth/10)) / (original.getWidth()/10)); |
||
184 | int[] rawOutput = new int[newWidth*newHeight]; |
||
185 | |||
186 | |||
187 | // YD compensates for the x loop by subtracting the width back out |
||
188 | int YD = (original.getHeight() / newHeight) * original.getWidth() - original.getWidth(); |
||
189 | int YR = original.getHeight() % newHeight; |
||
190 | int XD = original.getWidth() / newWidth; |
||
191 | int XR = original.getWidth() % newWidth; |
||
192 | |||
193 | |||
194 | |||
195 | int outOffset= 0; |
||
196 | int inOffset= 0; |
||
197 | |||
198 | for (int y= newHeight, YE= 0; y > 0; y--) { |
||
199 | for (int x= newWidth, XE= 0; x > 0; x--) { |
||
200 | rawOutput[outOffset++]= rawInput[inOffset]; |
||
201 | inOffset+=XD; |
||
202 | XE+=XR; |
||
203 | if (XE >= newWidth) { |
||
204 | XE-= newWidth; |
||
205 | inOffset++; |
||
206 | } |
||
207 | } |
||
208 | inOffset+= YD; |
||
209 | YE+= YR; |
||
210 | if (YE >= newHeight) { |
||
211 | YE -= newHeight; |
||
212 | inOffset+=original.getWidth(); |
||
213 | } |
||
214 | } |
||
215 | return Image.createRGBImage(rawOutput, newWidth, newHeight, false); |
||
216 | } |
||
217 | |||
218 | |||
219 | |||
206 | ligi | 220 | } |