Fun with varargs.
Displaying 1-2 of 2 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Overkill

Hey there!

After realizing how underdocumented some of my things were, I thought I'd make some use of the features I added in like... 2006, and share some fairly useful VC with you folks.

Firstly, I present DictConstruct, which can create and populate a dictionary in the same call.
// DictConstruct
// Constructs a dictionary and populates it using the string arguments given to it.
// Pass: args - strings of the form "key:value"
// Returns: A dictionary handle populated with the key-to-value mappings you supply.
int DictConstruct(... args)
{
int i;
int dict = DictNew();
string k, v;
for(i = 0; i < args.length; i++)
{
if(args.is_string[i])
{
k = GetToken(args.string[i], ":", 0);
v = TokenRight(args.string[i], ":", 1);
DictSetString(dict, k, v);
}
else
{
Exit("DictConstruct: Argument " + str(i) + " must be a string value.");
}
}
return dict;
}


Here is an example usage:
int dict = DictConstruct("name:Bob", "job:Fighter", "attack:54");


Secondly, I give you a fairly simple (but possibly handy) varargs function for logging text.
// Print
// Logs a message from a list of mixed string/int arguments and prints it with tabs
// Pass: args - The arguments to print
void Print(... args)
{
string s;
int i;
for(i = 0; i < args.length; i++)
{
if(args.is_int[i])
{
if(i) s += " ";
s += str(args.int[i]);
}
if(args.is_string[i])
{
if(i) s += " ";
s += args.string[i];
}
}
Log(s);
}


Example usage:
Print("Hey look it's like Log() except for ", 1, " thing, it takes varargs");

Posted on 2008-12-27 18:08:14 (last edited on 2008-12-28 00:58:53)

Overkill

Here's another one I just made, creates an "array"-like dictionary from a bunch of args.
// ListConstruct
// Constructs a dictionary with number-indexed keys mapped to the arguments supplied.
// Pass: args - values to populate the dictonary.
// Returns: A dictionary handle populated with the values you supply.
int ListConstruct(... args)
{
int i;
int dict = DictNew();
for(i = 0; i < args.length; i++)
{
if(args.is_string[i])
{
DictSetString(dict, str(i), args.string[i]);
}
else if(args.is_int[i])
{
DictSetInt(dict, str(i), args.int[i]);
}
}
return dict;
}


Example usage:
int list = ListConstruct(4, 5, 6, "Bob", 56);

Posted on 2008-12-30 18:12:53 (last edited on 2008-12-30 18:15:06)


Displaying 1-2 of 2 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.