123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- //
- // KMFileAttribute.swift
- // PDF Master
- //
- // Created by tangchao on 2023/10/12.
- //
- import Cocoa
- @objcMembers class KMFileAttribute: NSObject {
- var filePath: String?
- var oriFilePath: String?
- var myPDFDocument: PDFDocument?
- var pdfDocument: CPDFDocument?
- var bAllPage = true
- private var _selectPages: [NSNumber] = []
- var pagesString: String = ""
- var isLocked = false
- var password: String?
- var pagesType: KMPageRange = .all
-
- /*
- @property (nonatomic, assign) BOOL pageRangeError;
- */
-
- func fetchSelectPages() -> [NSNumber] {
- let _ = self.isInvalidString(self.pagesString)
- if(!self.bAllPage) {
- self.QuickSort(&self._selectPages, startIndex: 0, endIndex: self._selectPages.count-1)
- }
- return self._selectPages
- }
-
- func isInvalidString(_ text: String) -> Bool {
- var document: PDFDocument?
- if (self.myPDFDocument != nil) {
- document = self.myPDFDocument
- } else {
- document = PDFDocument(url: URL(fileURLWithPath: self.filePath ?? ""))
- }
-
- if let data = document?.isLocked, data {
- document?.unlock(withPassword: self.password ?? "")
- }
- let pageNumber = document?.pageCount ?? 1
- if (self.bAllPage) {
- self._selectPages = []
- for i in 1 ... pageNumber {
- self._selectPages.append(NSNumber(value: i))
- }
- return false
- }
-
- var pageNumbers: [NSNumber] = []
- var isInvalid = false
- var c: unichar = 0
- 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.stringValue == p {
- isEqual = true
- isInvalid = true
- break
- }
- }
- if (!isEqual) {
- pageNumbers.append(NSNumber(value: 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.intValue == i {
- isEqual = true
- isInvalid = true
- break
- }
- }
- }
- if (!isEqual) {
- for i in Int(p1)! ... Int(p2)! {
- pageNumbers.append(NSNumber(value: i))
- }
- }
- }
- }
- }
- }
- }
- if (text.isEmpty) {
- isInvalid = true
- }
- if (isInvalid) {
- self._selectPages = []
- }else{
- self._selectPages = pageNumbers
- }
- return isInvalid
- }
-
- func QuickSort(_ list: inout [NSNumber],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.intValue > t.intValue) {
- 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
-
- self.QuickSort(&list, startIndex: startIndex, endIndex: tempIndex-1)
- self.QuickSort(&list, startIndex: tempIndex+1, endIndex: endIndex)
- }
-
- /*
- /* give our representation to the image browser */
- - (id)imageRepresentation
- {
- return self.filePath;
- }
- /* use the absolute filepath as identifier */
- - (NSString *)imageUID
- {
- return self.filePath;
- }
- - (NSString*)imageTitle
- {
- return [[self.filePath lastPathComponent] stringByDeletingPathExtension];
- }
- */
- }
|