lok.lua 3.4 KB

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