You could use a linked list, and it would be relatively easy, but you could 
also use a pointer
array and malloc/free (str_dup and str_free in ROM).  Here's an incomplete 
example that
illustrates what I mean. Incomplete because I don't free memory (which you 
would have to do in
ROM, but I don't here since it is freed on exit), as well as other things such 
as when you
strings[] is full and you need to add the newest line.   To do this, freeing 
strings[0], set int
last_string to 0 (easier than freeing/mallocing all the strings down, and 
reallocing and storing
the newest string).  You 'wrap' through strings[20].  When you want to print 
the strings back
out, rather than just iterating through strings 0 to 19, you'd rather do 
(pseudocode):

'History' command:
if last_string = 19 then first_string = 0, else first_string = last_string + 1

' Examples
last_string = 5
first_string = 6
last_string = 19
first_string = 0

if (last_string <> 19)
    for (first_string; first_string < 20, first_string++)
        print strings on the right of last_string

for (first_string = 0; first_string < last_string; first_string++)
    print strings on the left

========
Here's an example of string copying that isn't ROM-based; in ROM it'd be 
easier--use str_dup and
str_free.  This fakes various gossip lines (well, words) as instead being 
command-line inputs :)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main (int argc, char *argv[])
{

    int args;
    char *strings[20];

    if (argc < 2)
    {
        printf("Need at least one argument to save!\n");
        exit(1);
    }

    for (args = 2; args <= argc; args++)
    {
        strings[args - 1] =  malloc( strlen(argv[args - 1] + 1 ));

        if (!strings[args - 1])
        {
            printf("Memory allocation failed.\n");
            exit(1);
        }

        strcpy(strings[args-1], argv[args-1]);
    }

    printf("Done copying strings; printing results:\n");
    for (args = 2; args <= argc; args++)
        printf("Argc: %d. Copied string: %s\n", args-1, strings[args-1]);

    return;
}

[EMAIL PROTECTED] jeremy $ time ./a.out hello bob my name
Done copying strings; printing results:
Argc: 1. Copied string: hello
Argc: 2. Copied string: bob
Argc: 3. Copied string: my
Argc: 4. Copied string: name

real    0m0.002s
user    0m0.000s
sys     0m0.000s


Hope you find this useful.

-- Jeremy

----- Original Message ----- 
From: "Robin Björklund" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, August 07, 2003 9:37 AM
Subject: storage fixed linked list


> Greetings,
>
> I'm currently trying to code something that'll store the last 20
> tells/gossips/whatever more channel we want.
> This history should be viewable through a command let's call it history.
> Syntax would be: history <channel>
> Anyway I first did this with arrays but I was told that it consumed lots of
> memory I was also told to take a
> look at linked lists. I've been taking a look at them for the last 3 hours.
> Though, linked lists doesn't have a
> size it seems, they keep growing, what would I do to make them just list the
> *last* 20 tells or last 20
> gossips. Would I remove the first node once I hit 21 nodes? Would I list the
> last 20 nodes? wouldn't that
> list be huge if I don't remove a node once I hit 21. Is there anyway to give
> a linked list a fixed size? Or
> am I just dreaming...
>
>
> -- 
> ROM mailing list
> [email protected]
> http://www.rom.org/cgi-bin/mailman/listinfo/rom


Reply via email to