// // KMFileAttribute.swift // PDF Reader Pro // // Created by tangchao on 2023/10/12. // import Cocoa @objc enum KMNFileAttributePageType: Int { case AllPages = 0 case OnlyOdd case OnlyEven case PagesString } @objcMembers class KMNFileAttribute: NSObject { var filePath: String = "" var pdfDocument: CPDFDocument? var bAllPage = false var pagesString: String = "" var isLocked = false var password: String? var pagesType: KMNFileAttributePageType = .AllPages private var selectPages: [Int] = [] func fetchSelectPages() -> [Int] { if let doc = self.pdfDocument, doc.isLocked { doc.unlock(withPassword: self.password) } guard let pdfDocument = pdfDocument else { return [] } selectPages.removeAll() if pagesType == .AllPages { for i in 0.. Bool { var document: CPDFDocument? if (pdfDocument != nil) { document = pdfDocument } else { document = CPDFDocument(url:URL(fileURLWithPath: self.filePath )) } if let data = document?.isLocked, data { document?.unlock(withPassword: self.password ) } var pageNumber = document?.pageCount ?? 0 if (self.bAllPage) { self.selectPages = [] for i in 1 ... pageNumber { self.selectPages.append(Int(i)) } return false } var pageNumbers: [Int] = [] var isInvalid = false for c in text { if (c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-") { isInvalid = true break }else{ isInvalid = false } } if (!isInvalid) { let array = text.components(separatedBy: ",") for s in array { if s.isEmpty { isInvalid = true break }else{ let pages = s.components(separatedBy: "-") if (pages.count>2) { isInvalid = true break }else if(pages.count==1){ let p = pages.first! if p.isEmpty || Int(p)! > pageNumber || Int(p) == 0 { isInvalid = true break }else{ var isEqual = false for pageNumber in pageNumbers { if pageNumber == Int(p) { isEqual = true isInvalid = true break } } if (!isEqual) { pageNumbers.append(Int(p)!) } } }else if(pages.count==2){ let p1 = pages[0] let p2 = pages[1] if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1) == 0 { isInvalid = true break }else{ var isEqual = false for i in Int(p1)! ... Int(p2)! { for pageNumber in pageNumbers { if pageNumber == i { isEqual = true isInvalid = true break } } } if (!isEqual) { for i in Int(p1)! ... Int(p2)! { pageNumbers.append(i) } } } } } } } if (text.isEmpty) { isInvalid = true } if (isInvalid) { self.selectPages = [] }else{ self.selectPages = pageNumbers } return isInvalid } func quickSort(_ list: inout [Int],startIndex: Int, endIndex: Int) { if(startIndex >= endIndex) { return } let temp = list[startIndex] var tempIndex = startIndex for i in startIndex+1 ... endIndex { let t = list[i] if (temp > t) { tempIndex = tempIndex + 1 let tmp = list[tempIndex] list[tempIndex] = list[i] list[i] = tmp } } let tmp = list[tempIndex] list[tempIndex] = list[startIndex] list[startIndex] = tmp quickSort(&list, startIndex: startIndex, endIndex: tempIndex-1) quickSort(&list, startIndex: tempIndex+1, endIndex: endIndex) } }