Simple database: Difference between revisions

Content added Content deleted
(D entry: shorter lines, modules themselves are namespaces, small formatting changes)
(D entry: all functions now are module-private.)
Line 640: Line 640:
std.file, std.csv, std.datetime;
std.file, std.csv, std.datetime;


immutable filename = "simdb.csv";
private immutable filename = "simdb.csv";

struct Item {
private struct Item {
string name, date, category;
string name, date, category;
}
}


void addItem(in string[] item) {
private void addItem(in string[] item) {
if (item.length < 3)
if (item.length < 3)
return printUsage();
return printUsage();
Line 655: Line 656:
}
}


void printLatest(in string[] a) {
private void printLatest(in string[] a) {
auto db = load();
auto db = load();
if (db.empty)
if (db.empty)
Line 669: Line 670:
}
}


void printAll() {
private void printAll() {
auto db = load();
auto db = load();
if (db.empty)
if (db.empty)
Line 678: Line 679:
}
}


Item[] load() {
private Item[] load() {
Item[] db;
Item[] db;
if (filename.exists && filename.isFile) {
if (filename.exists && filename.isFile) {
Line 692: Line 693:
}
}


void store(in Item[] db) {
private void store(in Item[] db) {
auto f = File(filename, "w+");
auto f = File(filename, "w+");
foreach (item; db)
foreach (item; db)
Line 698: Line 699:
}
}


void printUsage() {
private void printUsage() {
writeln(
writeln(
`Usage:
`Usage: