Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » string::resize and string::max_size

This thread is locked; no one can reply to it. rss feed Print
 1   2 
string::resize and string::max_size
julian_boolean
Member #8,201
January 2007

Does anyone know how to use either of this in a function to set the max length of a string?

Thomas Fjellstrom
Member #476
June 2000
avatar

I don't think you can besides subclassing and implementing max_size yourself. And thats only if the base class bothers caring about max_size.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

TeamTerradactyl
Member #7,733
September 2006
avatar

Thomas Fjellstrom
Member #476
June 2000
avatar

Well, I guess it depends if he wants a HARD max limit, or just wants to reserve some space.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

julian_boolean
Member #8,201
January 2007

If reserve() works by crashing the program, it works pretty good.

void textfield::set_length(int l)
{
  str.reserve(l);
}

What am I doing wrong. :P I figured I might have to do it myself, but can't think of how. Maybe by using clear_keybuf(), but that thing is just a pain in the ass.

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

What am I doing wrong.

Probably using it at all. :) It's not like it affects the string's behavior in any way.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

julian_boolean
Member #8,201
January 2007

What I had before was:

void textfield::set_length(int l)
{
  length = l

  if(str.length() >= length)
  {
    clear_keybuf();
  }
}

It worked up until it got to what was in int l.

Matthew Dalrymple
Member #7,922
October 2006
avatar

reserve() is meant for situations where you know the total size of the STL object you are working with, and in the process of getting to that point you would normally be reallocating memory lots of times. For example: concatenating a string one letter at a time when you know the word is going to be 15 letters long. You would use reserve so it would allocate the memory all at one time. At least that is what I have taken from it.

Quote:

It worked up until it got to what was in int l.

So do you mean doing this would fix it:

if(str.length() > length)

I'd like to see more code to see why reserve is crashing. The full class and how it is being used would be nice. 8-)

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

http://www.allegro.cc/forums/thread/589667 I think I have it posted in there.

Matthew Dalrymple
Member #7,922
October 2006
avatar

Alright, well did my little fix make your program work correctly? (The greaterthan or equals to just a greaterthan)

I didn't see much code in that post but oh well.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

I really don't know what I'm doing with this. All the clear_keybuf is doing is preventing me from inputting, indefinitely (can't backspace.) I was trying to find another way to do it without using it.

Matthew Dalrymple
Member #7,922
October 2006
avatar

Maybe structure this like so... (this is more pseudo-code then anything)

1string input;
2const int MAX_INPUT = 1024;
3while(!key[KEY_ENTER] || BUTTON_OK.isPressed())
4{
5 if(input.size() < MAX_INPUT)
6 {
7 // never really worked with this yet so you fill in the syntax
8 // also need to check to see if the key is a viewable character
9 input += read_key();
10 }
11 if(key[KEY_BACKSPACE])
12 {
13 // string doesn't have a pop method so you're going to have to create this on your own
14 input.pop();
15 }
16}

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

Ok! This sorta works..

1void textfield::set_length(int l)
2{
3 length = l
4 
5 if(edittext.length() >= length)
6 {
7 clear_keybuf();
8 }
9}
10 
11void textfield::poll()
12{
13 if(is_active)
14 {
15 while(keypressed())
16 {
17 int newkey = readkey();
18 char ASCII = newkey & 0xff;
19 char scancode = newkey >> 8;
20 
21 if(ASCII >= 32 && ASCII <= 126)
22 {
23 if(insert || iter == edittext.end())
24 iter = edittext.insert(iter, ASCII);
25
26 else
27 edittext.replace(caret, 1, 1, ASCII);
28 caret++;
29 iter++;
30 }
31 
32 else
33 switch(scancode)
34 {
35 case KEY_DEL:
36 if(iter != edittext.end()) iter = edittext.erase(iter);
37 break;
38 
39 case KEY_BACKSPACE:
40 if(iter != edittext.begin())
41 {
42 caret--;
43 iter--;
44 iter = edittext.erase(iter);
45 if(edittext.length() >= length) readkey(); // <- This
46 }break;
47
48 case KEY_RIGHT:
49 if(iter != edittext.end()) caret++, iter++;
50 break;
51
52 case KEY_LEFT:
53 if(iter != edittext.begin()) caret--, iter--;
54 break;
55
56 case KEY_INSERT:
57 insert = !insert;
58 break;
59 
60 default:
61 break;
62 }
63 }
64 }
65}

For the most part, it seems a little buggy though.

Matthew Dalrymple
Member #7,922
October 2006
avatar

What specifically seems to be buggy with it?

For future reference

else
  edittext.replace(caret, 1, 1, ASCII);
  caret++;
  iter++;

I believe that else statement will only work for the next line down without braces. If thats true then you should un-indent those next two lines for better readability.

If you meant for all three of those to statements to go only on the else case then use the {}'s.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

I didn't write the code for text input but it seems to work well, 100 x better then gstream anyway.

It's kinda hard to explain, once you go beyond the max amount of characters it looks like it's laggy almost, but you can still backspace.. It's just difficult sometimes (have to hold in backspace or press it a bunch of times.)

Is there anyway to say something like:

if(str.length() >= length)
{
  clear_keybuf();
}

else
  do_not_clear_keybuf();

Kauhiz
Member #4,798
July 2004

Err... It doesn't clear the keybuffer unless you explicitly tell it to :P. What do you mean by do_not_clear_keybuf()? A function that does nothing?

Quote:

It's just difficult sometimes (have to hold in backspace or press it a bunch of times.)

I haven't looked at your code at all, but try it without the call to clear_keybuf().

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

julian_boolean
Member #8,201
January 2007

Well, just something I can use to make it stop clear_keybuf(). I tried to find different things but nothing seems to work very well.. Someone suggested just to make the text code stop polling whenever you reach the max text limit, which I CAN do though I just want to find a different way.

Kauhiz
Member #4,798
July 2004

Quote:

make it stop clear_keybuf()

Don't call clear_keybuf(), maybe?

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

julian_boolean
Member #8,201
January 2007

How can I prevent the user from inputting text beyond the max limit then?

Kauhiz
Member #4,798
July 2004

If the max limit has been reached, ignore them.

while(keypressed() && text.length() < max_length)
{
  // find out what key the user pressed
}

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Matthew Dalrymple
Member #7,922
October 2006
avatar

julian_boolean said:

How can I prevent the user from inputting text beyond the max limit then?

Did you look at my pseudo-code? It will just skip the line of reading key input if the max has been reached. But it still allows for key input.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

julian_boolean
Member #8,201
January 2007

I didn't really know what you mean't by a pop function or what it does exactly.

Edit:

while(keypressed() && text.length() < max_length)
{
  // find out what key the user pressed
}

I was thinking that awhile ago, but wouldn't that boot me out of the while loop? How would I be able to backspace?

Kauhiz
Member #4,798
July 2004

Ok, so put it in an if statement inside the while loop, and put the backspace check outside that if statement. Think before you post! ;)

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

julian_boolean
Member #8,201
January 2007

Sorry :P sometimes my head isn't screwed on properly. That seems to work very, VERY nicely.. I guess as long as I'm not getting any errors, and as long as the program isn't acting funky and/or crashing it'll be fine.

Matthew Dalrymple
Member #7,922
October 2006
avatar

Quote:

I didn't really know what you mean't by a pop function

It's just a term to refer to removing/returning something on the end of a stack/queue.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

 1   2 


Go to: