C programming: text strings

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
C programming: text strings
by on (#40407)
- An Allegro text box object is a pointer, and it requires a string or a buffer. Usually, I use malloc() for such text buffer, and sprintf() to input text.

- Is there another way? Example, a full disassembly requires a big buffer firstly.
Code:
//poor draft - current version:

char *text_string = malloc(0x2000);

//to input text
sprintf(text_string, "my text string\nand this is another line\nand one more\nagain...");

//the text box object
object[n].data = text_string;
do_dialog();

//======================
//poor draft - what I want to do:
char *text_string; //no buffer.

//to input text, somehow:
ADD_TEXT(text_string, "my text string\n");
ADD_TEXT(text_string, "and this is another line\n")
ADD_TEXT(text_string, "and one more\n");
ADD_TEXT(text_string, "again...");

//the text box object
object[n].data = text_string;
do_dialog();
Re: C programming: text strings
by on (#40409)
I'm not sure what exactly you're looking for but I will just give a comment regarding the code.

Instead of allocating the buffer in a different pointer then giving back the address to your object, wouldn't it be easier to just do:

Code:
//to input text
ADD_TEXT(object[n].data, "my text string\n");

do_dialog();


Unless text_string is defined elsewhere and you do some error handling to make sure the buffer is allocated I think it easier this way.

I'm no C expert but this is what I saw. If this is not what you meant then you will need to explain it differently what is "another way".

by on (#40410)
I edited my post a bit, to clarify the things.

by on (#40413)
I get almost no clarity. We will be able to help much better if you describe the situation and what you want to do.

What is object[n].data? If you want to build a string at run-time, you must have a buffer for it. Does object[n].data point to a buffer? If so, how big is it? If data is just a pointer that you must initialize, then you must allocate a buffer. It could be a local variable if it won't be used once the function exits, otherwise it must be dynamically allocated (or static, but that's just wasting memory all the time).

by on (#40414)
So what you want is some kind of string builder I guess? I know there is some class in java and C# but nothing like that in C unless you build it yourself ( I could be wrong, didn't do real C for a loooong time).

- You want to be able easily add strings to a buffer, to concatenate them together
- You want that method to manipulate the allocation of memory automatically

so your ADD_TEXT method would manage the memory if the buffer is not enough and add the next string at the end of it.

Am I close to what you're looking for?

by on (#40420)
blargg wrote:
I get almost no clarity. We will be able to help much better if you describe the situation and what you want to do.


- Think object[] as a structure within functions callbacks for GUI and X,Y values. One of these .members is a text box - a pointer to a string.

blargg wrote:
If you want to build a string at run-time, you must have a buffer for it. Does object[n].data point to a buffer?


Yes, it points to a text buffer. That's why I put a malloc() as example. I use char for text.

blargg wrote:
If so, how big is it? If data is just a pointer that you must initialize, then you must allocate a buffer. It could be a local variable if it won't be used once the function exits, otherwise it must be dynamically allocated (or static, but that's just wasting memory all the time).


It depends of what I want to do. For a disassembly, I estimate a buffer of 0x20000 bytes. Then, I use a lot of sprintf() to insert lines of text into it. In the end, the object[] points to this buffer and displays on-screen.

Banshaku wrote:
So what you want is some kind of string builder I guess?


That's the idea, but I have no clue if it's possible without a buffer. We can do like:

Code:
char *text = "This is my string"


but for larger strings, a memory buffer is required:

Code:
char *text = malloc(0x2000);
sprintf(text, "This is my string\n");


Quote:
- You want that method to manipulate the allocation of memory automatically


Here's the problem: I always have to allocate some amount of memory to be used as text buffer. I can't concatenate strings, like:

Code:
char *text = "this is" + "an example" + "of multiple strings";


Quote:
so your ADD_TEXT method would manage the memory if the buffer is not enough and add the next string at the end of it.


If there's a buffer, ADD_TEXT is sprintf().

by on (#40421)
Fx3 wrote:
That's the idea, but I have no clue if it's possible without a buffer.


Without a buffer it is not possible.

Fx3 wrote:
Here's the problem: I always have to allocate some amount of memory to be used as text buffer. I can't concatenate strings, like:

Code:
char *text = "this is" + "an example" + "of multiple strings";



What you want to do is exactly what you can do in languages like C# or java: It cannot be done in C.

What I will say is mostly theoretical since I didn't do C for a long time (and other peoples may correct me after) but I guess that you should be able to get the idea.

You will need a structure that will contain a pointer to a string, the current size of the buffer used and the maximum size.

Code:
typedef struct {
    char *myString;
    int currentSize;
    int maxSize;
} StringBuffer;


Then You will have a method, hmm, let say addToStringBuffer that knows how to add string to it.

Code:
   int addToStringBuffer(char *toAdd, StringBuffer sb)


The int return value is only to tell your program if the method succeeded or not.

First, you can init the buffer with 2k or smaller and write this value in max size. Then you call the method to add to your buffer. The currentSize will tell you where to write the next string inside the buffer and how much of the buffer is used.

Before writting to the buffer with sprintf, you much check if currentSize + len of string is smaller than max size.

If you reach the max size of the buffer, you must realloc and extra 2k of memory, reset the max size and then you can write inside the buffer. This way, the buffer will grow only when required.

I hope this idea will help you. I can elaborate more if required but I guess this should be enough to get you going. I miss doing C. Hope do to some more again some day.

by on (#40422)
No way then... OK.
Thanks for the help. ^_^;;