public TBook(string name, TParsingParameters parsingParameters, EBookType bookType = EBookType.folder, string relativePath = "") : this() { if (bookType == EBookType.unknown) { BookType = _FindBookType(name); } else { BookType = bookType; } string ProcessedName = BookType.IsFileType() ? name.BeforeLast(".") : name; if (!_NewParse(ProcessedName, parsingParameters)) { if (!_Parse(ProcessedName)) { Name = ProcessedName; } } if (!CollectionNameComponents.Any()) { int i = 0; foreach (string PathItem in relativePath.Split('\\')) { CollectionNameComponents.Add(i++, PathItem); } } RelativePath = relativePath; }
public TParsingParameters(TParsingParameters parameters) { _TitleStartDelimiter = parameters.TitleStartDelimiter; _TitleEndDelimiter = parameters.TitleEndDelimiter; _NumberStartDelimiter = parameters.NumberStartDelimiter; _NumberEndDelimiter = parameters.NumberEndDelimiter; _CollectionNameStartDelimiter = parameters.CollectionNameStartDelimiter; _CollectionNameEndDelimiter = parameters.CollectionNameEndDelimiter; _CollectionNamesOrder = parameters.CollectionNamesOrder; }
private bool _NewParse(string processedName, TParsingParameters parsingParameters) { if (string.IsNullOrWhiteSpace(processedName)) { return(false); } Regex _GetNumber = new Regex($@"(?<number>{Regex.Escape(parsingParameters.NumberStartDelimiter)}.*?{Regex.Escape(parsingParameters.NumberEndDelimiter)})"); Regex _GetName = new Regex($@"(?<title>{Regex.Escape(parsingParameters.TitleStartDelimiter)}.*?{Regex.Escape(parsingParameters.TitleEndDelimiter)})"); Regex _GetCollectionNames = new Regex($@"(?<coll>{Regex.Escape(parsingParameters.CollectionNameStartDelimiter)}.*?{Regex.Escape(parsingParameters.CollectionNameEndDelimiter)})"); Match MatchNumber = _GetNumber.Match(processedName); Match MatchName = _GetName.Match(processedName); MatchCollection MatchCollectionNames = _GetCollectionNames.Matches(processedName); if (!MatchNumber.Success && !MatchName.Success && MatchCollectionNames.Count == 0) { return(false); } if (MatchNumber.Success) { Number = MatchNumber.Groups["number"].Value.After(parsingParameters.NumberStartDelimiter).BeforeLast(parsingParameters.NumberEndDelimiter).Trim(); } if (MatchName.Success) { Name = MatchName.Groups["title"].Value.After(parsingParameters.TitleStartDelimiter).BeforeLast(parsingParameters.TitleEndDelimiter).Trim(); } //} else { // Name = "(missing)"; //} int i; switch (parsingParameters.CollectionNamesOrder) { case TParsingParameters.ECollectionNameOrder.Normal: i = 0; break; case TParsingParameters.ECollectionNameOrder.Reverse: i = int.MaxValue; break; default: i = 0; break; } foreach (Match MatchCollectionNameItem in MatchCollectionNames) { string TextItem = MatchCollectionNameItem.Value.After(parsingParameters.CollectionNameStartDelimiter).BeforeLast(parsingParameters.CollectionNameEndDelimiter).Trim(); Match MatchOrder = GetOrder.Match(TextItem); if (MatchOrder.Success) { CollectionNameComponents.Add((byte)(MatchOrder.Groups["order"].Value.After('(').First()), TextItem.Substring(3)); } else { CollectionNameComponents.Add(i, TextItem); switch (parsingParameters.CollectionNamesOrder) { case TParsingParameters.ECollectionNameOrder.Normal: i++; break; case TParsingParameters.ECollectionNameOrder.Reverse: i--; break; } } } return(true); }
private void _ImportFolder(string folder, TParsingParameters parsingParameters) { #region Validate parameters if (string.IsNullOrWhiteSpace(folder)) { Trace.WriteLine("Unable to import empty folder"); return; } string FolderFullPath = Path.GetFullPath(folder); if (!Directory.Exists(FolderFullPath)) { Trace.WriteLine($"Unable to access directory \"{folder}\" : directory is missing or access is denied."); return; } #endregion Validate parameters TParsingParameters LocalParsingParameters = new TParsingParameters(parsingParameters); string BdIndexParameterFilename = Path.Combine(folder, "bdindex.json"); if (File.Exists(BdIndexParameterFilename)) { LocalParsingParameters.Load(BdIndexParameterFilename); } DirectoryInfo FolderInfo = new DirectoryInfo(FolderFullPath); IEnumerable <DirectoryInfo> InnerDirectories = FolderInfo.EnumerateDirectories(); IEnumerable <FileInfo> InnerFiles = FolderInfo.EnumerateFiles() .Where(f => TBook.Extensions.Contains(f.Extension.ToLower())); #region Folder as book or folder as group foreach (DirectoryInfo DirectoryItem in InnerDirectories) { IEnumerable <FileInfo> PageFiles = DirectoryItem.EnumerateFiles() .Where(f => TPage.Extensions.Contains(f.Extension.ToLower())); if (PageFiles.Any()) { #region Found a book as a folder TBook NewBook = new TBook(DirectoryItem.Name, LocalParsingParameters, EBookType.folder, DirectoryItem.FullName.After(RootPath)); //Trace.WriteLine(NewBook.ToString()); Books.Add(NewBook); #endregion Found a book as a folder } else { #region Dig deeper into directories _ImportFolder(DirectoryItem.FullName, LocalParsingParameters); #endregion Dig deeper into directories } } #endregion Folder as book or folder as group #region Archived files as books if (InnerFiles.Any()) { foreach (FileInfo FileItem in InnerFiles) { TBook NewBook = new TBook(FileItem.Name, LocalParsingParameters, EBookType.unknown, FileItem.FullName.After(RootPath).BeforeLast(@"\")); Books.Add(NewBook); } } #endregion Archived files as books }