VE Lua Documentation

Press F to search!

readFiles

Definition


-- @/lua/common/utils.lua:905

-- reads multiple files and returns a table of contents
function readFiles(filenames, maxBatchSize)
  maxBatchSize = maxBatchSize or 100
  local contents, failed, batches = {}, {}, {}


  -- split into batches
  for i = 1, #filenames, maxBatchSize do
    table.insert(batches, {from = i, to = math.min(i + maxBatchSize - 1, #filenames)})
  end

  log("D","","Processing "..#filenames.." files in "..#batches.." batches. Max batch size: "..maxBatchSize)
  -- process each batch: open all, then read and close all
  for _, batch in ipairs(batches) do
    profilerPushEvent("readFiles batch")
    local batchFiles = {}
    -- open all files in batch
    profilerPushEvent("readFiles open batch")
    for i = batch.from, batch.to do
      local filename = filenames[i]
      profilerPushEvent("readFiles open file "..filename)
      local f = io.open(filename, "r")
      profilerPopEvent("readFiles open file "..filename)
      batchFiles[filename] = f
      if f == nil then
        failed[filename] = true
      end
    end
    profilerPopEvent("readFiles open batch")
    -- read and close all files in batch
    profilerPushEvent("readFiles readClose batch")
    for filename, f in pairs(batchFiles) do
      if f ~= nil then
        profilerPushEvent("readFiles readClose file "..filename)
        contents[filename] = f:read("*all")
        f:close()
        profilerPopEvent("readFiles readClose file "..filename)
      end
    end
    profilerPopEvent("readFiles readClose batch")
    profilerPopEvent("readFiles batch")
  end
  return contents, failed
end

Callers

@/lua/ge/extensions/core/vehicles.lua

    local contents, failed = readFiles(filesAll, 100)