Commit ea5159a1 authored by rswindell's avatar rswindell
Browse files

Created a printf method (works like fprintf).

open method now opens in text mode (on Win32) if 'b' mode not specified..
parent bc5e7b8b
......@@ -125,6 +125,8 @@ static int fopenflags(char *mode)
if(strchr(mode,'b'))
flags|=O_BINARY;
else
flags|=O_TEXT;
if(strchr(mode,'w')) {
flags|=O_CREAT|O_TRUNC;
......@@ -658,6 +660,52 @@ js_clear_error(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rva
return(JS_TRUE);
}
static JSBool
js_fprintf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
char* cp;
uintN i;
JSString * fmt;
JSString * str;
va_list arglist[64];
private_t* p;
*rval = BOOLEAN_TO_JSVAL(JS_FALSE);
if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
return(JS_FALSE);
if(p->fp==NULL)
return(JS_TRUE);
if((fmt=JS_ValueToString(cx, argv[0]))==NULL)
return(JS_FALSE);
memset(arglist,0,sizeof(arglist)); /* Initialize arglist to NULLs */
for (i = 1; i < argc && i<sizeof(arglist)/sizeof(arglist[0]); i++) {
if(JSVAL_IS_STRING(argv[i])) {
if((str=JS_ValueToString(cx, argv[i]))==NULL)
return(JS_FALSE);
arglist[i-1]=JS_GetStringBytes(str); /* exception here July-29-2002 */
}
else if(JSVAL_IS_DOUBLE(argv[i]))
arglist[i-1]=(char*)(unsigned long)*JSVAL_TO_DOUBLE(argv[i]);
else if(JSVAL_IS_INT(argv[i]) || JSVAL_IS_BOOLEAN(argv[i]))
arglist[i-1]=(char *)JSVAL_TO_INT(argv[i]);
else
arglist[i-1]=NULL;
}
if((cp=JS_vsmprintf(JS_GetStringBytes(fmt),(char*)arglist))==NULL)
return(JS_FALSE);
*rval = INT_TO_JSVAL(fwrite(cp,1,strlen(cp),p->fp));
JS_smprintf_free(cp);
return(JS_TRUE);
}
/* File Object Properites */
enum {
......@@ -870,6 +918,9 @@ static jsMethodSpec js_file_functions[] = {
{"writeAll", js_writeall, 0, JSTYPE_BOOLEAN, "array lines"
,"write an array of strings to file"
},
{"printf", js_fprintf, 0, JSTYPE_NUMBER, "string format [,args]"
,"write a formatted string to the file"
},
{0}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment