Ejemplo n.º 1
0
        private FileContents ReadFile(FullPathName fullName)
        {
            try {
                var       fileInfo          = new SlimFileInfo(fullName);
                const int trailingByteCount = 2;
                var       block             = NativeFile.ReadFileNulTerminated(fileInfo, trailingByteCount);
                var       contentsByteCount = (int)block.ByteLength - trailingByteCount; // Padding added by ReadFileNulTerminated
                var       kind = NativeMethods.Text_GetKind(block.Pointer, contentsByteCount);

                switch (kind)
                {
                case NativeMethods.TextKind.Ascii:
                    return(new AsciiFileContents(new FileContentsMemory(block, 0, contentsByteCount), fileInfo.LastWriteTimeUtc));

                case NativeMethods.TextKind.AsciiWithUtf8Bom:
                    const int utf8BomSize = 3;
                    return(new AsciiFileContents(new FileContentsMemory(block, utf8BomSize, contentsByteCount - utf8BomSize), fileInfo.LastWriteTimeUtc));

                case NativeMethods.TextKind.Utf8WithBom:
                    var utf16Block = Conversion.UTF8ToUnicode(block);
                    block.Dispose();
                    return(new UTF16FileContents(new FileContentsMemory(utf16Block, 0, utf16Block.ByteLength), fileInfo.LastWriteTimeUtc));

                case NativeMethods.TextKind.Unknown:
                default:
                    // TODO(rpaquay): Figure out a better way to detect encoding.
                    //Logger.Log("Text Encoding of file \"{0}\" is not recognized.", fullName);
                    return(new AsciiFileContents(new FileContentsMemory(block, 0, contentsByteCount), fileInfo.LastWriteTimeUtc));
                    //throw new NotImplementedException(string.Format("Text Encoding of file \"{0}\" is not recognized.", fullName));
                }
            }
            catch (Exception e) {
                Logger.LogException(e, "Error reading content of text file \"{0}\", skipping file.", fullName);
                return(StringFileContents.Empty);
            }
        }
Ejemplo n.º 2
0
 public SafeHeapBlockHandle ReadFileNulTerminated(IFileInfoSnapshot fileInfo, int trailingByteCount)
 {
     return(NativeFile.ReadFileNulTerminated(((FileInfoSnapshot)fileInfo).SlimFileInfo, trailingByteCount));
 }
Ejemplo n.º 3
0
 public SafeHeapBlockHandle ReadFileNulTerminated(FullPath path, long fileSize, int trailingByteCount)
 {
     return(NativeFile.ReadFileNulTerminated(path, fileSize, trailingByteCount));
 }