I don't know how to make this in C
I have made in lua/prisma using output from accel-cmd
prisma:
http://linguagemprisma.br4.biz/prisma_forum/index.php
Code: Select all
#!/usr/local/bin/prisma
local json = inclua'json'
local a = es.pabra('accel-cmd show sessions ifname,username,ip,state,uptime,calling-sid,called-sid,rx-bytes-raw,tx-bytes-raw,rate-limit')
s = a:leia('*t')
// imprima(s)
funcao apare(s)
retorne (string.troque(s, "^%s*(.-)%s*$", "%1"))
fim
funcao tab_remova(tab,c) //tabela sempre é passada por referência
local removaitem = tabela.remova;
para i=1,#tab inicio
se tab[i] == c entao
removaitem(tab,i); //remove este indice da tabela e recompoe a tabela.
fim
fim
para i=1,#tab inicio
tab[i]=apare(tab[i]); //apara os espaços ao redor das strings;
fim
fim
funcao obt_tab(s)
local lin, col,tab,tmp=nulo,nulo,{},nulo;
lin = s:analise('\n');
se nao lin entao retorne nulo, 'erro ao analisar string'; fim
col = lin[1]:analise('|'); //guardamos os nomes da colunas aqui
tab_remova(col,'|');//remove todos os '|';
tab_remova(lin,'\n');//remove todos os '\n' em lin
tabela.remova(lin,2);//remove o -----------+----------- ...
//agora o indice 3 é realocado para o 2, o 4 para o 3 e cada indice um abaixo.
para i=2,#lin inicio //um valor para cada item em col;
tmp = lin[i]:analise('|');
tab_remova(tmp,'|');//remove os '|';
tab[#tab+1] = {};// tab[i] = {campo1=valor,campo2 = valor2 ...}
para j=1,#col inicio
tab[#tab][col[j]] = tmp[j];//tab[i] [nomeCol] = tmp[j]
fim
fim
retorne tab;
fim
local tab = obt_tab(s);
imprima(json.cod(tab))
in lua:
Code: Select all
#!/usr/bin/lua5.3
local json = require'json'
-- local a = io.popen('accel-cmd show sessions ifname,username,ip,state,uptime,calling-sid,called-sid,rx-bytes-raw,tx-bytes-raw,rate-limit')
-- s = a:read('*a')
s = [[ ifname | username | ip | state | uptime | calling-sid | called-sid | rx-bytes | tx-bytes | rate-limit
--------+------------------------+-----------------+--------+----------+-------------------+------------+------------+------------+-------------
ppp44 | fulano | 170.170.170.170 | active | 03:52:20 | BR:AS:IL:BR:AS:IL | rede1 | 10.4 MiB | 905.5 KiB | 11000/1100 ]]
function analise(s, delimiter)
result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
function apare(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
function tab_remova(tab,c) --table is always passed as reference
local removaitem = table.remove;
for i=1,#tab do
if tab[i] == c then
removaitem(tab,i); --remove este indice da tabela e recompoe a tabela.
end
end
for i=1,#tab do
tab[i]=apare(tab[i]); --apara os espaços ao redor das strings;
end
end
function obt_tab(s)
local lin, col,tab,tmp=nil,nil,{},nil;
lin = analise(s,'\n');
if not lin then return nil, 'erro ao analisar string'; end
col = analise(lin[1],'|'); --column name are stored here
tab_remova(col,'|'); --remove all '|';
tab_remova(lin,'\n'); --remove all '\n' in lin
table.remove(lin,2); --remove -----------+----------- ...
--now index 3 is reallocated for 2, 4 for 3 and each other one below.
for i=2,#lin do --um valor para cada item em col;
tmp = analise(lin[i],'|');
tab_remova(tmp,'|'); --remove os '|';
tab[#tab+1] = {}; --tab[i] = {campo1=valor,campo2 = valor2 ...}
for j=1,#col do
tab[#tab][col[j]] = tmp[j]; --tab[i] [nomeCol] = tmp[j]
end
end
return tab;
end
local tab = obt_tab(s);
print(json.encode(tab))