Check-in [07c2ea91bc]
Overview
SHA1:07c2ea91bc6c90eadee982dc6c4b450b2d5d7501
Date: 2013-11-24 08:24:20
User: suchenwi
Comment:added [file join], [file split]
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2013-11-24
10:21
[b0688623ce] Leaf: factored out infovars, added README.txt (user: suchenwi, tags: trunk)
08:24
[07c2ea91bc] added [file join], [file split] (user: suchenwi, tags: trunk)
2013-11-23
21:28
[4f3db7f982] added several file commands, fixed backslash substitutions (user: suchenwi, tags: trunk)
Changes

Modified tcl053.js from [623beb5ebc] to [dc45822eaf].

    60     60       this.incrLevel = function() {
    61     61         this.callframe[++this.level] = {};
    62     62         return this.level;
    63     63       }
    64     64       this.decrLevel = function() {
    65     65         this.callframe[this.level] = null;
    66     66         this.level--;
    67         -      if (this.level < 0) throw "Exit application";
           67  +      if (this.level < 0) this.level = 0;
    68     68         this.result = null;
    69     69       }
    70     70       this.getCommand = function(name) {
    71     71           try {
    72     72               return this.commands[name];
    73     73           } catch (e) {throw "No such command '"+name+"'";}
    74     74       }
................................................................................
   265    265   	throw 'key "'+key+'" not known in dictionary';
   266    266         });
   267    267       this.registerSubCommand("dict", "keys", function (interp, args) {
   268    268   	if(args.length < 2 || args.length > 3) 
   269    269   	  throw 'wrong # args: should be "dict keys dictionary ?globPattern?"';
   270    270   	var dict    = args[1].toList();
   271    271   	var pattern = ".*";
   272         -	if(args.length == 3) pattern = "^"+args[2].toString().replace(/\*/g,".*");
          272  +	if(args.length == 3) 
          273  +	  pattern = "^"+args[2].toString().replace(/\*/g,".*");
   273    274   	var res  = [];
   274         -	for (var i=0;i < dict.length;i+=2) {
          275  +	for (var i = 0; i < dict.length; i+=2) {
   275    276   	  if(dict[i].toString().match(pattern)) res.push(dict[i]);
   276    277   	}
   277    278   	return res;
   278    279         });
   279    280       this.registerSubCommand("dict", "set", function (interp, args) {
   280    281   	this.arity(args, 4);
   281    282   	var name  = args[1];
................................................................................
   394    395   	return stat.atime.getTime()/1000;
   395    396         })
   396    397       this.registerSubCommand("file", "dirname", function (interp, args) {
   397    398           this.arity(args, 2);
   398    399   	return interp.dirname(args[1].toString());
   399    400        });
   400    401       this.dirname = function(p) { // also used in [glob]
   401         -      //require("path"); //not working :(
   402         -      //return path.dirname(p.toString());
   403         -      if(p == ".") p = process.cwd();
   404         -      p = p.split("/"); 
   405         -      p.pop();
   406         -      if(p == "") return("/");
   407         -      return p.join("/");
          402  +      var path = require("path");
          403  +      return path.dirname(p.toString());
   408    404       };
   409    405       this.registerSubCommand("file", "exists", function (interp, args) {
   410    406           this.arity(args, 2);
   411    407   	var file = args[1].toString();
   412    408   	try {var fd = fs.openSync(file,"r");} catch(e) {return 0;}
   413    409   	fs.closeSync(fd);
   414    410   	return 1;
................................................................................
   416    412       this.registerSubCommand("file", "extension", function (interp, args) {
   417    413           this.arity(args, 2);
   418    414   	var fn  = args[1].toString();
   419    415   	var res = fn.split(".").pop();
   420    416   	res = (res == fn)? "" : "."+res;
   421    417   	return res;
   422    418        });
          419  +    this.registerSubCommand("file", "join", function (interp, args) {
          420  +        this.arity(args, 2, Infinity);
          421  +	args.shift();
          422  +	var res = "", sep = "";
          423  +	for (var arg in args) {
          424  +	  var part = args[arg].toString();
          425  +	  if(part.match("^[/]")) 
          426  +	    res = part; else res = res+sep+part;
          427  +	  sep = "/";
          428  +	}
          429  +	return res;
          430  +      });
   423    431       this.registerSubCommand("file", "mtime", function (interp, args) {
   424    432           this.arity(args, 2);
   425    433   	var stat = fs.statSync(args[1].toString());
   426    434   	return stat.mtime.getTime()/1000;
   427         -      })
          435  +      });
   428    436       this.registerSubCommand("file", "size", function (interp, args) {
   429    437           this.arity(args, 2);
   430    438   	var stat = fs.statSync(args[1].toString());
   431    439   	return stat.size;
   432         -      })
          440  +      });
          441  +    this.registerSubCommand("file", "split", function (interp, args) {
          442  +        this.arity(args, 2);
          443  +	var path = args[1].toString().split("/");
          444  +	if(path[0] == "") path[0] = "/";
          445  +	return path;
          446  +      });
   433    447       this.registerSubCommand("file", "tail", function (interp, args) {
   434    448           this.arity(args, 2);
   435    449   	return args[1].toString().split("/").pop();
   436    450         });
   437    451       this.registerCommand("for", function (interp, args) {
   438    452           this.arity(args, 5);
   439    453           interp.eval(args[1].toString());
................................................................................
   495    509   	//interp.buf = "";
   496    510   	//while(interp.buf == "") {
   497    511   	//interp.timeout = setTimeout(function(){}, 10000);
   498    512   	//  if(interp.getsing==0) break;
   499    513   	//}
   500    514   	return; // result will be in interp.buf when done
   501    515        });
   502         -   this.gets = function(char) {
          516  +/*   this.gets = function(char) {
   503    517        try {
   504    518          if(char.match(/foo[\r\n]/)) {
   505    519   	 this.getsing = 0;
   506    520   	 puts("received: "+this.buf);
   507    521          } else {
   508    522   	 puts("<"+char+">"+this.getsing);
   509    523   	 this.buf += char;
   510    524          }
   511    525        } catch(e) {puts(e)};
   512         -   }
          526  +   }*/
   513    527      this.registerCommand("glob", function (interp, args) {
   514    528   	this.arity(args, 2, Infinity);
   515    529   	args.shift();
   516    530   	var res    = [];
   517    531   	var prefix = "";
   518    532   	var dir    = ".";
   519    533   	for (var arg in args) {

Modified test_tcljs.tcl from [744eb2cb5e] to [93e8b14455].

    98     98   e.g. {set x -44; expr {-$x}} -> 44
    99     99   e.g. {expr 1<<3} -> 8
   100    100   
   101    101   e.g. {file dirname foo/bar/grill}  -> foo/bar
   102    102   e.g. {file dirname /foo/bar/grill} -> /foo/bar
   103    103   e.g. {file extension foo.txt}      -> .txt
   104    104   e.g. {file extension Makefile}     -> ""
          105  +e.g. {file join a b c}             -> a/b/c
          106  +e.g. {file join a /b c}            -> /b/c
          107  +e.g. {file split a/b/c}            -> {a b c}
          108  +e.g. {file split /a/b/c}           -> {/ a b c}
   105    109   e.g. {file tail foo/bar/grill}     -> grill
   106    110   
   107    111   set forres ""
   108    112   e.g. {for {set i 0} {$i < 5} {incr i} {append forres $i}; set forres} -> 01234
   109    113   e.g. {foreach i {a b c d e} {append foreachres $i}; set foreachres}   -> abcde
   110    114   
   111    115   e.g. {format %x 255} -> ff