1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

new method parseListTasks();.

This commit is contained in:
Alain Magloire 2003-04-30 16:06:10 +00:00
parent 790c73438f
commit 80e6ea86a0

View file

@ -1,14 +1,22 @@
package org.eclipse.cdt.internal.core.win32; package org.eclipse.cdt.internal.core.win32;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.StreamTokenizer; import java.io.StreamTokenizer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.IProcessInfo; import org.eclipse.cdt.core.IProcessInfo;
import org.eclipse.cdt.core.IProcessList; import org.eclipse.cdt.core.IProcessList;
import org.eclipse.cdt.utils.spawner.ProcessFactory; import org.eclipse.cdt.utils.spawner.ProcessFactory;
import org.eclipse.core.runtime.IPluginDescriptor;
import org.eclipse.core.runtime.model.PluginDescriptorModel;
import org.eclipse.core.runtime.model.PluginFragmentModel;
/* /*
* Currently this will only work for Windows XP since tasklist * Currently this will only work for Windows XP since tasklist
@ -19,6 +27,7 @@ import org.eclipse.cdt.utils.spawner.ProcessFactory;
public class ProcessList implements IProcessList { public class ProcessList implements IProcessList {
private IProcessInfo[] NOPROCESS = new IProcessInfo[0];
private final int NAME = 1; private final int NAME = 1;
private final int PID = 2; private final int PID = 2;
private final int OTHER = 3; private final int OTHER = 3;
@ -27,20 +36,49 @@ public class ProcessList implements IProcessList {
String OS = System.getProperty("os.name").toLowerCase(); String OS = System.getProperty("os.name").toLowerCase();
Process p = null; Process p = null;
String command = null; String command = null;
InputStream in = null; InputStream in = null;
if ((OS.indexOf("windows xp") > -1)) { if ((OS.indexOf("windows xp") > -1)) {
command = "tasklist /fo csv /nh /svc"; command = "tasklist /fo csv /nh /svc";
try {
p = ProcessFactory.getFactory().exec(command);
in = p.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
return parseTaskList(reader);
} catch (IOException e) {
}
} else { } else {
return new IProcessInfo[0]; IPluginDescriptor desc = CCorePlugin.getDefault().getDescriptor();
if (desc instanceof PluginDescriptorModel) {
PluginDescriptorModel model = (PluginDescriptorModel) desc;
PluginFragmentModel[] fragments = model.getFragments();
for (int i = 0; i < fragments.length; i++) {
String location = fragments[i].getLocation();
try {
URL url = new URL(location);
File path = new File(url.getFile(), "listtasks");
if (path.exists()) {
command = path.getCanonicalPath();
break;
}
} catch (MalformedURLException e1) {
} catch (IOException e) {
}
}
}
if (command != null) {
try {
p = ProcessFactory.getFactory().exec(command);
in = p.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
return parseListTasks(reader);
} catch (IOException e) {
}
}
} }
try { return NOPROCESS;
p = ProcessFactory.getFactory().exec(command); }
}
catch (IOException e) { public IProcessInfo[] parseTaskList(InputStreamReader reader) {
return null;
}
in = p .getInputStream();
InputStreamReader reader = new InputStreamReader(in);
StreamTokenizer tokenizer = new StreamTokenizer(reader); StreamTokenizer tokenizer = new StreamTokenizer(reader);
tokenizer.eolIsSignificant(true); tokenizer.eolIsSignificant(true);
tokenizer.parseNumbers(); tokenizer.parseNumbers();
@ -48,42 +86,64 @@ public class ProcessList implements IProcessList {
ArrayList processList = new ArrayList(); ArrayList processList = new ArrayList();
String name = null; String name = null;
int pid = 0, token_state = NAME; int pid = 0, token_state = NAME;
while( !done ) { while (!done) {
try { try {
switch ( tokenizer.nextToken() ) { switch (tokenizer.nextToken()) {
case StreamTokenizer.TT_EOL: case StreamTokenizer.TT_EOL :
if ( name != null ) { if (name != null) {
processList.add(new ProcessInfo(pid, name)); processList.add(new ProcessInfo(pid, name));
name = null; name = null;
} }
break; break;
case StreamTokenizer.TT_EOF: case StreamTokenizer.TT_EOF :
done = true; done = true;
break; break;
case '"': case '"' :
switch ( token_state ) { switch (token_state) {
case NAME: case NAME :
name = tokenizer.sval; name = tokenizer.sval;
token_state = PID; token_state = PID;
break; break;
case PID: case PID :
try { try {
pid = Integer.parseInt(tokenizer.sval); pid = Integer.parseInt(tokenizer.sval);
} catch (NumberFormatException e ) { } catch (NumberFormatException e) {
name = null; name = null;
} }
token_state = OTHER; token_state = OTHER;
break; break;
case OTHER: case OTHER :
token_state = NAME; token_state = NAME;
break; break;
} }
break; break;
} }
} } catch (IOException e) {
catch (IOException e) {
} }
} }
return (IProcessInfo[]) processList.toArray(new IProcessInfo[processList.size()]); return (IProcessInfo[]) processList.toArray(new IProcessInfo[processList.size()]);
} }
public IProcessInfo[] parseListTasks(InputStreamReader reader) {
BufferedReader br = new BufferedReader(reader);
ArrayList processList = new ArrayList();
try {
String line;
while ((line = br.readLine()) != null) {
int tab = line.indexOf('\t');
if (tab != -1) {
String proc = line.substring(0, tab).trim();
String name = line.substring(tab).trim();
try {
int pid = Integer.parseInt(proc);
processList.add(new ProcessInfo(pid, name));
} catch (NumberFormatException e) {
name = null;
}
}
}
} catch (IOException e) {
}
return (IProcessInfo[]) processList.toArray(new IProcessInfo[processList.size()]);
}
} }