コード例 #1
0
ファイル: MapFile.cs プロジェクト: smejko80/samschanneledit
        public bool ReadFrom(string directory)
        {
            Changed = false;

            string fullPathFileName = Path.Combine(directory, FileName);

            if (LOG.IsDebugEnabled)
            {
                LOG.Debug("Read MapFile " + fullPathFileName);
            }

            int index = 0;

            if (!File.Exists(fullPathFileName))
            {
                throw new FileLoadException(string.Format("File {0} not found", fullPathFileName), "Open file error");
            }

            long filelen = FileUtils.GetFileSize(fullPathFileName);

            RegSize = getRecordSize(MapType, filelen);

            /*
             * switch (MapType)
             * {
             * case SCMFileContentType.mapAirA:
             * case SCMFileContentType.mapCableA:
             *  recordsize = 40; // C Series
             *  if (filelen % (recordsize * 1000) == 0)
             *  {
             *    RegSize = recordsize;
             *    RegCount = (int)(filelen / recordsize);
             *  }
             *  else
             *  {
             *    recordsize = 64; // D Series
             *    if (filelen % (recordsize * 1000) == 0)
             *    {
             *      RegSize = recordsize;
             *      RegCount = (int)(filelen / recordsize);
             *    }
             *  }
             *  break;
             *
             * case SCMFileContentType.mapSateD:
             *  recordsize = 172; // 172 multiple
             *  if (filelen % (recordsize * 1000) == 0)
             *  {
             *    RegSize = recordsize;
             *    RegCount = (int)(filelen / recordsize);
             *  }
             *  else
             *  {
             *    recordsize = 144; // LE37C670 ??
             *    if (filelen % (recordsize * 1000) == 0)
             *    {
             *      RegSize = recordsize;
             *      RegCount = (int)(filelen / recordsize);
             *    }
             *  }
             *  break;
             *
             * case SCMFileContentType.mapAstraHDPlusD:
             *  recordsize = 212; // 212 multiple
             *  if (filelen % (recordsize * 1000) == 0)
             *  {
             *    RegSize = recordsize;
             *    RegCount = (int)(filelen / recordsize);
             *  }
             *  break;
             *
             * default:
             *  recordsize = 292; // C Series
             *  if (filelen % (recordsize * 1000) == 0)
             *  {
             *    RegSize = recordsize;
             *    RegCount = (int)(filelen / recordsize);
             *  }
             *  else
             *  {
             *    recordsize = 320; // D Series
             *    if (filelen % (recordsize * 1000) == 0)
             *    {
             *      RegSize = recordsize;
             *      RegCount = (int)(filelen / recordsize);
             *    }
             *  }
             *  break;
             * }*/

            if (RegSize == 0)
            {
                throw new FileLoadException(string.Format("Bad file size ({0}).", filelen), "Open file error");
            }

            RegCount = (int)(filelen / RegSize);

            Regtmp = new byte[RegSize];

            Channels.Clear();
            using (FileStream fs = File.Open(fullPathFileName, FileMode.Open))
            {
                int readed = fs.Read(Regtmp, 0, Regtmp.Length);
                while (readed > 0)
                {
                    IChannel ch;

                    switch (MapType)
                    {
                    case SCMFileContentType.mapSateD:
                        ch = new StateChannel(index++, Regtmp);
                        break;

                    case SCMFileContentType.mapAstraHDPlusD:
                        ch = new AstraHDChannel(index++, Regtmp);
                        break;

                    case SCMFileContentType.mapAirA:
                    case SCMFileContentType.mapCableA:
                        ch = new MapChannelAnalog(index++, Regtmp);
                        break;

                    default:
                        ch = new MapChannel(index++, Regtmp);
                        break;
                    }

#if DEBUG
                    // Check if current checksums are Ok
                    byte bcalculated = ch.CalcChecksum(false);
                    byte breal       = ch.Data[RegSize - 1];
                    if (bcalculated != breal)
                    {
                        throw new InvalidDataException(string.Format("Checksum error: Expected {0:x2} and current is {1:x2}", bcalculated, breal));
                    }
#endif
                    if (ch.IsOk())
                    {
                        Channels.Add(ch);
                    }

                    readed = fs.Read(Regtmp, 0, Regtmp.Length);
                }
                fs.Close();
            }
            return(true);
        }
コード例 #2
0
ファイル: MapFile.cs プロジェクト: Tiggor/samschanneledit
        public bool ReadFrom(string directory)
        {
            Changed = false;

              string fullPathFileName = Path.Combine(directory, FileName);

              if (LOG.IsDebugEnabled)
            LOG.Debug("Read MapFile " + fullPathFileName);

              int index = 0;

              if (!File.Exists(fullPathFileName))
            throw new FileLoadException(string.Format("File {0} not found", fullPathFileName), "Open file error");

              long filelen = FileUtils.GetFileSize(fullPathFileName);
              RegSize = getRecordSize(MapType, filelen);

              /*
              switch (MapType)
              {
            case SCMFileContentType.mapAirA:
            case SCMFileContentType.mapCableA:
              recordsize = 40; // C Series
              if (filelen % (recordsize * 1000) == 0)
              {
            RegSize = recordsize;
            RegCount = (int)(filelen / recordsize);
              }
              else
              {
            recordsize = 64; // D Series
            if (filelen % (recordsize * 1000) == 0)
            {
              RegSize = recordsize;
              RegCount = (int)(filelen / recordsize);
            }
              }
              break;

            case SCMFileContentType.mapSateD:
              recordsize = 172; // 172 multiple
              if (filelen % (recordsize * 1000) == 0)
              {
            RegSize = recordsize;
            RegCount = (int)(filelen / recordsize);
              }
              else
              {
            recordsize = 144; // LE37C670 ??
            if (filelen % (recordsize * 1000) == 0)
            {
              RegSize = recordsize;
              RegCount = (int)(filelen / recordsize);
            }
              }
              break;

            case SCMFileContentType.mapAstraHDPlusD:
              recordsize = 212; // 212 multiple
              if (filelen % (recordsize * 1000) == 0)
              {
            RegSize = recordsize;
            RegCount = (int)(filelen / recordsize);
              }
              break;

            default:
              recordsize = 292; // C Series
              if (filelen % (recordsize * 1000) == 0)
              {
            RegSize = recordsize;
            RegCount = (int)(filelen / recordsize);
              }
              else
              {
            recordsize = 320; // D Series
            if (filelen % (recordsize * 1000) == 0)
            {
              RegSize = recordsize;
              RegCount = (int)(filelen / recordsize);
            }
              }
              break;
              }*/

              if (RegSize == 0)
            throw new FileLoadException(string.Format("Bad file size ({0}).", filelen), "Open file error");

              RegCount = (int)(filelen / RegSize);

              Regtmp = new byte[RegSize];

              Channels.Clear();
              using (FileStream fs = File.Open(fullPathFileName, FileMode.Open))
              {
            int readed = fs.Read(Regtmp, 0, Regtmp.Length);
            while (readed > 0)
            {
              IChannel ch;

              switch (MapType)
              {
            case SCMFileContentType.mapSateD:
              ch = new StateChannel(index++, Regtmp);
              break;

            case SCMFileContentType.mapAstraHDPlusD:
              ch = new AstraHDChannel(index++, Regtmp);
              break;

            case SCMFileContentType.mapAirA:
            case SCMFileContentType.mapCableA:
              ch = new MapChannelAnalog(index++, Regtmp);
              break;

            default:
              ch = new MapChannel(index++, Regtmp);
              break;
              }

            #if DEBUG
              // Check if current checksums are Ok
              byte bcalculated = ch.CalcChecksum(false);
              byte breal = ch.Data[RegSize - 1];
              if (bcalculated != breal)
            throw new InvalidDataException(string.Format("Checksum error: Expected {0:x2} and current is {1:x2}", bcalculated, breal));
            #endif
              if (ch.IsOk())
            Channels.Add(ch);

              readed = fs.Read(Regtmp, 0, Regtmp.Length);
            }
            fs.Close();
              }
              return true;
        }