AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.util.Sys Class Reference

System related utilities. More...

+ Collaboration diagram for aud.util.Sys:

Classes

class  ExternalProgram
 Get path of an external program.
 

Static Public Member Functions

static synchronized String env (String varname)
 Get environment variable varname. More...
 
static String indent (int level)
 get indentation string filled with spaces More...
 
static File writeToFile (File file, String text)
 write text to file @endiliteral
More...
 
static File writeToTempFile (String text, String suffix)
 write text to temporary file More...
 
static String readFile (File file)
 read entire file and return contents as String
More...
 
static void execAndDetach (String command)
 Execute command in a new process and detach. More...
 
static void viewPDFFile (String filename)
 open PDF viewer More...
 
static String whereAmI (int depth)
 get code location (like __FILE__,__LINE__
More...
 
static String whereAmI ()
 get code location (like __FILE__,__LINE__
More...
 
static void emacsclient (String file, int line, int column)
 open emacs client (Un*x only) (or no action otherwise) More...
 
static void openCallersSourceInEmacs (int depth)
 open emacs whereAmI (Un*x only) More...
 

Detailed Description

System related utilities.

Some functions require external tools. The paths of the tools can be set as envionment variables.

Required GraphViz binaries
tool environment variable default path (colon-separated list)

purpose

PDF viewer AUD_PDFVIEWER /usr/bin/evince

view PDF files

GraphViz dot AUD_DOT /usr/bin/dot:/usr/local/bin/dot:/local/usr/bin/dot:/opt/homebrew/bin

layout and render graphs

Emacs AUD_EMACS /usr/bin/emacs

editor (Un*x only)

Emacs client AUD_EMACSCLIENT /usr/bin/emacsclient

editor (Un*x only)

Notes

  • This is not "production code"! There are tests missing, and not all potential errors are handled. In this sense, this is "bad" code!

  • In particular, some functions just return null on error and use the fact that Java checks references and would raise a NullPointerException if return values are not checked. – Don't try this in C++! Never!

Definition at line 58 of file Sys.java.

Member Function Documentation

◆ emacsclient()

static void aud.util.Sys.emacsclient ( String  file,
int  line,
int  column 
)
static

open emacs client (Un*x only) (or no action otherwise)

Definition at line 273 of file Sys.java.

273 {
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 }
static void execAndDetach(String command)
Execute command in a new process and detach.
Definition: Sys.java:237

Referenced by aud.util.Sys.openCallersSourceInEmacs().

+ Here is the caller graph for this function:

◆ env()

static synchronized String aud.util.Sys.env ( String  varname)
static

Get environment variable varname.

Parameters
varnamevariable name
Returns
value or null if undefined

Definition at line 176 of file Sys.java.

176 {
177 if (env_==null)
178 env_=System.getenv();
179 return env_.get(varname);
180 }

Referenced by aud.util.SingleStepper.SingleStepper().

+ Here is the caller graph for this function:

◆ execAndDetach()

static void aud.util.Sys.execAndDetach ( String  command)
static

Execute command in a new process and detach.

Java seems unable to "really" detach processes, so we start the process within a new Java thread.

Parameters
commandsame as for Runtime.exec

Definition at line 237 of file Sys.java.

237 {
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 }

Referenced by aud.util.Sys.viewPDFFile().

+ Here is the caller graph for this function:

◆ indent()

static String aud.util.Sys.indent ( int  level)
static

get indentation string filled with spaces

Definition at line 183 of file Sys.java.

183 {
184 String spaces="";
185 for (int i=0;i<level;++i) spaces+=" ";
186 return spaces;
187 }

Referenced by aud.BinaryTree< T >.toTikZ().

+ Here is the caller graph for this function:

◆ openCallersSourceInEmacs()

static void aud.util.Sys.openCallersSourceInEmacs ( int  depth)
static

open emacs whereAmI (Un*x only)

Definition at line 284 of file Sys.java.

284 {
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 }
static void emacsclient(String file, int line, int column)
open emacs client (Un*x only) (or no action otherwise)
Definition: Sys.java:273

References aud.util.Sys.emacsclient().

Referenced by aud.util.SingleStepper.showSource().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ readFile()

static String aud.util.Sys.readFile ( File  file)
static

read entire file and return contents as String

Definition at line 216 of file Sys.java.

216 {
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 }

Referenced by aud.util.DotViewer.main().

+ Here is the caller graph for this function:

◆ viewPDFFile()

static void aud.util.Sys.viewPDFFile ( String  filename)
static

open PDF viewer

Definition at line 255 of file Sys.java.

255 {
256 Sys.execAndDetach(PDFVIEWER.getPath()+" "+filename);
257 }

References aud.util.Sys.execAndDetach().

Referenced by aud.util.Graphviz.displayAsPDF().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ whereAmI() [1/2]

static String aud.util.Sys.whereAmI ( )
static

get code location (like __FILE__,__LINE__

Definition at line 268 of file Sys.java.

268 {
269 return whereAmI(0);
270 }
static String whereAmI()
get code location (like __FILE__,__LINE__
Definition: Sys.java:268

References aud.util.Sys.whereAmI().

Referenced by aud.util.Sys.whereAmI().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ whereAmI() [2/2]

static String aud.util.Sys.whereAmI ( int  depth)
static

get code location (like __FILE__,__LINE__

Definition at line 260 of file Sys.java.

260 {
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 }

Referenced by aud.util.SingleStepper.halt(), and aud.util.SingleStepper.whereAmI().

+ Here is the caller graph for this function:

◆ writeToFile()

static File aud.util.Sys.writeToFile ( File  file,
String  text 
)
static

write text to file @endiliteral

Returns
file

Definition at line 192 of file Sys.java.

192 {
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 }

Referenced by aud.util.DotViewer.display(), and aud.util.Sys.writeToTempFile().

+ Here is the caller graph for this function:

◆ writeToTempFile()

static File aud.util.Sys.writeToTempFile ( String  text,
String  suffix 
)
static

write text to temporary file

Returns
file

Definition at line 206 of file Sys.java.

206 {
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 }
static File writeToFile(File file, String text)
write text to file @endiliteral
Definition: Sys.java:192

References aud.util.Sys.writeToFile().

Referenced by aud.util.Graphviz.displayAsPDF().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: