6 registered members (TipmyPip, Niels, dBc, Ed_Love, 3run, 1 invisible),
17,843
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
file_str_write / str_stri not working with unicode??
#435915
01/16/14 08:45
01/16/14 08:45
|
Joined: Aug 2008
Posts: 394 Germany
Benni003
OP
Senior Member
|
OP
Senior Member
Joined: Aug 2008
Posts: 394
Germany
|
Hi, I hope someone can help me with two problems.. 1 - I use file_str_write() to write the string into a textfile, but it's not correct working. There is just one symbol. I think with unicode I have to do it in a different way, but how? 2 - I need to get the position of a word in a string. I use str_stri (STRING* content,STRING* search) for that, but it's also not working with unicode. I did it in two different ways: * str content - unicode / str search - standard -> Sometimes working, sometimes crash with nexus too small..(but nexus is enough) * str content - unicode / str search - unicode -> No crash, but search was not found in content. I hope someone can help me  It's really important...
|
|
|
Re: file_str_write / str_stri not working with unicode??
[Re: Benni003]
#436024
01/17/14 10:14
01/17/14 10:14
|
Joined: Aug 2008
Posts: 394 Germany
Benni003
OP
Senior Member
|
OP
Senior Member
Joined: Aug 2008
Posts: 394
Germany
|
Is there anyone who knows how to do it? I really need it soon  the first point is for writing a savegame name in unicode. The second point is to paste a value into a text of a message box. Example: in the textfile: You have got |#VALUE#| points left. in the game: You have got 12 points left. - with str_stri() thats easy to do, but with unicode something is not working... I need to do it in this way because the game will release in different languages. And the sentence constructions will be different.
|
|
|
Re: file_str_write / str_stri not working with unicode??
[Re: Benni003]
#436027
01/17/14 10:31
01/17/14 10:31
|
Joined: Aug 2002
Posts: 3,258 Mainz
oliver2s
Expert
|
Expert
Joined: Aug 2002
Posts: 3,258
Mainz
|
I can't directly solve your problems. But I've worked a lot with unicode strings. And you have to be always sure that the string content ist Unicode. I do it this way: 1. Create a TEXT with a Unicode font
FONT* arial15b_unicode_font="Arial#15b";
TEXT* txt_unicode_helper = { strings = 1; font = arial15b_unicode_font; }
2. Make a Unicode text file (for example "unicode_helper.txt"). The content of this file will be just 1 char (for example "#"). Make sure you save this file with Unicode encoding! 4. Load the external text file into the TEXT
txt_loadw(txt_unicode_helper ,"unicode_helper.txt");
5. Now on every string you want to use do the following:
str_cpy(myString,(txt_unicode_helper .pstring)[0]); //copy the unicode char '#' in the string to make it unicode
str_cat(myString,... //do with your string whatever you want
str_clip(myString,1);//remove the helper unicode char
|
|
|
Re: file_str_write / str_stri not working with unicode??
[Re: Benni003]
#436035
01/17/14 13:27
01/17/14 13:27
|
Joined: Aug 2002
Posts: 3,258 Mainz
oliver2s
Expert
|
Expert
Joined: Aug 2002
Posts: 3,258
Mainz
|
str_createw("") should create Unicode strings, but why this is not working? In the way you wrote before everything is fine. Yes I know, that's why I use the approach above. I also don't know why str_createw("") doesn't work. Maybe because it only works with non-empty strings (didn't tested it).
|
|
|
Re: file_str_write / str_stri not working with unicode??
[Re: oliver2s]
#436036
01/17/14 13:34
01/17/14 13:34
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
It's because it expects a Unicode string already. You supply it with an ASCII string, where each character is 1 byte. In UTF16, each character is at least 2 bytes, with surrogate pairs having 4 bytes. You can write your own transform function easily, luckily all it takes is taking each character of your ASCII string and putting it into a 2 byte integer, and then supplying that to str_createw(). Here is an example (don't forget to free the string):
short *convert_ASCII_to_utf16(const char *string)
{
size_t length = strlen(string);
short *utf16 = malloc(length + 1 * sizeof(short));
short *temp = utf16;
while(*string)
*temp ++ = *string ++;
*temp ++ = 0; // 0 Terminate the string
return utf16;
}
Of course it makes sense to wrap the above in a function like this:
STRING *str_createw_from_ASCII(const char *cstring)
{
short *wstring = convert_ASCII_to_utf16(cstring);
STRING *string = str_createw(wstring);
free(wstring);
return string;
}
Also, it gets more complicated once you go outside of the 7 bit ASCII range, but the first code page in Unicode is luckily identical, making the example above work. Edit: If Lite-C is still unable to handle postfix increments correctly, you need to adapt the function above accordingly (and probably file some bug reports)
Last edited by JustSid; 01/17/14 13:39.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: file_str_write / str_stri not working with unicode??
[Re: Benni003]
#436200
01/21/14 14:33
01/21/14 14:33
|
Joined: Aug 2002
Posts: 3,258 Mainz
oliver2s
Expert
|
Expert
Joined: Aug 2002
Posts: 3,258
Mainz
|
Sorry, I just didn't see your answer. You have add 2 bytes (255 and 254) at the beginning of your unicode file for encoding. Then you can use file_str_write as usual Here you go:
var file=file_open_write("unicodeFile.txt");
//add unicode encoding
file_asc_write(file,255);
file_asc_write(file,254);
//writing unicode strings works with file_str_write
file_str_write(file,myUnicodeString);
file_close(file);
|
|
|
|