[javascript-dev] Re: include statements possible?
sean ahern
sxa at whiterabbitdesign.co.uk
Thu Sep 27 05:21:47 MDT 2007
- Previous message: [javascript-dev] Re: [sharingisfun] passing messages via pattrstorage
- Next message: [javascript-dev] experience with stockwatch anyone?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
I know this is a -very- old thread, but, I was looking for a means of using include files, and wound up writing a basic DIY version. It basically relies on eval(), which -is- possibly inefficient, but it sucessfully allows me to use included files in my javascript code.
The code needs to be pasted into one's own js code, and supports
both 'local' and 'global' includes; the former live in the same directory as one's own js file, the latter live in a single central repository (defaulting to "<blah>\Cycling '74\jsincludes\" as Im using Windows).
The syntax is derived from cpp, so
include("lib") is a local file and
include("<lib>") is a global file
For ease of developing a library of includable routines, the include routine looks for a file suffixed '.h.js' -then- a file suffixed '.js'. I use the former filenaming convention for 'cleaned' libraries stripped of comments and whitespace (so there's less to read/eval), and the latter for 'development' libraries which might be heavily commented and indented.
example usage
--------------
//initialise buffer
var includes = "";
// include various files
include("local");
include("<global>");
include("<global2>");
// merge included code
eval(includes);
// reclaim string buffer
includes = "";
Hopefully its useful to someone. Ive used it to structure my code, as well as write 'support' libraries to do things like simplify inlet/outlet creation, and provide 'max-identical' attribute/argument declaration/handling.
-include.js code attached-
-------------- next part --------------
// include.js ****************************************************
// VERSION 0.4
//
//******************************************************************************
// history
// ??/05/2007: v 0.1 : first version
// 08/08/2007: v 0.2 : first proper reference version
// 08/08/2007: v 0.3 : INCLUDEPATH and compiled/uncompiled handling
// 27/09/2007: v 0.4 : minor tidying and better comments
//******************************************************************************
// documentation
// function : a standard javascript include handler (embed in js files)
// can include file from local directory path or a global
// directory path (default is set in INCLUDEPATH)
// will search for a 'compiled' (ie stripped of whitespace/comments)
// '.h.js' suffixed file first, then a raw '.js' suffixed file
//
// author : sean ahern (c) 2007
//
//
// usage example
// var includes = ""; // set up include buffer
// include("local"); // include local includefile 'local.h.js' or 'local.js'
// include("<global>"); // include global includefile 'global.h.js' or 'global.js'
// eval(includes); // initialise included code
// includes = ""; // clear buffer
//
//******************************************************************************
// --------------------------------------------------------- include function
//----------------------------------------------
include.local = 1;
function include(filename){
var INCLUDEPATH = ".\\Cycling '74\\jsincludes\\"; //or wherever suits
length = filename.length;
// determine full path for filename
if ((filename.substring(0,1) == "<")){ //< indicates global library
if ((filename.substring(length-1,length) == ">")){ // well-formed global
base_filename = INCLUDEPATH + filename.substring(1,length-1);
compiled_filename = base_filename + ".h.js";
raw_filename = base_filename + ".js";
}else{ //badly formed global
post("ERROR in include; malformed filename '",filename, "'\n");
return;
}
}else{
if ((filename.substring(length-1,length) == ">")){//badly formed include
post("ERROR in include; malformed filename '",filename, "'\n");
return;
}else{
compiled_filename = filename + ".h.js";
raw_filename = filename + ".js";
}
}
// open file to copy into a string
// prioritise the compiled version first
file = new File(compiled_filename, "read");
file.open();
if (!(file.isopen)){
file = new File(raw_filename, "read");
file.open();
}
if (file.isopen){
// read in lines of up to 120 chars at a time (compensates for strange filesize/buffer issue)
// this is probably slower, but strings get weirdly truncated otherwise.
fileposition = 0;
while (fileposition < file.eof){
chunkread = file.readline(120);
includes = includes + chunkread + "\n";
fileposition = file.position;
}
file.close();
}else{
post("ERROR in include; cannot open ", filename, "\n");
}
}// end function include
// =============================================================================
// end of code
// =============================================================================
- Previous message: [javascript-dev] Re: [sharingisfun] passing messages via pattrstorage
- Next message: [javascript-dev] experience with stockwatch anyone?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
