-
Notifications
You must be signed in to change notification settings - Fork 2
/
modMain.twin
155 lines (145 loc) · 5.28 KB
/
modMain.twin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Module modMain
'gPdfMerge v1.2.15
'Last update: 27 Jun 2024
'by Jon Johnson (fafalone)
'See full comments in modPdfium.twin
'Startup, command line handling, and utility function
' Command line usage:
' Merge: gPdfMerge.exe /i "C:\...\Input1.pdf" "C:\...\Input2.pdf" /o "C:\path\Output.pdf"
' Append: gPdfMerge.exe /i "C:\...\Input1.pdf" "C:\...\Input2.pdf"
' Append or merge with ranges and/or insert idx not support via command line in v1.0
Public gUI As Boolean
Public Sub AppendLog(smsg As String)
If gUI Then
Form1.AddLog(smsg)
Else
Debug.Print smsg
End If
End Sub
Sub Main()
If Command$() = "" Then
gUI = True
Form1.Show
Else
'On Error GoTo e0
Dim sFilesIn() As String, sFileOut As String
Dim sCmd As String = Command$()
sCmd = Trim$(sCmd)
If (Left$(sCmd, 2) <> "/i") And (Left$(sCmd, 2) <> "/o") Then
MsgBox "Invalid command.", vbCritical + vbOKOnly, App.Title
Exit Sub
End If
Dim ptOut As Long = InStr(sCmd, "/o " & ChrW$(34))
Dim sTmp As String
If ptOut = 0 Then 'Append
If (Left$(sCmd, 2) <> "/i") Then
MsgBox "Invalid command.", vbCritical + vbOKOnly, App.Title
Exit Sub
End If
sTmp = Mid$(sCmd, 3)
ParseQuotedList sTmp, sFilesIn
Else 'Merge
sFileOut = ParseOutput(sCmd, ptOut)
If (Left$(sCmd, 2) <> "/i") Then
MsgBox "Invalid command.", vbCritical + vbOKOnly, App.Title
Exit Sub
End If
sTmp = Mid$(sCmd, 3)
ParseQuotedList sTmp, sFilesIn
End If
If UBound(sFilesIn) < 1 Then
MsgBox "Invalid command: Could not find two or more input files.", vbCritical + vbOKOnly, App.Title
Exit Sub
End If
If FilesToTPDF(sFilesIn, sFileOut) > 2 Then
Dim hr As Long
If sFileOut <> "" Then
hr = MergePdfs(sFileOut, sList)
Else
hr = AppendPdfs(sList)
End If
' Dim i As Long
' Debug.Print "Out=" & sFileOut
' For i = 0 To UBound(sList)
' Debug.Print "In(" & CStr(i) & ")=" & sList(i).sName & "::" & sList(i).sFullPath
' Next
Else
MsgBox "Invalid command: Could not find two or more input files.", vbCritical + vbOKOnly, App.Title
Exit Sub
End If
End If
Exit Sub
e0:
Debug.Print "Failed to parse command line " & Command$()
End Sub
Private Function FilesToTPDF(sIn() As String, sOut As String) As Long
Dim i As Long
Dim j As Long
For i = 0 To UBound(sIn)
' If PathFileExists(sIn(i)) Then
ReDim Preserve sList(j)
sList(j).sFullPath = sIn(i)
sList(j).sName = Right$(sIn(i), Len(sIn(i)) - InStrRev(sIn(i), "\"))
j += 1
' End If
Next
Return j
End Function
Private Function ParseOutput(sCmd As String, pt As Long) As String
'Extract file from /o "file" and remove it
On Error GoTo e0
Dim i As Long
Dim sTmp As String
sTmp = Mid$(sCmd, pt + 4)
Debug.Print "PraseOutput sTmp1" & sTmp
sTmp = Left$(sTmp, InStr(sTmp, ChrW$(34)) - 1)
Debug.Print "PraseOutput sTmp2" & sTmp
sCmd = Replace$(sCmd, "/o " & ChrW$(34) & sTmp & ChrW$(34), "")
Debug.Print "PraseOutput " & sCmd & " :::: " & sTmp
Return sTmp
e0:
Debug.Print "Failed to parse output path::sCmd=" & sCmd
End Function
Public Sub ParseQuotedList(sIn As String, sOut() As String, Optional chrStop As String)
'Takes a string full of quoted items and converts them into an array
'chrStop terminates the search if found outside a quote
Debug.Print "PQL In->" & sIn
Dim i As Long, j As Long
Dim cch As String
Dim sHold As String
Dim iq As Boolean
ReDim sOut(0)
If InStr(sIn, Chr$(34)) Then
For i = 1 To Len(sIn)
cch = Mid$(sIn, i, 1)
If (chrStop = cch) And (iq = False) Then Exit Sub
If cch = Chr$(34) Then
If iq = True Then
'closing quote, add item
ReDim Preserve sOut(j)
sOut(j) = sHold
Debug.Print "PQL add " & sOut(j)
j = j + 1
sHold = ""
iq = False
Else
iq = True
End If
Else
If iq = True Then
sHold = sHold & cch
End If
End If
Next i
Else
'single item
ReDim sOut(0)
sOut(0) = sIn
If chrStop <> "" Then
If InStr(sIn, chrStop) Then
sOut(0) = Left$(sOut(0), InStr(sOut(0), chrStop) - 2)
End If
End If
End If
End Sub
End Module