From 5eac3f26dccda7d7ff367fd96b8412de9806348f Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 27 Feb 2020 17:12:07 +0100 Subject: [PATCH] Save region, clear edition before saving. --- tools/csv2sqlite/main.d | 45 +++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/tools/csv2sqlite/main.d b/tools/csv2sqlite/main.d index 8bf119f..1b3f7f3 100644 --- a/tools/csv2sqlite/main.d +++ b/tools/csv2sqlite/main.d @@ -3,9 +3,10 @@ import std.csv; import std.algorithm; import std.typecons; import etc.c.sqlite3; -import std.string : toStringz, fromStringz, empty, strip, split; +import std.string; import std.exception; import std.conv : to; +import std.path : baseName; private class SQLiteException : Exception { this(int code, string msg1, string msg2, string file = __FILE__, size_t line = __LINE__) { @@ -17,6 +18,36 @@ extern(C) private int callback(void */*NotUsed*/, int /*argc*/, char **/*argv*/, return 0; } +enum Regions : int { + Unknown, Europe, Japan, USA, Asia, Germany, Spain, Korea, All, Italy, + France, Russia, Poland, Netherlands, Portugal, India, Belgium, + UnitedKingdom, Australia +}; + +private int region_id(string path) pure { + switch (baseName(path, ".png")) { + case "E": return Regions.Europe; + case "J": return Regions.Japan; + case "U": return Regions.USA; + case "A": return Regions.Asia; + case "G": return Regions.Germany; + case "S": return Regions.Spain; + case "K": return Regions.Korea; + case "W": return Regions.All; + case "I": return Regions.Italy; + case "F": return Regions.France; + case "R": return Regions.Russia; + case "P": return Regions.Poland; + case "N": return Regions.Netherlands; + case "Pt": return Regions.Portugal; + case "In": return Regions.India; + case "Be": return Regions.Belgium; + case "Uk": return Regions.UnitedKingdom; + case "Au": return Regions.Australia; + default: return Regions.Unknown; + } +} + private void insert_into_sqlite(sqlite3* db, string path) { auto file = File(path, "r"); sqlite3_stmt* statement; @@ -33,15 +64,13 @@ private void insert_into_sqlite(sqlite3* db, string path) { // Expected CSV format: title,edition,version,languages,serial,region foreach (record; file.byLine.joiner("\n").csvReader!(Tuple!(string, string, string, string, string, string))) { const int ver = cast(int)((record[2].empty ? 0.0 : to!double(record[2])) * 100.0); - string[] serials = record[4].split(";"); + string serials = record[4]; string languages = record[3]; - string edition = record[1]; + string edition = record[1].splitter(";").map!strip.join(";"); string title = record[0]; string region = record[5]; - const int region_id = 0; - foreach (serial; serials) { - serial = serial.strip; + foreach (serial; serials.splitter(";").map!strip) { if (serial in serials_seen) { continue; } @@ -55,8 +84,8 @@ private void insert_into_sqlite(sqlite3* db, string path) { else sqlite3_bind_null(statement, 4); sqlite3_bind_text(statement, 5, serial.ptr, cast(int)serial.length, SQLITE_STATIC); //serial - sqlite3_bind_int(statement, 6, region_id); //region - stderr.writefln("insert into: \"%s\", \"%s\", \"%d\", \"%s\", \"%s\", \"%d\"", record[0], record[1], ver, record[3], serial, region_id); + sqlite3_bind_int(statement, 6, region_id(region)); //region + //stderr.writefln("insert into: \"%s\", \"%s\", \"%d\", \"%s\", \"%s\", \"%d\", \"%s\"", title, edition, ver, languages, serial, region_id(region), region); const int step_ret = sqlite3_step(statement); if (step_ret && SQLITE_DONE != step_ret)