示例#1
0
        /// <summary>
        /// This function searches for patterns like aaa=bbb in the provided string. If it finds such a item, it creates a column named aaa
        /// and stores the value bbb at the same position in it as in the text column.
        /// </summary>
        /// <param name="strg">The string where to search for the patterns described above.</param>
        /// <param name="store">The column collection where to store the newly created columns of properties.</param>
        /// <param name="index">The index into the column where to store the property value.</param>
        public static void ExtractPropertiesFromString(string strg, Altaxo.Data.DataColumnCollection store, int index)
        {
            string pat;

            pat = @"(\S+)=(\S+)";

            Regex r = new Regex(pat, RegexOptions.Compiled | RegexOptions.IgnoreCase);

            for (Match m = r.Match(strg); m.Success; m = m.NextMatch())
            {
                string propname  = m.Groups[1].ToString();
                string propvalue = m.Groups[2].ToString();

                // System.Diagnostics.Trace.WriteLine("Found the pair " + propname + " : " + propvalue);

                if (!store.ContainsColumn(propname))
                {
                    Altaxo.Data.DataColumn col;
                    if (Altaxo.Serialization.DateTimeParsing.IsDateTime(propvalue))
                    {
                        col = new Altaxo.Data.DateTimeColumn();
                    }
                    else if (Altaxo.Serialization.NumberConversion.IsNumeric(propvalue))
                    {
                        col = new Altaxo.Data.DoubleColumn();
                    }
                    else
                    {
                        col = new Altaxo.Data.TextColumn();
                    }

                    store.Add(col, propname); // add the column to the collection
                }

                // now the column is present we can store the value in it.
                store[propname][index] = new Altaxo.Data.AltaxoVariant(propvalue);
            }
        }
示例#2
0
        public void ImportAscii(AsciiImportOptions impopt, Altaxo.Data.DataTable table)
        {
            string sLine;

            stream.Position = 0; // rewind the stream to the beginning
            System.IO.StreamReader           sr      = new System.IO.StreamReader(stream, System.Text.Encoding.Default, true);
            Altaxo.Data.DataColumnCollection newcols = new Altaxo.Data.DataColumnCollection();

            Altaxo.Data.DataColumnCollection newpropcols = new Altaxo.Data.DataColumnCollection();

            // in case a structure is provided, allocate already the columsn

            if (null != impopt.recognizedStructure)
            {
                for (int i = 0; i < impopt.recognizedStructure.Count; i++)
                {
                    if (impopt.recognizedStructure[i] == typeof(Double))
                    {
                        newcols.Add(new Altaxo.Data.DoubleColumn());
                    }
                    else if (impopt.recognizedStructure[i] == typeof(DateTime))
                    {
                        newcols.Add(new Altaxo.Data.DateTimeColumn());
                    }
                    else if (impopt.recognizedStructure[i] == typeof(string))
                    {
                        newcols.Add(new Altaxo.Data.TextColumn());
                    }
                    else
                    {
                        newcols.Add(new Altaxo.Data.DBNullColumn());
                    };
                }
            }

            // add also additional property columns if not enough there
            if (impopt.nMainHeaderLines > 1) // if there are more than one header line, allocate also property columns
            {
                int toAdd = impopt.nMainHeaderLines - 1;
                for (int i = 0; i < toAdd; i++)
                {
                    newpropcols.Add(new Data.TextColumn());
                }
            }

            // if decimal separator statistics is provided by impopt, create a number format info object
            System.Globalization.NumberFormatInfo numberFormatInfo = null;
            if (impopt.m_DecimalSeparatorCommaCount > 0 || impopt.m_DecimalSeparatorDotCount > 0)
            {
                numberFormatInfo = (System.Globalization.NumberFormatInfo)System.Globalization.NumberFormatInfo.CurrentInfo.Clone();

                // analyse the statistics
                if (impopt.m_DecimalSeparatorCommaCount > impopt.m_DecimalSeparatorDotCount) // the comma is the decimal separator
                {
                    numberFormatInfo.NumberDecimalSeparator = ",";
                    if (numberFormatInfo.NumberGroupSeparator == numberFormatInfo.NumberDecimalSeparator)
                    {
                        numberFormatInfo.NumberGroupSeparator = ""; // in case now the group separator is also comma, remove the group separator
                    }
                }
                else if (impopt.m_DecimalSeparatorCommaCount < impopt.m_DecimalSeparatorDotCount) // the comma is the decimal separator
                {
                    numberFormatInfo.NumberDecimalSeparator = ".";
                    if (numberFormatInfo.NumberGroupSeparator == numberFormatInfo.NumberDecimalSeparator)
                    {
                        numberFormatInfo.NumberGroupSeparator = ""; // in case now the group separator is also comma, remove the group separator
                    }
                }
            }
            else // no decimal separator statistics is provided, so retrieve the numberFormatInfo object from the program options or from the current thread
            {
                numberFormatInfo = System.Globalization.NumberFormatInfo.CurrentInfo;
            }


            char [] splitchar = new char[] { impopt.cDelimiter };

            // first of all, read the header if existent
            for (int i = 0; i < impopt.nMainHeaderLines; i++)
            {
                sLine = sr.ReadLine();
                if (null == sLine)
                {
                    break;
                }

                string[] substr = sLine.Split(splitchar);
                int      cnt    = substr.Length;
                for (int k = 0; k < cnt; k++)
                {
                    if (substr[k].Length == 0)
                    {
                        continue;
                    }

                    if (k >= newcols.ColumnCount)
                    {
                        continue;
                    }

                    if (i == 0) // is it the column name line
                    {
                        newcols.SetColumnName(k, substr[k]);
                    }
                    else // this are threated as additional properties
                    {
                        ((Data.DataColumn)newpropcols[i - 1])[k] = substr[k]; // set the properties
                    }
                }
            }

            for (int i = 0; true; i++)
            {
                sLine = sr.ReadLine();
                if (null == sLine)
                {
                    break;
                }

                string[] substr = sLine.Split(splitchar);
                int      cnt    = Math.Min(substr.Length, newcols.ColumnCount);
                for (int k = 0; k < cnt; k++)
                {
                    if (substr[k].Length == 0)
                    {
                        continue;
                    }

                    if (newcols[k] is Altaxo.Data.DoubleColumn)
                    {
                        try { ((Altaxo.Data.DoubleColumn)newcols[k])[i] = System.Convert.ToDouble(substr[k], numberFormatInfo); }
                        catch {}
                    }
                    else if (newcols[k] is Altaxo.Data.DateTimeColumn)
                    {
                        try { ((Altaxo.Data.DateTimeColumn)newcols[k])[i] = System.Convert.ToDateTime(substr[k]); }
                        catch {}
                    }
                    else if (newcols[k] is Altaxo.Data.TextColumn)
                    {
                        ((Altaxo.Data.TextColumn)newcols[k])[i] = substr[k];
                    }
                    else if (null == newcols[k] || newcols[k] is Altaxo.Data.DBNullColumn)
                    {
                        bool     bConverted  = false;
                        double   val         = Double.NaN;
                        DateTime valDateTime = DateTime.MinValue;

                        try
                        {
                            val        = System.Convert.ToDouble(substr[k]);
                            bConverted = true;
                        }
                        catch
                        {
                        }
                        if (bConverted)
                        {
                            Altaxo.Data.DoubleColumn newc = new Altaxo.Data.DoubleColumn();
                            newc[i] = val;
                            newcols.Replace(k, newc);
                        }
                        else
                        {
                            try
                            {
                                valDateTime = System.Convert.ToDateTime(substr[k]);
                                bConverted  = true;
                            }
                            catch
                            {
                            }
                            if (bConverted)
                            {
                                Altaxo.Data.DateTimeColumn newc = new Altaxo.Data.DateTimeColumn();
                                newc[i] = valDateTime;

                                newcols.Replace(k, newc);
                            }
                            else
                            {
                                Altaxo.Data.TextColumn newc = new Altaxo.Data.TextColumn();
                                newc[i] = substr[k];
                                newcols.Replace(k, newc);
                            }
                        } // end outer if null==newcol
                    }
                }         // end of for all cols
            }             // end of for all lines

            // insert the new columns or replace the old ones
            table.Suspend();
            bool tableWasEmptyBefore = table.DataColumns.ColumnCount == 0;

            for (int i = 0; i < newcols.ColumnCount; i++)
            {
                if (newcols[i] is Altaxo.Data.DBNullColumn) // if the type is undefined, use a new DoubleColumn
                {
                    table.DataColumns.CopyOrReplaceOrAdd(i, new Altaxo.Data.DoubleColumn(), newcols.GetColumnName(i));
                }
                else
                {
                    table.DataColumns.CopyOrReplaceOrAdd(i, newcols[i], newcols.GetColumnName(i));
                }

                // set the first column as x-column if the table was empty before, and there are more than one column
                if (i == 0 && tableWasEmptyBefore && newcols.ColumnCount > 1)
                {
                    table.DataColumns.SetColumnKind(0, Altaxo.Data.ColumnKind.X);
                }
            } // end for loop

            // add the property columns
            for (int i = 0; i < newpropcols.ColumnCount; i++)
            {
                table.PropCols.CopyOrReplaceOrAdd(i, newpropcols[i], newpropcols.GetColumnName(i));
            }
            table.Resume();
        } // end of function ImportAscii
示例#3
0
    public void ImportAscii(AsciiImportOptions impopt, Altaxo.Data.DataTable table)
    {
      string sLine;
      stream.Position=0; // rewind the stream to the beginning
      System.IO.StreamReader sr = new System.IO.StreamReader(stream,System.Text.Encoding.Default,true);
      Altaxo.Data.DataColumnCollection newcols = new Altaxo.Data.DataColumnCollection();
    
      Altaxo.Data.DataColumnCollection newpropcols = new Altaxo.Data.DataColumnCollection();

      // in case a structure is provided, allocate already the columsn
      
      if(null!=impopt.recognizedStructure)
      {
        for(int i=0;i<impopt.recognizedStructure.Count;i++)
        {
          if(impopt.recognizedStructure[i]==typeof(Double))
            newcols.Add(new Altaxo.Data.DoubleColumn());
          else if(impopt.recognizedStructure[i]==typeof(DateTime))
            newcols.Add(new Altaxo.Data.DateTimeColumn());
          else if(impopt.recognizedStructure[i]==typeof(string))
            newcols.Add(new Altaxo.Data.TextColumn());
          else
            newcols.Add(new Altaxo.Data.DBNullColumn());;
        }
      }

      // add also additional property columns if not enough there
      if(impopt.nMainHeaderLines>1) // if there are more than one header line, allocate also property columns
      {
        int toAdd = impopt.nMainHeaderLines-1;
        for(int i=0;i<toAdd;i++)
          newpropcols.Add(new Data.TextColumn());
      }

      // if decimal separator statistics is provided by impopt, create a number format info object
      System.Globalization.NumberFormatInfo numberFormatInfo=null;
      if(impopt.m_DecimalSeparatorCommaCount>0 || impopt.m_DecimalSeparatorDotCount>0)
      {
        numberFormatInfo = (System.Globalization.NumberFormatInfo)System.Globalization.NumberFormatInfo.CurrentInfo.Clone();

        // analyse the statistics
        if(impopt.m_DecimalSeparatorCommaCount>impopt.m_DecimalSeparatorDotCount) // the comma is the decimal separator
        {
          numberFormatInfo.NumberDecimalSeparator=",";
          if(numberFormatInfo.NumberGroupSeparator==numberFormatInfo.NumberDecimalSeparator)
            numberFormatInfo.NumberGroupSeparator=""; // in case now the group separator is also comma, remove the group separator
        }
        else if(impopt.m_DecimalSeparatorCommaCount<impopt.m_DecimalSeparatorDotCount) // the comma is the decimal separator
        {
          numberFormatInfo.NumberDecimalSeparator=".";
          if(numberFormatInfo.NumberGroupSeparator==numberFormatInfo.NumberDecimalSeparator)
            numberFormatInfo.NumberGroupSeparator=""; // in case now the group separator is also comma, remove the group separator
        }
      }
      else // no decimal separator statistics is provided, so retrieve the numberFormatInfo object from the program options or from the current thread
      {
        numberFormatInfo = System.Globalization.NumberFormatInfo.CurrentInfo;
      }


      char [] splitchar = new char[]{impopt.cDelimiter};

      // first of all, read the header if existent
      for(int i=0;i<impopt.nMainHeaderLines;i++)
      {
        sLine = sr.ReadLine();
        if(null==sLine) break;

        string[] substr = sLine.Split(splitchar);
        int cnt = substr.Length;
        for(int k=0;k<cnt;k++)
        {
          if(substr[k].Length==0)
            continue;

          if(k>=newcols.ColumnCount)
            continue;
        
          if(i==0) // is it the column name line
          {
            newcols.SetColumnName(k, substr[k]);
          }
          else // this are threated as additional properties
          {
            ((Data.DataColumn)newpropcols[i-1])[k] = substr[k]; // set the properties
          }
        }
      }
      
      for(int i=0;true;i++)
      {
        sLine = sr.ReadLine();
        if(null==sLine) break;

        string[] substr = sLine.Split(splitchar);
        int cnt = Math.Min(substr.Length,newcols.ColumnCount);
        for(int k=0;k<cnt;k++)
        {
          if(substr[k].Length==0)
            continue;

          if(newcols[k] is Altaxo.Data.DoubleColumn)
          {
            try { ((Altaxo.Data.DoubleColumn)newcols[k])[i] = System.Convert.ToDouble(substr[k],numberFormatInfo); }
            catch {}
          }
          else if( newcols[k] is Altaxo.Data.DateTimeColumn)
          {
            try { ((Altaxo.Data.DateTimeColumn)newcols[k])[i] = System.Convert.ToDateTime(substr[k]); }
            catch {}
          }
          else if( newcols[k] is Altaxo.Data.TextColumn)
          {
            ((Altaxo.Data.TextColumn)newcols[k])[i] = substr[k];
          }
          else if(null==newcols[k] || newcols[k] is Altaxo.Data.DBNullColumn)
          {
            bool bConverted = false;
            double val=Double.NaN;
            DateTime valDateTime=DateTime.MinValue;

            try
            { 
              val = System.Convert.ToDouble(substr[k]);
              bConverted=true;
            }
            catch
            {
            }
            if(bConverted)
            {
              Altaxo.Data.DoubleColumn newc = new Altaxo.Data.DoubleColumn();
              newc[i]=val;
              newcols.Replace(k,newc);
            }
            else
            {
              try
              { 
                valDateTime = System.Convert.ToDateTime(substr[k]);
                bConverted=true;
              }
              catch
              {
              }
              if(bConverted)
              {
                Altaxo.Data.DateTimeColumn newc = new Altaxo.Data.DateTimeColumn();
                newc[i]=valDateTime;
                
                newcols.Replace(k, newc);
              }
              else
              {
                Altaxo.Data.TextColumn newc = new Altaxo.Data.TextColumn();
                newc[i]=substr[k];
                newcols.Replace(k,newc);
              }
            } // end outer if null==newcol
          }
        } // end of for all cols


      } // end of for all lines
      
      // insert the new columns or replace the old ones
      table.Suspend();
      bool tableWasEmptyBefore = table.DataColumns.ColumnCount==0;
      for(int i=0;i<newcols.ColumnCount;i++)
      {
        if(newcols[i] is Altaxo.Data.DBNullColumn) // if the type is undefined, use a new DoubleColumn
          table.DataColumns.CopyOrReplaceOrAdd(i,new Altaxo.Data.DoubleColumn(), newcols.GetColumnName(i));
        else
          table.DataColumns.CopyOrReplaceOrAdd(i,newcols[i], newcols.GetColumnName(i));

        // set the first column as x-column if the table was empty before, and there are more than one column
        if(i==0 && tableWasEmptyBefore && newcols.ColumnCount>1)
          table.DataColumns.SetColumnKind(0,Altaxo.Data.ColumnKind.X);

      } // end for loop

      // add the property columns
      for(int i=0;i<newpropcols.ColumnCount;i++)
      {
        table.PropCols.CopyOrReplaceOrAdd(i,newpropcols[i], newpropcols.GetColumnName(i));
      }
      table.Resume();
    } // end of function ImportAscii