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;
@ -30,17 +39,46 @@ public class ProcessList implements IProcessList {
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";
} else {
return new IProcessInfo[0];
}
try { try {
p = ProcessFactory.getFactory().exec(command); p = ProcessFactory.getFactory().exec(command);
}
catch (IOException e) {
return null;
}
in = p.getInputStream(); in = p.getInputStream();
InputStreamReader reader = new InputStreamReader(in); InputStreamReader reader = new InputStreamReader(in);
return parseTaskList(reader);
} catch (IOException e) {
}
} else {
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) {
}
}
}
return NOPROCESS;
}
public IProcessInfo[] parseTaskList(InputStreamReader reader) {
StreamTokenizer tokenizer = new StreamTokenizer(reader); StreamTokenizer tokenizer = new StreamTokenizer(reader);
tokenizer.eolIsSignificant(true); tokenizer.eolIsSignificant(true);
tokenizer.parseNumbers(); tokenizer.parseNumbers();
@ -80,10 +118,32 @@ public class ProcessList implements IProcessList {
} }
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()]);
}
} }