js-cfb/bits/85_api.js

56 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-09-14 21:14:22 +00:00
function cfb_new(opts/*:?any*/)/*:CFBContainer*/ {
var o/*:CFBContainer*/ = ({}/*:any*/);
init_cfb(o, opts);
return o;
}
function cfb_add(cfb/*:CFBContainer*/, name/*:string*/, content/*:?RawBytes*/, opts/*:?any*/)/*:CFBEntry*/ {
var unsafe = opts && opts.unsafe;
if(!unsafe) init_cfb(cfb);
var file = !unsafe && CFB.find(cfb, name);
2017-09-14 21:14:22 +00:00
if(!file) {
var fpath/*:string*/ = cfb.FullPaths[0];
if(name.slice(0, fpath.length) == fpath) fpath = name;
else {
if(fpath.slice(-1) != "/") fpath += "/";
fpath = (fpath + name).replace("//","/");
}
file = ({name: filename(name), type: 2}/*:any*/);
2017-09-14 21:14:22 +00:00
cfb.FileIndex.push(file);
cfb.FullPaths.push(fpath);
if(!unsafe) CFB.utils.cfb_gc(cfb);
2017-09-14 21:14:22 +00:00
}
/*:: if(!file) throw new Error("unreachable"); */
file.content = (content/*:any*/);
file.size = content ? content.length : 0;
if(opts) {
if(opts.CLSID) file.clsid = opts.CLSID;
}
return file;
}
function cfb_del(cfb/*:CFBContainer*/, name/*:string*/)/*:boolean*/ {
init_cfb(cfb);
var file = CFB.find(cfb, name);
if(file) for(var j = 0; j < cfb.FileIndex.length; ++j) if(cfb.FileIndex[j] == file) {
cfb.FileIndex.splice(j, 1);
cfb.FullPaths.splice(j, 1);
return true;
}
return false;
}
function cfb_mov(cfb/*:CFBContainer*/, old_name/*:string*/, new_name/*:string*/)/*:boolean*/ {
init_cfb(cfb);
var file = CFB.find(cfb, old_name);
if(file) for(var j = 0; j < cfb.FileIndex.length; ++j) if(cfb.FileIndex[j] == file) {
cfb.FileIndex[j].name = filename(new_name);
cfb.FullPaths[j] = new_name;
return true;
}
return false;
}
function cfb_gc(cfb/*:CFBContainer*/)/*:void*/ { rebuild_cfb(cfb, true); }