Gamestudio Links
Zorro Links
Newest Posts
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
Data from CSV not parsed correctly
by jcl. 04/20/24 08:32
Zorro FIX plugin - Experimental
by jcl. 04/20/24 08:30
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (7th_zorro, Aku_Aku, 1 invisible), 579 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Reading and displaying Unicode #464330
02/06/17 13:45
02/06/17 13:45
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
Hello everyone,

I recently upgraded to A8 (finally) and one of the first things I really wanted to do, was using the Unicode features.
Unfortunately it does not really work and I am not sure what I am doing wrong.
I have looked through the forum and I found some helpful topics but (and maybe I am just too stupid) I can't really solve my problem.

Here is a piece of coding for reading out some text of a .txt-file (ANSI coded) which I had used before upgrading to A8. (It is all still ANSI for showing how I did it before)

Quote:

///////////////////////////////
#include <acknex.h>
#include <default.c>

///////////////////////////////

FONT* unifont = "Arial Unicode MS#30";
TEXT* display = {layer = 1; font = unifont;}


function main()
{

STRING* str = "";
var fhandle = file_open_read("unicodetext.txt");

//The "unicodetext.txt" contains one line of text: test = Cool sentence;

file_find(fhandle,"test");
file_str_readto(fhandle,str,";",200);
file_close(fhandle);

(display.pstring)[0] = str;

set(display,SHOW);
display.pos_x = 200;
display.pos_y = 300;

}



This code works fine. The .txt is opened, 'file_find' looks for "test" and then copies everything that follows in the string (str) until it reads the semicolon.
So on my screen I can read the following: ' = Cool sentence'


Now I want to do the same thing with a Unicode .txt.
So I opened up my notepad again and saved it just as before but with Unicode this time instead of ANSI.

Then I adjusted
file_str_readto to
file_str_readtow

Aaaaand it does not work. Nothing gets displayed.
Using "txt_loadw" for directly filling the TEXT-object would work, but I want to precisely select rows from a txt and not load everything at once.

How can I load text out of my Unicode .txt and display it by using the above method?
Is it possible?

Yours,
Yking

Re: Reading and displaying Unicode [Re: Yking] #464349
02/07/17 01:09
02/07/17 01:09
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
I was reading through some other posts, specifically this one
UNICODE Text to String
and after a lot of trial and error I figured out what the problem is.

It is this part here:
Quote:
file_find(fhandle,"test");


The .txt is coded with unicode but since I wrote this "test" manually in the SED it is coded as ASCII I guess. So I am looking for something that technically does not exist in my .txt and therefore it cannot be displayed.

Looks like its not possible to read out Unicode when using
Quote:
file_find
which also makes it impossible to read out specific sections of a .txt

I think

Re: Reading and displaying Unicode [Re: Yking] #464352
02/07/17 07:36
02/07/17 07:36
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
I did some test with your code and 'file_find' works when you pass a short pointer or unicode string pointer as parameter.

Code:
short t[5];
t[0] = 116; // t
t[1] = 101; // e
t[2] = 115; // s
t[3] = 116; // t
t[4] = NULL;
STRING *strT = str_createw(t);
file_find(fhandle,strT);
//file_find(fhandle,t); // It also works



but not the whole field is oregano and the read pointer is located on the second byte of the coincidence so you need to move it back one byte.
Code:
file_seek (fhandle,-1,1);



I slightly remember having troubles with some 'str_...' functions with unicode too. In a fast search I found these three functions into my base code:
Code:
int str16_stri ( short *_s1, short *_s2 )
{
	int _pos = 0;
	for ( ; *_s1!=NULL; _s1++ )
	{
		short *_s1T = _s1;
		short *_s2T = _s2;
		for ( ; *_s2T!=NULL; _s2T++ )
		{
			if ( *_s1T != *_s2T )
				break;
			_s1T ++;
		}
		if ( *_s2T == NULL )
			return _pos;
		_pos += 1;
	}
	return -1;
}

int str16_len ( short *_s )
{
	int _pos = 0;
	for ( ; *_s!=NULL; _s++ )
		_pos += 1;
	return _pos;
}

BOOL str16_cmp ( short *_s1, short *_s2 )
{
	for ( ; *_s1!=NULL; _s1++ )
	{
		if ( *_s1 != *_s2 )
			return FALSE;
		_s2 ++;
	}
	if ( *_s2 == NULL )
		return TRUE;
	else
		return FALSE;
}



I needed to build up a TEXT struct filled of unicode strings in order to search for and compare specific strings anyway.

Salud!

Re: Reading and displaying Unicode [Re: txesmi] #464357
02/07/17 09:57
02/07/17 09:57
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
Hello txesmi,

Thanks to your input I could completely solve my problems laugh

I have written a small Ascii to Unicode Converter Function,
I will post it for anyone who is interested laugh.

Quote:

function ascii_to_unicode(STRING* ascii_string)
{

short t[30];
var current_ascii_value;

var i = 1;
for (i=1; i<=30; i++)
{

if (str_getchr(ascii_string, i) != 0)
{
t[i-1] = str_getchr(ascii_string, i);
}

}

return(str_createw(t));

}




To use this function accordingly, all I have to do is the following:

Quote:


STRING* str = "";
var fhandle = file_open_read("file.txt");

STRING* unicode_string = ascii_to_unicode("test1");
file_find(fhandle,unicode_string);
file_seek (fhandle,-1,1);

file_str_readtow(fhandle,str,";",NULL);
file_close(fhandle);




EDITED:

This code still has a bug, scroll down

Last edited by Yking; 02/07/17 13:55.
Re: Reading and displaying Unicode [Re: Yking] #464365
02/07/17 14:03
02/07/17 14:03
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
I was a little bit too quick with my last post.
Actually there is still a problem that I just discovered.

And it is about here:
Quote:

STRING* unicode_string = ascii_to_unicode("test1");
file_find(fhandle,unicode_string);


The unicode_string is working and can get loaded into a TEXT*-object. So that is working.

But file_find does not exactly work the way intended.
Basically it stops searching as soon as it finds the first letter/number (compared from my unicode_string) in my textfile.

For example, my textfile looks like:
test1 = hello;
test2 = goodbye;

My unicode_string contains "test2", so that is where I want file_find to go.

Buuut as soon as file_find hits the letter "t" from 'test1' it's thinking: "Hey I found the first letter, this must be the right one".
This means that it only compared the first letter. I wonder how I can fix that smirk

Re: Reading and displaying Unicode [Re: Yking] #464374
02/08/17 05:46
02/08/17 05:46
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
True. I'm sorry about that frown
The only solution I can see is to build your own 'file_find_U' function.

Salud!

Re: Reading and displaying Unicode [Re: txesmi] #464383
02/09/17 01:00
02/09/17 01:00
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
No hard feelings laugh !

I guess the only way to properly achieve what I need is to read the whole document into a String or a Buffer with 'file_str_readtow' and then search and cut out the part I am looking for. laugh

My only wish for the future would be, that more functions will support unicode, like for example 'inkey(String*)' or 'file_find(handle,string)'

Thanks for the help !
Yking

Re: Reading and displaying Unicode [Re: Yking] #464440
02/13/17 16:48
02/13/17 16:48
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
I am interested on this issue so I gived a try to a duplicated 'file_asc_read' reading the file short by short. It moves the read pointer same as 'file_find'.

Code:
short *short_from_char ( short *_s, char *_chr )
{
	short *_sT = _s;
	for ( ; *_chr!=NULL; _chr++ )
	{
		*_sT = *_chr;
		_sT ++;
	}
	*_sT = NULL;
	return _s;
}

BOOL _ffu_rec ( var _hdlFile, short *_s )
{
	if ( *_s == NULL )
		return TRUE;
	char _chr[2];
	_chr[0] = file_asc_read ( _hdlFile );
	_chr[1] = file_asc_read ( _hdlFile );
	if ( *_s != *((short*)_chr) )
		return FALSE;
	if ( _ffu_rec ( _hdlFile, ++_s ) )
	{
		file_seek ( _hdlFile, -2, 1 );
		return TRUE;
	}
	else
	{
		file_seek ( _hdlFile, -2, 1 );
		return FALSE;
	}
}

int file_findw ( var _hdlFile, char *_chrText )
{
	short _sText[32];
	short_from_char ( _sText, _chrText );
	file_seek ( _hdlFile, 0, 0 );
	int _iLength = file_length ( _hdlFile );
	int _iPos = 0;
	for ( ; _iPos<_iLength; _iPos+=2 )
	{
		if ( _ffu_rec ( _hdlFile, _sText ) )
			return _iPos;
	}
	return -1;
}



Salud!

Last edited by txesmi; 02/15/17 06:05.
Re: Reading and displaying Unicode [Re: txesmi] #464472
02/16/17 12:59
02/16/17 12:59
Joined: Aug 2014
Posts: 57
Y
Yking Offline OP
Junior Member
Yking  Offline OP
Junior Member
Y

Joined: Aug 2014
Posts: 57
Awesome stuff grin !
Will try it out as soon as I have time laugh !

Re: Reading and displaying Unicode [Re: Yking] #464493
02/19/17 06:50
02/19/17 06:50
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
You will not be disappointed grin

Enjoy!


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