Lua解析plist成Lua tabel
来源:互联网 发布:soho程序员兼职平台 编辑:程序博客网 时间:2024/06/05 15:59
local plistParser = {}
function plistParser.nextTag(s, i)
return string.find(s, "<(%/?)([%w:]+)(%/?)>", i)
end
function plistParser.array(s, i)
local arr, nextTag, array, dictionary = {}, plistParser.nextTag, plistParser.array, plistParser.dictionary
local ni, j, c, label, empty
while true do
ni, j, c, label, empty = nextTag(s, i)
assert(ni)
if c == "" then
if empty == "/" then
if label == "dict" or label == "array" then
arr[#arr+1] = {}
else
arr[#arr+1] = (label == "true") and true or false
end
elseif label == "array" then
arr[#arr+1], i, j = array(s, j+1)
elseif label == "dict" then
arr[#arr+1], i, j = dictionary(s, j+1)
else
i = j + 1
ni, j, c, label, empty = nextTag(s, i)
local val = string.sub(s, i, ni-1)
if label == "integer" or label == "real" then
arr[#arr+1] = tonumber(val)
else
arr[#arr+1] = val
end
end
elseif c == "/" then
assert(label == "array")
return arr, j+1, j
end
i = j + 1
end
end
function plistParser.dictionary(s, i)
local dict, nextTag, array, dictionary = {}, plistParser.nextTag, plistParser.array, plistParser.dictionary
local ni, j, c, label, empty
while true do
ni, j, c, label, empty = nextTag(s, i)
assert(ni)
if c == "" then
if label == "key" then
i = j + 1
ni, j, c, label, empty = nextTag(s, i)
assert(c == "/" and label == "key")
local key = string.sub(s, i, ni-1)
i = j + 1
ni, j, c, label, empty = nextTag(s, i)
if empty == "/" then
if label == "dict" or label == "array" then
dict[key] = {}
else
dict[key] = (label == "true") and true or false
end
else
if label == "dict" then
local dictData = nil;
dictData, i, j = dictionary(s, j+1)
local position = string.find(key,"New item");
if position then
table.insert(dict,dictData);
dump(dictData);
else
dict[key] = dictData;
end
elseif label == "array" then
dict[key], i, j = array(s, j+1)
else
i = j + 1
ni, j, c, label, empty = nextTag(s, i)
local val = string.sub(s, i, ni-1)
if label == "integer" or label == "real" then
dict[key] = tonumber(val)
else
local position = string.find(key,"New item");
if position then
table.insert(dict,val);
else
dict[key] = val
end
end
end
end
end
elseif c == "/" then
assert(label == "dict")
return dict, j+1, j
end
i = j + 1
end
end
function plistParse(s)
local i, ni, tag, version, empty = 0
local label = 0;
while label ~= "plist" do
ni, i, label, version = string.find(s, "<([%w:]+)(.-)>", i+1)
assert(ni)
end
ni, i, _, label, empty = plistParser.nextTag(s, i)
if empty == "/" then
return {}
elseif label == "dict" then
return plistParser.dictionary(s, i+1)
elseif label == "array" then
return plistParser.array(s, i+1)
end
end
function plistParser.nextTag(s, i)
return string.find(s, "<(%/?)([%w:]+)(%/?)>", i)
end
function plistParser.array(s, i)
local arr, nextTag, array, dictionary = {}, plistParser.nextTag, plistParser.array, plistParser.dictionary
local ni, j, c, label, empty
while true do
ni, j, c, label, empty = nextTag(s, i)
assert(ni)
if c == "" then
if empty == "/" then
if label == "dict" or label == "array" then
arr[#arr+1] = {}
else
arr[#arr+1] = (label == "true") and true or false
end
elseif label == "array" then
arr[#arr+1], i, j = array(s, j+1)
elseif label == "dict" then
arr[#arr+1], i, j = dictionary(s, j+1)
else
i = j + 1
ni, j, c, label, empty = nextTag(s, i)
local val = string.sub(s, i, ni-1)
if label == "integer" or label == "real" then
arr[#arr+1] = tonumber(val)
else
arr[#arr+1] = val
end
end
elseif c == "/" then
assert(label == "array")
return arr, j+1, j
end
i = j + 1
end
end
function plistParser.dictionary(s, i)
local dict, nextTag, array, dictionary = {}, plistParser.nextTag, plistParser.array, plistParser.dictionary
local ni, j, c, label, empty
while true do
ni, j, c, label, empty = nextTag(s, i)
assert(ni)
if c == "" then
if label == "key" then
i = j + 1
ni, j, c, label, empty = nextTag(s, i)
assert(c == "/" and label == "key")
local key = string.sub(s, i, ni-1)
i = j + 1
ni, j, c, label, empty = nextTag(s, i)
if empty == "/" then
if label == "dict" or label == "array" then
dict[key] = {}
else
dict[key] = (label == "true") and true or false
end
else
if label == "dict" then
local dictData = nil;
dictData, i, j = dictionary(s, j+1)
local position = string.find(key,"New item");
if position then
table.insert(dict,dictData);
dump(dictData);
else
dict[key] = dictData;
end
elseif label == "array" then
dict[key], i, j = array(s, j+1)
else
i = j + 1
ni, j, c, label, empty = nextTag(s, i)
local val = string.sub(s, i, ni-1)
if label == "integer" or label == "real" then
dict[key] = tonumber(val)
else
local position = string.find(key,"New item");
if position then
table.insert(dict,val);
else
dict[key] = val
end
end
end
end
end
elseif c == "/" then
assert(label == "dict")
return dict, j+1, j
end
i = j + 1
end
end
function plistParse(s)
local i, ni, tag, version, empty = 0
local label = 0;
while label ~= "plist" do
ni, i, label, version = string.find(s, "<([%w:]+)(.-)>", i+1)
assert(ni)
end
ni, i, _, label, empty = plistParser.nextTag(s, i)
if empty == "/" then
return {}
elseif label == "dict" then
return plistParser.dictionary(s, i+1)
elseif label == "array" then
return plistParser.array(s, i+1)
end
end
0 0
- Lua解析plist成Lua tabel
- Lua的tabel,,查找字符串和值
- lua之读写plist文件
- cocos-Lua加载plist文件
- cocos2dx lua解析csv成lua配置文件,方便程序调用
- Lua笔记:加载plist图片资源文件
- Lua笔记:应用plist图片集中的图片
- 【cocos2d-x3.5】Lua Plist与SpriteFrameCache
- cocos lua 对plist文件的操作
- LUA
- Lua
- LUA
- lua
- Lua
- lua
- lua
- lua
- lua
- Java冒泡排序
- FunctionMapper异常
- Python3.2官方文档---while关键字
- 第三周作业
- 第三周作业——冒泡排序和归并排序
- Lua解析plist成Lua tabel
- Android应用开发中对Bitmap的内存优化
- tortoisegit 设置密码
- Android内存管理试验——浅谈ImageView的Bitmap的使用
- Oralce 调用Delphi写DLL去访问C# WebService的问题
- c#封装jmail的pop3收邮件
- 改进的C#插件构架
- 黑马程序员(八) 实现Aop功能的封装和配置的小程序
- C#中生成中文繁体web页面