Subversion Repositories Projects

Rev

Rev 382 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 382 Rev 493
1
/***************************************************************
1
/***************************************************************
2
 *
2
 *
3
 * Helper functions for DUBwise
3
 * Helper functions for DUBwise
4
 *                                                          
4
 *                                                          
5
 * Author:        Marcus -LiGi- Bueschleb
5
 * Author:        Marcus -LiGi- Bueschleb
6
 * Mailto:        LiGi @at@ LiGi DOTT de                    
6
 * Mailto:        LiGi @at@ LiGi DOTT de                    
7
 *
7
 *
8
 ***************************************************************/
8
 ***************************************************************/
9
 
9
 
10
import java.util.Vector;
10
import java.util.Vector;
11
import java.io.*;
11
import java.io.*;
12
import javax.microedition.io.*;
12
import javax.microedition.io.*;
13
 
-
 
14
 
-
 
-
 
13
import javax.microedition.lcdui.*;
15
 
14
 
16
public final class DUBwiseHelper
15
public final class DUBwiseHelper
17
{
16
{
18
    public final static String ip_digit_zeroes(int digit)
17
    public final static String ip_digit_zeroes(int digit)
19
    {   return "" + digit/100 + "" +   (digit/10)%10 + "" + (digit)%10;   }
18
    {   return "" + digit/100 + "" +   (digit/10)%10 + "" + (digit)%10;   }
20
 
19
 
21
 
20
 
22
    public final static String[] combine_str_arr(String[] arr1 , String[] arr2)
21
    public final static String[] combine_str_arr(String[] arr1 , String[] arr2)
23
    {
22
    {
24
        String[] res=new String[arr1.length+arr2.length];
23
        String[] res=new String[arr1.length+arr2.length];
25
        for (int pos=0;pos<res.length;pos++)
24
        for (int pos=0;pos<res.length;pos++)
26
            if (pos<arr1.length)
25
            if (pos<arr1.length)
27
                res[pos]=arr1[pos];
26
                res[pos]=arr1[pos];
28
            else
27
            else
29
                res[pos]=arr2[pos-arr1.length];
28
                res[pos]=arr2[pos-arr1.length];
30
        return res;
29
        return res;
31
    }
30
    }
32
 
31
 
33
    public final static String pound_progress(int val,int max)
32
    public final static String pound_progress(int val,int max)
34
    {
33
    {
35
        String res="" + (val+1) + "/" + max + " |";
34
        String res="" + (val+1) + "/" + max + " |";
36
        for (int i=0;i<max;i++)
35
        for (int i=0;i<max;i++)
37
                res+=(i<(val+1))?"#":"_";
36
                res+=(i<(val+1))?"#":"_";
38
        res+="|";
37
        res+="|";
39
        return res;
38
        return res;
40
    }
39
    }
41
               
40
               
42
 
41
 
43
 
42
 
44
    public final static  String ip_str(int[] ip,boolean with_zeroes)
43
    public final static  String ip_str(int[] ip,boolean with_zeroes)
45
    {
44
    {
46
        if(with_zeroes)
45
        if(with_zeroes)
47
            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]) ;
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]) ;
48
        else
47
        else
49
            return ip[0]+"."+ip[1]+"."+ip[2]+"."+ip[3]+":"+ip[4];
48
            return ip[0]+"."+ip[1]+"."+ip[2]+"."+ip[3]+":"+ip[4];
50
           
49
           
51
    }
50
    }
52
 
51
 
53
 
52
 
54
 
53
 
55
 
54
 
56
   
55
   
57
    public final static String get_http_string(String url)
56
    public final static String get_http_string(String url)
58
    {
57
    {
59
       
58
       
60
        try {
59
        try {
61
 
60
 
62
            InputStream stream = null;
61
            InputStream stream = null;
63
            StringBuffer buff = new StringBuffer();
62
            StringBuffer buff = new StringBuffer();
64
            StreamConnection conn=null;
63
            StreamConnection conn=null;
65
           
64
           
66
            System.out.println("starting conn");
65
            System.out.println("starting conn");
67
            conn = (StreamConnection)Connector.open(url);
66
            conn = (StreamConnection)Connector.open(url);
68
            stream = conn.openInputStream();
67
            stream = conn.openInputStream();
69
            int ch;
68
            int ch;
70
           
69
           
71
            while((ch = stream.read()) != -1)
70
            while((ch = stream.read()) != -1)
72
                    buff.append((char) ch);
71
                    buff.append((char) ch);
73
       
72
       
74
            if(stream != null)
73
            if(stream != null)
75
                stream.close();
74
                stream.close();
76
           
75
           
77
            if(conn != null)
76
            if(conn != null)
78
                conn.close();
77
                conn.close();
79
           
78
           
80
           
79
           
81
            return buff.toString();
80
            return buff.toString();
82
           
81
           
83
        }
82
        }
84
        catch ( Exception e)
83
        catch ( Exception e)
85
            {
84
            {
86
                return "err";
85
                return "err";
87
            }
86
            }
88
       
87
       
89
    }
88
    }
90
   
89
   
91
   
90
   
92
 
91
 
93
 
92
 
94
 
93
 
95
    public final static  int pow(int val,int pow)
94
    public final static  int pow(int val,int pow)
96
    {
95
    {
97
        int res=1;
96
        int res=1;
98
 
97
 
99
        for (int p=0;p<pow;p++)
98
        for (int p=0;p<pow;p++)
100
            res*=val;
99
            res*=val;
101
 
100
 
102
        return res;
101
        return res;
103
    }
102
    }
104
 
103
 
105
 
104
 
106
 
105
 
107
 
106
 
108
 
107
 
109
    public final static  String[] split_str(String str,String sep)
108
    public final static  String[] split_str(String str,String sep)
110
    {
109
    {
111
 
110
 
112
        Vector nodes = new Vector();
111
        Vector nodes = new Vector();
113
 
112
 
114
        // Parse nodes into vector
113
        // Parse nodes into vector
115
        int index = str.indexOf(sep);
114
        int index = str.indexOf(sep);
116
        while(index>=0) {
115
        while(index>=0) {
117
            nodes.addElement( str.substring(0, index) );
116
            nodes.addElement( str.substring(0, index) );
118
            str = str.substring(index+sep.length());
117
            str = str.substring(index+sep.length());
119
            index = str.indexOf(sep);
118
            index = str.indexOf(sep);
120
        }
119
        }
121
        // add  last element
120
        // add  last element
122
        nodes.addElement( str );
121
        nodes.addElement( str );
123
       
122
       
124
        // Create splitted string array
123
        // Create splitted string array
125
        String[] result = new String[ nodes.size() ];
124
        String[] result = new String[ nodes.size() ];
126
        if( nodes.size()>0 ) {
125
        if( nodes.size()>0 ) {
127
            for(int loop=0; loop<nodes.size(); loop++)
126
            for(int loop=0; loop<nodes.size(); loop++)
128
                {
127
                {
129
                    result[loop] = (String)nodes.elementAt(loop);
128
                    result[loop] = (String)nodes.elementAt(loop);
130
                    System.out.println(result[loop]);
129
                    System.out.println(result[loop]);
131
                }
130
                }
132
           
131
           
133
        }
132
        }
134
 
133
 
135
        return result;
134
        return result;
136
    }
135
    }
137
 
136
 
138
 
137
 
139
 
138
 
140
    public final static  int mod_decimal(int val,int mod_power,int modder,int setter,int clipper)
139
    public final static  int mod_decimal(int val,int mod_power,int modder,int setter,int clipper)
141
    {
140
    {
142
 
141
 
143
        int res=0;
142
        int res=0;
144
 
143
 
145
        for (int power=0;power<4;power++)
144
        for (int power=0;power<4;power++)
146
            {
145
            {
147
 
146
 
148
                int act_digit=(val/pow(10,power))%10;
147
                int act_digit=(val/pow(10,power))%10;
149
 
148
 
150
                int new_digit=act_digit;
149
                int new_digit=act_digit;
151
                if (power==mod_power)
150
                if (power==mod_power)
152
                    {
151
                    {
153
                        if (setter!=-1)
152
                        if (setter!=-1)
154
                            new_digit=setter;
153
                            new_digit=setter;
155
                       
154
                       
156
                        new_digit+=modder;
155
                        new_digit+=modder;
157
                       
156
                       
158
                        if(new_digit<0)
157
                        if(new_digit<0)
159
                            new_digit=0;
158
                            new_digit=0;
160
 
159
 
161
                        if(new_digit>clipper)
160
                        if(new_digit>clipper)
162
                            new_digit=clipper;
161
                            new_digit=clipper;
163
 
162
 
164
                    }
163
                    }
165
 
164
 
166
                //              new_digit=1;
165
                //              new_digit=1;
167
                res+=new_digit*pow(10,power);
166
                res+=new_digit*pow(10,power);
168
            }
167
            }
169
       
168
       
170
       
169
       
171
        return res;
170
        return res;
172
 
171
 
173
 
172
 
174
    }
173
    }
-
 
174
 
-
 
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
 
175
 
219
 
176
}
220
}
177
 
221