Anonymous user
Simple database: Difference between revisions
D entry: shorter lines, modules themselves are namespaces, small formatting changes
m (→{{header|D}}: small change) |
(D entry: shorter lines, modules themselves are namespaces, small formatting changes) |
||
Line 637:
=={{header|D}}==
<lang d>import std.stdio, std.
std.file, std.
void main(in string[] args) {▼
if (args.length < 2 || args.length > 4) ▼
return SimDB.printUsage();▼
}
switch (args[1].toLower()) {▼
if (item.length < 3)
case "add" : SimDB.addItem(args); break;▼
case "all" : SimDB.printAll(); break;▼
}▼
db ~= Item(item[2], date, cat);
}
▲private struct SimDB {
auto db = load();
▲ static immutable filename = "simdb.csv";
if (db.empty)
▲ static struct Item { string name, date, category; }
if (a.length == 3) {
foreach (item; db)▼
if (item.category == a[2])
writefln("%s, %s, %s", item.tupleof);▼
} else {
}
auto db =
if (db.empty)
▲ auto date = (cast(DateTime)Clock.currTime()).text();
foreach (item; db)
▲ auto cat = (a.length == 4) ? a[3] : "none";
}
▲ store(db);
Item[] load() {
▲ static void printLatest(in string[] a) {
▲ return writeln("No entries in database.");
▲ sort!("a.date > b.date")(db);
if (
}
}
▲ writefln("%s, %s, %s", db[0].tupleof);
}
}
foreach (item;
}
▲ sort!("a.date < b.date")(db);
▲ foreach (item; db)
▲ writefln("%s, %s, %s", item.tupleof);
`Usage:
writeln(" add\t add item, followed by optional category");▼
simdb cmd [categoryName]
add add item, followed by optional
all print all
writeln("\n for instance: add \"some item name\" " ~▼
▲ static Item[] load() {
}
▲ Item[] db;
▲ try {
▲ if (const string text = readText(filename))
▲ return db;
▲ f.writefln("%s,%s,%s", item.tupleof);
case "latest": printLatest(args); break;
}
}</lang>
Line 757 ⟶ 759:
all print all
File:
<pre>item1,2013-Mar-03 15:01:00,cat1
|