Dealing with softcode datatypes in hardcode

Submitted by raevnos on Wed, 2003-06-25 23:19

When adding a new softcode function to the Penn source, you'll quickly notice that all the arguments are passed as strings (In the args array), and it returns values by adding them to another string. But in softcode, you have functions that expect numbers, dbrefs, etc. Penn provides a set of functions for use in the hardcode for converting strings to and from C types upon which real work can be done, and for appending values to the return string buffer.

They're described in the following table:

Datatype C type Is a TYPE String to C C to string Adding to a string buffer
Integer int is_integer(char*) and is_strict_integer(char*) parse_integer(char*) unparse_integer(int) safe_integer(int, buff, bp)
Unsigned Integer unsigned int is_uinteger(char*) parse_uinteger(char*) unparse_uinteger(unsigned int) safe_uinteger(unsigned int, buff, bp)
Floating-point number NVAL is_number(char*) and is_strict_number(char*) parse_number(char*) unparse_number(NVAL) safe_number(NVAL, buff, bp)
Dbref dbref is_dbref(char*) parse_dbref(char*) unparse_dbref(dbref) safe_dbref(dbref, buff, bp)
Boolean int is_boolean(char *) parse_boolean(char *) unparse_boolean(int) safe_boolean(int, buff, bp)
Character char N/A N/A N/A safe_chr(char, buff, bp)
String char* N/A N/A N/A safe_str(string, buff, bp, string), safe_strl(string, length, buff, bp)
  • For adding to a string buffer, there's also safe_format(buff, bp, fmt, ...) where fmt is a printf()-like format.
  • safe_chr() is currently a macro, so avoid arguments with side-effects when using it.
  • For numeric types, is_FOO() obeys the tiny_math config option where if there aren't any numbers at the start of a string, it's treated as 0. The is_strict_FOO() functions require numbers of the proper type.