Commit 499ebd95 authored by rswindell's avatar rswindell
Browse files

New File properties to change .ini file style attributes:

- ini_key_len
- ini_key_prefix
- ini_section_separator
- ini_value_separator
- ini_bit_separator
- ini_literal_separator
parent d4400fcd
......@@ -62,6 +62,7 @@ typedef struct
BOOL b64encoded;
BOOL network_byte_order;
BOOL pipe; /* Opened with popen() use pclose() to close */
ini_style_t ini_style;
} private_t;
......@@ -79,7 +80,7 @@ static void dbprintf(BOOL error, private_t* p, char* fmt, ...)
vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
sbuf[sizeof(sbuf)-1]=0;
va_end(argptr);
lprintf(LOG_DEBUG,"%04u File %s%s",p->fp ? fileno(p->fp) : 0,error ? "ERROR: ":"",sbuf);
}
......@@ -146,7 +147,7 @@ js_open(JSContext *cx, uintN argc, jsval *arglist)
return(JS_FALSE);
}
if(p->fp!=NULL)
if(p->fp!=NULL)
return(JS_TRUE);
SAFECOPY(p->mode,"w+"); /* default mode */
......@@ -226,7 +227,7 @@ js_popen(JSContext *cx, uintN argc, jsval *arglist)
return(JS_FALSE);
}
if(p->fp!=NULL)
if(p->fp!=NULL)
return(JS_TRUE);
SAFECOPY(p->mode,"r+"); /* default mode */
......@@ -286,7 +287,7 @@ js_close(JSContext *cx, uintN argc, jsval *arglist)
dbprintf(FALSE, p, "closed");
p->fp=NULL;
p->fp=NULL;
JS_RESUMEREQUEST(cx, rc);
return(JS_TRUE);
......@@ -393,7 +394,7 @@ js_raw_read(JSContext *cx, uintN argc, jsval *arglist)
rc=JS_SUSPENDREQUEST(cx);
len = read(fileno(p->fp),buf,len);
if(len<0)
if(len<0)
len=0;
JS_RESUMEREQUEST(cx, rc);
......@@ -409,7 +410,7 @@ js_raw_read(JSContext *cx, uintN argc, jsval *arglist)
rc=JS_SUSPENDREQUEST(cx);
dbprintf(FALSE, p, "read %u raw bytes",len);
JS_RESUMEREQUEST(cx, rc);
return(JS_TRUE);
}
......@@ -458,13 +459,13 @@ js_read(JSContext *cx, uintN argc, jsval *arglist)
rc=JS_SUSPENDREQUEST(cx);
len = fread(buf,1,len,p->fp);
if(len<0)
if(len<0)
len=0;
buf[len]=0;
if(p->etx) {
cp=strchr(buf,p->etx);
if(cp) *cp=0;
if(cp) *cp=0;
len=strlen(buf);
}
......@@ -505,7 +506,7 @@ js_read(JSContext *cx, uintN argc, jsval *arglist)
rc=JS_SUSPENDREQUEST(cx);
dbprintf(FALSE, p, "read %u bytes",len);
JS_RESUMEREQUEST(cx, rc);
return(JS_TRUE);
}
......@@ -530,7 +531,7 @@ js_readln(JSContext *cx, uintN argc, jsval *arglist)
if(p->fp==NULL)
return(JS_TRUE);
if(argc) {
if(!JS_ValueToInt32(cx,argv[0],&len))
return(JS_FALSE);
......@@ -547,7 +548,7 @@ js_readln(JSContext *cx, uintN argc, jsval *arglist)
buf[len]=0;
if(p->etx) {
cp=strchr(buf,p->etx);
if(cp) *cp=0;
if(cp) *cp=0;
}
if(p->rot13)
rot13(buf);
......@@ -718,11 +719,11 @@ static jsval get_value(JSContext *cx, char* value)
return(val);
}
/* hexadecimal number? */
if(!strncmp(value,"0x",2)) {
if(!strncmp(value,"0x",2)) {
for(p=value+2;*p;p++)
if(!isxdigit((uchar)*p))
break;
if(*p==0) {
if(*p==0) {
val=DOUBLE_TO_JSVAL((double)strtoul(value,NULL,0));
return(val);
}
......@@ -933,7 +934,7 @@ js_iniSetValue_internal(JSContext *cx, JSObject *obj, uintN argc, jsval* argv, s
rc=JS_SUSPENDREQUEST(cx);
result = iniSetInteger(list,section,key,i,NULL);
JS_RESUMEREQUEST(cx, rc);
} else if(JSVAL_IS_OBJECT(value)
} else if(JSVAL_IS_OBJECT(value)
&& (value_obj = JSVAL_TO_OBJECT(value))!=NULL
&& (strcmp("Date",JS_GetClass(cx, value_obj)->name)==0)) {
tt=(time_t)(js_DateGetMsecSinceEpoch(cx,value_obj)/1000.0);
......@@ -975,7 +976,7 @@ js_iniSetValue(JSContext *cx, uintN argc, jsval *arglist)
return(JS_FALSE);
}
if(p->fp==NULL)
if(p->fp==NULL)
return(JS_TRUE);
rc=JS_SUSPENDREQUEST(cx);
......@@ -1021,7 +1022,7 @@ js_iniRemoveKey(JSContext *cx, uintN argc, jsval *arglist)
FREE_AND_NULL(section);
return JS_FALSE;
}
if(key==NULL) {
JS_ReportError(cx, "Invalid NULL key specified");
FREE_AND_NULL(section);
......@@ -1262,7 +1263,7 @@ js_iniSetObject(JSContext *cx, uintN argc, jsval *arglist)
return(JS_FALSE);
}
if(p->fp==NULL)
if(p->fp==NULL)
return(JS_TRUE);
set_argv[0]=argv[0]; /* section */
......@@ -1286,7 +1287,7 @@ js_iniSetObject(JSContext *cx, uintN argc, jsval *arglist)
rval = JSVAL_TRUE;
for(i=0; i<id_array->length; i++) {
/* property */
JS_IdToValue(cx,id_array->vector[i],&set_argv[1]);
JS_IdToValue(cx,id_array->vector[i],&set_argv[1]);
/* value */
cp=NULL;
JSVALUE_TO_MSTRING(cx, set_argv[1], cp, NULL);
......@@ -1313,7 +1314,7 @@ js_iniSetObject(JSContext *cx, uintN argc, jsval *arglist)
rval = BOOLEAN_TO_JSVAL(iniWriteFile(p->fp,list));
strListFree(&list);
JS_RESUMEREQUEST(cx, rc);
JS_SET_RVAL(cx, arglist, rval);
JS_DestroyIdArray(cx,id_array);
......@@ -1410,7 +1411,7 @@ _threadstart(void * 0x0227dab0) line 187 + 13 bytes
and July-15-2010:
20000000()
20000000()
js32.dll!JS_SetElement(JSContext * cx, JSObject * obj, long index, long * vp) Line 3178 + 0x20 bytes C
> sbbs.dll!js_iniGetAllObjects(JSContext * cx, JSObject * obj, unsigned int argc, long * argv, long * rval) Line 1081 + 0x18 bytes C
js32.dll!js_Invoke(JSContext * cx, unsigned int argc, unsigned int flags) Line 1375 + 0x17 bytes C
......@@ -1508,7 +1509,7 @@ js_iniSetAllObjects(JSContext *cx, uintN argc, jsval *arglist)
return(JS_FALSE);
}
if(p->fp==NULL)
if(p->fp==NULL)
return(JS_TRUE);
rc=JS_SUSPENDREQUEST(cx);
......@@ -1538,7 +1539,7 @@ js_iniSetAllObjects(JSContext *cx, uintN argc, jsval *arglist)
for(j=0; j<id_array->length; j++) {
/* property */
JS_IdToValue(cx,id_array->vector[j],&set_argv[1]);
JS_IdToValue(cx,id_array->vector[j],&set_argv[1]);
/* check if not name */
JSVALUE_TO_MSTRING(cx, set_argv[1], cp, NULL);
if(JS_IsExceptionPending(cx)) {
......@@ -1618,7 +1619,7 @@ js_raw_write(JSContext *cx, uintN argc, jsval *arglist)
}
JS_RESUMEREQUEST(cx, rc);
return(JS_TRUE);
}
......@@ -1716,7 +1717,7 @@ js_write(JSContext *cx, uintN argc, jsval *arglist)
}
JS_RESUMEREQUEST(cx, rc);
return(JS_TRUE);
}
......@@ -2069,7 +2070,7 @@ js_flush(JSContext *cx, uintN argc, jsval *arglist)
rc=JS_SUSPENDREQUEST(cx);
if(p->fp==NULL)
JS_SET_RVAL(cx, arglist, JSVAL_FALSE);
else
else
JS_SET_RVAL(cx, arglist, BOOLEAN_TO_JSVAL(fflush(p->fp)==0));
JS_RESUMEREQUEST(cx, rc);
......@@ -2188,25 +2189,25 @@ js_fprintf(JSContext *cx, uintN argc, jsval *arglist)
JS_SET_RVAL(cx, arglist, INT_TO_JSVAL(fwrite(cp,1,strlen(cp),p->fp)));
JS_RESUMEREQUEST(cx, rc);
js_sprintf_free(cp);
return(JS_TRUE);
}
/* File Object Properites */
/* File Object Properties */
enum {
FILE_PROP_NAME
FILE_PROP_NAME
,FILE_PROP_MODE
,FILE_PROP_ETX
,FILE_PROP_EXISTS
,FILE_PROP_DATE
,FILE_PROP_IS_OPEN
,FILE_PROP_EOF
,FILE_PROP_ERROR
,FILE_PROP_EXISTS
,FILE_PROP_DATE
,FILE_PROP_IS_OPEN
,FILE_PROP_EOF
,FILE_PROP_ERROR
,FILE_PROP_DESCRIPTOR
,FILE_PROP_DEBUG
,FILE_PROP_POSITION
,FILE_PROP_LENGTH
,FILE_PROP_DEBUG
,FILE_PROP_POSITION
,FILE_PROP_LENGTH
,FILE_PROP_ATTRIBUTES
,FILE_PROP_YENCODED
,FILE_PROP_UUENCODED
......@@ -2219,9 +2220,15 @@ enum {
,FILE_PROP_CRC32
,FILE_PROP_MD5_HEX
,FILE_PROP_MD5_B64
/* ini style */
,FILE_INI_KEY_LEN
,FILE_INI_KEY_PREFIX
,FILE_INI_SECTION_SEPARATOR
,FILE_INI_VALUE_SEPARATOR
,FILE_INI_BIT_SEPARATOR
,FILE_INI_LITERAL_SEPARATOR
};
static JSBool js_file_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp)
{
jsval idval;
......@@ -2229,6 +2236,7 @@ static JSBool js_file_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict,
jsint tiny;
private_t* p;
jsrefcount rc;
char* str = NULL;
if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
JS_ReportError(cx,getprivate_failure,WHERE);
......@@ -2298,6 +2306,36 @@ static JSBool js_file_set(JSContext *cx, JSObject *obj, jsid id, JSBool strict,
return(JS_FALSE);
p->etx = (uchar)i;
break;
case FILE_INI_KEY_LEN:
if(!JS_ValueToInt32(cx,*vp,&i))
return(JS_FALSE);
p->ini_style.key_len = i;
break;
case FILE_INI_KEY_PREFIX:
JSVALUE_TO_MSTRING(cx, *vp, str, NULL);
HANDLE_PENDING(cx, str);
p->ini_style.key_prefix = str;
break;
case FILE_INI_SECTION_SEPARATOR:
JSVALUE_TO_MSTRING(cx, *vp, str, NULL);
HANDLE_PENDING(cx, str);
p->ini_style.section_separator = str;
break;
case FILE_INI_VALUE_SEPARATOR:
JSVALUE_TO_MSTRING(cx, *vp, str, NULL);
HANDLE_PENDING(cx, str);
p->ini_style.value_separator = str;
break;
case FILE_INI_BIT_SEPARATOR:
JSVALUE_TO_MSTRING(cx, *vp, str, NULL);
HANDLE_PENDING(cx, str);
p->ini_style.bit_separator = str;
break;
case FILE_INI_LITERAL_SEPARATOR:
JSVALUE_TO_MSTRING(cx, *vp, str, NULL);
HANDLE_PENDING(cx, str);
p->ini_style.literal_separator = str;
break;
}
return(JS_TRUE);
......@@ -2307,6 +2345,7 @@ static JSBool js_file_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
{
jsval idval;
char str[128];
char* s = NULL;
size_t i;
size_t rd;
off_t offset;
......@@ -2332,14 +2371,10 @@ static JSBool js_file_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
switch(tiny) {
case FILE_PROP_NAME:
if((js_str=JS_NewStringCopyZ(cx, p->name))==NULL)
return(JS_FALSE);
*vp = STRING_TO_JSVAL(js_str);
s = p->name;
break;
case FILE_PROP_MODE:
if((js_str=JS_NewStringCopyZ(cx, p->mode))==NULL)
return(JS_FALSE);
*vp = STRING_TO_JSVAL(js_str);
s = p->mode;
break;
case FILE_PROP_EXISTS:
if(p->fp) /* open? */
......@@ -2494,7 +2529,7 @@ static JSBool js_file_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
MD5_close(&md5_ctx,digest);
if(tiny==FILE_PROP_MD5_HEX)
MD5_hex((BYTE*)str,digest);
else
else
b64_encode(str,sizeof(str)-1,(char *)digest,sizeof(digest));
js_str=JS_NewStringCopyZ(cx, str);
break;
......@@ -2505,6 +2540,30 @@ static JSBool js_file_get(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
if(js_str!=NULL)
*vp = STRING_TO_JSVAL(js_str);
break;
case FILE_INI_KEY_LEN:
*vp = INT_TO_JSVAL(p->ini_style.key_len);
break;
case FILE_INI_KEY_PREFIX:
s = p->ini_style.key_prefix;
break;
case FILE_INI_SECTION_SEPARATOR:
s = p->ini_style.section_separator;
break;
case FILE_INI_VALUE_SEPARATOR:
s = p->ini_style.value_separator;
break;
case FILE_INI_BIT_SEPARATOR:
s = p->ini_style.bit_separator;
break;
case FILE_INI_LITERAL_SEPARATOR:
s = p->ini_style.literal_separator;
break;
}
if(s != NULL) {
if((js_str = JS_NewStringCopyZ(cx, s)) == NULL)
return JS_FALSE;
*vp = STRING_TO_JSVAL(js_str);
}
return(JS_TRUE);
......@@ -2539,6 +2598,13 @@ static jsSyncPropertySpec js_file_properties[] = {
{ "chksum" ,FILE_PROP_CHKSUM ,FILE_PROP_FLAGS, 311},
{ "md5_hex" ,FILE_PROP_MD5_HEX ,FILE_PROP_FLAGS, 311},
{ "md5_base64" ,FILE_PROP_MD5_B64 ,FILE_PROP_FLAGS, 311},
/* ini style elements */
{ "ini_key_len" ,FILE_INI_KEY_LEN ,JSPROP_ENUMERATE, 317},
{ "ini_key_prefix" ,FILE_INI_KEY_PREFIX ,JSPROP_ENUMERATE, 317},
{ "ini_section_separator" ,FILE_INI_SECTION_SEPARATOR ,JSPROP_ENUMERATE, 317},
{ "ini_value_separator" ,FILE_INI_VALUE_SEPARATOR ,JSPROP_ENUMERATE, 317},
{ "ini_bit_separator" ,FILE_INI_BIT_SEPARATOR ,JSPROP_ENUMERATE, 317},
{ "ini_literal_separator" ,FILE_INI_LITERAL_SEPARATOR ,JSPROP_ENUMERATE, 317},
{0}
};
......@@ -2549,7 +2615,7 @@ static char* file_prop_desc[] = {
,"<i>true</i> if the file is open or exists (case-insensitive) - <small>READ ONLY</small>"
,"<i>true</i> if the file has been opened successfully - <small>READ ONLY</small>"
,"<i>true</i> if the current file position is at the <i>end of file</i> - <small>READ ONLY</small>"
,"the last occurred error value (use clear_error to clear) - <small>READ ONLY</small>"
,"the last occurred error value (use <i>clear_error</i> to clear) - <small>READ ONLY</small>"
,"the open file descriptor (advanced use only) - <small>READ ONLY</small>"
,"end-of-text character (advanced use only), if non-zero used by <i>read</i>, <i>readln</i>, and <i>write</i>"
,"set to <i>true</i> to enable debug log output"
......@@ -2558,7 +2624,7 @@ static char* file_prop_desc[] = {
,"the current length of the file (in bytes)"
,"file mode/attributes"
,"set to <i>true</i> if binary data is to be written and read in Network Byte Order (big end first)"
,"set to <i>true</i> to enable automatic ROT13 translatation of text"
,"set to <i>true</i> to enable automatic ROT13 translation of text"
,"set to <i>true</i> to enable automatic Unix-to-Unix encode and decode on <tt>read</tt> and <tt>write</tt> calls"
,"set to <i>true</i> to enable automatic yEnc encode and decode on <tt>read</tt> and <tt>write</tt> calls"
,"set to <i>true</i> to enable automatic Base64 encode and decode on <tt>read</tt> and <tt>write</tt> calls"
......@@ -2567,6 +2633,12 @@ static char* file_prop_desc[] = {
,"calculated 32-bit checksum of file contents - <small>READ ONLY</small>"
,"calculated 128-bit MD5 digest of file contents as hexadecimal string - <small>READ ONLY</small>"
,"calculated 128-bit MD5 digest of file contents as base64-encoded string - <small>READ ONLY</small>"
,"ini style: minimum key length (for left-justified white-space padded keys)"
,"ini style: key prefix (e.g. ' ', or '\t')"
,"ini style: section separator (e.g. '\n')"
,"ini style: value separator (e.g. '=' or ' = ')"
,"ini style: bit separator (e.g. '|' or ' | ')"
,"ini style: literal separator (e.g. ':' or ' : ')"
,NULL
};
#endif
......@@ -2578,7 +2650,7 @@ static jsSyncMethodSpec js_file_functions[] = {
"mode (default: <tt>'w+'</tt>) specifies the type of access requested for the file, as follows:<br>"
"<tt>r&nbsp</tt> open for reading; if the file does not exist or cannot be found, the open call fails<br>"
"<tt>w&nbsp</tt> open an empty file for writing; if the given file exists, its contents are destroyed<br>"
"<tt>a&nbsp</tt> open for writing at the end of the file (appending); creates the file first if it doesnt exist<br>"
"<tt>a&nbsp</tt> open for writing at the end of the file (appending); creates the file first if it doesn't exist<br>"
"<tt>r+</tt> open for both reading and writing (the file must exist)<br>"
"<tt>w+</tt> open an empty file for both reading and writing; if the given file exists, its contents are destroyed<br>"
"<tt>a+</tt> open for reading and appending<br>"
......@@ -2599,13 +2671,13 @@ static jsSyncMethodSpec js_file_functions[] = {
"<tt>w+</tt> DENYALL - Does not allow other scripts to open the file when <i>shareable</i> is set to true<br>"
"<tt>a+</tt> DENYALL - Does not allow other scripts to open the file when <i>shareable</i> is set to true<br>"
"When <i>shareable</i> is true uses fopen(), "
"and will only attempt to open the file once and will perform no locking. The behaviour "
"and will only attempt to open the file once and will perform no locking. The behavior "
"when one script has a file opened with <i>shareable</i> set to a different value than is used "
"with a new call is OS specific. On Windows, the second open will always fail and on *nix, "
"the second open will always succeed.<br>"
)
,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>"
......@@ -2615,11 +2687,11 @@ static jsSyncMethodSpec js_file_functions[] = {
"(<b>only functional on UNIX systems</b>)"
)
,315
},
},
{"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
......@@ -2646,11 +2718,11 @@ static jsSyncMethodSpec js_file_functions[] = {
{"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")
......@@ -2659,7 +2731,7 @@ static jsSyncMethodSpec js_file_functions[] = {
{"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)")
......@@ -2697,7 +2769,7 @@ static jsSyncMethodSpec js_file_functions[] = {
{"writeAll", js_writeall, 0, JSTYPE_BOOLEAN, JSDOCSTR("array lines")
,JSDOCSTR("write an array of strings to file")
,310
},
},
{"raw_write", js_raw_write, 1, JSTYPE_BOOLEAN, JSDOCSTR("text")
,JSDOCSTR("write a string to the underlying file descriptor. "
"Undefined results when mixed with any other read/write methods except raw_read, including indirect ones.")
......@@ -2776,7 +2848,7 @@ static jsSyncMethodSpec js_file_functions[] = {
static void js_finalize_file(JSContext *cx, JSObject *obj)
{
private_t* p;
if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
return;
......@@ -2785,6 +2857,11 @@ static void js_finalize_file(JSContext *cx, JSObject *obj)
dbprintf(FALSE, p, "closed: %s",p->name);
FREE_AND_NULL(p->ini_style.key_prefix);
FREE_AND_NULL(p->ini_style.section_separator);
FREE_AND_NULL(p->ini_style.value_separator);
FREE_AND_NULL(p->ini_style.bit_separator);
FREE_AND_NULL(p->ini_style.literal_separator);
free(p);
JS_SetPrivate(cx, obj, NULL);
......@@ -2797,7 +2874,7 @@ static JSBool js_file_resolve(JSContext *cx, JSObject *obj, jsid id)
if(id != JSID_VOID && id != JSID_EMPTY) {
jsval idval;
JS_IdToValue(cx, id, &idval);
if(JSVAL_IS_STRING(idval))
JSSTRING_TO_MSTRING(cx, JSVAL_TO_STRING(idval), name, NULL);
......
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