1 57 58 59 60 package util; 61 62 63 64 85 86 public class Arguments { 87 private boolean fDbug = false; 88 private Queue queueOfSwitches = new Queue(20); 89 private Queue queueStringParameters = new Queue(20); 90 private Queue queueOfOtherStringParameters = new Queue(20); 91 private String [] messageArray = null; 92 private int lastPopArgument = 0; 93 94 95 public Arguments() { 96 } 97 98 128 public void parseArgumentTokens( String [] arguments , char[] argsWithOptions ){ 129 int theDash = 0; 130 int lengthOfToken = 0; 131 char []bufferOfToken = null; 132 Object [] temp; 133 134 int argLength = arguments.length; 135 136 outer: 137 for ( int i = 0; i<argLength; i++ ){ 138 bufferOfToken = arguments[i].toCharArray(); 139 lengthOfToken = bufferOfToken.length; 140 if ( bufferOfToken[0] == '-' ){ 141 int token; 142 for ( int j = 1; j<lengthOfToken; j++ ){ 145 token = bufferOfToken[j]; 146 queueOfSwitches.push( (Object ) new Integer ( token )); 148 for ( int k = 0; k< argsWithOptions.length; k++) { 149 if ( token == argsWithOptions[k] ){ 150 if ( this.fDbug ) { 151 System.out.println( "token = " + token ); 152 } 153 queueStringParameters.push( arguments[++i] ); 155 continue outer; 156 } 157 } 158 159 } 160 161 if ( i+1 < argLength ){ 162 if ( !( arguments[i+1].charAt(0) == '-') ) queueOfSwitches.push( (Object ) new Integer ( -1 )); } 165 166 } else{ 167 queueOfOtherStringParameters.push( arguments[i] ); 168 } 169 } 170 171 172 if ( this.fDbug ) { 173 queueOfSwitches.print(); 174 queueStringParameters.print(); 175 queueOfOtherStringParameters.print(); 176 } 177 } 178 179 180 184 public int getArguments(){ 185 if ( this.fDbug ){ 186 queueOfSwitches.print(); 187 } 188 189 return queueOfSwitches.empty() ? -1:((Integer ) queueOfSwitches.pop()).intValue(); 194 } 195 196 197 198 202 public String getStringParameter(){ 203 String s = (String ) queueStringParameters.pop(); 204 if ( this.fDbug ){ 205 queueStringParameters.print(); 206 } 207 if ( this.fDbug ) { 208 System.out.println( "string par = " + s ); 209 } 210 return s; 211 } 212 213 214 public String getlistFiles(){ 215 216 if ( this.fDbug ) { 217 queueOfOtherStringParameters.print(); 218 } 219 220 String s = (String ) queueOfOtherStringParameters.pop(); 221 return s; 222 } 223 224 225 226 public int stringParameterLeft( ){ 227 return queueStringParameters.size(); 228 } 229 230 231 public void setUsage( String [] message ){ 232 messageArray = message; 233 } 234 235 public void printUsage() { 236 for ( int i = 0; i< messageArray.length; i++ ){ 237 System.err.println( messageArray[i] ); 238 } 239 } 240 241 243 245 246 private static final int maxIncrement = 10; 247 248 private class Queue { 249 private Object [] queue; 251 private int max; 252 private int front; 253 private int rear; 254 private int items; 255 256 257 public Queue( int size) { 258 queue = new Object [size]; 259 front = 0; 260 rear = -1; 261 items = 0; 262 max = size; 263 } 265 public void push( Object token ) { 266 try { 267 queue[++rear] = token; 268 items++; 269 } catch ( ArrayIndexOutOfBoundsException ex ){ 270 Object [] holdQueue = new Object [max + maxIncrement]; 271 System.arraycopy(queue, 0, holdQueue,0,max ); 272 queue = holdQueue; 273 max += maxIncrement; 274 queue[rear] = token; 275 items++; 276 } 277 278 } 280 public Object pop() { 281 Object token = null; 282 if ( items != 0 ) { 283 token = queue[front++]; 284 items--; 285 } 286 return token; 287 } 288 public boolean empty(){ 289 return(items==0); 290 } 291 292 public int size(){ 293 return items; 294 } 295 296 public void clear(){ 297 front = 0; 298 rear = -1; 299 items = 0; 300 } 301 302 303 public void print(){ 304 for ( int i = front; i <= rear;i++ ){ 305 System.out.println( "token[ " + i 306 + "] = " + queue[i] ) ; 307 } 308 309 } 310 311 } 312 } 313 | Popular Tags |