Beispiel #1
0
        public void TestReadAllLines()
        {
            string filename = Util.CreateNewFile(longPathDirectory);

            try
            {
                using (var sw = File.AppendText(filename))
                {
                    sw.WriteLine("end of file");
                }
                var lines = File.ReadAllLines(filename);
                Assert.IsTrue(new[] { "beginning of file", "end of file" }.SequenceEqual(lines));
            }
            finally
            {
                File.Delete(filename);
            }
        }
        /*
         * public static async Task<IObject> ObjectFromPath(this IDirectory dir, string fullname)
         * {
         *  while (!dir.IsRoot)
         *  {
         *      dir = dir.Parent;
         *  }
         *  string[] parts = fullname.Split('\\');
         *  int start = 0;
         *  bool repeat;
         *  do
         *  {
         *      repeat = false;
         *      if (!dir.IsPopulated)
         *          await dir.PopulateAsync();
         *      foreach (IDirectory d in dir.Directories)
         *      {
         *          if (d.Name.Equals(parts[start], StringComparison.InvariantCultureIgnoreCase))
         *          {
         *              if (start == parts.Length - 1)
         *                  return d;
         *              repeat = true;
         *              start++;
         *              dir = d;
         *              break;
         *          }
         *      }
         *      if ((!repeat) && (start == parts.Length-1))
         *      {
         *          foreach (IFile d in dir.Files)
         *          {
         *              if (d.Name.Equals(parts[start], StringComparison.InvariantCultureIgnoreCase))
         *              {
         *                  return d;
         *              }
         *          }
         *      }
         *  } while (repeat);
         *  return null;
         * }
         */
        public static string HashFromExtendedFile(string file, string type = "md5")
        {
            if (File.Exists(file + "." + type))
            {
                string[] lines = File.ReadAllLines(file + "." + type);
                foreach (string s in lines)
                {
                    if ((s.Length >= 32) && (s[0] != '#'))
                    {
                        return(s.Substring(0, 32));
                    }
                }
            }
            FileInfo f   = new FileInfo(file);
            string   dir = string.Empty;

            if (f.Directory != null)
            {
                dir = f.Directory.Name;
            }
            string bas = Path.Combine(Path.GetDirectoryName(file) ?? string.Empty, dir);

            if (File.Exists(bas + ".md5"))
            {
                string[] lines = File.ReadAllLines(bas + "." + type);
                foreach (string s in lines)
                {
                    if ((s.Length >= 35) && (s[0] != '#'))
                    {
                        string hash  = s.Substring(0, 32);
                        string fname = s.Substring(32).Replace("*", string.Empty).Trim();
                        if (string.Equals(f.Name, fname, StringComparison.InvariantCultureIgnoreCase))
                        {
                            return(hash);
                        }
                    }
                }
            }
            if (File.Exists(bas + ""))
            {
                string[] lines = File.ReadAllLines(bas + "");
                bool     hash  = false;
                for (int x = 0; x < lines.Length; x++)
                {
                    string s = lines[x];
                    if ((s.Length > 5) && (s.StartsWith("#" + type + "#")))
                    {
                        hash = true;
                    }
                    else if ((s.Length >= 35) && (s[0] != '#') && hash)
                    {
                        string md    = s.Substring(0, 32);
                        string fname = s.Substring(32).Replace("*", string.Empty).Trim();
                        if (string.Equals(f.Name, fname, StringComparison.InvariantCultureIgnoreCase))
                        {
                            return(md);
                        }
                        hash = false;
                    }
                    else
                    {
                        hash = false;
                    }
                }
            }
            return(string.Empty);
        }
Beispiel #3
0
        public List <Stream> GetStreams(string filename)
        {
            string ext = Path.GetExtension(filename);

            if (string.IsNullOrEmpty(ext))
            {
                return(null);
            }
            ext = ext.Replace(".", string.Empty).ToLower();
            string name = Path.GetFileNameWithoutExtension(filename);

            if (string.IsNullOrEmpty(name))
            {
                return(null);
            }
            Regex           lm       = new Regex(".+\\.([^\\.]+)$", RegexOptions.Singleline);
            MatchCollection col      = lm.Matches(name);
            string          language = "xx";

            foreach (Match m in col)
            {
                if (m.Success)
                {
                    string val = m.Groups[1].Value.ToLower();
                    if (SubtitleHelper.Iso639_3_TO_Iso639_1.ContainsKey(val))
                    {
                        language = SubtitleHelper.Iso639_3_TO_Iso639_1[val];
                        break;
                    }
                    if (SubtitleHelper.Iso639_1_TO_Languages.ContainsKey(val))
                    {
                        language = val;
                        break;
                    }
                    if (SubtitleHelper.Languages_TO_ISO639_1_Lower.ContainsKey(val))
                    {
                        language = SubtitleHelper.Languages_TO_ISO639_1_Lower[val];
                        break;
                    }
                }
            }
            string format = null;

            if ((ext == "txt") || (ext == "sub"))
            {
                string[] lines     = File.ReadAllLines(filename);
                string   firstline = null;
                foreach (string ws in lines)
                {
                    string k = ws.Trim();
                    if (!string.IsNullOrEmpty(k))
                    {
                        firstline = k;
                        break;
                    }
                }
                if (firstline != null)
                {
                    lm = new Regex("^\\{[0-9]+\\}\\{[0-9]*\\}", RegexOptions.Singleline);
                    Match m = lm.Match(firstline);
                    if (m.Success)
                    {
                        format = "microdvd";
                    }
                    else
                    {
                        lm = new Regex("^[0-9]{1,2}:[0-9]{2}:[0-9]{2}[:=,]", RegexOptions.Singleline);
                        m  = lm.Match(firstline);
                        if (m.Success)
                        {
                            format = "txt";
                        }
                        else
                        {
                            if (firstline.Contains("[SUBTITLE]"))
                            {
                                format = "subviewer";
                            }
                        }
                    }
                }
            }
            ext = ext.Replace("ass", "ssa");
            if (format == null)
            {
                format = ext;
            }
            Stream s = new Stream();

            s.Format       = format;
            s.StreamType   = "3";
            s.File         = filename;
            s.LanguageCode = SubtitleHelper.Iso639_1_TO_Iso639_3[language];
            s.Language     = SubtitleHelper.Iso639_1_TO_Languages[language];
            List <Stream> sts = new List <Stream>();

            sts.Add(s);
            return(sts);
        }
Beispiel #4
0
 public static string[] ReadAllLines(string path, Encoding encoding)
 {
     return(File.ReadAllLines(path, encoding));
 }
Beispiel #5
0
 public static string[] ReadAllLines(string path)
 {
     return(File.ReadAllLines(path));
 }