public static Tuple <string, int> GetSanitisedCardName(string rawCardName) { try { var namer = new Regex(@"\s*([0-9]+)x?\s+(.*)"); //download webpage //trim white space at ends, and merge white space string c = StringExtras.MergeWhiteSpace(StringExtras.CleanString(rawCardName)); int cardCount = 1; //try and scrape count var matches = namer.Match(c); if (matches.Groups.Count == 3) { cardCount = Int32.Parse(matches.Groups[1].Value); c = matches.Groups[2].Value; } //wrap each word with +[] var words = c.Split(null); c = words.Aggregate("", (current, w) => current + ("+[" + w + "]")); return(new Tuple <string, int>(c, cardCount)); } catch (Exception ex) { } return(null); }
private void LoadFiles(IEnumerable <string> files) { string text = ""; foreach (var f in files) { var fi = new System.IO.FileInfo(f); if (text.Length > 0) { text += ","; } text += fi.Name; } text = StringExtras.Truncate(text, 100); Text = defaultTitle + " - " + text; var op = new textoptions(ignoreHTML.Checked, ignoreNonAlphabetCharactersToolStripMenuItem.Checked); var errorfile = controller.LoadFiles(files.ToList(), op); if (errorfile != null) { MessageBox.Show("Error loading file:" + errorfile); return; } SetControlsEnable(true); RefreshCharts(); }
/// <summary> /// deserialise an object from a serialised string /// </summary> /// <param name="objectType">Type of the object.</param> /// <param name="serialisedObjectString">The serialised object string.</param> /// <param name="ignoreErrors">if set to <c>true</c> [ignore errors].</param> /// <returns></returns> public static object DeserialiseObject(Type objectType, string serialisedObjectString, bool ignoreErrors = true) { var s2 = StringExtras.SplitString(serialisedObjectString, Newline); var tl = new List <Tuple <string, string> >(); foreach (var s3 in s2) { var s4 = StringExtras.SplitString(s3, Separator.ToString()); if (s4.Length != 2) { if (ignoreErrors) { continue; } return(null); } var fieldname = s4[0]; var fieldval = s4[1]; tl.Add(new Tuple <string, string>(fieldname, fieldval)); } var instance = DeserialiseObject(objectType, tl, ignoreErrors); return(instance); }
public static void autoCapitalise(IEnumerable <Mp3Lib.Mp3File> mp3s, bool checkFirst, bool capSpecials, bool spaceAfter) { var CWS = new List <string>(); if (capSpecials) { CWS.Add("dj"); CWS.Add("mc"); } foreach (var MF in mp3s) { var newData = new TagHandlerUpdate(MF); var orig = MF.TagHandler; newData.Title = StringExtras.ToCamelCase(newData.Title, true, CWS, spaceAfter); newData.Album = StringExtras.ToCamelCase(newData.Album, true, CWS, spaceAfter); newData.Artist = StringExtras.ToCamelCase(newData.Artist, true, CWS, spaceAfter); if (queryUserMakeChangesAndContinue(newData, MF, checkFirst) == false) { return; } } }
/// <summary> /// returns card names and counts /// </summary> /// <param name="rawCardNames"></param> /// <returns></returns> public static List <Tuple <string, int> > GetSanitisedCardNames(string[] rawCardNames) { var ret = new List <Tuple <string, int> >(); var namer = new Regex(@"\s*([0-9]+)x?\s+(.*)"); //download webpage foreach (var cr in rawCardNames) { //trim white space at ends, and merge white space string c = StringExtras.MergeWhiteSpace(StringExtras.CleanString(cr)); int cardCount = 1; //try and scrape count var matches = namer.Match(c); if (matches.Groups.Count == 3) { cardCount = Int32.Parse(matches.Groups[1].Value); c = matches.Groups[2].Value; } //wrap each word with +[] var words = c.Split(null); c = words.Aggregate("", (current, w) => current + ("+[" + w + "]")); ret.Add(new Tuple <string, int>(c, cardCount)); } return(ret); }
protected AbstractSieveRunner(string name = null) { _thread = new Thread(proc) { Name = $"{name ?? StringExtras.random()}" }; }
private static string Clean(string instr) { var str1 = StringExtras.CleanString(instr); //manually replace weird shit str1 = str1.Replace("—", "-"); return(str1); }
private static List <List <string> > SanatiseFile(string filestr, textoptions options) { var sepNewLine = new[] { "\r\n", "\n" }; var sepWord = new[] { " " }; var sepWordChar = new[] { ' ' }; //split by new lines var lines = filestr.Split(sepNewLine, StringSplitOptions.RemoveEmptyEntries); var output = new List <List <string> >(); //split into words foreach (var sentence in lines) { var sentence1 = sentence; if (options.ignoreHTML) { sentence1 = Regex.Replace(sentence1, "<.*?>", ""); } if (options.ignoreNonAlpha) { sentence1 = StringExtras.RemoveAllNonAlphabetChars(sentence1); } //trim trailing whitespace sentence1 = sentence1.Trim(sepWordChar); if (String.IsNullOrWhiteSpace(sentence1)) { continue; } var words = sentence1.Split(sepWord, StringSplitOptions.RemoveEmptyEntries); var newSentence = new List <string>(); foreach (var word in words) { var word1 = word; word1 = word1.ToLower(); if (word1.Length > 0) { newSentence.Add(word1); } } if (newSentence.Count > 0) { output.Add(newSentence); } } return(output); }
public static void SaveChartCSV(Chart c, string filename) { String save = "text,count\r\n"; foreach (var dp in c.Series[0].Points.Reverse()) { save += dp.AxisLabel + "," + dp.YValues[0] + "\r\n"; } FileExtras.SaveToFile(StringExtras.ReplaceAllChars(filename, ":", "_"), save); }
public static void camelcase(Form1.IncrementStatusDel incrementStatus, ref TreeListView change, TreeListViewItem fromthis) { for (int a = 0; a < fromthis.Items.Count; a++) { String s = StringExtras.ToCamelCase(fromthis.Items[a].Text, true, null); TreeListViewItem TLVI = getItemByName(change, fromthis.Items[a].fullPath); if (TLVI != null) { TLVI.Text = s; } fromthis.Items[a].Text = s; incrementStatus(); camelcase(incrementStatus, ref change, fromthis.Items[a]); } }
private void updateExample() { var ex = ""; if (titleoption.Checked) { ex = titleExample; } else if (artistoption.Checked) { ex = artistExample; } else if (albumoption.Checked) { ex = albumExample; } else { ex = otherExample; } var trimfront = false; if (frontbutton.Checked) { trimfront = true; } var len = 0; var newtext = ""; if (trimlength.Text.Length > 0) { len = int.Parse(trimlength.Text); newtext = StringExtras.ApplyTrim(ex, trimfront, len); } else { newtext = ex; } exampletext1.Text = ex; exampletext2.Text = newtext; }
/// <summary> /// /// </summary> /// <param name="word"></param> /// <param name="val"></param> /// <returns></returns> public static string Pluralise(this string word, double val) { string ret = val.ToString("N") + " " + word; if (val != 1) { if (ret.EndsWith("s") == false) { ret = ret + "s"; } } else { if (ret.EndsWith("s")) { ret = StringExtras.ApplyTrim(ret, false, 1); } } return(ret); }
public static void replaceChar(IEnumerable <Mp3Lib.Mp3File> mp3s, bool checkFirst) { var RC = new replacechar(); var newDataEX = new TagHandlerUpdate(mp3s.First()); RC.titleExample = newDataEX.Title; RC.albumExample = newDataEX.Album; RC.artistExample = newDataEX.Artist; RC.ShowDialog(); if (RC.result1 == '\0') { return; } foreach (var MF in mp3s) { var newData = new TagHandlerUpdate(MF); var orig = MF.TagHandler; if (RC.titleoptionb) { newData.Title = StringExtras.ReplaceAllChars(newData.Title, RC.result1, RC.result2); } else if (RC.albumoptionb) { newData.Album = StringExtras.ReplaceAllChars(newData.Album, RC.result1, RC.result2); } else if (RC.artistoptionb) { newData.Artist = StringExtras.ReplaceAllChars(newData.Artist, RC.result1, RC.result2); } if (queryUserMakeChangesAndContinue(newData, MF, checkFirst) == false) { return; } } }
public static void trimText(IEnumerable <Mp3Lib.Mp3File> mp3s, bool checkFirst) { var newDataEX = new TagHandlerUpdate(mp3s.First()); var TF = new trimform(); TF.titleExample = newDataEX.Title; TF.albumExample = newDataEX.Album; TF.artistExample = newDataEX.Artist; TF.ShowDialog(); if (TF.result == -1) { return; } foreach (var MF in mp3s) { var newData = new TagHandlerUpdate(MF); var orig = MF.TagHandler; if (TF.titleoptionb) { newData.Title = StringExtras.ApplyTrim(newData.Title, TF.isfront, TF.result); } else if (TF.albumoptionb) { newData.Album = StringExtras.ApplyTrim(newData.Album, TF.isfront, TF.result); } else if (TF.artistoptionb) { newData.Artist = StringExtras.ApplyTrim(newData.Artist, TF.isfront, TF.result); } if (queryUserMakeChangesAndContinue(newData, MF, checkFirst) == false) { return; } } }
/// <summary> /// Updates the application. /// </summary> /// <param name="dsd">The DSD.</param> private static void UpdateApplication(LicensingDetails dsd) { string folder; string localfile; //we need the exe file for later execution var exefile = ""; string exefolder; try { //0: reset current directory in case it was changed Directory.SetCurrentDirectory(Application.StartupPath); //1: Get the online files folder = _sd.AppName + "v" + DateTime.Now.Ticks; Directory.CreateDirectory(folder); } catch (Exception ex) { MessageBox.Show("Error while creating the temporary folder\n:" + ex); return; } try { var client = new WebClient(); localfile = dsd.FileLocation.Substring(dsd.FileLocation.LastIndexOf('/') + 1); client.DownloadFile(dsd.FileLocation, localfile); } catch (Exception ex) { MessageBox.Show("Error while downloading new files\n:" + ex); return; } try { var myname = AppDomain.CurrentDomain.FriendlyName; const string vshost = ".vshost."; //remove .vshost for testing if (myname.Contains(vshost)) { myname = myname.Replace(vshost, "."); } //2.1 unpack ZipExtras.ExtractZipFile(localfile, folder); //2.2 find exe foreach (var f in DirectoryExtras.GetFilesRecursive(folder)) { if (f.Contains(".exe")) { exefile = f; } if (f.EndsWith(myname)) { break; } } const string br = "\\"; exefolder = ""; //ignore everything above this dir while (exefile.Length > 0) { var c = StringExtras.ContainsSubStringCount(exefile, br); if (c > 0) { var s = exefile.Substring(0, exefile.IndexOf(br, StringComparison.Ordinal)); exefolder += s + br; exefile = exefile.Substring(exefile.IndexOf(br, StringComparison.Ordinal) + 1); } else { break; } } } catch (Exception ex) { MessageBox.Show("Error while unzipping new files\n:" + ex); return; } //move folder/* to the local dir //delete the zip file //delete folder remnants //start the exefile we found before var operations = "move /Y \"" + exefolder + "\"\\* . " + "& del /Q \"" + localfile + "\" " + "& rmdir /Q /S \"" + folder + "\" " + "& start \"\" \"" + exefile + "\" "; RunCommands(operations); //4: Kill process Application.Exit(); }
protected AbstractSieveRunner(string name = null) { _name = $"{name ?? StringExtras.random()}"; }
/// <summary> /// load the saved config file. will automatically load all the control values, and return the manual strings /// </summary> /// <param name="baseform">pass the base form</param> /// <param name="filename">the saved config flename</param> /// <returns> /// returns null on error, and a list of tuples of saved literal strings otherwise /// </returns> public static List <Tuple <string, string> > LoadConfig(Form baseform, string filename) { var ret = new List <Tuple <string, string> >(); try { if (File.Exists(filename) == false) { return(null); } var f = FileExtras.LoadFile(filename); var parts = StringExtras.SplitString(f, TypeSep); //first part is controls and stuff var controls = StringExtras.SplitString(parts[0], NewLine); foreach (var line in controls) { var split = StringExtras.SplitString(line, Separator); if (split.Length < 2) { continue; } string v = null; if (split.Length >= 3) { v = split[2]; } LoadProperty(baseform, split[0], split[1], v); } //second part is literal strings if (parts.Length >= 2) { var strings = StringExtras.SplitString(parts[1], NewLine); foreach (var line in strings) { var split = StringExtras.SplitString(line, Separator); if (split.Length < 2) { continue; } ret.Add(new Tuple <string, string>(split[0], split[1])); } } return(ret); } catch (Exception) { //on error, delete the config try { File.Delete(filename); } catch { } return(null); } }
/// <summary> /// Load a file into a tree structure based on levels. by default '1 \n \t 2' in a file will create a parent with a /// child node /// </summary> /// <typeparam name="T">class type, usually string</typeparam> /// <param name="filename">The filename.</param> /// <param name="root">The root.</param> /// <param name="addfunc">T must be able to be instantiated with a string. call with a=>new T(a) where T is your class, or /// the return string method</param> /// <param name="levelSeparator">The level separator.</param> /// <param name="RecreateFileIfInvalid">if set to <c>true</c> [recreate file if invalid].</param> /// <exception cref="Exception"></exception> public static void LoadFileIntoTree <T>(string filename, Btree <T> root, Func <string, T> addfunc, string levelSeparator = "\t", bool RecreateFileIfInvalid = true) { root.Children = new List <Btree <T> >(); FileStream fs = null; StreamReader sr = null; try { fs = new FileStream(filename, FileMode.OpenOrCreate); sr = new StreamReader(fs); var line = sr.ReadLine(); var parentT = root; var currentlevel = 0; while (line != null) { var level = StringExtras.ContainsSubStringCount(line, levelSeparator); if (level > (currentlevel + 1)) { throw new Exception(); } if (level == 0) { parentT = root; } else if (currentlevel > (level - 1)) { while (currentlevel != (level - 1)) { parentT = parentT.Parent; currentlevel--; } } var rc = StringExtras.ReplaceAllChars(line, levelSeparator, ""); var t = new Btree <T> { Name = addfunc(rc), Parent = parentT }; if (parentT.Children == null) { parentT.Children = new List <Btree <T> >(); } parentT.Children.Add(t); parentT = t; currentlevel = level; redo: line = sr.ReadLine(); if (line != null && line.Length == 0) { goto redo; } } sr.Close(); fs.Close(); } catch { if (sr != null) { sr.Close(); } if (fs != null) { fs.Close(); } if (RecreateFileIfInvalid) { if (File.Exists(filename)) { File.Delete(filename); } File.Create(filename); } root.Children = new List <Btree <T> >(); } }
public static void cleanMP3(TagHandlerUpdate TH) { TH.Title = StringExtras.CleanString(TH.Title); TH.Album = StringExtras.CleanString(TH.Album); TH.Artist = StringExtras.CleanString(TH.Artist); }