Skip to content
Snippets Groups Projects
Commit 8b59ee66 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Add format_name() method to format a filename for display

It's a wrapper for filedat.c -> format_filename() which can truncate and pad a filename for display purposes.
parent 3fa3ad98
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
Pipeline #1888 failed
......@@ -749,6 +749,48 @@ js_get_file_name(JSContext *cx, uintN argc, jsval *arglist)
return JS_TRUE;
}
static JSBool
js_format_file_name(JSContext *cx, uintN argc, jsval *arglist)
{
jsval* argv = JS_ARGV(cx, arglist);
char* filepath = NULL;
int32 size = 12;
bool pad = false;
JS_SET_RVAL(cx, arglist, JSVAL_NULL);
if(!js_argc(cx, argc, 1))
return JS_FALSE;
uintN argn = 0;
JSVALUE_TO_MSTRING(cx, argv[argn], filepath, NULL);
HANDLE_PENDING(cx, filepath);
argn++;
if(argn < argc && JSVAL_IS_NUMBER(argv[argn])) {
JS_ValueToInt32(cx, argv[argn], &size);
argn++;
}
if(argn < argc && JSVAL_IS_BOOLEAN(argv[argn])) {
pad = JSVAL_TO_BOOLEAN(argv[argn]);
argn++;
}
if(size < 1) {
JS_ReportError(cx, "Invalid size: %d", size);
return JS_FALSE;
}
char* buf = calloc(size + 1, 1);
if(buf == NULL) {
JS_ReportError(cx, "malloc failure: %d", size + 1);
return JS_FALSE;
}
JSString* js_str;
if((js_str = JS_NewStringCopyZ(cx, format_filename(getfname(filepath), buf, size, pad))) != NULL)
JS_SET_RVAL(cx, arglist, STRING_TO_JSVAL(js_str));
free(buf);
return JS_TRUE;
}
static JSBool
js_get_file_path(JSContext *cx, uintN argc, jsval *arglist)
{
......@@ -1441,6 +1483,11 @@ static jsSyncMethodSpec js_filebase_functions[] = {
,JSDOCSTR("dump file metadata to an array of strings for diagnostic uses")
,31900
},
{"format_name", js_format_file_name,1, JSTYPE_STRING
,JSDOCSTR("path/filename [,number size=13] [,boolean pad=false]")
,JSDOCSTR("returns formatted (e.g. shortened) version of filename without path (file base does not have to be open) for display")
,31900
},
{0}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment