/// <summary> /// Reads all data from the given file /// </summary> /// <param name="db">The icaoDatabase to fill</param> /// <param name="fName">A fully qualified name</param> /// <returns>The result string, either empty or error</returns> public static string ReadDb(ref icaoDatabase db, string fName) { if (!File.Exists(fName)) { return($"File {fName} does not exist\n"); } string ret = ""; using (var sr = new StreamReader(fName)) { string buffer = sr.ReadLine( ); // header line buffer = sr.ReadLine( ); while (!sr.EndOfStream) { var rec = FromNative(buffer); if (rec.IsValid) { ret += db.Add(rec); // collect adding information } buffer = sr.ReadLine( ); } // } return(ret); }
/// <summary> /// Reads one file to fill the db /// </summary> /// <param name="db">The navDatabase to fill</param> /// <param name="fName">The qualified filename</param> /// <returns>The result string, either empty or error</returns> private static string ReadDbFile(ref icaoDatabase db, string fName) { if (!File.Exists(fName)) { return($"File {fName} does not exist\n"); } var icaoPre = Path.GetFileNameWithoutExtension(fName).ToUpperInvariant(); string ret = ""; using (var sr = new StreamReader(fName)) { string buffer = sr.ReadToEnd( ); buffer = buffer.Replace("\n", "").Replace("\r", "").Trim( ); // cleanup any CR, LFs and whitespaces buffer = buffer.Substring(1); // skip enclosing { var fragment = JsonParser.ExtractFragment(buffer); while (!string.IsNullOrEmpty(fragment)) { buffer = buffer.Substring(fragment.Length + 1); // remove extracted + comma var rec = FromNative(fragment); rec.AddPrefix(icaoPre); // make it a valid one - the FA db icao is without the prefix from the file... if (rec.IsValid) { ret += db.Add(rec); // collecting add information } fragment = JsonParser.ExtractFragment(buffer); } } return(ret); }
/// <summary> /// Reads all data from the given file /// </summary> /// <param name="db">The icaoDatabase to fill from the file</param> /// <param name="csvFile">The file to read</param> /// <returns>The result string, either empty or error</returns> public static string ReadDb(ref icaoDatabase db, string csvFile) { if (!File.Exists(csvFile)) { return($"File does not exist\n"); } return(ReadDbFile(ref db, csvFile)); }
/// <summary> /// Write the ModeS db as CSV formatted file /// </summary> /// <param name="db">The database to dump</param> /// <param name="csvOutStream">The stream to write to</param> /// <returns>True for success</returns> public static bool WriteCsv(icaoDatabase db, Stream csvOutStream) { using (var sw = new StreamWriter(csvOutStream, Encoding.UTF8)) { sw.WriteLine(icaoRec.CsvHeader); foreach (var c in PREFIXES) { WriteFile(sw, db.GetSubtable(c.ToString( ))); } } return(true); }
/// <summary> /// Write the modeS db as FA formatted Json files into the given folder /// </summary> /// <param name="db">The database to dump</param> /// <param name="dbFolder">The folder to write to</param> /// <returns>True for success</returns> public static bool WriteDb(icaoDatabase db, string dbFolder) { if (!Directory.Exists(dbFolder)) { Directory.CreateDirectory(dbFolder); } foreach (var c in PREFIXES) { DecomposeTable(dbFolder, db.GetSubtable(c.ToString( ))); // level one get always decomposed } WriteReadme(dbFolder); return(true); }
/// <summary> /// Reads all data from the given folder /// </summary> /// <param name="db">The icaoDatabase to fill</param> /// <param name="dbFolder">A fully qualified path to where the db files are located</param> /// <returns>The result string, either empty or error</returns> public static string ReadDb(ref icaoDatabase db, string dbFolder) { if (!Directory.Exists(dbFolder)) { return($"Folder {dbFolder} does not exist\n"); } ; string ret = ""; IEnumerable <string> dbFiles = Directory.EnumerateFiles(dbFolder, "*.json", SearchOption.TopDirectoryOnly); foreach (var dbfile in dbFiles) { ret += ReadDbFile(ref db, dbfile); } return(ret); }
/// <summary> /// Reads one db file /// </summary> /// <param name="db">The icaoDatabase to fill from the file</param> /// <param name="fName">The qualified filename</param> /// <returns>The result string, either empty or error</returns> private static string ReadDbFile(ref icaoDatabase db, string fName) { var icaoPre = Path.GetFileNameWithoutExtension(fName); string ret = ""; using (var sr = new StreamReader(fName)) { string buffer = sr.ReadLine( ); // header line buffer = sr.ReadLine( ); while (!sr.EndOfStream) { var rec = FromNative(buffer); if (rec.IsValid) { ret += db.Add(rec); // collect adding information } buffer = sr.ReadLine( ); } // } return(ret); }
/// <summary> /// Write complete aircraft db into the supplied database as one transaction /// </summary> /// <param name="db">The aircraft db to dump</param> /// <param name="sqConnection">The db connection</param> /// <returns>The result string, either empty or error</returns> public static string WriteSqDB(icaoDatabase db, SQLiteConnection sqConnection) { using (SQLiteCommand sqlite_cmd = sqConnection.CreateCommand( )) { sqlite_cmd.CommandText = "BEGIN TRANSACTION;"; sqlite_cmd.ExecuteNonQuery( ); try { foreach (var c in PREFIXES) { WriteFile(sqConnection, db.GetSubtable(c.ToString( ))); } sqlite_cmd.CommandText = "COMMIT;"; sqlite_cmd.ExecuteNonQuery( ); } catch (SQLiteException sqex) { sqlite_cmd.CommandText = "ROLLBACK;"; sqlite_cmd.ExecuteNonQuery( ); return($"ERROR - inserting rows failed: {sqex.Message}\n"); } } return(""); }