mirror of
https://github.com/xomboverlord/ldc.git
synced 2026-08-28 17:20:04 +02:00
[svn r136] MAJOR UNSTABLE UPDATE!!!
Initial commit after moving to Tango instead of Phobos. Lots of bugfixes... This build is not suitable for most things.
This commit is contained in:
97
tango/example/system/arguments.d
Normal file
97
tango/example/system/arguments.d
Normal file
@@ -0,0 +1,97 @@
|
||||
/*******************************************************************************
|
||||
Illustrates use of the Arguments class.
|
||||
*******************************************************************************/
|
||||
|
||||
import tango.util.Arguments;
|
||||
import tango.io.Stdout;
|
||||
import tango.io.FileConduit;
|
||||
import tango.text.stream.LineIterator;
|
||||
|
||||
void usage()
|
||||
{
|
||||
Stdout("Usage: [OPTIONS]... FILES...").newline;
|
||||
Stdout("This is a program that does something.").newline;
|
||||
Stdout.newline;
|
||||
Stdout("OPTIONS: ").newline;
|
||||
Stdout("Output this help message: -?, --help").newline;
|
||||
Stdout("Do cool things to your files: -c, -C, --cool").newline;
|
||||
Stdout("Use filename as response file: -r, -R, --response").newline;
|
||||
}
|
||||
|
||||
void main(char[][] cmdlArgs)
|
||||
{
|
||||
char[][] implicitArguments = ["files"];
|
||||
|
||||
char[][][] argumentAliases;
|
||||
argumentAliases ~= ["help", "?"];
|
||||
argumentAliases ~= ["cool", "C", "c"];
|
||||
argumentAliases ~= ["response", "R", "r"];
|
||||
|
||||
auto args = new Arguments(cmdlArgs, implicitArguments, argumentAliases);
|
||||
|
||||
bool fileExistsValidation(char[] arg)
|
||||
{
|
||||
bool rtn;
|
||||
FilePath argFile = new FilePath(arg);
|
||||
rtn = argFile.exists;
|
||||
if (!rtn)
|
||||
Stdout.format("Specified path does not exist: {}", arg).newline;
|
||||
return rtn;
|
||||
}
|
||||
|
||||
bool singleFileValidation(char[][] args, inout char[] invalidArg)
|
||||
{
|
||||
if (args.length > 1)
|
||||
{
|
||||
Stdout("Cannot specify multiple paths for argument.").newline;
|
||||
invalidArg = args[1];
|
||||
}
|
||||
else
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
args.addValidation("response", &fileExistsValidation);
|
||||
args.addValidation("response", &singleFileValidation);
|
||||
args.addValidation("files", true, true);
|
||||
|
||||
bool argsValidated = true;
|
||||
try
|
||||
args.validate;
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
if (ex.reason == ArgumentException.ExceptionReason.MISSING_ARGUMENT)
|
||||
Stdout.format("Missing Argument: {} ({})", ex.name, ex.msg).newline;
|
||||
else if (ex.reason == ArgumentException.ExceptionReason.MISSING_PARAMETER)
|
||||
Stdout.format("Missing Parameter to Argument: {} ({})", ex.name, ex.msg).newline;
|
||||
else if (ex.reason == ArgumentException.ExceptionReason.INVALID_PARAMETER)
|
||||
Stdout.format("Invalid Parameter: {} ({})", ex.name, ex.msg).newline;
|
||||
Stdout.newline;
|
||||
argsValidated = false;
|
||||
}
|
||||
|
||||
if ((!argsValidated) || ("help" in args))
|
||||
usage();
|
||||
else
|
||||
{// ready to run
|
||||
if ("response" in args)
|
||||
{
|
||||
Stdout(args["response"][0]).newline;
|
||||
auto file = new FileConduit(args["response"][0]);
|
||||
auto lines = new LineIterator!(char)(file);
|
||||
char[][] arguments;
|
||||
foreach (line; lines)
|
||||
arguments ~= line.dup;
|
||||
args.parse(arguments, implicitArguments, argumentAliases);
|
||||
}
|
||||
if ("cool" in args)
|
||||
{
|
||||
Stdout ("Listing the files to be actioned in a cool way.").newline;
|
||||
foreach (char[] file; args["files"])
|
||||
Stdout.format("{}", file).newline;
|
||||
Stdout ("Cool and secret action performed.").newline;
|
||||
}
|
||||
if ("x" in args)
|
||||
Stdout.format("User set the X factor to '{}'", args["x"]).newline;
|
||||
}
|
||||
}
|
||||
54
tango/example/system/localtime.d
Normal file
54
tango/example/system/localtime.d
Normal file
@@ -0,0 +1,54 @@
|
||||
/*******************************************************************************
|
||||
|
||||
localtime.d
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
private import tango.io.Stdout;
|
||||
|
||||
private import tango.time.WallClock;
|
||||
|
||||
/******************************************************************************
|
||||
|
||||
Example code to format a local time in the following format:
|
||||
"Wed Dec 31 16:00:00 GMT-0800 1969". The day and month names
|
||||
would typically be extracted from a locale instance, but we
|
||||
convert them locally here for the sake of simplicity
|
||||
|
||||
******************************************************************************/
|
||||
|
||||
void main ()
|
||||
{
|
||||
/// list of day names
|
||||
static char[][] days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||
|
||||
/// list of month names
|
||||
static char[][] months =
|
||||
[
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
|
||||
];
|
||||
|
||||
// retreive local time
|
||||
auto dt = WallClock.toDate;
|
||||
|
||||
// get GMT difference in minutes
|
||||
auto tz = cast(int) WallClock.zone.minutes;
|
||||
char sign = '+';
|
||||
if (tz < 0)
|
||||
tz = -tz, sign = '-';
|
||||
|
||||
// format date
|
||||
Stdout.formatln ("{}, {} {:d2} {:d2}:{:d2}:{:d2} GMT{}{:d2}:{:d2} {}",
|
||||
days[dt.date.dow],
|
||||
months[dt.date.month-1],
|
||||
dt.date.day,
|
||||
dt.time.hours,
|
||||
dt.time.minutes,
|
||||
dt.time.seconds,
|
||||
sign,
|
||||
tz / 60,
|
||||
tz % 60,
|
||||
dt.date.year
|
||||
);
|
||||
}
|
||||
51
tango/example/system/normpath.d
Normal file
51
tango/example/system/normpath.d
Normal file
@@ -0,0 +1,51 @@
|
||||
/*****************************************************************
|
||||
|
||||
Simple example that shows possible inputs to normalize and the
|
||||
corresponding outputs.
|
||||
|
||||
Put into public domain by Lars Ivar Igesund.
|
||||
|
||||
*****************************************************************/
|
||||
|
||||
import tango.io.Stdout;
|
||||
|
||||
import tango.util.PathUtil;
|
||||
|
||||
int main()
|
||||
{
|
||||
version (Posix) {
|
||||
Stdout(normalize ( "/foo/../john")).newline;
|
||||
Stdout(normalize ( "foo/../john")).newline;
|
||||
Stdout(normalize ( "foo/bar/..")).newline;
|
||||
Stdout(normalize ( "foo/bar/../john")).newline;
|
||||
Stdout(normalize ( "foo/bar/doe/../../john")).newline;
|
||||
Stdout(normalize ( "foo/bar/doe/../../john/../bar")).newline;
|
||||
Stdout(normalize ( "./foo/bar/doe")).newline;
|
||||
Stdout(normalize ( "./foo/bar/doe/../../john/../bar")).newline;
|
||||
Stdout(normalize ( "./foo/bar/../../john/../bar")).newline;
|
||||
Stdout(normalize ( "foo/bar/./doe/../../john")).newline;
|
||||
Stdout(normalize ( "../../foo/bar/./doe/../../john")).newline;
|
||||
Stdout(normalize ( "../../../foo/bar")).newline;
|
||||
Stdout("** Should now throw exception as the following path is invalid for normalization.").newline;
|
||||
Stdout(normalize ( "/../../../foo/bar")).newline;
|
||||
}
|
||||
version (Windows) {
|
||||
Stdout(normalize ( "C:\\foo\\..\\john")).newline;
|
||||
Stdout(normalize ( "foo\\..\\john")).newline;
|
||||
Stdout(normalize ( "foo\\bar\\..")).newline;
|
||||
Stdout(normalize ( "foo\\bar\\..\\john")).newline;
|
||||
Stdout(normalize ( "foo\\bar\\doe\\..\\..\\john")).newline;
|
||||
Stdout(normalize ( "foo\\bar\\doe\\..\\..\\john\\..\\bar")).newline;
|
||||
Stdout(normalize ( ".\\foo\\bar\\doe")).newline;
|
||||
Stdout(normalize ( ".\\foo\\bar\\doe\\..\\..\\john\\..\\bar")).newline;
|
||||
Stdout(normalize ( ".\\foo\\bar\\..\\..\\john\\..\\bar")).newline;
|
||||
Stdout(normalize ( "foo\\bar\\.\\doe\\..\\..\\john")).newline;
|
||||
Stdout(normalize ( "..\\..\\foo\\bar\\.\\doe\\..\\..\\john")).newline;
|
||||
Stdout(normalize ( "..\\..\\..\\foo\\bar")).newline;
|
||||
Stdout("** Should now throw exception as the following path is invalid for normalization.").newline;
|
||||
Stdout(normalize ( "C:\\..\\..\\..\\foo\\bar")).newline;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
60
tango/example/system/process.d
Normal file
60
tango/example/system/process.d
Normal file
@@ -0,0 +1,60 @@
|
||||
/*******************************************************************************
|
||||
copyright: Copyright (c) 2006 Juan Jose Comellas. All rights reserved
|
||||
license: BSD style: $(LICENSE)
|
||||
author: Juan Jose Comellas <juanjo@comellas.com.ar>
|
||||
*******************************************************************************/
|
||||
|
||||
private import tango.io.Stdout;
|
||||
private import tango.sys.Process;
|
||||
private import tango.core.Exception;
|
||||
|
||||
private import tango.text.stream.LineIterator;
|
||||
|
||||
|
||||
/**
|
||||
* Example program for the tango.sys.Process class.
|
||||
*/
|
||||
void main()
|
||||
{
|
||||
version (Windows)
|
||||
char[] command = "ping -n 4 localhost";
|
||||
else version (Posix)
|
||||
char[] command = "ping -c 4 localhost";
|
||||
else
|
||||
assert(false, "Unsupported platform");
|
||||
|
||||
try
|
||||
{
|
||||
auto p = new Process(command, null);
|
||||
|
||||
Stdout.formatln("Executing {0}", p.toString());
|
||||
p.execute();
|
||||
|
||||
Stdout.formatln("Output from process: {0} (pid {1})\n---",
|
||||
p.programName, p.pid);
|
||||
|
||||
foreach (line; new LineIterator!(char)(p.stdout))
|
||||
{
|
||||
Stdout.formatln("{0}", line);
|
||||
}
|
||||
|
||||
Stdout.print("---\n");
|
||||
|
||||
auto result = p.wait();
|
||||
|
||||
Stdout.formatln("Process '{0}' ({1}) finished: {2}",
|
||||
p.programName, p.pid, result.toString());
|
||||
}
|
||||
catch (ProcessException e)
|
||||
{
|
||||
Stdout.formatln("Process execution failed: {0}", e.toString());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
Stdout.formatln("Input/output exception caught: {0}", e.toString());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Stdout.formatln("Unexpected exception caught: {0}", e.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user