//removes custom sign indicators private string RemoveCustomPatternParts(string str) { /* Note: * Symbols are added here so the removing tries to not affect the coordinate too much * Strings to be removed then are evaluated from the longest ones to the shortes ones * So when searching for "P" does not affect "PN" as "PN" is evaluated earlier * */ //CustomHemisphereIndicator is used here so another object does not have to be created //only for the string cleanning ArrayList StringsToRemove = CustomPtrn.HemisphereIndicators; //add degree symbol CustomHemisphereIndicator StringToRemove = new CustomHemisphereIndicator("Degree", CustomPtrn.DegreeSymbol,CustomPtrn.DegreeSymbol.Length, false); StringsToRemove.Add(StringToRemove); //add minute symbol StringToRemove = new CustomHemisphereIndicator("Minute", CustomPtrn.MinuteSymbol, CustomPtrn.MinuteSymbol.Length, false); StringsToRemove.Add(StringToRemove); //add second symbol StringToRemove = new CustomHemisphereIndicator("Second", CustomPtrn.SecondSymbol, CustomPtrn.SecondSymbol.Length, false); StringsToRemove.Add(StringToRemove); //sorth the arraylist (by element's Length property) StringsToRemove.Sort(); for (int x = StringsToRemove.Count - 1; x >= 0; x--) { CustomHemisphereIndicator ToBeRemoved = (CustomHemisphereIndicator)StringsToRemove[x]; //check if the string exists so replacing does not yield errors if (ToBeRemoved.Length > 0) { //check if the supplied pattern was marked as case insensitive? string CaseInsensitive = ""; if (CustomPtrn.CaseInsensitive) { CaseInsensitive = "(?i)"; } //create regex for replacing Regex TempRegex = new Regex(CaseInsensitive + ToBeRemoved.Indicator); if (ToBeRemoved.Name == "Degree" | ToBeRemoved.Name == "Minute") { //replace with a symbol used later for splitting str = TempRegex.Replace(str, ":"); } else { //remove the string str = TempRegex.Replace(str, ""); } } } return str; }
//This adds custom pattern to a list of already predefined patterns //useful for batch conversions - allows for totally mixed input data (predefined & custom) public void AddCustomPattern(CustomPatternIn patternIn) { //new custom pattern object - to pass the needed data farther CustomPattern pattern = new CustomPattern(); //keep indicators for parsing ArrayList indicators = new ArrayList(); //north CustomHemisphereIndicator ind = new CustomHemisphereIndicator("North", patternIn.North, patternIn.North.Length,true); indicators.Add(ind); //south ind = new CustomHemisphereIndicator("South", patternIn.South, patternIn.South.Length, false); indicators.Add(ind); //east ind = new CustomHemisphereIndicator("East", patternIn.East, patternIn.East.Length, true); indicators.Add(ind); //west ind = new CustomHemisphereIndicator("West", patternIn.West, patternIn.West.Length, false); indicators.Add(ind); //sort the arraylist indicators.Sort(); //add it to the pattern object pattern.HemisphereIndicators = indicators; //case insensitive pattern.CaseInsensitive = patternIn.CaseInsensitive; //keep symbols for parsing pattern.DegreeSymbol = patternIn.DegreeSymbol; pattern.MinuteSymbol = patternIn.MinuteSymbol; pattern.SecondSymbol = patternIn.SecondSymbol; //save the data CustomPtrn = pattern; //----------------build custom patterns---------------- //prepare hemisphere indicators string north = EscapeChars(patternIn.North); string south = EscapeChars(patternIn.South); string east = EscapeChars(patternIn.East); string west = EscapeChars(patternIn.West); //prepare symbols string degreesymbol = ""; if (patternIn.DegreeSymbol != "") { degreesymbol = "(" + EscapeChars(patternIn.DegreeSymbol) + ")?"; } string minutesymbol = ""; if (patternIn.MinuteSymbol != "") { minutesymbol = "(" + EscapeChars(patternIn.MinuteSymbol) + ")?"; } string secondsymbol = ""; if (EscapeChars(patternIn.SecondSymbol) != "") { secondsymbol = "(" + EscapeChars(patternIn.SecondSymbol) + ")?"; } //is the pattern to be case insensitive? string CaseInsensitive = ""; if (patternIn.CaseInsensitive) { CaseInsensitive = "(?i)"; } //allow whitespace string WhiteSpace = ""; if (patternIn.AllowWhiteSpace == true) { WhiteSpace = "(\\s)*"; } //hemisphere indicator string HemisphereIndicator = ""; //add north if present if (north == "") { HemisphereIndicator += south; } else { HemisphereIndicator += north; if (south != "") { HemisphereIndicator += "|" + south; } } //add east if (north == "" & south == "") { HemisphereIndicator += east; } else { if (east != "") { HemisphereIndicator += "|" + east; } } //add west if (north == "" & south == "" & east == "") { HemisphereIndicator += west; } else { if (west != "") { HemisphereIndicator += "|" + west; } } //add remaining bits if not empty if (HemisphereIndicator != "") { HemisphereIndicator = "(" + HemisphereIndicator + ")?"; } ArrayList customPatterns = new ArrayList(); //create custom patterns based on the specified user's input string[] ptrn; //Custom variation of DD.DDD ptrn = new string[2]; ptrn[0] = "Custom variation of DD.DDD"; ptrn[1] = CaseInsensitive + "(^" + WhiteSpace + HemisphereIndicator + WhiteSpace + "(" + "(\\d{1,3}(\\.|\\,)?" + WhiteSpace + degreesymbol + WhiteSpace + "$)|(\\d{1,3}(\\.|\\,)\\d+" + WhiteSpace + degreesymbol + WhiteSpace + "$)" + ")" + "|(^" + WhiteSpace + "(" + "(\\d{1,3}(\\.|\\,)?" + WhiteSpace + degreesymbol + WhiteSpace + ")|(\\d{1,3}(\\.|\\,)\\d+" + WhiteSpace + degreesymbol + WhiteSpace + ")" + ")" + HemisphereIndicator + WhiteSpace + "$" + "))" ; customPatterns.Add(ptrn); //Custom variation of DD:MM.MMM ptrn = new string[2]; ptrn[0] = "Custom variation of DD:MM.MMM"; ptrn[1] = CaseInsensitive + "(^" + WhiteSpace + HemisphereIndicator + WhiteSpace + "(" + "(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "$)|(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "\\d{1,2}(\\.|\\,)?" + WhiteSpace + minutesymbol + WhiteSpace + "$)|(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "\\d{1,2}(\\.|\\,)\\d+" + WhiteSpace + minutesymbol + WhiteSpace + "$)" + ")" + "|(^" + WhiteSpace + "(" + "(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + ")|(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "\\d{1,2}(\\.|\\,)?" + WhiteSpace + minutesymbol + WhiteSpace + ")|(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "\\d{1,2}(\\.|\\,)\\d+" + WhiteSpace + minutesymbol + WhiteSpace + ")" + ")" + HemisphereIndicator + WhiteSpace + "$" + "))" ; customPatterns.Add(ptrn); //Custom variation of DD:MM:SS.SSS ptrn = new string[2]; ptrn[0] = "Custom variation of DD:MM:SS.SSS"; ptrn[1] = CaseInsensitive + "(^" + WhiteSpace + HemisphereIndicator + WhiteSpace + "(" + "(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "$)|(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "\\d{1,2}" + WhiteSpace + minutesymbol + WhiteSpace + "$)|(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "\\d{1,2}" + WhiteSpace + minutesymbol + WhiteSpace + "\\d{1,2}(\\.|\\,)?" + WhiteSpace + secondsymbol + WhiteSpace + "$)|(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "\\d{1,2}" + WhiteSpace + minutesymbol + WhiteSpace + "\\d{1,2}(\\.|\\,)\\d+" + WhiteSpace + secondsymbol + WhiteSpace + "$)" + ")" + "|(^" + WhiteSpace + "(" + "(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + ")|(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "\\d{1,2}" + WhiteSpace + minutesymbol + WhiteSpace + ")|(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "\\d{1,2}" + WhiteSpace + minutesymbol + WhiteSpace + "\\d{1,2}(\\.|\\,)?" + WhiteSpace + secondsymbol + WhiteSpace + ")|(\\d{1,3}" + WhiteSpace + degreesymbol + WhiteSpace + "\\d{1,2}" + WhiteSpace + minutesymbol + WhiteSpace + "\\d{1,2}(\\.|\\,)\\d+" + WhiteSpace + secondsymbol + WhiteSpace + ")" + ")" + HemisphereIndicator + WhiteSpace + "$" + "))" ; customPatterns.Add(ptrn); //check if the default patterns are to be used if (patternIn.DisableDefaultPatterns) { patterns = customPatterns; } else //if all patterns are to be used check which set has the matching priority { //check if the custom patterns are to have priority over the default ones if (patternIn.PriorityOverDefaultPatterns) { //add default patterns to the custom patterns for (int i = 0; i < patterns.Count; i++) { customPatterns.Add(patterns[i]); } //swap array lists patterns = customPatterns; } else { //add custom patterns to the default patterns for (int i = 0; i < customPatterns.Count; i++) { patterns.Add(customPatterns[i]); } } } }