putstr, getstr (similar to putvar, getvar)

Posted By: AndrewAMD

putstr, getstr (similar to putvar, getvar) - 07/24/19 18:36

jcl,

Currently, Zorro has putvar and getvar, to get and set vars.

In the same vein, perhaps a similar pair of functions can be used to get and set strings. Maybe call them putstr and getstr.

Thanks,
Andrew
Posted By: SBGuy

Re: putstr, getstr (similar to putvar, getvar) - 07/25/19 17:48

I agree. putvar and getvar and poorly implemented, supposedly for speed. I can't even store a 8 digit int YYYYMMDD using putvar because the printf used to write it to text file has limited float resolution.

Now it goes into the Windows registry? - Oy vey! Who's bright idea that? All we need is more junk in the Windows registry.

Better to store as string and use a faster computer :-)

I had to write my own getstr and putstr. For what it's worth, here it is

Code

string mygetstr(string fName, string itemName)
{
	string content = file_content(fName);
	if (!content) return 0;
	
	string myitem = strtext(content,strf("%s,",itemName),"0");
	if (!myitem) return 0;
	
	return myitem;
	
}

int myputstr(string fName, string itemName, string newval)
{
	string content = file_content(fName);
	if (!content) return 0;

	if (strstr(content,strf("%s,",itemName)))	{
		string myitem = strtext(content,strf("%s,",itemName),"0");
		if (!myitem) return 0;
			
		string old2 = strf("%s,%s",itemName,myitem);
		string newval = strf("%s,%s",itemName,newval);
		
		content = strx(content,old2,newval);
		file_write(fName,content,0);
	}
	else			
		file_append(fName,strf("%s,%s\n",itemName,newval));
}

var mygetvar(string fName, string itemName){
	return (atof(mygetstr(fName, itemName)));
}

function myputvar(string fName, string itemName, var val) {
	myputstr(fName, itemName,strf("%.3f",val));
}

Posted By: laz

Re: putstr, getstr (similar to putvar, getvar) - 09/05/19 23:29

Isn't it a problem, to use so many temporary strings?

Or to return 0 for return type string?
Posted By: SBGuy

Re: putstr, getstr (similar to putvar, getvar) - 09/10/19 15:32

Hi Iaz,

Thanks for the code review. I swore that a few Zorro versions again, I read that temp strings lose their content after something like 10 other temp string calls. But the latest manual in 2.18.1b says temp strings loose value immediately after another temp string call. Yikes! I better fix my code :-)

As for return 0 for string time, isn't that the same as a NULL string pointer?
Posted By: jcl

Re: putstr, getstr (similar to putvar, getvar) - 09/17/19 13:29

I can at least confirm that temp strings are recycled after 10 calls, not immediately. 0 is the same as NULL.
© 2024 lite-C Forums