Exemple #1
0
 public DataWriter(System.IO.Stream stream, FileMarkers type, int dataVersion = -1, Bitmap thumbnail = null) : base(stream, System.Text.Encoding.Unicode)
 {
     WriteByte((byte)type);
     if (dataVersion < 0)
     {
         dataVersion = DataUtilities.FILEVERSION;
     }
     Version = dataVersion;
     InitialiseFile(thumbnail, type);
 }
Exemple #2
0
        public List <Datum> ReadDataList(FileMarkers eRequireType = FileMarkers.Undetermined)
        {
            // like the above, apart from the return type.  Also if eRequireType is changed to any other type, then only that type object will be permitted
            int          last = Reader.ReadInt16() - 1;
            List <Datum> list = new List <Datum>(last + 1);

            for (int index = 0; index <= last; index++)
            {
                list.Add(ReadData(eRequireType));
            }
            return(list);
        }
Exemple #3
0
 public DataReader(System.IO.Stream fs, FileMarkers type)
 {
     m_File = "[Stream]";
     Reader = new System.IO.BinaryReader(fs, System.Text.Encoding.Unicode);
     try
     {
         InitialiseFile(type);
     }
     catch
     {
         Reader.Close();
         throw;
     }
 }
Exemple #4
0
 public DataReader(string file, FileMarkers type)
 {
     m_File = file;
     Reader = new System.IO.BinaryReader(new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read), System.Text.Encoding.Unicode);
     try
     {
         InitialiseFile(type);
     }
     catch
     {
         Reader.Close();
         throw;
     }
 }
Exemple #5
0
        internal static Paper CreateFromTypeCode(FileMarkers type)
        {
            // this is only used when loading, or possibly undoing so no values need to be set sensibly
            switch (type)
            {
            case FileMarkers.GraphPaper:
                return(new GraphPaper());

            case FileMarkers.Paper:
                return(new Paper(Papers.Plain));

            default:
                throw new ArgumentException("Paper.CreateFromTypeCode, unexpected type code: " + type);
            }
        }
Exemple #6
0
 private void InitialiseFile(Bitmap thumbnail, FileMarkers type)
 {
     Write(Version);
     WriteByte(NUMBERSTRINGS);
     m_Strings = new string[NUMBERSTRINGS + 1];             // +1 since they are numbered 1..N
     for (int index = 1; index <= NUMBERSTRINGS; index++)
     {
         m_Strings[index] = "";
     }
     if (Version < DataUtilities.MINIMUMSUPPORTEDVERSION)
     {
         throw new UserException("[File_TooOld]");
     }
     Debug.Assert(DataUtilities.MINIMUMSUPPORTEDVERSION >= 60); // assumed throughout now
     Write(SoftwareVersion.Version);                            // software version
     Write(DataUtilities.MINIMUMCOMPATIBLEVERSION);             // minimum version to read this file
     WriteOptionalPNG(thumbnail);
 }
Exemple #7
0
        // strings used are 1..n (0 not used)
        // each 'Buffered' string written is preceeded by a byte. 0=empty - string itself not written.
        // 1..n = new string, normal BinaryWriter string to follow
        // 128+n = repeat of string number n

        public DataWriter(string file, FileMarkers type, int dataVersion = -1, Bitmap thumbnail = null) : base(new System.IO.FileStream(file, System.IO.FileMode.Create, System.IO.FileAccess.Write), System.Text.Encoding.Unicode)
        {
            try
            {
                WriteByte((byte)type);
                if (dataVersion < 0)
                {
                    dataVersion = DataUtilities.FILEVERSION;
                }
                Version = dataVersion;
                InitialiseFile(thumbnail, type);
            }
            catch
            {
                base.Close();
                throw;
            }
        }
Exemple #8
0
 private void InitialiseFile(FileMarkers type)
 {
     // Shared between the constructors
     FileType = (FileMarkers)Reader.ReadByte();
     if (FileType != type && type != FileMarkers.Undetermined)
     {
         throw new FileTypeException("Wrong file type marker: " + FileType);
     }
     Version = Reader.ReadInt32();
     Debug.WriteLineIf(Version == 71, "Opening file version 71: this should only happen for the latest v1 files (this number was also used for the earliest v2 files");
     if (Version < DataUtilities.MINIMUMSUPPORTEDVERSION)
     {
         throw new UserException("[File_TooOld]", true);
     }
     m_NumberStrings        = ReadByte();
     m_Strings              = new string[m_NumberStrings + 1];
     SoftwareVersion        = ReadSingle();
     MinimumSoftwareVersion = ReadSingle();
     //if (Globals.Root != null && MinimumSoftwareVersion > SAW.SoftwareVersion.Version) // AM is nothing when loading initial configs
     //	throw new UserException(Strings.Item("File_Too_New").Replace("%0", SAW.SoftwareVersion.VersionStringFromNumber(MinimumSoftwareVersion)));
     Thumbnail = ReadOptionalPNG();
 }
Exemple #9
0
        public static Datum Create(FileMarkers type)
        {
            switch (type)
            {
#if APPLICATION
            case FileMarkers.Page:
                return(new Page());

            case FileMarkers.Paper:
            case FileMarkers.GraphPaper:
                return(Paper.CreateFromTypeCode(type));

            case FileMarkers.Document:
                return(new Document(true));

            case FileMarkers.SharedBitmap:
                return(new SharedImage());

            case FileMarkers.Config:
                return(new Config());

            case FileMarkers.ButtonStyle:
                return(new ButtonStyle());

            case FileMarkers.SharedResource:
                return(new SharedResource());
#endif
            default:
                if (type <= FileMarkers.MaximumShape)
                {
                    return(Shape.CreateShape((Shape.Shapes)type));
                }
                else
                {
                    throw new ArgumentException("Datum.Create: unknown type code: " + type);
                }
            }
        }
Exemple #10
0
        public Dictionary <Guid, Datum> LoadedObjects = new Dictionary <Guid, Datum>();       // list of Datum loaded by ID
        // LoadedObjects will survive the Close command
        public Datum ReadData(FileMarkers expectedType = FileMarkers.Undetermined)
        {
            FileMarkers type = (FileMarkers)ReadByte();

            if (type == FileMarkers.Omitted)
            {
                return(null);                // note that Omitted does not store the length
            }
            var expectedLength = ReadInt32();
            int startPosition  = (int)Reader.BaseStream.Position;

            if (expectedType != FileMarkers.Undetermined && expectedType != type)
            {
                throw new InvalidDataException("Expected datatype: " + expectedType);
            }
            // in practice all the data will be derived from Datum
            var create = Datum.Create(type);

            if (create.TypeByte == (byte)FileMarkers.Document && LoadedObjects.Count > 0)
            {
                // in case of documents need to isolate the created object IDs.  But not if this was the root object (hence second condition)
                Dictionary <Guid, Datum> old = LoadedObjects;
                try
                {
                    LoadedObjects = new Dictionary <Guid, Datum>();
                    create.Load(this);
                }
                finally
                {
                    LoadedObjects = old;
                }
            }
            else
            {
                create.Load(this);
            }
            if (expectedLength > -1)
            {
                int actualLength = (int)(Reader.BaseStream.Position - startPosition);
                if (actualLength > expectedLength)
                {
                    throw new InvalidDataException("Reading datatype " + type + " used " + actualLength + ", whereas the record was only actually " + expectedLength + " bytes long");
                }
                else if (actualLength < expectedLength)
                {
                    Debug.WriteLine("Discarding " + (expectedLength - actualLength) + " bytes when reading datatype: " + type);
                    Reader.BaseStream.Seek(expectedLength - actualLength, System.IO.SeekOrigin.Current);
                }
            }
            if (!create.ID.IsEmpty() && !create.ID.Equals(ButtonStyle.idSelectionDefault))             // ButtonStyles can use empty IDs for those which don't need referencing
            {
                if (!LoadedObjects.ContainsKey(create.ID))
                {
                    LoadedObjects.Add(create.ID, create);
                }
                else
                {
                    Guid old = create.ID;
                    create.ID = Guid.NewGuid();
                    if (FileType != FileMarkers.ErrorReport)
                    {
                        Utilities.LogSubError("Object ID repeated in file: type=" + create.TypeByte + ", ID changed from: " + old + ", to: " + create.ID + ", file=" + m_File);
                    }
                }
            }
            return(create);
        }