Exemplo n.º 1
0
        public void AppendDataLineArray(object[] data)
        {
            if (st == null)
            {
                throw new Exception("File not open for Data Add");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (data.Length != vars.Count)
            {
                throw new ArgumentException("Count of Variables is wrong");
            }

            var writeData = new List <Action>();
            int i         = 0;

            foreach (var item in vars)
            {
                var dataItem = data[i];
                if (dataItem == null)
                {
                    dataItem = StataMissingValues._empty;
                }

                StataMissingValues?mv = null;
                if (dataItem is StataMissingValues)
                {
                    mv = (StataMissingValues)data[i];
                }

                switch (item.VarType)
                {
                    #region Byte
                case StataVariable.StataVarType.Byte:
                {
                    sbyte b = 0;
                    if (mv.HasValue)
                    {
                        b = b.GetMissingValue(mv);
                    }
                    else
                    {
                        b = Convert.ToSByte(dataItem);
                    }
                    writeData.Add(new Action(() => bw.Write(b)));
                }
                break;
                    #endregion

                    #region Int
                case StataVariable.StataVarType.Int:
                {
                    Int16 b = 0;
                    if (mv.HasValue)
                    {
                        b = b.GetMissingValue(mv);
                    }
                    else
                    {
                        b = Convert.ToInt16(data[i]);
                    }
                    writeData.Add(new Action(() => bw.Write(b)));
                }
                break;
                    #endregion

                    #region Long
                case StataVariable.StataVarType.Long:
                {
                    Int32 b = 0;
                    if (mv.HasValue)
                    {
                        b = b.GetMissingValue(mv);
                    }
                    else
                    {
                        b = Convert.ToInt32(data[i]);
                    }
                    writeData.Add(new Action(() => bw.Write(b)));
                }
                break;
                    #endregion

                    #region Float
                case StataVariable.StataVarType.Float:
                {
                    float f = 0;
                    if (mv.HasValue)
                    {
                        f = f.GetMissingValue(mv);
                    }
                    else
                    {
                        f = Convert.ToSingle(data[i]);
                    }
                    writeData.Add(new Action(() => bw.Write(f)));
                }
                break;
                    #endregion

                    #region Double
                case StataVariable.StataVarType.Double:
                {
                    Double d = 0;
                    if (mv.HasValue)
                    {
                        d = d.GetMissingValue(mv);
                    }
                    else
                    {
                        d = Convert.ToDouble(data[i]);
                    }
                    writeData.Add(new Action(() => bw.Write(d)));
                }
                break;
                    #endregion

                    #region Fixed Strings
                case StataVariable.StataVarType.FixedString:
                {
                    string s = data[i].ToString().PadRight(item.FixedStringLength, (char)0x00).Substring(0, item.FixedStringLength);
                    writeData.Add(new Action(() => bw.Write(env.GetString(Encoding.Default.GetBytes(s)))));
                }
                break;
                    #endregion

                    #region GSO Strings
                case StataVariable.StataVarType.String:
                {
                    string s = env.GetString(Encoding.Default.GetBytes(data[i].ToString()));

                    UInt32 v = 0;
                    UInt32 o = 0;

                    if (s != "")
                    {
                        v = (UInt32)(i + 1);
                        o = dataCount + (UInt32)1;
                        if (!smallMemoryFootprint)
                        {
                            #region local optimized Cache
                            if (!GSO.ContainsKey(v))
                            {
                                GSO.Add(v, new Dictionary <string, UInt32>());
                            }

                            if (GSO[v].ContainsKey(s))
                            {
                                // already in memory, find object number
                                o = GSO[v][s];
                            }
                            else
                            {
                                // not in memory add new
                                GSO[v].Add(s, o);
                            }
                            //var idx = GSO[v].IndexOf(s);
                            //o = (UInt32)idx;
                            //idx = -1;
                            //if (idx == -1)
                            //{
                            //    GSO[v].Add(s);
                            //    o = (UInt32)GSO[v].Count;
                            //}
                            #endregion
                        }
                        else
                        {
                            // direct write to tmp GSO File
                            tmpGSObw.WriteGSO(v, o, s);
                        }
                    }

                    writeData.Add(new Action(() =>
                        {
                            bw.Write(v);
                            bw.Write(o);
                        }));
                }
                break;
                    #endregion
                }
                i++;
            }

            foreach (var item in writeData)
            {
                item.Invoke();
            }

            dataCount++;
        }