Exemple #1
0
 /// <summary>
 /// Opens the specified file and creates a editable document
 /// </summary>
 public DockContent OpenEditableDocument(String org, Encoding encoding, Boolean restorePosition)
 {
     DockContent createdDoc;
     EncodingFileInfo info = new EncodingFileInfo();
     String file = PathHelper.GetPhysicalPathName(org);
     TextEvent te = new TextEvent(EventType.FileOpening, file);
     EventManager.DispatchEvent(this, te);
     if (te.Handled)
     {
         if (this.Documents.Length == 0)
         {
             this.New(null, null);
             return null;
         }
         else return null;
     }
     else if (file.EndsWith(".fdz"))
     {
         this.CallCommand("ExtractZip", file);
         return null;
     }
     try
     {
         Int32 count = this.Documents.Length;
         for (Int32 i = 0; i < count; i++)
         {
             if (this.Documents[i].IsEditable && this.Documents[i].FileName.ToUpper() == file.ToUpper())
             {
                 this.Documents[i].Activate();
                 return null;
             }
         }
     }
     catch {}
     if (encoding == null)
     {
         info = FileHelper.GetEncodingFileInfo(file);
         if (info.CodePage == -1) return null; // If the file is locked, stop.
     }
     else
     {
         info = FileHelper.GetEncodingFileInfo(file);
         info.Contents = FileHelper.ReadFile(file, encoding);
         info.CodePage = encoding.CodePage;
     }
     DataEvent de = new DataEvent(EventType.FileDecode, file, null);
     EventManager.DispatchEvent(this, de); // Lets ask if a plugin wants to decode the data..
     if (de.Handled)
     {
         info.Contents = de.Data as String;
         info.CodePage = Encoding.UTF8.CodePage; // assume plugin always return UTF8
     }
     try
     {
         if (this.CurrentDocument != null && this.CurrentDocument.IsUntitled && !this.CurrentDocument.IsModified && this.Documents.Length == 1)
         {
             this.closingForOpenFile = true;
             this.CurrentDocument.Close();
             this.closingForOpenFile = false;
             createdDoc = this.CreateEditableDocument(file, info.Contents, info.CodePage);
         }
         else createdDoc = this.CreateEditableDocument(file, info.Contents, info.CodePage);
         ButtonManager.AddNewReopenMenuItem(file);
     }
     catch
     {
         createdDoc = this.CreateEditableDocument(file, info.Contents, info.CodePage);
         ButtonManager.AddNewReopenMenuItem(file);
     }
     TabbedDocument document = (TabbedDocument)createdDoc;
     document.SciControl.SaveBOM = info.ContainsBOM;
     document.SciControl.BeginInvoke((MethodInvoker)delegate 
     {
         if (this.appSettings.RestoreFileStates)
         {
             FileStateManager.ApplyFileState(document, restorePosition);
         }
     });
     ButtonManager.UpdateFlaggedButtons();
     return createdDoc;
 }
Exemple #2
0
 /// <summary>
 /// Acquires encoding related info on one read.
 /// </summary>
 public static EncodingFileInfo GetEncodingFileInfo(String file)
 {
     Int32 startIndex = 0;
     EncodingFileInfo info = new EncodingFileInfo();
     try
     {
         if (File.Exists(file))
         {
             Byte[] bytes = File.ReadAllBytes(file);
             if (bytes.Length > 2 && (bytes[0] == 0xef && bytes[1] == 0xbb && bytes[2] == 0xbf))
             {
                 startIndex = 3;
                 info.ContainsBOM = true;
                 info.CodePage = Encoding.UTF8.CodePage;
             }
             else if (bytes.Length > 3 && (bytes[0] == 0xff && bytes[1] == 0xfe && bytes[2] == 0x00 && bytes[3] == 0x00))
             {
                 startIndex = 4;
                 info.ContainsBOM = true;
                 info.CodePage = Encoding.UTF32.CodePage;
             }
             else if (bytes.Length > 4 && ((bytes[0] == 0x2b && bytes[1] == 0x2f && bytes[2] == 0x76) && (bytes[3] == 0x38 || bytes[3] == 0x39 || bytes[3] == 0x2b || bytes[3] == 0x2f) && bytes[4] == 0x2D))
             {
                 startIndex = 5;
                 info.ContainsBOM = true;
                 info.CodePage = Encoding.UTF7.CodePage;
             }
             else if (bytes.Length > 3 && ((bytes[0] == 0x2b && bytes[1] == 0x2f && bytes[2] == 0x76) && (bytes[3] == 0x38 || bytes[3] == 0x39 || bytes[3] == 0x2b || bytes[3] == 0x2f)))
             {
                 startIndex = 4;
                 info.ContainsBOM = true;
                 info.CodePage = Encoding.UTF7.CodePage;
             }
             else if (bytes.Length > 1 && (bytes[0] == 0xff && bytes[1] == 0xfe))
             {
                 startIndex = 2;
                 info.ContainsBOM = true;
                 info.CodePage = Encoding.Unicode.CodePage;
             }
             else if (bytes.Length > 1 && (bytes[0] == 0xfe && bytes[1] == 0xff))
             {
                 startIndex = 2;
                 info.ContainsBOM = true;
                 info.CodePage = Encoding.BigEndianUnicode.CodePage;
             }
             else
             {
                 if (!ContainsInvalidUTF8Bytes(bytes)) info.CodePage = Encoding.UTF8.CodePage;
                 else info.CodePage = Encoding.Default.CodePage;
             }
             Int32 contentLength = bytes.Length - startIndex;
             if (bytes.Length > 0 && bytes.Length > startIndex)
             {
                 Encoding encoding = Encoding.GetEncoding(info.CodePage);
                 info.Contents = encoding.GetString(bytes, startIndex, contentLength);
             }
         }
     }
     catch (Exception)
     {
         info = new EncodingFileInfo();
     }
     return info;
 }
Exemple #3
0
        /// <summary>
        /// Reads the file and returns its contents (autodetects encoding and fallback codepage)
        /// </summary>
        public static String ReadFile(String file)
        {
            EncodingFileInfo info = GetEncodingFileInfo(file);

            return(info.Contents);
        }
Exemple #4
0
        /// <summary>
        /// Acquires encoding related info on one read.
        /// </summary>
        public static EncodingFileInfo GetEncodingFileInfo(String file)
        {
            Int32            startIndex = 0;
            EncodingFileInfo info       = new EncodingFileInfo();

            try
            {
                if (File.Exists(file))
                {
                    Byte[] bytes = File.ReadAllBytes(file);
                    if (bytes.Length > 2 && (bytes[0] == 0xef && bytes[1] == 0xbb && bytes[2] == 0xbf))
                    {
                        startIndex       = 3;
                        info.BomLength   = 3;
                        info.ContainsBOM = true;
                        info.CodePage    = Encoding.UTF8.CodePage;
                    }
                    else if (bytes.Length > 3 && (bytes[0] == 0xff && bytes[1] == 0xfe && bytes[2] == 0x00 && bytes[3] == 0x00))
                    {
                        startIndex       = 4;
                        info.BomLength   = 4;
                        info.ContainsBOM = true;
                        info.CodePage    = Encoding.UTF32.CodePage;
                    }
                    else if (bytes.Length > 4 && ((bytes[0] == 0x2b && bytes[1] == 0x2f && bytes[2] == 0x76) && (bytes[3] == 0x38 || bytes[3] == 0x39 || bytes[3] == 0x2b || bytes[3] == 0x2f) && bytes[4] == 0x2D))
                    {
                        startIndex       = 5;
                        info.BomLength   = 5;
                        info.ContainsBOM = true;
                        info.CodePage    = Encoding.UTF7.CodePage;
                    }
                    else if (bytes.Length > 3 && ((bytes[0] == 0x2b && bytes[1] == 0x2f && bytes[2] == 0x76) && (bytes[3] == 0x38 || bytes[3] == 0x39 || bytes[3] == 0x2b || bytes[3] == 0x2f)))
                    {
                        startIndex       = 4;
                        info.BomLength   = 4;
                        info.ContainsBOM = true;
                        info.CodePage    = Encoding.UTF7.CodePage;
                    }
                    else if (bytes.Length > 1 && (bytes[0] == 0xff && bytes[1] == 0xfe))
                    {
                        startIndex       = 2;
                        info.BomLength   = 2;
                        info.ContainsBOM = true;
                        info.CodePage    = Encoding.Unicode.CodePage;
                    }
                    else if (bytes.Length > 1 && (bytes[0] == 0xfe && bytes[1] == 0xff))
                    {
                        startIndex       = 2;
                        info.BomLength   = 2;
                        info.ContainsBOM = true;
                        info.CodePage    = Encoding.BigEndianUnicode.CodePage;
                    }
                    else
                    {
                        if (!ContainsInvalidUTF8Bytes(bytes))
                        {
                            info.CodePage = Encoding.UTF8.CodePage;
                        }
                        else
                        {
                            info.CodePage = Encoding.Default.CodePage;
                        }
                    }
                    Int32 contentLength = bytes.Length - startIndex;
                    if (bytes.Length > 0 && bytes.Length > startIndex)
                    {
                        Encoding encoding = Encoding.GetEncoding(info.CodePage);
                        info.Contents = encoding.GetString(bytes, startIndex, contentLength);
                    }
                }
            }
            catch (Exception)
            {
                info = new EncodingFileInfo();
            }
            return(info);
        }
Exemple #5
0
        /// <summary>
        /// Checks if the file contains BOM
        /// </summary>
        public static Boolean ContainsBOM(String file)
        {
            EncodingFileInfo info = GetEncodingFileInfo(file);

            return(info.ContainsBOM);
        }
Exemple #6
0
        /// <summary>
        /// Reads the file codepage from the file data
        /// </summary>
        public static Int32 GetFileCodepage(String file)
        {
            EncodingFileInfo info = GetEncodingFileInfo(file);

            return(info.CodePage);
        }
Exemple #7
0
 /// <summary>
 /// Acquires encoding related info on one read.
 /// </summary>
 public static EncodingFileInfo GetEncodingFileInfo(String file)
 {
     Int32 startIndex = 0;
     EncodingFileInfo info = new EncodingFileInfo();
     try
     {
         if (File.Exists(file))
         {
             Byte[] bytes = File.ReadAllBytes(file);
             if (bytes.Length > 2 && (bytes[0] == 0xef && bytes[1] == 0xbb && bytes[2] == 0xbf))
             {
                 startIndex = 3;
                 info.BomLength = 3;
                 info.ContainsBOM = true;
                 info.Charset = Encoding.UTF8.WebName;
                 info.CodePage = Encoding.UTF8.CodePage;
             }
             else if (bytes.Length > 3 && (bytes[0] == 0xff && bytes[1] == 0xfe && bytes[2] == 0x00 && bytes[3] == 0x00))
             {
                 startIndex = 4;
                 info.BomLength = 4;
                 info.ContainsBOM = true;
                 info.Charset = Encoding.UTF32.WebName;
                 info.CodePage = Encoding.UTF32.CodePage;
             }
             else if (bytes.Length > 4 && ((bytes[0] == 0x2b && bytes[1] == 0x2f && bytes[2] == 0x76) && (bytes[3] == 0x38 || bytes[3] == 0x39 || bytes[3] == 0x2b || bytes[3] == 0x2f) && bytes[4] == 0x2D))
             {
                 startIndex = 5;
                 info.BomLength = 5;
                 info.ContainsBOM = true;
                 info.Charset = Encoding.UTF7.WebName;
                 info.CodePage = Encoding.UTF7.CodePage;
             }
             else if (bytes.Length > 3 && ((bytes[0] == 0x2b && bytes[1] == 0x2f && bytes[2] == 0x76) && (bytes[3] == 0x38 || bytes[3] == 0x39 || bytes[3] == 0x2b || bytes[3] == 0x2f)))
             {
                 startIndex = 4;
                 info.BomLength = 4;
                 info.ContainsBOM = true;
                 info.Charset = Encoding.UTF7.WebName;
                 info.CodePage = Encoding.UTF7.CodePage;
             }
             else if (bytes.Length > 1 && (bytes[0] == 0xff && bytes[1] == 0xfe))
             {
                 startIndex = 2;
                 info.BomLength = 2;
                 info.ContainsBOM = true;
                 info.Charset = Encoding.Unicode.WebName;
                 info.CodePage = Encoding.Unicode.CodePage;
             }
             else if (bytes.Length > 1 && (bytes[0] == 0xfe && bytes[1] == 0xff))
             {
                 startIndex = 2;
                 info.BomLength = 2;
                 info.ContainsBOM = true;
                 info.Charset = Encoding.BigEndianUnicode.WebName;
                 info.CodePage = Encoding.BigEndianUnicode.CodePage;
             }
             else
             {
                 if (!ContainsInvalidUTF8Bytes(bytes))
                 {
                     info.Charset = Encoding.UTF8.WebName;
                     info.CodePage = Encoding.UTF8.CodePage;
                 }
                 else // Try detecting using Ude...
                 {
                     Ude.CharsetDetector detector = new Ude.CharsetDetector();
                     detector.Feed(bytes, 0, bytes.Length); detector.DataEnd();
                     if (detector.Charset != null)
                     {
                         Encoding encoding = Encoding.GetEncoding(detector.Charset);
                         info.Charset = encoding.WebName;
                         info.CodePage = encoding.CodePage;
                     }
                     else
                     {
                         info.Charset = Encoding.Default.WebName;
                         info.CodePage = Encoding.Default.CodePage;
                     }
                 }
             }
             Int32 contentLength = bytes.Length - startIndex;
             if (bytes.Length > 0 && bytes.Length > startIndex)
             {
                 Encoding encoding = Encoding.GetEncoding(info.CodePage);
                 info.Contents = encoding.GetString(bytes, startIndex, contentLength);
             }
         }
     }
     catch (Exception)
     {
         info = new EncodingFileInfo();
     }
     return info;
 }