public void TestRetrieveDataTypes() { DataTypeDefinitions result = _reader.GetColumnType(); string[] expectedDataTypes = new string[] { "System.Int32", "System.Int32", "System.String", "System.String", "System.String", "System.String", "System.String", "System.String", "System.String", "System.Int32", "System.String", "System.Int32", "System.Int32", "System.String", "System.String", "System.String", "System.String", "System.String" }; result.DataTypes.Should().BeEquivalentTo(expectedDataTypes); }
/* * implementation of getColumnType() method. To find out the data types, we will * read the first line after the header row from the file and extract the field values from it. In * the previous assignment, we have tried to convert a specific field value to * Integer or Double. However, in this assignment, we are going to use Regular * Expression to find the appropriate data type of a field. Integers: should * contain only digits without decimal point Double: should contain digits as * well as decimal point Date: Dates can be written in many formats in the CSV * file. However, in this assignment,we will test for the following date * formats('dd/mm/yyyy','mm/dd/yyyy','dd-mon-yy','dd-mon-yyyy','dd-month-yy','dd-month-yyyy','yyyy-mm-dd') */ public override DataTypeDefinitions GetColumnType() { string[] secondRowArray; DataTypeDefinitions dataTypeDefinitions = new DataTypeDefinitions(); _reader = new StreamReader(_fileName); _reader.ReadLine(); string secondRow = _reader.ReadLine(); secondRowArray = secondRow.Split(','); string[] typeArray = new string[secondRowArray.Length]; for (int i = 0; i < secondRowArray.Length; i++) { if (Regex.Match(secondRowArray[i], "^[0-9]+$").Success) { typeArray[i] = "System.Int32"; } else if (Regex.Match(secondRowArray[i], "^[0-9]+.[0-9]+$").Success) { typeArray[i] = "System.Double"; } // checking for date format yyyy-mm-dd else if (Regex.Match(secondRowArray[i], "^[0-9]{4}-[0-9]{2}-[0-9]{2}$").Success) { typeArray[i] = "System.DateTime"; } // checking for date format dd/mm/yyyy else if (Regex.Match(secondRowArray[i], "^[0-9]{2}/[0-9]{2}/[0-9]{4}$").Success) { typeArray[i] = "System.DateTime"; } // checking for date format dd-mon-yyyy else if (Regex.Match(secondRowArray[i], "^[0-9]{2}-[a-z]{3}-[0-9]{2}$").Success) { typeArray[i] = "System.DateTime"; } else if (secondRowArray[i].Length == 0) { typeArray[i] = "System.Object"; } else { typeArray[i] = "System.String"; } } dataTypeDefinitions.DataTypes = typeArray; return(dataTypeDefinitions); }
/* * Implementation of getColumnType() method. To find out the data types, we will * read the first line from the file and extract the field values from it. If a * specific field value can be converted to Integer, the data type of that field * will contain "System.Int32", otherwise if it can be converted to Double, * then the data type of that field will contain "System.Double", otherwise, * the field is to be treated as String. * Note: Return Type of the method will be DataTypeDefinitions */ public override DataTypeDefinitions GetColumnType() { if (File.Exists(_fileName)) { using (_reader = new StreamReader(_fileName)) { _reader.ReadLine(); string firstDataRow = _reader.ReadLine(); if (!string.IsNullOrEmpty(firstDataRow)) { int intValue = 0; double doubleValue = 0; List <string> dataTypes = new List <string>(); DataTypeDefinitions dataTypeDefinitions = new DataTypeDefinitions(); foreach (string field in firstDataRow.Split(',')) { if (int.TryParse(field, out intValue)) { dataTypes.Add(typeof(System.Int32).ToString()); } else if (double.TryParse(field, out doubleValue)) { dataTypes.Add(typeof(System.Double).ToString()); } else { dataTypes.Add(typeof(System.String).ToString()); } } dataTypeDefinitions.DataTypes = dataTypes.ToArray(); return(dataTypeDefinitions); } } } return(null); }
/* * implementation of getColumnType() method. To find out the data types, we will * read the first line after the header row from the file and extract the field values from it. In * the previous assignment, we have tried to convert a specific field value to * Integer or Double. However, in this assignment, we are going to use Regular * Expression to find the appropriate data type of a field. Integers: should * contain only digits without decimal point Double: should contain digits as * well as decimal point Date: Dates can be written in many formats in the CSV * file. However, in this assignment,we will test for the following date * formats('dd/mm/yyyy','mm/dd/yyyy','dd-mon-yy','dd-mon-yyyy','dd-month-yy','dd-month-yyyy','yyyy-mm-dd') */ public override DataTypeDefinitions GetColumnType() { DataTypeDefinitions data; try { using (FileStream fs = new FileStream(this._fileName, FileMode.Open, FileAccess.Read)) { try { using (StreamReader sr = new StreamReader(fs)) { try { int num; string[] str, str1; string str2; str2 = sr.ReadLine(); str = (sr.ReadLine()).Split(","); str1 = new string[str.Length]; for (int i = 0; i < str.Length; i++) { Match match = Regex.Match(str[i], @"\d{1,2}/\d{2,4}/\d{2,4}|\d{1,2}-\d{2,4}-\d{2,4}|\d{1,2}-\w*-\d{2,4}"); if (int.TryParse(str[i], out num)) { str1[i] = "System.Int32"; } else if (match.Index != 0) { str1[i] = "System.DateTime"; } else if (str[i] == "") { str1[i] = "System.Object"; } else { str1[i] = "System.String"; } } data = new DataTypeDefinitions(str1); return(data); } catch (Exception e) { Console.WriteLine(e.Message); } finally { sr.Close(); } } } catch (Exception e) { Console.WriteLine(e.Message); } finally { fs.Close(); } } } catch (Exception e) { Console.WriteLine(e.Message); } return(null); }
/* * implementation of getColumnType() method. To find out the data types, we will * read the first line from the file and extract the field values from it. In * the previous assignment, we have tried to convert a specific field value to * Integer or Double. However, in this assignment, we are going to use Regular * Expression to find the appropriate data type of a field. Integers: should * contain only digits without decimal point Double: should contain digits as * well as decimal point Date: Dates can be written in many formats in the CSV * file. However, in this assignment,we will test for the following date * formats('dd/mm/yyyy','mm/dd/yyyy','dd-mon-yy','dd-mon-yyyy','dd-month-yy','dd-month-yyyy','yyyy-mm-dd') */ public override DataTypeDefinitions GetColumnType() { using (_reader = new StreamReader(_fileName)) { _reader.ReadLine(); string firstDataRow = _reader.ReadLine(); if (!string.IsNullOrEmpty(firstDataRow)) { string ddString = $"((0[1-9])|([12]\\d)|(3[01]))"; string mmString = $"((0[1-9])|(1[012]))"; string yyString = $"(\\d{{2}})"; string yyyyString = $"(\\d{{4}})"; string shortMonthString = $"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"; string longMonthString = $"(January|February|March|April|May|June|July|August|September|October|November|December)"; List <string> dataTypes = new List <string>(); foreach (string field in firstDataRow.Split(',')) { // checking for Integer if (Regex.IsMatch(field, $"^([0-9]+)$")) { dataTypes.Add(typeof(System.Int32).ToString()); } // checking for floating point numbers else if (Regex.IsMatch(field, $"^([0-9]+.[0-9]+)$")) { dataTypes.Add(typeof(System.Double).ToString()); } // checking for date format dd/mm/yyyy else if (Regex.IsMatch(field, $"^({ddString}/{mmString}/{yyyyString})$")) { dataTypes.Add(typeof(System.DateTime).ToString()); } // checking for date format mm/dd/yyyy else if (Regex.IsMatch(field, $"^({mmString}/{ddString}/{yyyyString})$")) { dataTypes.Add(typeof(System.DateTime).ToString()); } // checking for date format dd-mon-yy else if (Regex.IsMatch(field, $"^({ddString}-{shortMonthString}-{yyString})$", RegexOptions.IgnoreCase)) { dataTypes.Add(typeof(System.DateTime).ToString()); } // checking for date format dd-mon-yyyy else if (Regex.IsMatch(field, $"^({ddString}-{shortMonthString}-{yyyyString})$", RegexOptions.IgnoreCase)) { dataTypes.Add(typeof(System.DateTime).ToString()); } // checking for date format dd-month-yy else if (Regex.IsMatch(field, $"^({ddString}-{longMonthString}-{yyString})$", RegexOptions.IgnoreCase)) { dataTypes.Add(typeof(System.DateTime).ToString()); } // checking for date format dd-month-yyyy else if (Regex.IsMatch(field, $"^({ddString}-{longMonthString}-{yyyyString})$", RegexOptions.IgnoreCase)) { dataTypes.Add(typeof(System.DateTime).ToString()); } // checking for date format yyyy-mm-dd else if (Regex.IsMatch(field, $"^({yyyyString}-{mmString}-{ddString})$")) { dataTypes.Add(typeof(System.DateTime).ToString()); } else if (string.IsNullOrEmpty(field)) { dataTypes.Add(typeof(System.Object).ToString()); } else { dataTypes.Add(typeof(System.String).ToString()); } } DataTypeDefinitions dataTypeDefinitions = new DataTypeDefinitions(dataTypes.ToArray()); return(dataTypeDefinitions); } } return(null); }
public static extern int SDcreate(int sd_id, string name, DataTypeDefinitions data_type, int rank, int[] dimsizes);
public static extern short SDsetattr(int obj_id, string attr_name, DataTypeDefinitions data_type, int count, StringBuilder values);
public static extern short SDattrinfo(int obj_id, int attr_index, StringBuilder attr_name, out DataTypeDefinitions data_type, out int count);
public static extern short SDgetinfo(int sds_id, StringBuilder sds_name, out int rank, [In, Out] int[] dimsizes, out DataTypeDefinitions data_type, out int num_attrs);
public static extern short SDsetdimscale(int dim_id, int count, DataTypeDefinitions data_type, IntPtr data);
public static extern short SDdiminfo(int dim_id, StringBuilder name, out int size, out DataTypeDefinitions datay_type, out int num_attrs);
/* * Implementation of getColumnType() method. To find out the data types, we will * read the first line from the file and extract the field values from it. If a * specific field value can be converted to Integer, the data type of that field * will contain "System.Int32", otherwise if it can be converted to Double, * then the data type of that field will contain "System.Double", otherwise, * the field is to be treated as String. * Note: Return Type of the method will be DataTypeDefinitions */ public override DataTypeDefinitions GetColumnType() { int num; double d; string[] arr; DataTypeDefinitions h; try { using (FileStream fs = new FileStream(this._fileName, FileMode.Open, FileAccess.Read)) { try { using (StreamReader sr = new StreamReader(fs)) { try { while (!sr.EndOfStream) { string a = sr.ReadLine(); arr = sr.ReadLine().Split(","); // h = new DataTypeDefinitions(sr.ReadLine().Split(",", StringSplitOptions.RemoveEmptyEntries)); string[] res = new string[arr.Length]; for (int i = 0; i < arr.Length; i++) { try { if (int.TryParse(arr[i], out num)) { res[i] = "System.Int32"; } else { res[i] = "System.String"; } } catch (Exception) { } } h = new DataTypeDefinitions(res); return(h); } } catch (Exception) { throw new Exception("some eror occured."); } finally { sr.Close(); sr.Dispose(); } } } catch (Exception) { fs.Close(); fs.Dispose(); } } } catch (FileNotFoundException) { throw new FileNotFoundException("File does not exist"); } catch (Exception ex) { Console.WriteLine("Error occured."); Console.WriteLine("Error:", ex.Message); } finally { } return(null); }
/* * implementation of getColumnType() method. To find out the data types, we will * read the first line from the file and extract the field values from it. In * the previous assignment, we have tried to convert a specific field value to * Integer or Double. However, in this assignment, we are going to use Regular * Expression to find the appropriate data type of a field. Integers: should * contain only digits without decimal point Double: should contain digits as * well as decimal point Date: Dates can be written in many formats in the CSV * file. However, in this assignment,we will test for the following date * formats('dd/mm/yyyy','mm/dd/yyyy','dd-mon-yy','dd-mon-yyyy','dd-month-yy','dd-month-yyyy','yyyy-mm-dd') */ public override DataTypeDefinitions GetColumnType() { // checking for Integer // checking for floating point numbers // checking for date format dd/mm/yyyy // checking for date format mm/dd/yyyy // checking for date format dd-mon-yy // checking for date format dd-mon-yyyy // checking for date format dd-month-yy // checking for date format dd-month-yyyy // checking for date format yyyy-mm-dd string[] arr; int num; DataTypeDefinitions h; try { using (FileStream fs = new FileStream(this._fileName, FileMode.Open, FileAccess.Read)) { try { using (StreamReader sr = new StreamReader(fs)) { try { while (!sr.EndOfStream) { string a = sr.ReadLine(); string b = sr.ReadLine(); arr = b.Split(","); // h = new DataTypeDefinitions(sr.ReadLine().Split(",", StringSplitOptions.RemoveEmptyEntries)); string[] res = new string[arr.Length]; try { // MatchCollection Integers = Regex.Matches(b, @"^\d$"); //MatchCollection Dates = Regex.Matches(b, @"^\d{4}-((0\d)|(1[012]))-(([012]\d)|3[01])$"); for (int i = 0; i < arr.Length; i++) { Match Integer = Regex.Match(arr[i], @"\d+"); Match Date = Regex.Match(arr[i], @"\d{1,2}/\d{2,4}/\d{2,4}|\d{1,2}-\d{2,4}-\d{2,4}|\d{1,2}-\w*-\d{2,4}"); if (Date.Index != 0) { res[i] = "System.DateTime"; } else if (int.TryParse(arr[i], out num)) { res[i] = "System.Int32"; } else if (arr[i] == "") { res[i] = "System.Object"; } else { res[i] = "System.String"; } } h = new DataTypeDefinitions(res); return(h); } catch (Exception) { } h = new DataTypeDefinitions(res); return(h); } } catch (Exception) { throw new Exception("some eror occured."); } finally { sr.Close(); sr.Dispose(); } } } catch (Exception) { fs.Close(); fs.Dispose(); } } } catch (FileNotFoundException) { throw new FileNotFoundException("File does not exist"); } catch (Exception ex) { Console.WriteLine("Error occured."); Console.WriteLine("Error:", ex.Message); } finally { } return(null); }