strcmp and strncmp builtins

This commit is contained in:
Wolfgang Bumiller 2012-12-23 19:22:38 +01:00
parent 275b6f777a
commit ee7051c5a4
2 changed files with 73 additions and 20 deletions

View file

@ -56,36 +56,64 @@ Append a float parameter to be passed to \fImain\fR.
Append a string parameter to be passed to \fImain\fR. Append a string parameter to be passed to \fImain\fR.
.SH BUILTINS .SH BUILTINS
The following builtin functions are available: The following builtin functions are available:
.TP .fi
.RI "1) " void " print(" string... ") = " "#1" ; .RI "1) " void " print(" string... ") = " "#1" ;
.in +8
Print the passed strings to stdout. At most 8 strings are allowed. Print the passed strings to stdout. At most 8 strings are allowed.
.TP .in
.RI "2) " string " ftos(" float ") = " "#2" ; .RI "2) " string " ftos(" float ") = " "#2" ;
.in +8
Convert a float to a string. Convert a float to a string.
.TP .in
.RI "3) " entity " spawn() = " "#3" ; .RI "3) " entity " spawn() = " "#3" ;
.in +8
Spawn an entity. Spawn an entity.
.TP .in
.RI "4) " void " remove(" entity ") = " "#4" ; .RI "4) " void " remove(" entity ") = " "#4" ;
.in +8
Remove an entity. Remove an entity.
.TP .in
.RI "5) " string " vtos(" vector ") = " "#5" ; .RI "5) " string " vtos(" vector ") = " "#5" ;
.in +8
Convert a vector to a string. Convert a vector to a string.
.TP .in
.RI "6) " void " error(" string... ") = " "#6" ; .RI "6) " void " error(" string... ") = " "#6" ;
.in +8
Print at most 8 strings to stdout and then exit with an error. Print at most 8 strings to stdout and then exit with an error.
.TP .in
.RI "7) " float " vlen(" vector ") = " "#7" ; .RI "7) " float " vlen(" vector ") = " "#7" ;
.in +8
Get the length of a vector. Get the length of a vector.
.TP .in
.RI "8) " string " etos(" entity ") = " "#8" ; .RI "8) " string " etos(" entity ") = " "#8" ;
.in +8
Get the entity ID as string. Get the entity ID as string.
.TP .in
.RI "9) " float " stof(" string ") = " "#9" ; .RI "9) " float " stof(" string ") = " "#9" ;
.in +8
Convert a string to a float. Convert a string to a float.
.TP .in
.RI "10) " string " strcat(" string ", " string ") = " "#10" ; .RI "10) " string " strcat(" string ", " string ") = " "#10" ;
.in +8
Concatenate two strings, returning a tempstring. Concatenate two strings, returning a tempstring.
.in
.RI "11) " float " strcmp (" string ", " string ") = " "#11" ;
.fi
.RI " " float " strncmp(" string ", " string ", " float ") = " "#11" ;
.in +8
Compare two strings. Returns the same as the corresponding C functions.
.in
.SH BUGS .SH BUGS
Please report bugs on <http://github.com/graphitemaster/gmqcc/issues>, Please report bugs on <http://github.com/graphitemaster/gmqcc/issues>,
or see <http://graphitemaster.github.com/gmqcc> on how to contact us. or see <http://graphitemaster.github.com/gmqcc> on how to contact us.

27
exec.c
View file

@ -768,6 +768,30 @@ static int qc_strcat(qc_program *prog)
return 0; return 0;
} }
static int qc_strcmp(qc_program *prog)
{
char *cstr1, *cstr2;
qcany *str1, *str2;
qcany out;
if (prog->argc != 2 && prog->argc != 3) {
printf("ERROR: invalid number of arguments for strcmp/strncmp: %i, expected 2 or 3\n",
prog->argc);
return -1;
}
str1 = GetArg(0);
str2 = GetArg(1);
cstr1 = prog_getstring(prog, str1->string);
cstr2 = prog_getstring(prog, str2->string);
if (prog->argc == 3)
out._float = strncmp(cstr1, cstr2, GetArg(2)->_float);
else
out._float = strcmp(cstr1, cstr2);
Return(out);
return 0;
}
static prog_builtin qc_builtins[] = { static prog_builtin qc_builtins[] = {
NULL, NULL,
&qc_print, /* 1 */ &qc_print, /* 1 */
@ -779,7 +803,8 @@ static prog_builtin qc_builtins[] = {
&qc_vlen, /* 7 */ &qc_vlen, /* 7 */
&qc_etos, /* 8 */ &qc_etos, /* 8 */
&qc_stof, /* 9 */ &qc_stof, /* 9 */
&qc_strcat /* 10 */ &qc_strcat, /* 10 */
&qc_strcmp /* 11 */
}; };
static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]); static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);