Exemplo n.º 1
0
    internal object GetCell(System.Type type, int col, int row)
    {
        string value;

        if (col >= cols || row >= rows)
        {
            throw new System.IndexOutOfRangeException("Cell (" + col + ", " + row + ") is out of bounds of spreadsheet (" + cols + ", " + rows + ").");
        }

        if (!cells.TryGetValue(new Index(col, row), out value) || string.IsNullOrEmpty(value))
        {
            ES3Debug.Log("Getting cell (" + col + "," + row + ") is empty, so default value is being returned");
            return(null);
        }

        // If we're loading a string, simply return the string value.
        if (type == typeof(string))
        {
            var str = (object)value;
            ES3Debug.Log("Getting cell (" + col + "," + row + ") with value " + str);
            return(str);
        }

        var settings = new ES3Settings();

        using (var ms = new MemoryStream(settings.encoding.GetBytes(value)))
        {
            using (var jsonReader = new ES3JSONReader(ms, settings, false))
            {
                var obj = ES3TypeMgr.GetOrCreateES3Type(type, true).Read <object>(jsonReader);
                ES3Debug.Log("Getting cell (" + col + "," + row + ") with value " + obj);
                return(obj);
            }
        }
    }
Exemplo n.º 2
0
    public void SetCell <T>(int col, int row, T value)
    {
        ES3Debug.Log("Setting cell (" + col + "," + row + ") to value " + value);

        // If we're writing a string, add it without formatting.
        if (value.GetType() == typeof(string))
        {
            SetCellString(col, row, (string)(object)value);
            return;
        }

        var settings = new ES3Settings();

        using (var ms = new MemoryStream())
        {
            using (var jsonWriter = new ES3JSONWriter(ms, settings, false, false))
                jsonWriter.Write(value, ES3.ReferenceMode.ByValue);

            SetCellString(col, row, settings.encoding.GetString(ms.ToArray()));
        }

        // Expand the spreadsheet if necessary.
        if (col >= cols)
        {
            cols = (col + 1);
        }
        if (row >= rows)
        {
            rows = (row + 1);
        }
    }
Exemplo n.º 3
0
 internal virtual void StartWriteProperty(string name)
 {
     if (name == null)
     {
         throw new ArgumentNullException("Key or field name cannot be NULL when saving data.");
     }
     ES3Debug.Log("<b>" + name + "</b> (writing property)", null, serializationDepth);
 }
Exemplo n.º 4
0
    public void Save(ES3Settings settings, bool append)
    {
        using (var writer = new StreamWriter(ES3Stream.CreateStream(settings, append ? ES3FileMode.Append : ES3FileMode.Write)))
        {
            // If data already exists and we're appending, we need to prepend a newline.
            if (append && ES3.FileExists(settings))
            {
                writer.Write(NEWLINE_CHAR);
            }

            var array = ToArray();
            for (int row = 0; row < rows; row++)
            {
                if (row != 0)
                {
                    writer.Write(NEWLINE_CHAR);
                }

                for (int col = 0; col < cols; col++)
                {
                    if (col != 0)
                    {
                        writer.Write(COMMA_CHAR);
                    }

                    ES3Debug.Log("Writing cell (" + col + "," + row + ") to file with value " + array[col, row]);

                    writer.Write(Escape(array[col, row]));
                }
            }
        }
        if (!append)
        {
            ES3IO.CommitBackup(settings);
        }
    }
Exemplo n.º 5
0
 internal virtual void StartWriteProperty(string name)
 {
     ES3Debug.Log("<b>" + name + "</b> (writing property)", null, serializationDepth);
 }
Exemplo n.º 6
0
 public ES3Spreadsheet()
 {
     ES3Debug.Log("ES3Spreadsheet created");
 }
Exemplo n.º 7
0
    private void Load(Stream stream, ES3Settings settings)
    {
        using (var reader = new StreamReader(stream))
        {
            int    c_int;
            char   c;
            string value = "";
            int    col   = 0;
            int    row   = 0;

            ES3Debug.Log("Reading spreadsheet " + settings.path + " from " + settings.location);

            // Read until the end of the stream.
            while (true)
            {
                c_int = reader.Read();
                c     = (char)c_int;
                if (c == QUOTE_CHAR)
                {
                    while (true)
                    {
                        c = (char)reader.Read();

                        if (c == QUOTE_CHAR)
                        {
                            // If this quote isn't escaped by another, it is the last quote, so we should stop parsing this value.
                            if (((char)reader.Peek()) != QUOTE_CHAR)
                            {
                                break;
                            }
                            else
                            {
                                c = (char)reader.Read();
                            }
                        }
                        value += c;
                    }
                }
                // If this is the end of a column, row, or the stream, add the value to the spreadsheet.
                else if (c == COMMA_CHAR || c == NEWLINE_CHAR || c_int == -1)
                {
                    SetCell(col, row, value);
                    value = "";
                    if (c == COMMA_CHAR)
                    {
                        col++;
                    }
                    else if (c == NEWLINE_CHAR)
                    {
                        col = 0;
                        row++;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    value += c;
                }
            }
        }
        ES3Debug.Log("Finished reading spreadsheet " + settings.path + " from " + settings.location);
    }