Newer
Older
"<tt>b </tt> open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed (e.g. <tt>r+b</tt>)<br>"
"<tt>e </tt> open a <i>non-shareable</i> file (that must not already exist) for <i>exclusive</i> access <i>(introduced in v3.12)</i><br>"
"<br><b>Note:</b> When using the <tt>iniSet</tt> methods to modify a <tt>.ini</tt> file, "
"the file must be opened for both reading and writing.<br>"
"<br><b>Note:</b> To open an existing or create a new file for both reading and writing, "
"use the <i>file_exists</i> function like so:<br>"
"<tt>file.open(file_exists(file.name) ? 'r+':'w+');</tt>"
,310
{"popen", js_popen, 1, JSTYPE_BOOLEAN, JSDOCSTR("[mode=<tt>\"r+\"</tt>] [,buffer_length]")
,JSDOCSTR("open pipe to command, <i>buffer_length</i> defaults to 2048 bytes, "
"mode (default: <tt>'r+'</tt>) specifies the type of access requested for the file, as follows:<br>"
"<tt>r </tt> read the programs stdout;<br>"
"<tt>w </tt> write to the programs stdin<br>"
"<tt>r+</tt> open for both reading stdout and writing stdin<br>"
"(<b>only functional on UNIX systems</b>)"
)
{"close", js_close, 0, JSTYPE_VOID, JSDOCSTR("")
,JSDOCSTR("close file")
,310
{"remove", js_delete, 0, JSTYPE_BOOLEAN, JSDOCSTR("")
,JSDOCSTR("remove the file from the disk")
,310
{"clearError", js_clear_error, 0, JSTYPE_ALIAS },
{"clear_error", js_clear_error, 0, JSTYPE_BOOLEAN, JSDOCSTR("")
,JSDOCSTR("clears the current error value (AKA clearError)")
,310
{"flush", js_flush, 0, JSTYPE_BOOLEAN, JSDOCSTR("")
,310
{"rewind", js_rewind, 0, JSTYPE_BOOLEAN, JSDOCSTR("")
,JSDOCSTR("repositions the file pointer (<i>position</i>) to the beginning of a file "
"and clears error and end-of-file indicators")
,311
{"truncate", js_truncate, 0, JSTYPE_BOOLEAN, JSDOCSTR("[length=<tt>0</tt>]")
,JSDOCSTR("changes the file <i>length</i> (default: 0) and repositions the file pointer "
"(<i>position</i>) to the new end-of-file")
{"lock", js_lock, 2, JSTYPE_BOOLEAN, JSDOCSTR("[offset=<tt>0</tt>] [,length=<i>file_length</i>-<i>offset</i>]")
,JSDOCSTR("lock file record for exclusive access (file must be opened <i>shareable</i>)")
,310
{"unlock", js_unlock, 2, JSTYPE_BOOLEAN, JSDOCSTR("[offset=<tt>0</tt>] [,length=<i>file_length</i>-<i>offset</i>]")
,JSDOCSTR("unlock file record for exclusive access")
,310
{"read", js_read, 0, JSTYPE_STRING, JSDOCSTR("[maxlen=<i>file_length</i>-<i>file_position</i>]")
,JSDOCSTR("read a string from file (optionally unix-to-unix or base64 decoding in the process), "
"<i>maxlen</i> defaults to the current length of the file minus the current file position")
,310
{"readln", js_readln, 0, JSTYPE_STRING, JSDOCSTR("[maxlen=<tt>512</tt>]")
,JSDOCSTR("read a line-feed terminated string, <i>maxlen</i> defaults to 512 characters")
,310
{"readBin", js_readbin, 0, JSTYPE_NUMBER, JSDOCSTR("[bytes=<tt>4</tt> [,count=<tt>1</tt>]")
,JSDOCSTR("read one or more binary integers from the file, default number of <i>bytes</i> is 4 (32-bits). "
"if count is not equal to 1, an array is returned (even if no integers were read)")
,310
{"readAll", js_readall, 0, JSTYPE_ARRAY, JSDOCSTR("[maxlen=<tt>512</tt>]")
,JSDOCSTR("read all lines into an array of strings, <i>maxlen</i> defaults to 512 characters")
,310
{"write", js_write, 1, JSTYPE_BOOLEAN, JSDOCSTR("text [,length=<i>text_length</i>]")
,JSDOCSTR("write a string to the file (optionally unix-to-unix or base64 decoding in the process)")
,310
{"writeln", js_writeln, 0, JSTYPE_BOOLEAN, JSDOCSTR("[text]")
,JSDOCSTR("write a line-feed terminated string to the file")
,310
{"writeBin", js_writebin, 1, JSTYPE_BOOLEAN, JSDOCSTR("value(s) [,bytes=<tt>4</tt>]")
,JSDOCSTR("write one or more binary integers to the file, default number of <i>bytes</i> is 4 (32-bits)."
"If value is an array, writes the entire array to the file.")
,310
{"writeAll", js_writeall, 0, JSTYPE_BOOLEAN, JSDOCSTR("array lines")
,JSDOCSTR("write an array of strings to file")
,310
{"printf", js_fprintf, 0, JSTYPE_NUMBER, JSDOCSTR("format [,args]")
,JSDOCSTR("write a formatted string to the file (ala fprintf) - "
"<small>CAUTION: for experienced C programmers ONLY</small>")
,310
{"iniGetSections", js_iniGetSections, 0, JSTYPE_ARRAY, JSDOCSTR("[prefix=<i>none</i>]")
,JSDOCSTR("parse all section names from a <tt>.ini</tt> file (format = '<tt>[section]</tt>') "
"and return the section names as an <i>array of strings</i>, "
"optionally, only those section names that begin with the specified <i>prefix</i>")
,311
{"iniGetKeys", js_iniGetKeys, 1, JSTYPE_ARRAY, JSDOCSTR("[section=<i>root</i>]")
,JSDOCSTR("parse all key names from the specified <i>section</i> in a <tt>.ini</tt> file "
"and return the key names as an <i>array of strings</i>. "
"if <i>section</i> is undefined, returns key names from the <i>root</i> section")
,311
{"iniGetValue", js_iniGetValue, 3, JSTYPE_UNDEF, JSDOCSTR("section, key [,default=<i>none</i>]")
,JSDOCSTR("parse a key from a <tt>.ini</tt> file and return its value (format = '<tt>key = value</tt>'). "
"returns the specified <i>default</i> value if the key or value is missing or invalid. "
"to parse a key from the <i>root</i> section, pass <i>null</i> for <i>section</i>. "
"will return a <i>bool</i>, <i>number</i>, <i>string</i>, or an <i>array of strings</i> "
"determined by the type of <i>default</i> value specified")
,311
{"iniSetValue", js_iniSetValue, 3, JSTYPE_BOOLEAN, JSDOCSTR("section, key, [value=<i>none</i>]")
,JSDOCSTR("set the specified <i>key</i> to the specified <i>value</i> in the specified <i>section</i> "
"of a <tt>.ini</tt> file. "
"to set a key in the <i>root</i> section, pass <i>null</i> for <i>section</i>. ")
,312
{"iniGetObject", js_iniGetObject, 1, JSTYPE_OBJECT, JSDOCSTR("[section=<i>root</i>]")
,JSDOCSTR("parse an entire section from a .ini file "
"and return all of its keys and values as properties of an object. "
"if <i>section</i> is undefined, returns key and values from the <i>root</i> section")
,311
{"iniSetObject", js_iniSetObject, 2, JSTYPE_BOOLEAN, JSDOCSTR("section, object")
,JSDOCSTR("write all the properties of the specified <i>object</i> as separate <tt>key=value</tt> pairs "
"in the specified <i>section</i> of a <tt>.ini</tt> file. "
"to write an object in the <i>root</i> section, pass <i>null</i> for <i>section</i>. ")
,312
{"iniGetAllObjects",js_iniGetAllObjects,1, JSTYPE_ARRAY, JSDOCSTR("[name_property] [,prefix=<i>none</i>]")
,JSDOCSTR("parse all sections from a .ini file and return all (non-<i>root</i>) sections "
"in an array of objects with each section's keys as properties of each object. "
"<i>name_property</i> is the name of the property to create to contain the section's name "
"(default is <tt>\"name\"</tt>), "
"the optional <i>prefix</i> has the same use as in the <tt>iniGetSections</tt> method, "
"if a <i>prefix</i> is specified, it is removed from each section's name" )
,311
{"iniSetAllObjects",js_iniSetAllObjects,1, JSTYPE_ARRAY, JSDOCSTR("object array [,name_property=<tt>\"name\"</tt>]")
,JSDOCSTR("write an array of objects to a .ini file, each object in its own section named "
"after the object's <i>name_property</i> (default: <tt>name</tt>)")
,312
},
{"iniRemoveKey", js_iniRemoveKey, 2, JSTYPE_BOOLEAN, JSDOCSTR("section, key")
,JSDOCSTR("remove specified <i>key</i> from specified <i>section</i> in <tt>.ini</tt> file.")
},
{"iniRemoveSection",js_iniRemoveSection,1, JSTYPE_BOOLEAN, JSDOCSTR("section")
,JSDOCSTR("remove specified <i>section</i> from <tt>.ini</tt> file.")
/* File Destructor */
static void js_finalize_file(JSContext *cx, JSObject *obj)
{
private_t* p;
if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
return;
if(p->external==JS_FALSE && p->fp!=NULL)
fclose(p->fp);
dbprintf(FALSE, p, "closed: %s",p->name);
free(p);
JS_SetPrivate(cx, obj, NULL);
}
static JSBool js_file_resolve(JSContext *cx, JSObject *obj, jsval id)
{
char* name=NULL;
if(id != JSVAL_NULL)
name=JS_GetStringBytes(JSVAL_TO_STRING(id));
return(js_SyncResolve(cx, obj, name, js_file_properties, js_file_functions, NULL, 0));
}
static JSBool js_file_enumerate(JSContext *cx, JSObject *obj)
{
return(js_file_resolve(cx, obj, JSVAL_NULL));
}
static JSClass js_file_class = {
"File" /* name */
,JSCLASS_HAS_PRIVATE /* flags */
,JS_PropertyStub /* addProperty */
,JS_PropertyStub /* delProperty */
,js_file_get /* getProperty */
,js_file_set /* setProperty */
,js_file_enumerate /* enumerate */
,js_file_resolve /* resolve */
,JS_ConvertStub /* convert */
,js_finalize_file /* finalize */
};
/* File Constructor (creates file descriptor) */
static JSBool
js_file_constructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
JSString* str;
private_t* p;
if((str = JS_ValueToString(cx, argv[0]))==NULL) {
JS_ReportError(cx,"No filename specified");
return(JS_FALSE);
}
*rval = JSVAL_VOID;
if((p=(private_t*)calloc(1,sizeof(private_t)))==NULL) {
JS_ReportError(cx,"calloc failed");
return(JS_FALSE);
}
SAFECOPY(p->name,JS_GetStringBytes(str));
if(!JS_SetPrivate(cx, obj, p)) {
dbprintf(TRUE, p, "JS_SetPrivate failed");
return(JS_FALSE);
}
#ifdef BUILD_JSDOCS
js_DescribeSyncObject(cx,obj,"Class used for opening, creating, reading, or writing files on the local file system<p>"
"Special features include:</h2><ol type=disc>"
"<li>Exclusive-access files (default) or shared files<ol type=circle>"
"<li>optional record-locking"
"<li>buffered or non-buffered I/O"
"</ol>"
"<li>Support for binary files<ol type=circle>"
"<li>native or network byte order (endian)"
"<li>automatic Unix-to-Unix (<i>UUE</i>), yEncode (<i>yEnc</i>) or Base64 encoding/decoding"
"</ol>"
"<li>Support for ASCII text files<ol type=circle>"
"<li>supports line-based I/O<ol type=square>"
"<li>entire file may be read or written as an array of strings"
"<li>individual lines may be read or written one line at a time"
"</ol>"
"<li>supports fixed-length records<ol type=square>"
"<li>optional end-of-text (<i>etx</i>) character for automatic record padding/termination"
"<li>Synchronet <tt>.dat</tt> files use an <i>etx</i> value of 3 (Ctrl-C)"
"</ol>"
"<li>supports <tt>.ini</tt> formated configuration files<ol type=square>"
"<li>concept and support of <i>root</i> ini sections added in v3.12"
"</ol>"
"<li>optional ROT13 encoding/translation"
"</ol>"
"<li>Dynamically-calculated industry standard checksums (e.g. CRC-16, CRC-32, MD5)"
"</ol>"
,310
js_DescribeSyncConstructor(cx,obj,"To create a new File object: <tt>var f = new File(<i>filename</i>)</tt>");
js_CreateArrayOfStrings(cx, obj, "_property_desc_list", file_prop_desc, JSPROP_READONLY);
#endif
dbprintf(FALSE, p, "object constructed");
return(JS_TRUE);
}
JSObject* DLLCALL js_CreateFileClass(JSContext* cx, JSObject* parent)
{
JSObject* sockobj;
sockobj = JS_InitClass(cx, parent, NULL
,&js_file_class
,js_file_constructor
,1 /* number of constructor args */
,NULL /* props, set in constructor */
,NULL /* funcs, set in constructor */
,NULL,NULL);
return(sockobj);
}
JSObject* DLLCALL js_CreateFileObject(JSContext* cx, JSObject* parent, char *name, FILE* fp)
{
JSObject* obj;
private_t* p;
obj = JS_DefineObject(cx, parent, name, &js_file_class, NULL
,JSPROP_ENUMERATE|JSPROP_READONLY);
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
if(obj==NULL)
return(NULL);
if((p=(private_t*)calloc(1,sizeof(private_t)))==NULL)
return(NULL);
p->fp=fp;
p->debug=JS_FALSE;
p->external=JS_TRUE;
if(!JS_SetPrivate(cx, obj, p)) {
dbprintf(TRUE, p, "JS_SetPrivate failed");
return(NULL);
}
dbprintf(FALSE, p, "object created");
return(obj);
}