private void frmNewDB_Load(object sender, EventArgs e) { txtLoc.Text = $"{slash}usr{slash}share{slash}DBs{slash}"; //gather active nutrients, TODO: work this into frmMain as it is with logRunner configuredFields = new List <string>(); configuredFields.Add("NDBNo"); configuredFields.Add("FoodName"); foreach (string s in File.ReadAllLines($"{Application.StartupPath}{slash}usr{slash}profile{frmMain.currentUser.index}{slash}configured_fields.py")) { if (!s.StartsWith("#") && s.Contains("|")) { configuredFields.Add(s.Split('#')[0].Split('|')[0]); } } foreach (string s in configuredFields) { lstBoxNutes.Items.Add(s); } //populate txtConfig box List <string> tmp = new List <string>(); int tmax = 0; foreach (string s in arr) { if (s.Length > tmax) { tmax = s.Length; } } for (int i = 0; i < arr.Count; i++) { tmp.Add($"{arr[i].PadRight(tmax, ' ')}="); } txtConfig.Lines = tmp.ToArray(); tmp = null; //adds the primary keys pbw = new progBarWait(); Thread t = new Thread(() => { pbw.ShowDialog(); pbw.finished = true; }); t.Start(); while (!pbw.ready) { Thread.Sleep(60); } pbw.setTitle($"Identifying possible primary keys..."); pbw.setProgMax(frmParseCustomDatabase.columns.Length, 1); List <string> _isUniqueList; primKeys = new List <string>(); int _z = 0; foreach (frmParseCustomDatabase.Column c in frmParseCustomDatabase.columns) { if (pbw.abort) { break; } _z++; pbw.setLblCurObj(c.header); bool unique = true; _isUniqueList = new List <string>(); foreach (string s in c.items) { _isUniqueList.Add(s); if (_isUniqueList.Distinct().Count() != _isUniqueList.Count) { unique = false; break; } } _isUniqueList = null; if (unique) { primKeys.Add(c.header); } } if (pbw.abort) { this.Close(); } this.Activate(); if (primKeys.Count == 0) { //TODO: make generate extra column chkGeneratePrimKey.Checked = true; chkGeneratePrimKey.Enabled = false; } foreach (string s in primKeys) { comboPrimKey.Items.Add(s); } comboPrimKey.SelectedIndex = 0; lblStatus.Text = $"You have {n} entries — {arr.Count} properties (0 configured)"; }
private void parseInput() { int colSpan = 0; foreach (string s in sourceInput) { if (s.Split('\t').Length > colSpan) { colSpan = s.Split('\t').Length; } } txtInput.Text += "\r\n# of headers:\t" + sourceInput[0].Split('\t').Length.ToString(); txtInput.Text += "\r\n# of max columns:\t" + colSpan.ToString(); txtInput.Text += "\r\n# of rows:\t\t" + sourceInput.Length.ToString(); for (int i = 0; i < sourceInput.Length; i++) { if (sourceInput[i].Split('\t').Length != colSpan) { if (MessageBox.Show($"Error on row #{i}\r\nonly has {sourceInput[i].Split('\t').Length} entries, {colSpan} expected!!\r\n{sourceInput[i]}", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.Cancel) { return; } } } progBarWait pbw = new progBarWait(); Thread t = new Thread(() => { pbw.ShowDialog(); pbw.finished = true; }); t.Start(); while (!pbw.ready) { Thread.Sleep(60); } pbw.setTitle("Adding the columns..."); int m = sourceInput[0].Split('\t').Length; int n = sourceInput.Length; pbw.setProgMax(n, 50); try { columns = new Column[colSpan]; for (int i = 0; i < colSpan; i++) { columns[i] = new Column(); } for (int i = 0; i < m; i++) { columns[i].header = sourceInput[0].Split('\t')[i]; columns[i].items = new string[sourceInput.Length]; for (int j = 0; j < n; j++) { pbw.setLblCurObj($"{j}/{n}"); columns[i].items[j] = sourceInput[j].Split('\t')[i]; } } } catch (Exception ex) { MessageBox.Show("Parse error.\n" + ex.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ListViewItem[] itms = new ListViewItem[sourceInput.Length - 1]; for (int j = 0; j < sourceInput.Length - 1; j++) { itms[j] = new ListViewItem(); } for (int i = 0; i < colSpan; i++) { lstViewResult.Columns.Add(columns[i].header); for (int j = 1; j < sourceInput.Length; j++) { if (i == 0) { itms[j - 1].Text = columns[0].items[j]; } else { itms[j - 1].SubItems.Add(columns[i].items[j]); } } } lstViewResult.BeginUpdate(); lstViewResult.Items.AddRange(itms); lstViewResult.EndUpdate(); itms = null; lstViewResult.AutoResizeColumns(System.Windows.Forms.ColumnHeaderAutoResizeStyle.HeaderSize); }
private void btnCreate_Click(object sender, EventArgs e) { string fp = Application.StartupPath + txtLoc.Text; if (Directory.Exists(fp)) { DialogResult dRG = MessageBox.Show("A directory with this name was already found, are you sure you want to overwrite it?", "Overwrite database?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (dRG == DialogResult.No) { return; } string[] st = Directory.GetFiles(fp); for (int i = 0; i < st.Length; i++) { File.Delete(st[i]); } } else if (txtLoc.Text.ToUpper() == "FOODS" || txtLoc.Text.ToUpper() == "RECIPES") { MessageBox.Show("ERROR: 'foods' and 'recipes' are reserved keywords!", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else { Directory.CreateDirectory(fp); } List <int> colInts = new List <int>(); for (int i = 0; i < arr.Count; i++) { foreach (string s in configuredFields) { foreach (string st in txtConfig.Lines) { if (st.Replace(" ", "") == $"{arr[i]}={s}") { colInts.Add(i); } } } } itmsOut = new List <string>(); entryKeyLang = new List <string>(); string langColumnStr = "", keyColumnStr = ""; int langColumn = 0, keyColumn = 0; foreach (nutNameKey na in nutNameKeys) { if (na.nutrient == "FoodName") { langColumnStr = na.columnHeader; } else if (na.nutrient == "NDBNo") { keyColumnStr = na.columnHeader; } } for (int i = 0; i < arr.Count; i++) { if (arr[i] == keyColumnStr) { keyColumn = i; } else if (arr[i] == langColumnStr) { langColumn = i; } } nutEntries = new List <nutEntry>(); nutEntry nu; pbw = new progBarWait(); Thread t = new Thread(() => { pbw.ShowDialog(); pbw.finished = true; }); t.Start(); while (!pbw.ready) { Thread.Sleep(60); } pbw.setTitle("Pairing the keys..."); pbw.setProgMax(n, 50); for (int i = 0; i < n; i++) //loop over each ENTRY in the database, e.g. 8760 for USDA { nu = new nutEntry(); bool prevMatch = false; string title = mainForm.getVal(i, langColumn).Substring(0, 3); pbw.setLblCurObj(title); foreach (nutEntry nut in nutEntries) { if (nut.fileName.ToUpper() == title.ToUpper()) { prevMatch = true; } } nu.fileName = prevMatch ? $"{title}{i}" : title; string[] res = { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" }; foreach (string s in res) { if (nu.fileName == s) { nu.fileName += i.ToString(); } } string lang = "", key = ""; foreach (int j in colInts) { string nutrient = ""; string val = mainForm.getVal(i, j); foreach (nutNameKey nut in nutNameKeys) { if (nut.columnHeader == arr[j]) { nutrient = nut.nutrient; } } nu.conts.Add($"{nutrient}:{val}"); if (j == langColumn) { lang = val; //hashLangOut.Add($"{nu.fileName}|{val}"); } else if (j == keyColumn) { key = val; //hashKeyOut.Add($"{nu.fileName}|{val}"); } } entryKeyLang.Add($"{nu.fileName}|{key}|{lang}"); nutEntries.Add(nu); nu = null; } File.WriteAllLines($"{fp}{slash}_fieldInit.ini", txtConfig.Lines); File.WriteAllLines($"{fp}{slash}_entryKeyLang.ini", entryKeyLang); //File.WriteAllLines($"{fp}{slash}_hashLang.ini", hashLangOut); //File.WriteAllLines($"{fp}{slash}_hashKey.ini", hashKeyOut); pbw = new progBarWait(); t = new Thread(() => { pbw.ShowDialog(); pbw.finished = true; pbw = null; }); t.Start(); while (!pbw.ready) { Thread.Sleep(60); } pbw.setTitle("Writing the entries..."); n = nutEntries.Count; pbw.setProgMax(n, 50); for (int i = 0; i < n; i++) { try { pbw.setLblCurObj(nutEntries[i].fileName); } catch { } File.WriteAllLines($"{fp}{slash}{nutEntries[i].fileName}.TXT", nutEntries[i].conts); } entryKeyLang = null; nutEntries = null; itmsOut = null; MessageBox.Show("Database created successfully. Please use the search function on the main page to try it out.", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); }
public static void loadDB(string db, Form callingForm) { foreach (DB d in loadedDBs) { if (d.name == db) { return; } } DB dB = new DB(); dB.name = db; dB.fields = new List <string>(); dB.columns = new List <string>(); foreach (string s in File.ReadAllLines($"{Application.StartupPath}{slash}usr{slash}share{slash}DBs{slash}{db}{slash}_fieldInit.ini")) { if (frmMain.currentBasicFields.Contains(s.Split('=')[1])) { string f = s.Split('=')[1]; dB.fields.Add(f); if (s.Split('=')[0].Contains("(") && s.Split('=')[0].Contains(")")) { dB.columns.Add($"{f} ({s.Split('(')[1].Split(')')[0]})"); } else { dB.columns.Add(f); } } } string[] rawEntryKeyLang = File.ReadAllLines($"{Application.StartupPath}{slash}usr{slash}share{slash}DBs{slash}{db}{slash}_entryKeyLang.ini"); dB.hashLang.fileNames = new List <string>(); dB.hashLang.primKeys = new List <string>(); dB.hashLang.foodNames = new List <string>(); dB.hashLang.fileEntries = new List <string[]>(); foreach (string s in rawEntryKeyLang) { dB.hashLang.fileNames.Add((s.Split('|')[0])); dB.hashLang.primKeys.Add((s.Split('|')[1])); dB.hashLang.foodNames.Add((s.Split('|')[2].ToUpper())); } dB.numOfEntries = rawEntryKeyLang.Length; rawEntryKeyLang = null; pbw = new progBarWait(); Thread t = new Thread(() => { pbw.ShowDialog(); pbw.finished = true; }); t.Start(); while (!pbw.ready && !pbw.abort) { Thread.Sleep(60); } pbw.setTitle($"Reading in {db}..."); pbw.setProgMax(dB.hashLang.foodNames.Count, 50); for (int i = 0; i < dB.hashLang.fileNames.Count; i++) { if (pbw.abort) { break; } dB.hashLang.fileEntries.Add(File.ReadAllLines($"{Application.StartupPath}{slash}usr{slash}share{slash}DBs{slash}{db}{slash}{dB.hashLang.fileNames[i]}.TXT")); pbw.setLblCurObj(dB.hashLang.foodNames[i]); } if (!pbw.abort) { loadedDBs.Add(dB); } callingForm.Activate(); }