srcanaapps/depexplorer/com.nokia.s60tools.appdep/src/com/nokia/s60tools/appdep/core/CacheCreationProgressLineReader.java
changeset 0 a02c979e8dfd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/srcanaapps/depexplorer/com.nokia.s60tools.appdep/src/com/nokia/s60tools/appdep/core/CacheCreationProgressLineReader.java	Sat Jan 09 10:04:11 2010 +0530
@@ -0,0 +1,82 @@
+/*
+* Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 
+* All rights reserved.
+* This component and the accompanying materials are made available
+* under the terms of "Eclipse Public License v1.0"
+* which accompanies this distribution, and is available
+* at the URL "http://www.eclipse.org/legal/epl-v10.html".
+*
+* Initial Contributors:
+* Nokia Corporation - initial contribution.
+*
+* Contributors:
+*
+* Description:
+*
+*/
+ 
+ 
+package com.nokia.s60tools.appdep.core;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+
+import com.nokia.s60tools.util.cmdline.ICmdLineCommandExecutorObserver;
+import com.nokia.s60tools.util.cmdline.ICustomLineReader;
+
+public class CacheCreationProgressLineReader implements ICustomLineReader {
+
+	public CacheCreationProgressLineReader() {
+	}
+
+    private void filterProgressStatus(String line, 
+			   						  ICmdLineCommandExecutorObserver observer){
+    	String[] splitArr = line.split("%"); //$NON-NLS-1$
+    	if(splitArr.length == 2){
+	    	String token1 = splitArr[0];
+	    	String progressPercentageStr = token1.substring(token1.length()-3);
+	    	int progressPercentageInt = Integer.parseInt(progressPercentageStr.trim());
+	    	observer.progress(progressPercentageInt);
+    	}
+    }
+
+	public String readLine(BufferedReader br, 
+						   ICmdLineCommandExecutorObserver observer) throws IOException {
+		
+    	StringBuffer line = new StringBuffer(""); //$NON-NLS-1$
+    	// Marking is used to go back one character in the stresm after all the
+    	// backspaces are skipped. So we allow reading of one character and
+    	// the mark is still preserved.
+    	final int readAheadLimit = 1;
+    	int charAsInteger;
+    	char ch;
+    	while((charAsInteger = br.read()) != -1){
+    		ch = (char) charAsInteger;
+    		if(ch == '\n' || ch == '\r'){
+    			 filterProgressStatus(line.toString(), observer);
+	    		 return line.toString();
+    		}
+    		else if(ch == '\b'){
+    			// Consuming all the backspaces
+    			while(ch == '\b'){
+    				// Marking the place before read
+    				br.mark(readAheadLimit);
+    				charAsInteger = br.read();
+    				if(charAsInteger == -1){
+    					break;
+    				}
+    	    		ch = (char) charAsInteger;
+    			}
+    			// Going back into marked place (one character back)
+    			br.reset();
+    			 filterProgressStatus(line.toString(), observer);
+	    		return line.toString();
+    		}
+    		else{
+    			line.append(ch);	    			
+    		}
+    	}
+    	return null;
+	}
+
+}