#!/bin/sh true = [[ # Shell/Lua script to convert from MobileDB to JFile input="$1" output="$2" if [ -z "$input" ]; input="-"; fi if [ -z "$output" ]; output="-"; fi pdbconv unpack "$input" m2j.ascii$$ echo "dofile('m2j.ascii$$'); dofile('$0'); Database = m2j(Database)" | pdbconv pack - "$output" rc="$?" rm -f m2j.ascii$$ exit $rc ]] $debug function searchcat(recs, c) local i i = 1 while recs[i] and recs[i].category ~= c do i = i + 1 end if not recs[i] then error("MDB had no record with category " .. c) else return recs[i].data end end function m2j(mdb) local jdb, f, r, r2, i, j, fnames, numfields, nxt if mdb.type ~= "Mdb1" or mdb.creator ~= "Mdb1" then error("Source database is not a MobileDB database") end jdb = { name = "J: " .. gsub(mdb.name, "^[MJ]: ", ""), version = 1, type = "JbDb", creator = "JBas", dates = mdb.dates, appInfo = { version = 452, fields = { } }, records = { } -- records and fields have to get filled in } fnames = searchcat(mdb.records, 1) fwidths = searchcat(mdb.records, 6) fnames = fnames.fields fwidths = fwidths.fields -- write("fnames = ") show(fnames) -- write("fwidths = ") show(fwidths) i = 1 -- for now, all MobileDB stuff is strings. We could do better by recognizing -- and converting integers and dates while fnames[i] do jdb.appInfo.fields[i] = { type = "string", typenum = 1, width = fwidths[i], name = fnames[i] } i = i + 1 end numfields = i - 1 -- write("database has ", numfields, " fields\n") nxt = 1 i = 1 while mdb.records[i] do if mdb.records[i].data.type == "Data record" then r = mdb.records[i].data.fields -- write("converting record "); show(r) r2 = { } j = 1 while j <= numfields do -- write("converting field ", j, "\n") if r[j] then r2[j] = r[j] else r2[j] = "" end j = j + 1 end -- write("new fields is ") show(r2) jdb.records[nxt] = { category = 0, data = r2 } -- write("result is ") show(jdb.records[nxt]) nxt = nxt + 1 end i = i + 1 end return jdb end -- for debugging function show(x, indent) local i, v, i2, pfx, nl nl = "" if not indent then indent = ""; nl = "\n" end if type(x) == "table" then write("{\n") if x[1] then i = 1 pfx = indent .. " " while x[i] do write(pfx); show(x[i], indent .. " ") pfx = ",\n" .. indent .. " " i = i + 1 end else i = nil pfx = indent .. " " repeat i, v = next(x, i) if i ~= nil then write(pfx, i, " = "); show(v, indent .. " ") pfx = ",\n" .. indent .. " " end until (i == nil) end write ("\n", indent, "}") elseif type(x) == "string" then write(format('%q', x)) elseif type(x) == "number" then write(x) elseif type(x) == "nil" then write("nil") else print(x) end write(nl) end