Allegro.cc - Online Community

Allegro.cc Forums » Installation, Setup & Configuration » Differences betwen al_ustr_new and al_ustr_new()

This thread is locked; no one can reply to it. rss feed Print
Differences betwen al_ustr_new and al_ustr_new()
AMCerasoli
Member #11,955
May 2010
avatar

Hi there!.

Who is kind enough to give me an explanation about the differences between al_ustr_new_from_buffer() and al_ustr_new(). Obviously a technical explanation, since have just save me a lot of trouble, I was getting a lot of garbage from one of my char pointers when reading from file an using the al_ustr_new_from_buffer() function the problem have just disappeared... It's like eliminates the garbage automatically. But that garbage seems to be created by the al_fread function. :-/

Thanks.

PS: I know I could search in the source, but I doubt I could understand something...

DAMN!! THE TITTLE! fuck!

Matthew Leverton
Supreme Loser
January 1999
avatar

AMCerasoli
Member #11,955
May 2010
avatar

Well... On my system they don't return the same result.

al_ustr_new() gives me a lot of garbage and al_ustr_new_from_buffer() works fine, this is the smaller example I could come up with. Probable I'm doing something wrong.

Check out the console, not the allegro window.

#SelectExpand
1#include <iostream> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_font.h> 4#include <allegro5/allegro_ttf.h> 5 6class OPC_DATA{ 7 8 public: 9 10 bool modalidad_normal; 11 bool catg0, catg1, catg2, catg3, catg4; 12 ALLEGRO_USTR* URL_page; 13 float sfx, music; 14 bool fullscreen; 15 int len; 16 17 18 OPC_DATA() { 19 20 ALLEGRO_FILE* file = al_fopen("datos.dat", "rb"); 21 22 if(file){ ///Si el archivo existe... 23 24 modalidad_normal = al_fread32be(file); 25 catg0 = al_fread32be(file); 26 catg1 = al_fread32be(file); 27 catg2 = al_fread32be(file); 28 catg3 = al_fread32be(file); 29 catg4 = al_fread32be(file); 30 len = al_fread32be(file); 31 32 std::cout<<"len: "<<len<<std::endl; 33 if(len==5)Beep(600,20); 34 35 char *buf = new char [len]; 36 al_fread(file, buf, len); 37 38 if(len==0) // This is because if the len==0 the URL_page variable is filled with garbage 39 URL_page = al_ustr_new(""); 40 else 41 ///Here!! <<<---- Change the functions a see the difference. 42 /// remember to delete the datos.dat file after changing the function 43 //URL_page = al_ustr_new(buf); //This is bad 44 URL_page = al_ustr_new_from_buffer(buf, len); // This is good 45 46 delete [] buf; 47 48 int T_sfx = al_fread32be(file); 49 sfx = *reinterpret_cast<float*>(&T_sfx); 50 51 int T_music = al_fread32be(file); 52 music = *reinterpret_cast<float*>(&T_music); 53 54 fullscreen = al_fread32be(file); 55 56 al_fclose(file); 57 //Beep(800,200); 58 59 }else{ /// Si el archivo NO existe... 60 61 modalidad_normal = true; 62 catg0 = true; 63 catg1 = true; 64 catg2 = true; 65 catg3 = true; 66 catg4 = true; 67 URL_page = al_ustr_new(""); 68 sfx = 1.6; 69 music = 1.5; 70 fullscreen = false; 71 72 al_fclose(file); 73 } 74 75 } 76 77 ~OPC_DATA() { 78 79 ALLEGRO_FILE* file = al_fopen("datos.dat", "wb"); 80 81 al_fwrite32be(file, modalidad_normal); 82 al_fwrite32be(file, catg0); 83 al_fwrite32be(file, catg1); 84 al_fwrite32be(file, catg2); 85 al_fwrite32be(file, catg3); 86 al_fwrite32be(file, catg4); 87 88 al_fwrite32be(file, (int)al_ustr_size(URL_page)); 89 al_fwrite(file, al_cstr(URL_page), al_ustr_size(URL_page)); 90 91 al_fwrite32be(file, *reinterpret_cast<int*>(&sfx)); 92 al_fwrite32be(file, *reinterpret_cast<int*>(&music)); 93 al_fwrite32be(file, fullscreen); 94 95 al_fclose(file); 96 al_ustr_free(URL_page); 97 98 } 99 100}; 101 102int main() 103{ 104 al_init(); 105 106 al_init_font_addon(); // initialize the font addon 107 al_init_ttf_addon();// initialize the ttf (True Type Font) addon 108 ALLEGRO_FONT *font = al_load_ttf_font("consola.ttf",72,0 ); // load the font | Font from Larabie Free Fonts 109 ALLEGRO_DISPLAY *display = NULL; 110 display = al_create_display(640,480); 111 112// Closing and opening simulation 113 114 for (int i=0;i<5;i++){ 115 OPC_DATA *od = new OPC_DATA; 116 al_ustr_append_chr(od->URL_page, 97); // This adds the letter "a" 117 delete od; 118 } 119// - - - - - - - - - - - - - - - - 120 121 OPC_DATA *od1 = new OPC_DATA; 122 al_ustr_append_chr(od1->URL_page, 97); 123 124 al_clear_to_color(al_map_rgb(50,10,70)); 125 al_draw_ustr(font, al_map_rgb(255,255,255), 640/2, (480/4),ALLEGRO_ALIGN_CENTRE, od1->URL_page); 126 127 al_flip_display(); 128 129 std::cout<<al_cstr(od1->URL_page); 130 al_rest(0.9); 131 132 al_destroy_display(display); 133 delete od1; 134 135 return 0; 136}

SiegeLord
Member #7,827
October 2006
avatar

Well, as Matthew said, al_ustr_new uses strlen which computes the length of the string by looking for the first 0 character in that memory block. I'd guess that you don't write the trailing 0 when you save the .dat file, so strlen reports something much larger than what it should be.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Matthew Leverton
Supreme Loser
January 1999
avatar

Change it to:

char *buf = new char [len + 1];
buf[len] = 0;

You must null terminate each of your strings. (Or you can use al_ustr_new_from_buffer() for every case.)

SiegeLord
Member #7,827
October 2006
avatar

That won't help though since he does this (confirming my guess):

#SelectExpand
89al_fwrite(file, al_cstr(URL_page), al_ustr_size(URL_page));

And al_ustr_size doesn't include the trailing 0.

Just use the al_ustr_new_from_buffer and be done with it.

EDIT: Nvm, can't read.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Matthew Leverton
Supreme Loser
January 1999
avatar

SiegeLord said:

That won't help though since he does this (confirming my guess):

I'm talking about when he loads the strings.

But I'd use al_ustr_new_from_buffer() since it exists for this purpose.

AMCerasoli
Member #11,955
May 2010
avatar

Hmm, I get it now, I didn't know that al_ustr_new() was looking for the null character, but otherwise how would know when to stop?.

Yhea the best choice is to keep using al_ustr_new_from_buffer() since I'm already saving a flag into the file, otherwise I could save the string with the null character included, and read the file until I find that null charter and then move on with the rest, and I wouldn't need the len flag.

Go to: