自动生成lua格式配置文件

安卓上找不到路径这个问题暂时用sd卡的根目录好啦。这里已经修复并测试通过
但这个测试工程是安装在有sd卡的手机的sd卡上的
或者如阳光七月所说
将Lua的源代码用Quick提供的工具打包,用加载包的方式进行代码的加载工作,就不会出现问题了。
如果不愿意打包,也可以自己修改executeScriptFile接口,先自己载入文件数据,用luaL_dostring接口来调用。这样也行
------------dataSave.lua--------------
local function readInFiles(filePath, str, sType)
local result = {}
– 把字符串写入文件
local function writeFile(fileName, strVal)
if device.platform == “android” then
local tab = string.split(fileName, “/”)
local tmpPath = “/mnt/sdcard/”
fileName = tmpPath … tab#tab]
end
local pFile,err = assert(io.open(fileName, sType))
if err then
return false
else
pFile:write(strVal)
pFile:close()
return true
end
end
return writeFile(filePath, str)
end

–打印输出信息
function showLog( … )
local traceback = string.split(debug.traceback("", 2), “\n”)
local str = string.split(string.trim(traceback), “/”)
print(str#str] … “\n”, …)
end

– 格式化table表
local function forMatTable(object, nesting)
if type(nesting) ~= “number” then nesting = 99 end
local lookupTable = {}
local result = {}
local function _v(v)
if type(v) == “string” then
v = “”" … v … “”"
end
return tostring(v)
end
local function _forMat(object, label, indent, nest, keylen)
spc = “”
if type(keylen) == “number” then
spc = string.rep(" “, keylen - string.len(_v(label)))
end
if type(object) ~= “table” then
if label then
– 格式化每一行数据
result#result +1 ] = string.format(”%s%s]%s = %s,", indent, _v(label), spc, _v(object))
else
result#result +1 ] = string.format("%s,", _v(object))
end
elseif lookupTable then
– result#result +1 ] = string.format("%s%s%s = REF", indent, label, spc)
else
lookupTable = true
if nest > nesting then
– result#result +1 ] = string.format("%s%s = MAX NESTING", indent, label)
else
if label then
– 格式化每一行数据
result#result +1 ] = string.format("%s%s] = {", indent, _v(label))
else
result#result +1 ] = “{”
end
local indent2 = indent…" "
local keys = {}
local keylen = 0
local values = {}
for k, v in pairs(object) do
keys#keys + 1] = k
local vk = _v(k)
local vkl = string.len(vk)
if vkl > keylen then keylen = vkl end
values = v
end
table.sort(keys, function(a, b)
if type(a) == “number” and type(b) == “number” then
return a < b
else
return tostring(a) < tostring(b)
end
end)
for i, k in ipairs(keys) do
_forMat(values, k, indent2, nest + 1, keylen)
end
result#result +1] = string.format("%s},", indent)
end
end
end
_forMat(object, nil, “”, 1)
local tb = table.concat(result, “\n”)
return string.sub(tb,1,string.len(tb)-1)
end

function varForMat(object)
local lookupTable = {}
local result = {}
local function _v(v)
if type(v) == “string” then
v = “”" … v … “”"
end
return tostring(v)
end
local function _vardump(object, label, indent, nest)
local postfix = “”
if nest > 1 then postfix = “,” end
if type(object) ~= “table” then
if type(label) == “string” then
result#result +1] = string.format("%s%s = %s%s", indent, label, _v(object), postfix)
else
result#result +1] = string.format("%s%s%s", indent, _v(object), postfix)
end
elseif not lookupTable then
lookupTable = true
if label then
if type(label) == “string” then
result#result +1 ] = string.format("%s%s = {", indent, label)
else
result#result +1 ] = string.format("%s{", indent)
end
else
result#result +1 ] = string.format("%s{", indent)
end
local indent2 = indent … " "
local keys = {}
local values = {}
for k, v in pairs(object) do
keys#keys + 1] = k
values = v
end
table.sort(keys, function(a, b)
if type(a) == “number” and type(b) == “number” then
return a < b
else
return tostring(a) < tostring(b)
end
end)
for i, k in ipairs(keys) do
_vardump(values, k, indent2, nest + 1)
end
result#result +1] = string.format("%s}%s", indent, postfix)
end
end
_vardump(object, nil, “”, 1)
return table.concat(result, “\n”)
end

– 获取文件路径
local function getDataTablePath(fileName)
fileName = tostring(fileName)
local path = CCFileUtils:sharedFileUtils():getWritablePath()
if device.platform == “android” then
path = “/mnt/sdcard/”
end
local filePath = path … fileName … “.lua”
return filePath
end

– 判断文件是否存在
local function isDataTableExist(fileName)
local filePath = getDataTablePath(fileName)
return CCFileUtils:sharedFileUtils():isFileExist(filePath)
end


以下是需要暴露出去的接口
–]]
– 保存数据
function saveDataTableToFile(tab, fileName)
local fStr =

local table = %s

return table
]]
sType = sType or ‘w’
local filePath = getDataTablePath(fileName)
tab = forMatTable(tab)
–tab = varForMat(tab)
local str = string.format(fStr, tab)
if readInFiles(filePath, str, sType) then
–showLog(“数据保存到 “,filePath,” 操作成功”)
end
end

– 读取数据
function getDataTableFromFile(fileName)
local filePath = getDataTablePath(fileName)
if isDataTableExist(fileName) then
if device.platform == “android” then
local tmpPath = “/mnt/sdcard/”
fileName = tmpPath … fileName … “.lua”
end
local info = require(fileName)
return info
else
showLog(“读取失败,未找到: “,filePath,” 该文件”)
return {}
end
end

– 删除数据
function deleteDataTable(fileName)
local filePath = getDataTablePath(fileName)
if isDataTableExist(fileName) then
os.remove(filePath)
else
showLog(“删除失败,未找到: “,filePath,” 该文件”)
end
end

– 测试
– local t = {{x=2, b=“right”}, {2, 6,8}, {name = “哈哈”, sex = “女”}}
– 保存数据
– saveDataTableToFile(t, “tip”)
– 读取数据
– local a = getDataFromFile(“tip”)
– print(a.b)

抱歉,还是不行,之前的在win8平台上工作正常,改了之后不行,参见截图,在android上也不行哦

接楼上,win8平台测试结果

android平台测试结果

了解了 回头我再测过 没问题了再更新下

估计是require问题,参见http://www.cocoachina.com/bbs/read.php?tid=210335&fpage=2&page=1
5楼作者

喔喔 学习了 确实如此 互相进步 非常感谢

做了一个不太完美的修正 你试试吧

嗯,确实可以用,我们能不能做到存储到
/data/data/com.xxoo.xxoo/files/下面呢?

local tmpPath = "/mnt/sdcard/data/data/com.xxoo.xxoo/files/ 然后你要先判断下该路径存在 是否需要创建该路径所需文件夹 文件操作就好啦