Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

General string into circular buffer code

Status
Not open for further replies.

ElecDesigner

Member level 5
Joined
Jul 22, 2014
Messages
94
Helped
4
Reputation
8
Reaction score
5
Trophy points
1,288
Activity points
2,190
In C, what's the best way write a generalized function to get various strings into a circular character array buffer?

Is there a better (more efficient) way that looping one byte at a time, bearing in mind that we (may) have to deal with buffer wrap around halfway though the string.
 

You only need to create a header structure to store the pointer index and the String lenght.
Everything else consists of the queue control and monitoring mechanism.
 

Hello!
It very much depends on what you want to do.
Here are 2 examples:
-----------
0 This
1 is
2 an
3 example
-----------
This corresponds to your phrasing: circular character array buffer because it's a
buffer of (pointers to) character arrays.
This represents a circular buffer of 4 terms. Buf[0] = this. etc.... In this case.
Supposing this buffer effectively has 4 string spaces only, if you add another string,
Buf[0] (the string "This") will be replaced.

Now another representation of a "circular character array buffer" would be for example
char buf[32];
"This_is_an_example. "
NB: "_" means space, " " means 0.
This buffer also corresponds to your definition, it's a buffer containing a character array.
If you add a character, it will be appended at the end, and if you add from index 32, then
you will be back to buffer[0].

Dora.
 

Hi

If you add a character, it will be appended at the end,
in best case the new character is placed after the "." and the " " is shifted one position right.

Usual strings are delimited by a "\0".
But since the ring buffer has a read_pointer and a write_pointer it does not necessarily need the "\0" deleimiter.

To give more detailed answers we need to have more detailed informations about your problem:
* which microcontroller
* which language and compiler
* your code
* a detailed description of what you want to achieve

Klaus
 

Hello!

in best case the new character is placed after the "." and the " " is shifted one position right.

None of the circular buffers I used so far performed any shifting. Maybe we are not talking about the
same thing.

char buf[32];
uint8 pos
Let's rewrite the buffer similarily as above. But lets rewrite @ instead of the 0. We are talking here
of an array of characters, not a string. If we put "This is an example." in the buffer, then it becomse

[This_is_an_example.@@@@@@@@@@@@@]
pos = 19

If I then push "This is another one" into it, it becomes:

[er_ones_an_example.This_is_anoth]
pos = 6

But nothing is shifted.

Similarily, if it's an array of 6 pointers to string:

"This"
"is"
"an"
"example."
0
0

The above should be understood as pointer to strings, i.e. each component
is a pointer (4 bytes for instance on an STM32) to the memory location containing each string.

would become after adding "This is another one":

"another"
"one"
"an"
"example."
"This"
"is"

but there would be no shift, no copy either

Dora.
 

Hi,
"This_is_an_example. "
NB: "_" means space, " " means 0.
This buffer also corresponds to your definition, it's a buffer containing a character array.
If you add a character, it will be appended at the end, and if you add from index 32, then
you will be back to buffer[0].
"This_is_an_example. "
now add an "X"
the "X" is not simply added to: "This_is_an_example. X"
but it results in : "This_is_an_example.X " (mind: the position of "X" and " " is the other way round)
So the "X" will be set in the place of the " " and the initial " " now is one byte more right.
(like a simple keyboard buffer, for example)

Again: A usual ring buffer does not necessarily need a delimiter like '\0'
--> we need a more clear definition by the OP to give more focussed answers.


Klaus
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top