comparison mupdf-source/thirdparty/leptonica/lok.lua @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 --[[
2 Modify Leptonica's *.c file functions returning
3 "0 if OK, 1 on error" to use the l_ok typedef.
4 --]]
5
6 debug = false
7
8 ---
9 -- copy a file src to dst
10 -- \param src source filename
11 -- \param dst destination filename
12 -- \param blocksize block size for copying (default 1M)
13 -- \return true on success; nil,error on error
14 function copyfile(src, dst, blocksize)
15 blocksize = blocksize or 1024*1024
16 local fs, fd, err
17 -- return after closing the file descriptors
18 local function ret(...)
19 if fs then fs:close() end
20 if fd then fd:close() end
21 return ...
22 end
23 fs, err = io.open(src)
24 if not fs then return ret(nil, err) end
25 fd, err = io.open(dst, "wb")
26 if not fd then return ret(nil, err) end
27 while true do
28 local ok, data
29 data = fs:read(blocksize)
30 if not data then break end
31 ok, err = fd:write(data)
32 if not ok then return ret(nil, err) end
33 end
34 return ret(true)
35 end
36
37 ---
38 -- List the array table (t) contents to fd.
39 -- \param fd io file descriptor
40 -- \param t array table to list
41 function list(fd, t)
42 if t ~= nil then
43 for _,l in ipairs(t) do
44 fd:write(l .. '\n')
45 end
46 end
47 end
48
49 ---
50 --- Modify the file fs and write the result to fd.
51 -- \param fs source file stream
52 -- \param fd destination file stream
53 -- \return true on success
54 function modify(fs, fd)
55 local state = 0 -- parsing state
56 local to_l_ok = false -- true, if the l_int32 return type should be changed
57 local b_file = false -- true, have seen a \file comment
58
59 while true do
60 line = fs:read()
61 if line == nil then
62 -- end of file
63 break
64 end
65
66 if line:match('^/%*[!*]$') then
67 -- start of Doxygen comment
68 -- introduces a new function
69 -- go to state 1 (inside doxygen comment)
70 state = 1
71 end
72
73 if state == 3 then
74 -- 2nd line after a comment
75 -- contains the name of the function
76 -- go to state 4 (skip until end of function)
77 state = 4
78 end
79
80 if state == 2 then
81 -- 1st line after a comment
82 -- contains the return type
83 if to_l_ok and line == 'l_int32' then
84 line = 'l_ok'
85 end
86 if b_file then
87 -- back to state 0 (look for doxygen comment)
88 state = 0
89 to_l_ok = false
90 b_file = false
91 else
92 -- go to state 3 (2nd line after doxygen comment)
93 state = 3
94 end
95 end
96
97 if line == ' */' then
98 -- end of Doxygen comment
99 -- go to state 2 (1st line after doxygen comment)
100 state = 2
101 end
102
103 if state == 1 then
104 -- inside doxygen comment
105 if line:match("%\\return%s+0 if OK") then
106 -- magic words that indicate l_ok return type
107 to_l_ok = true
108 end
109 if line:match("%\\file%s+") then
110 -- this is a file comment
111 b_file = true
112 end
113 end
114
115 if debug then
116 if to_l_ok then
117 print("[" .. state .. ";1] " .. line)
118 else
119 print("[" .. state .. ";0] " .. line)
120 end
121 end
122
123 if state == 4 and line == '}' then
124 -- end of a function
125 -- print("name='" .. name .. "'")
126 -- back to state 0 (look for doxygen comment)
127 state = 0
128 to_l_ok = false
129 b_file = false
130 end
131 fd:write(line .. '\n')
132 end
133 return true
134 end
135
136 ---
137 -- Main function.
138 -- \param arg table array of command line parameters
139
140 script = arg[0] or ""
141 if #arg < 1 then
142 print("Usage: lua " .. script .." src/*.c")
143 end
144
145 for i = 1, #arg do
146 local input = arg[i]
147 local backup = input .. "~"
148 local ok, err = copyfile(input, backup)
149 if not ok then
150 print("Error copying " .. input .." to " .. backup .. ": " ..err)
151 end
152 local fs = io.open(backup)
153 local fd = io.open(input, "wb")
154 modify(fs, fd)
155 fd:close()
156 fs:close()
157 end