bool replace(std::string& str, const std::string& from, const std::string& to)
{
size_t start_pos = str.find(from);
if (start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
void as_printf(asIScriptGeneric *gen)
{
void *ref = gen->GetArgAddress(0);
int typeId = gen->GetArgTypeId(0);
string format = *static_cast<string*>(ref);
for (int i = 1; i < 16; i++)
{
ref = gen->GetArgAddress(i);
typeId = gen->GetArgTypeId(i);
switch (typeId)
{
case 67108876: //string?
{
string local = *static_cast<string*>(ref);
replace(format, "%s", local);
break;
}
case 2:
{
char local = *static_cast<char*>(ref);
replace(format, "%d", to_string(local));
break;
}
case 3:
{
short local = *static_cast<short*>(ref);
replace(format, "%d", to_string(local));
break;
}
case 4:
{
int local = *static_cast<int*>(ref);
replace(format, "%d", to_string(local));
break;
}
case 5:
{
long long local = *static_cast<long long*>(ref);
replace(format, "%d", to_string(local));
break;
}
case 6:
{
unsigned char local = *static_cast<unsigned char*>(ref);
replace(format, "%d", to_string(local));
break;
}
case 7:
{
unsigned short local = *static_cast<unsigned short*>(ref);
replace(format, "%d", to_string(local));
break;
}
case 8:
{
unsigned int local = *static_cast<unsigned int*>(ref);
replace(format, "%d", to_string(local));
break;
}
case 9:
{
unsigned long long local = *static_cast<unsigned long long*>(ref);
replace(format, "%d", to_string(local));
break;
}
case 10:
{
float local = *static_cast<float*>(ref);
replace(format, "%f", to_string(local));
break;
}
case 11:
{
double local = *static_cast<double*>(ref);
replace(format, "%f", to_string(local));
break;
}
}
}
cout << format << endl;
return;
}
//later
r = this->engine->RegisterGlobalFunction("void printf(string &in, ?&in var = 0, ?&in var2 = 0, ?&in var3 = 0, ?&in var4 = 0, ?&in var5 = 0, ?&in var6 = 0, ?&in var7 = 0, ?&in var8 = 0, ?&in var9 = 0, ?&in var10 = 0, ?&in var11 = 0, ?&in var12 = 0, ?&in var13 = 0, ?&in var14 = 0, ?&in var15 = 0)", asFUNCTION(as_printf), asCALL_GENERIC); assert(r >= 0);
Okay thanks! Got it working. Not the best but atleast it works.