AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
Sys.java
Go to the documentation of this file.
1package aud.util;
2
3import java.util.Map;
4import java.io.*;
5
58public class Sys {
59
60 static Map<String,String> env_ = null;
61
69 static class ExternalProgram {
70 String description = null;
71 String envvar = null;
72 String guess = null;
73 File program = null;
74
75 ExternalProgram(String description,String envvar,String guess,
76 boolean warn) {
77 this.description=description;
78 this.envvar=envvar;
79 this.guess=guess;
80
81 aud.Vector<String> paths=new aud.Vector<String>();
82
83 if (envvar!=null)
84 paths.push_back(env(envvar));
85 for (String p : guess.split(File.pathSeparator))
86 paths.push_back(p);
87 // for (String p : paths)
88 // System.err.println(p);
89 for (String p : paths)
90 if ((program=getProgram(p))!=null)
91 break;
92
93 // Search on windows
94 if (program==null &&
95 System.getProperty("os.name").startsWith("Windows")) {
96 program = searchOnWindows();
97 }
98
99 if (warn && program==null) {
100 System.err.println("Could not find program '"+description+"'");
101 System.err.println("Tried at paths");
102 for (String p : paths)
103 if (p!=null)
104 System.err.println(" '"+p+"'");
105 System.err.println("\n\n");
106 }
107 }
108 // provided by Kilian Gaertner
109 private File searchOnWindows() {
110 String programDir1 = System.getenv("ProgramFiles(X86)");
111 String programDir2 = System.getenv("ProgramFiles");
112 // Windows X 64 bit
113 if (programDir1 != null) {
114 File programDir = new File(programDir1);
115 for (File subDir : programDir.listFiles()) {
116 if (subDir.getName().startsWith("Graphviz")) {
117 return new File(subDir, "bin"+File.separator+"dot.exe");
118 }
119 }
120 }
121 // Windows X 32 bit
122 File programDir = new File(programDir2);
123 for (File subDir : programDir.listFiles()) {
124 if (subDir.getName().startsWith("Graphviz")) {
125 return new File(subDir, "bin"+File.separator+"dot.exe");
126 }
127 }
128 return null;
129 }
130
132 File getProgram(String path) {
133 if (path!=null) {
134 File prog=new File(path);
135 if (prog.exists())
136 return prog;
137 }
138 return null;
139 }
140
142 public boolean isAvailable() { return program!=null; }
146 public String getPath() {
147 if (program==null) {
148 String message="ERROR:\nExternal program '"+description+"' not found.\n";
149 if (guess!=null)
150 message+="The standard path is '"+guess+"'.\n";
151 if (envvar!=null) {
152 message+="Set environment variable '"+envvar+"' to customize path.";
153 if (env(envvar)!=null)
154 message+="\n(The current value is '"+envvar+"="+env(envvar)+"'.).";
155 }
156 message+="\n\n";
157
158 throw new RuntimeException(message);
159 }
160 return program.getPath();
161 }
162 }
163
164 static final ExternalProgram PDFVIEWER =
165 new ExternalProgram("PDF viewer","AUD_PDFVIEWER","/usr/bin/evince",false);
166 static final ExternalProgram EMACS =
167 new ExternalProgram("emacs editor","AUD_EMACS","/usr/bin/emacs",false);
168 static final ExternalProgram EMACSCLIENT =
169 new ExternalProgram("emacs-client editor",
170 "AUD_EMACSCLIENT","/usr/bin/emacsclient",false);
171
176 public static synchronized String env(String varname) {
177 if (env_==null)
178 env_=System.getenv();
179 return env_.get(varname);
180 }
181
183 public static String indent(int level) {
184 String spaces="";
185 for (int i=0;i<level;++i) spaces+=" ";
186 return spaces;
187 }
188
192 public static File writeToFile(File file,String text) {
193 try {
194 FileOutputStream f=new FileOutputStream(file);
195 f.write(text.getBytes());
196 f.close();
197 } catch (IOException e) {
198 System.err.println("ERROR: "+e.getMessage());
199 return null; // note: probably triggers NullPointerException
200 }
201 return file;
202 }
206 public static File writeToTempFile(String text,String suffix) {
207 try {
208 return writeToFile(File.createTempFile("aud-",suffix),text);
209 } catch (IOException e) {
210 System.err.println("ERROR: "+e.getMessage());
211 return null; // note: probably triggers NullPointerException
212 }
213 }
214
216 public static String readFile(File file) {
217 byte[] buffer = new byte[(int) file.length()];
218 BufferedInputStream f = null;
219 try {
220 f = new BufferedInputStream(new FileInputStream(file));
221 f.read(buffer);
222 } catch (IOException e) {
223 System.err.println("ERROR: "+e.getMessage());
224 return null; // note: probably triggers NullPointerException
225 } finally {
226 if (f != null) try { f.close(); } catch (IOException ignored) { }
227 }
228 return new String(buffer);
229 }
230
236 //@SuppressWarnings( "deprecation" )
237 public static void execAndDetach(String command) {
238 Thread t=new Thread(new Runnable()
239 {
240 public void run() {
241 try {
242 String cmd=Thread.currentThread().getName();
243 //System.err.println(cmd);
244 //Runtime.getRuntime().exec(cmd); // fix deprecation (since Java 18)
245 new ProcessBuilder(cmd.split(" ")).start();
246 } catch(IOException e) {
247 e.printStackTrace();
248 }
249 }
250 },command);
251 t.start();
252 }
253
255 public static void viewPDFFile(String filename) {
256 Sys.execAndDetach(PDFVIEWER.getPath()+" "+filename);
257 }
258
260 public static String whereAmI(int depth) {
261 StackTraceElement location=new Throwable().getStackTrace()[depth+1];
262 String rv=location.getFileName().toString()+":";
263 rv+=location.getLineNumber()+" ";
264 rv+=location.getClassName()+"#"+location.getMethodName();
265 return rv;
266 }
268 public static String whereAmI() {
269 return whereAmI(0);
270 }
271
273 public static void emacsclient(String file,int line,int column) {
274 if (EMACS.isAvailable() && EMACSCLIENT.isAvailable()) {
275 String command=
276 EMACSCLIENT.getPath()+" -a "+EMACS.getPath()+" -n +"+line+
277 (column>0 ? ":"+column+" " : " ")+file;
278 System.err.println(command);
279 execAndDetach(command);
280 }
281 }
282
284 public static void openCallersSourceInEmacs(int depth) {
285 StackTraceElement location=new Throwable().getStackTrace()[depth+1];
286 File file=new File(location.getFileName().toString());
287 if (file.exists())
288 emacsclient(file.getPath(),location.getLineNumber(),0);
289 else
290 System.err.println("unknown path for source file '"+file+"'");
291 }
292
293 //
294 // Same for gedit !
295 //
296}
Implementation of an array-based vector.
Definition: Vector.java:44
void push_back(T obj)
insert new entry obj at the end [O(1) for sufficient capacity]
Definition: Vector.java:155
System related utilities.
Definition: Sys.java:58
static synchronized String env(String varname)
Get environment variable varname.
Definition: Sys.java:176
static String indent(int level)
get indentation string filled with spaces
Definition: Sys.java:183
static void emacsclient(String file, int line, int column)
open emacs client (Un*x only) (or no action otherwise)
Definition: Sys.java:273
static File writeToTempFile(String text, String suffix)
write text to temporary file
Definition: Sys.java:206
static void execAndDetach(String command)
Execute command in a new process and detach.
Definition: Sys.java:237
static File writeToFile(File file, String text)
write text to file @endiliteral
Definition: Sys.java:192
static String whereAmI()
get code location (like __FILE__,__LINE__
Definition: Sys.java:268
static String readFile(File file)
read entire file and return contents as String
Definition: Sys.java:216
static String whereAmI(int depth)
get code location (like __FILE__,__LINE__
Definition: Sys.java:260
static void viewPDFFile(String filename)
open PDF viewer
Definition: Sys.java:255
static void openCallersSourceInEmacs(int depth)
open emacs whereAmI (Un*x only)
Definition: Sys.java:284
AuD lecture: Data structures, algorithms, examples.