Added FORMAT$ command to runtime

This commit is contained in:
Armando Rivera 2013-11-14 22:14:30 -05:00
parent 2315c249d5
commit 48c363f47b
2 changed files with 29 additions and 2 deletions

View File

@ -71,6 +71,10 @@ MAIN
PRINT ( "SAVEFILE()" ); PRINT ( "SAVEFILE()" );
SAVEFILE(fileContents,"output.txt"); SAVEFILE(fileContents,"output.txt");
PRINT ( "Contents saved to 'output.txt'"); PRINT ( "Contents saved to 'output.txt'");
PRINT ();
PRINT ( "FORMAT$(\"%d %s\", 10, \"TEN\")" );
PRINT ( FORMAT$("%d %s",10, "TEN") );
ENDMAIN ENDMAIN

View File

@ -14,10 +14,33 @@ DECLARE FUNCTION CSTRING ENC$ (CSTRING,INT=34,INT=34);
DECLARE FUNCTION CSTRING REVERSE$ (CSTRING); DECLARE FUNCTION CSTRING REVERSE$ (CSTRING);
DECLARE FUNCTION CSTRING REPLACE$ (CSTRING subject, CONSTANT CSTRING& search, CONSTANT CSTRING& replace); DECLARE FUNCTION CSTRING REPLACE$ (CSTRING subject, CONSTANT CSTRING& search, CONSTANT CSTRING& replace);
DECLARE FUNCTION INT VAL (CSTRING); DECLARE FUNCTION INT VAL (CSTRING);
DECLARE FUNCTION CSTRING FORMAT$ (CONSTANT CSTRING &fmt, ...);
DECLARE SUB PRINT (CSTRING); DECLARE SUB PRINT (CSTRING);
DECLARE SUB SAVEFILE(CSTRING src, CSTRING fname); DECLARE SUB SAVEFILE(CSTRING src, CSTRING fname);
FUNCTION CSTRING FORMAT$ (CONSTANT CSTRING &fmt, ...) BEGIN
DIM AS VECTOR<char> str(100,'\0');
DIM AS va_list ap;
DIM AS INT n;
WHILE (TRUE) {
va_start(ap, fmt);
n = vsnprintf(str.data(), str.size(), fmt.c_str(), ap);
va_end(ap);
IF ((n > -1) AND (size_t(n) < str.size())) THEN
RETURN str.data();
ENDIF
IF (n > -1) THEN
str.resize( n + 1 );
ELSE
str.resize( str.size() * 2);
ENDIF
WEND
RETURN str.data();
ENDFUNCTION
FUNCTION CSTRING REVERSE$ (CSTRING src) BEGIN FUNCTION CSTRING REVERSE$ (CSTRING src) BEGIN
RETURN CSTRING( src.rbegin(),src.rend() ); RETURN CSTRING( src.rbegin(),src.rend() );
ENDFUNCTION ENDFUNCTION