|
3 registered members (AndrewAMD, Grant, Neb),
908
guests, and 6
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
about custom printf function
#387160
11/15/11 01:54
11/15/11 01:54
|
Joined: Dec 2009
Posts: 128 China
frankjiang
OP
Member
|
OP
Member
Joined: Dec 2009
Posts: 128
China
|
//Gamestudio version 7.80 • Juli 31, 2009
//#include <src/implement.h>
#include <acknex.h>
#include <default.c>
STRING* Print(char* str,int* arr)
{
STRING* _str = "#1";
STRING* _res = "";
str_cat(_str,str);
int i = 0;
int n = 0;
int* address;
for(i = 0;i<str_len(_str);i++)
{
char c = (_str.chars)[i];
if(c=='%')
{
char c2 = (_str.chars)[i+1];
if(c2 == 'd')
{
address = arr[n];
str_cat(_res,str_for_int(NULL,*address));
}
else if(c2 == 'f')
{
address = arr[n];
double _t = (double)(*address) / pow(2,10);
str_cat(_res,str_for_float(NULL,_t));
_t = NULL;
}
else if(c2 == 's')
{
address = arr[n];
str_cat(_res,address);
}
n++;
address = NULL;
}
else if(c=='\0')
{
}
else
{
if(i > 0 && (_str.chars)[i-1] != '%' )
{
int len = (int)(str_len(_res));
(_res.chars)[len] = c;
(_res.chars)[len+1] = '\0';
}
}
}
ptr_remove(_str);
return _res;
}
void main()
{
// logS("-------------------------------------------------");
int i = 0;
if(true)
{
int _var = 12;
var _x = 2.1;
char* str = "come one man did you see face?";
int a[4];
a[0] = &_var;
a[1] = &_var;
a[2] = &_x;
a[3] = str;
STRING* s = Print(" %d*%d*%f,%s",a);
Print(" %d*%d*%f,%s",a);
//log(s);//data output by socket...
printf("%s",s->chars);
}
}
hey guys,call function Print one time is OK,but if you call Print second time ,problem will be crash , any one have good ideas for create dynamic parameter
Last edited by frankjiang; 11/15/11 02:00.
development 3d game is interesting!
|
|
|
Re: about custom printf function
[Re: frankjiang]
#387169
11/15/11 11:32
11/15/11 11:32
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
I can't tell you why it doesn't crash the first time, although it should, but I can tell you that this won't work:
STRING* _str = "#1";
STRING* _res = "";
The reason is that you doesn't set _str and _res to new STRING pointers, but to a static char array. Use str_create() and later ptr_remove() to create and later remove your STRING. Also, it highly looks you are trying to reinvent the wheel here, if you just want a STRING containing the printf stuff, you can use something like this:
char temp[1024];
sprintf(temp, "%d*%d*%f,%s", _var, _var, _x, str);
log(_str(temp));
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
|