Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

76
LINES

< > BotCompany Repo | #1004128 // fileOpBot

JavaX fragment (include)

static int fileOpBot_maxReadSize = 500000;
static int fileOpBot_maxWriteSize = 500000;

static S fileOpBot(S s) ctex {
  new Matches m;
  
  if "get user home"
    ret ok(userHome());
  
  if "list directory *"
    ret structure(listDirAwarenessStyle(new File(assertAbsolutePath(m.unq(0)))));
    
  if "mkdir *" {
    S dir = assertAbsolutePath(m.unq(0));
    mkdir(new File(dir));
    ret new File(dir).exists() ? "ok" : "problem";
  }
  
  if (matchOneOf(s, m, "rmdir *", "delete file *")) {
    S dir = assertAbsolutePath(m.unq(0));
    new File(dir).delete();
    ret !new File(dir).exists() ? "ok" : "problem";
  }
  
  if "load file * bin from * l *" {
    File f = new File(assertAbsolutePath(m.unq(0)));
    long from = parseLong(m.unq(1));
    int l = parseInt(m.unq(2));
    if (l > fileOpBot_maxReadSize)
      fail(format("Can't read more than * bytes per call", fileOpBot_maxReadSize));
    byte[] buf = new byte[l];
    RandomAccessFile raf = new RandomAccessFile(f, "r");
    try {
      raf.seek(from);
      raf.readFully(buf);
      ret "OK " + bytesToHex(buf);
    } finally {
      raf.close();
    }
  }
  
  if "get file * length" {
    File f = new File(assertAbsolutePath(m.unq(0)));
    ret str(f.length());
  }
  
  if "set file * length to *" {
    File f = new File(assertAbsolutePath(m.unq(0)));
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    try {
      raf.setLength(parseLong(m.unq(1)));
      ret "OK";
    } finally {
      raf.close();
    }
  }
    
  if "write file * bin from * data *" {
    print(shorten(s, 200));
    File f = new File(assertAbsolutePath(m.unq(0)));
    long from = parseLong(m.unq(1));
    byte[] data = hexToBytes(m.unq(2));
    if (l(data) > fileOpBot_maxWriteSize)
      fail(format("Can't write more than * bytes per call", fileOpBot_maxWriteSize));
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    try {
      raf.seek(from);
      raf.write(data);
      ret "OK";
    } finally {
      raf.close();
    }
  }
  
  null;
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004128
Snippet name: fileOpBot
Eternal ID of this version: #1004128/1
Text MD5: 98e354fa07f89a6f4c9fbe0cd8198c5c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-01-12 03:17:51
Source code size: 2089 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 577 / 900
Referenced in: [show references]