Exemple #1
0
        /// <summary>
        /// Adds a Csv Data into runtime Memory using a Reference. Use this reference in the various csv related Actions to work with that particular Data.
        /// </summary>
        /// <param name="data">Data.</param>
        /// <param name="reference">Reference.</param>
        public static void AddReference(CsvData data, string reference)
        {
            if (References == null)
            {
                References = new Dictionary <string, CsvData>();
            }

            References[reference] = data;
        }
Exemple #2
0
        public static CsvData LoadFromString(string file_contents, bool hasHeader = false, char delimiter = ',')
        {
            CsvData _result = new CsvData(hasHeader);

            int file_length = file_contents.Length;

            // read char by char and when a , or \n, perform appropriate action
            int           cur_file_index = 0;                   // index in the file
            List <string> cur_line       = new List <string>(); // current line of data


            StringBuilder cur_item      = new StringBuilder("");
            bool          inside_quotes = false;    // managing quotes

            while (cur_file_index < file_length)
            {
                char c = file_contents[cur_file_index++];

                if (c == '"')
                {
                    if (!inside_quotes)
                    {
                        inside_quotes = true;
                    }
                    else
                    {
                        if (cur_file_index == file_length)
                        {
                            // end of file
                            inside_quotes = false;
                            //goto case '\n';
                            cur_line.Add(cur_item.ToString());
                            cur_item.Length = 0;
                            if (c == '\n' || cur_file_index == file_length)
                            {
                                _result.AddRecord(cur_line);
                                cur_line.Clear();
                            }
                        }
                        else if (file_contents [cur_file_index] == '"')
                        {
                            // double quote, save one
                            cur_item.Append("\"");
                            cur_file_index++;
                        }
                        else
                        {
                            // leaving quotes section
                            inside_quotes = false;
                        }
                    }
                }
                else if (c == '\r')
                {
                    // ignore it completely
                    Debug.Log("OUPPS found a \\r");
                }
                else if (c == '\n' || c == delimiter)
                {
                    if (inside_quotes)
                    {
                        // inside quotes, this characters must be included
                        cur_item.Append(c);
                    }
                    else
                    {
                        // end of current item
                        cur_line.Add(cur_item.ToString());
                        cur_item.Length = 0;
                        if (c == '\n' || cur_file_index == file_length)
                        {
                            _result.AddRecord(cur_line);
                            cur_line.Clear();
                        }
                    }
                }
                else
                {
                    // other cases, add char
                    cur_item.Append(c);
                }
            }

            // fix for last item of last line not using quotes
            if (cur_item.Length != 0)
            {
                // end of current item
                cur_line.Add(cur_item.ToString());
                cur_item.Length = 0;
                _result.AddRecord(cur_line);
                cur_line.Clear();
            }

            _result.OnParseEnded();

            return(_result);
        }