|
Autosizing string
#413245
12/10/12 19:25
12/10/12 19:25
|
Joined: Apr 2011
Posts: 75 Malta
Ercoles
OP
Junior Member
|
OP
Junior Member
Joined: Apr 2011
Posts: 75
Malta
|
Can someone suggest a routine where if a string is greater than a certain length it is made to become multiline by inserting \n at certain instances of the string.
This way a string would never get out of the allocated text view but would be truncated on to the next line if it is longer that the text view.
Last edited by Ercoles; 12/10/12 19:26.
|
|
|
Re: Autosizing string
[Re: Ercoles]
#413299
12/11/12 17:19
12/11/12 17:19
|
Joined: Apr 2011
Posts: 75 Malta
Ercoles
OP
Junior Member
|
OP
Junior Member
Joined: Apr 2011
Posts: 75
Malta
|
I am trying to do this but I stumbled with a strange bug can anyone shed light?
#include <acknex.h> #include <default.c> #include <strio.c>
STRING* tester="This is a test to check something.";
function getword(STRING* kelma) { int i; int ii; i=str_len(kelma); ii=str_stri(kelma," "); if(ii==0){return(kelma);} str_trunc(kelma,i-ii); return(kelma); } function removeword(STRING* kelmab) { int i; i=str_stri(kelmab," "); if(i==0){str_cpy(kelmab," ");return(kelmab);} str_clip(kelmab,i); return(kelmab); } function main() { STRING* kelma2=""; STRING* kelma3=""; STRING* kelma4=""; STRING* kelma5=""; str_cpy(kelma2,tester); kelma4=getword(kelma2); str_cat(kelma4," "); str_cpy(kelma2,tester); kelma5=removeword(kelma2); error(kelma5); //<----------This should give same result str_cpy(kelma2,kelma5); error(kelma5); //<-----------As this why contents dissappear here? while(str_len(kelma2)>0) { kelma3=getword(kelma2); str_cpy(kelma2,kelma5); kelma5=removeword(kelma2); str_cpy(kelma2,kelma5); str_cat(kelma4,kelma3); str_cat(kelma4," "); } error(kelma4); }
Last edited by Ercoles; 12/11/12 17:21.
|
|
|
Re: Autosizing string
[Re: Superku]
#413318
12/12/12 08:01
12/12/12 08:01
|
Joined: Nov 2012
Posts: 62 Istanbul
Talemon
Junior Member
|
Junior Member
Joined: Nov 2012
Posts: 62
Istanbul
|
Just yesterday, I wrote some code to manually word-wrap my strings so I'll just paste it here. Maybe it'll help you or someone. If it's not clear, just tell me. P.S: This is written for unicode strings so you may need to change here and there. First function finds where to split, it's called by the second one. UnT_WordWrap takes a string, splits it where it should be wrapped and fills a pre-allocated string array which it takes as the first argument.
short const_null_uchar = 0x0;
int UnT_WrapSplit(STRING* arg_str, FONT* arg_font, var arg_width)
{
if(arg_str == NULL || arg_font == NULL)
{
diag("\nUnT_WrapSplit: bad argument");
return -1;
}
if(str_len(arg_str) < 1)
{
diag("\nUnT_WrapSplit: str_len(arg_str) < 1");
return -1;
}
if(str_width(arg_str, arg_font) < arg_width) return str_len(arg_str);
var chr = str_chr(arg_str, -str_len(arg_str) + 1, 0x20);
if(chr == 0)
{
STRING* temp_s = str_createw(&const_null_uchar);
var temp_w = 0;
chr = str_len(arg_str) - 1;
do
{
str_cut(temp_s, arg_str, 0, chr);
temp_w = str_width(temp_s, arg_font);
chr -= 1;
}while(temp_w > arg_width);
ptr_remove(temp_s);
return chr;
}
STRING* temp_s = str_cut(NULL, arg_str, 0, chr);
var temp_v = UnT_WrapSplit(temp_s, arg_font, arg_width);
ptr_remove(temp_s);
return temp_v;
}
int UnT_WordWrap(STRING** out_str_array, int arg_arrayCapacity, STRING* arg_in_str, FONT* arg_font, var arg_width)
{
if(out_str_array == NULL || arg_in_str == NULL || arg_font == NULL)
{
diag("\nUnT_WordWrap: bad argument");
return -1;
}
var loc_str_len = str_len(arg_in_str);
int loc_idx = 0;
STRING* loc_temp_str = str_createw(&const_null_uchar);
STRING* loc_temp_str_rest = str_createw(&const_null_uchar);
str_cpy(loc_temp_str, arg_in_str);
while(loc_idx < arg_arrayCapacity)
{
var end = UnT_WrapSplit(loc_temp_str, arg_font, arg_width);
if(end < 1) break;
str_cut(out_str_array[loc_idx], loc_temp_str, 0, end);
str_trim(out_str_array[loc_idx]);
str_cut(loc_temp_str_rest, loc_temp_str, end, 0);
ptr_remove(loc_temp_str);
loc_temp_str = loc_temp_str_rest;
loc_idx++;
}
return loc_idx;
}
Last edited by Talemon; 12/12/12 08:03.
|
|
|
Re: Autosizing string
[Re: Talemon]
#413360
12/12/12 17:15
12/12/12 17:15
|
Joined: Apr 2011
Posts: 75 Malta
Ercoles
OP
Junior Member
|
OP
Junior Member
Joined: Apr 2011
Posts: 75
Malta
|
Thanks all of you. In the meantime I had made this routine:
function Stringer(STRING* kelma) { var i; var ii; var iii; STRING* kelma2=""; STRING* kelma3=""; STRING* kelma4=""; iii=str_len(kelma); kelma2=str_parse(NULL,kelma,1); str_cat(kelma3,kelma2); str_cat(kelma3," "); ii=str_len(kelma3); while(ii<iii) { str_parse(kelma2,kelma,0); str_cat(kelma3,kelma2); ii +=(str_len(kelma2)+1); str_cat(kelma3," "); i=str_len(kelma3); if(i>65){str_cat(kelma3,"\n");str_cat(kelma4,kelma3);str_cpy(kelma3,"");} } str_cat(kelma4,kelma3); return(kelma4); }
I do not need it because the WWRAP method solved my problem but can anyone tell me why the function I wrote above works well only when called the first time and when called the second time it leaves fragments from the previous call string?
|
|
|
Re: Autosizing string
[Re: Ch40zzC0d3r]
#413364
12/12/12 18:33
12/12/12 18:33
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
That's wrong. "" is a char array or a pointer to a char but never a STRING. You can define strings globally this way but not local.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Autosizing string
[Re: Uhrwerk]
#413365
12/12/12 19:29
12/12/12 19:29
|
Joined: Apr 2011
Posts: 75 Malta
Ercoles
OP
Junior Member
|
OP
Junior Member
Joined: Apr 2011
Posts: 75
Malta
|
So how should they be defined locally?
Last edited by Ercoles; 12/12/12 19:31.
|
|
|
Re: Autosizing string
[Re: Ercoles]
#413376
12/12/12 21:26
12/12/12 21:26
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
So how should they be defined locally? Not at all. A STRING* is an engine object. Allocation is costly. You should use global or static strings whereever possible. If you have good reason to create a local string with str_create don't forget to free them afterwards, or you'll create a memory leak.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|