Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
3 registered members (AndrewAMD, Grant, Neb), 908 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
about custom printf function #387160
11/15/11 01:54
11/15/11 01:54
Joined: Dec 2009
Posts: 128
China
frankjiang Offline OP
Member
frankjiang  Offline OP
Member

Joined: Dec 2009
Posts: 128
China
Code:
//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 Offline
Expert
WretchedSid  Offline
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:
Code:
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:
Code:
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

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1