* updated libkcddb (21.12.1 -> 21.12.2), untested
[t2-trunk.git] / misc / lua / sde / desc.lua
blob9793901d94c6e51dc39fe31f86042bad970fe3fb
1 -- --- T2-COPYRIGHT-NOTE-BEGIN ---
2 -- This copyright note is auto-generated by ./scripts/Create-CopyPatch.
3 --
4 -- T2 SDE: misc/lua/sde/desc.lua
5 -- Copyright (C) 2005 - 2006 The T2 SDE Project
6 -- Copyright (C) 2005 - 2006 Juergen "George" Sawinski
7 --
8 -- More information can be found in the files COPYING and README.
9 --
10 -- This program is free software; you can redistribute it and/or modify
11 -- it under the terms of the GNU General Public License as published by
12 -- the Free Software Foundation; version 2 of the License. A copy of the
13 -- GNU General Public License can be found in the file COPYING.
14 -- --- T2-COPYRIGHT-NOTE-END ---
16 -- TODO:
17 -- - parser should also accept string as input
18 -- - implement the desc.validator
20 -- DESCRIPTION:
21 -- t = desc.parse(line-iterator)
22 -- Parse ".desc" file format by passing a line iterator and
23 -- return a table, where lower-case of last entry in
24 -- PKG-DESC-FORMAT
25 -- is the key. Example:
26 -- Parsing "[I] Title" returns `{ title = "Title" }'
28 -- package object structure
29 desc = desc or {}
30 desc.__format__ = {}
32 -- FIXME setmetatable(desc, { __call = function })
34 -- parse .desc text ; expects line iterator function as argument
36 -- no, actually it expects a FILENAME. who changed that back again ?
37 -- Please do not touch that often, because this code is used in production environment
38 -- Valentin
39 function desc.parse(iter)
40 local retval = {}
42 -- FIXME: Perhaps we'll gain some performance by not reading
43 -- line by line
44 file=io.open(iter)
45 for line in file:lines() do
46 local tag,cnt
48 _,_,tag,cnt = string.find(line, "([[][^]]*[]])[ ]+(.*)")
49 if tag then
50 local fmt = desc.__format__[tag]
51 if fmt then
52 retval[fmt.name] = retval[fmt.name] or {}
53 table.insert(retval[fmt.name], cnt)
54 end
55 end
56 end
57 file:close()
58 return retval
59 end
61 -- similar to desc.parse, but validate the description
62 function desc.validate(iter)
63 local retval = desc.parse(iter)
65 -- TODO implement validating
67 return retval
68 end
70 -- init: parse PKG-DESC-FORMAT
71 for line in io.open("misc/share/PKG-DESC-FORMAT"):lines() do
72 local required=false
73 local tag
75 if string.match(line, "^[[]") ~= nil then
76 -- check if tag is required
77 if string.match(line, "([*])") ~= nil then required=true; end
79 -- use last tag definition as name
80 for t in string.gfind(line,"[[]([^]]*)[]]") do tag = t; end
81 tag = string.lower(tag)
83 -- sort into __format__
84 for t in string.gfind(line,"([[][^]]*[]])") do
85 desc.__format__[t] = { name = tag; required = required }
86 end
87 end
88 end