parsing a to big string into 20char each - Unix

This is a discussion on parsing a to big string into 20char each - Unix ; Hi, I need to have a simple but fast way of parsing a string. My string for example is 80chars long. Because the string has to fit into an event box, and the event box is 20 chars wide, i ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: parsing a to big string into 20char each

  1. parsing a to big string into 20char each

    Hi,
    I need to have a simple but fast way of parsing a string. My string
    for example is 80chars long. Because the string has to fit into an
    event box, and the event box is 20 chars wide,
    i would like it to create a new event for every 20chars passed.
    Essentially, i want to create events of 20chars each.

    i am working with a pointer to char.
    I thought of something like this:

    char 20charevent[20];

    if(strlen(event)>20) {
    while(*event!='\0') {// or while(event[incrementer]!='\0')
    20charevent[i++]=event++;

    if(i==20) print_event();
    }
    }

    but it does not work properly. Is my while statement faulty? the event
    is null terminated.


  2. Re: parsing a to big string into 20char each

    Alef.Veld@gmail.com wrote On 04/13/07 17:27,:
    > Hi,
    > I need to have a simple but fast way of parsing a string. My string
    > for example is 80chars long. Because the string has to fit into an
    > event box, and the event box is 20 chars wide,
    > i would like it to create a new event for every 20chars passed.
    > Essentially, i want to create events of 20chars each.
    >
    > i am working with a pointer to char.
    > I thought of something like this:
    >
    > char 20charevent[20];
    >
    > if(strlen(event)>20) {
    > while(*event!='\0') {// or while(event[incrementer]!='\0')
    > 20charevent[i++]=event++;
    >
    > if(i==20) print_event();
    > }
    > }
    >
    > but it does not work properly. Is my while statement faulty? the event
    > is null terminated.


    Alef, you have asked an ineffective question. You
    have not provided enough context (what is the starting
    value of `i', for example?), you have not explained what
    is improper about the way the code behaves, and you have
    not even provided the actual code! Any diagnoses you get
    will therefore be tentative at best, and may be completely
    off the mark (they may address problems in the posted code
    that are not present in the actual code, and overlook the
    actual code's problems not visible in the posting).

    That said, a few points are fairly obvious:

    - `20charevent' is not a legal identifier, so the
    array declaration will not compile.

    - As I mentioned, you haven't shown how `i' gets its
    initial value. If `i' is 9900 or -42 or even 12,
    the code cannot be expected to "work properly."

    - `event' is a `char*' (because strlen() accepts it),
    but `20charevent' is an array of `char'. You cannot
    store a pointer value in a char variable, so the
    assignment statement will not compile.

    - You have not shown what happens to `i' after
    print_event() is called. Does it get reset to zero,
    or does it just march onward through 21 and beyond
    as the loop keeps going?

    - You have not shown us the workings of print_event().
    For example, if print_event() expects to find a
    null-terminated string in `20charevent', it will be
    disappointed.

    - What is supposed to happen if strlen(event) is 35?
    Assuming `i' starts out as zero, the code as shown
    will copy the first 20 characters to `20charevent'
    and then call print_event(), then copy the final 15
    characters to -- well, to "somewhere" is about the
    best we can say -- and then the loop will finish
    without calling print_event() a second time. Is
    that what you want?

    A programmer must learn to describe things precisely
    (that is the essence of writing code, after all), and
    the precision must extend to the questions he asks. Learn
    how to ask good questions!

    --
    Eric.Sosman@sun.com

  3. Re: parsing a to big string into 20char each


    >
    > Alef, you have asked an ineffective question. You
    > have not provided enough context (what is the starting
    > value of `i', for example?), you have not explained what
    > is improper about the way the code behaves, and you have
    > not even provided the actual code! Any diagnoses you get
    > will therefore be tentative at best, and may be completely
    > off the mark (they may address problems in the posted code
    > that are not present in the actual code, and overlook the
    > actual code's problems not visible in the posting).
    >
    > That said, a few points are fairly obvious:
    >
    > - `20charevent' is not a legal identifier, so the
    > array declaration will not compile.
    >
    > - As I mentioned, you haven't shown how `i' gets its
    > initial value. If `i' is 9900 or -42 or even 12,
    > the code cannot be expected to "work properly."
    >
    > - `event' is a `char*' (because strlen() accepts it),
    > but `20charevent' is an array of `char'. You cannot
    > store a pointer value in a char variable, so the
    > assignment statement will not compile.
    >
    > - You have not shown what happens to `i' after
    > print_event() is called. Does it get reset to zero,
    > or does it just march onward through 21 and beyond
    > as the loop keeps going?
    >
    > - You have not shown us the workings of print_event().
    > For example, if print_event() expects to find a
    > null-terminated string in `20charevent', it will be
    > disappointed.
    >
    > - What is supposed to happen if strlen(event) is 35?
    > Assuming `i' starts out as zero, the code as shown
    > will copy the first 20 characters to `20charevent'
    > and then call print_event(), then copy the final 15
    > characters to -- well, to "somewhere" is about the
    > best we can say -- and then the loop will finish
    > without calling print_event() a second time. Is
    > that what you want?
    >
    > A programmer must learn to describe things precisely
    > (that is the essence of writing code, after all), and
    > the precision must extend to the questions he asks. Learn
    > how to ask good questions!
    >
    > --
    > Eric.Sos...@sun.com


    Well, apart from your certainly not untrue points, my problem was
    stated quite clearly, at least, i thought.
    I did not go into specifics, and used semi-prototype code, because i
    thought the problem would be obvious.
    If the problem would not have been, i would have recognized this and
    provided more specifics. The lack of
    and certain errors only are there because i did not very much care to
    write out the concept [which is a simple one]
    because i already stated it :

    I want to divide a string into strings of 20 characters each.

    It is and was not meant as a lack of disrespect to possible people who
    would reply. I simply do not care out to write
    the specifics of a simple concept. That is just silly. I just need a
    good algorithm or way to do it. It seems wrong
    practice to give to much detail when only a type of implementation is
    required, or a algorithm. The fact that i used
    function names which names which very much signify pseudocode, you
    might have also understood that not all
    specifics or variables were filled in or accounted for.

    Then again, i might be completely mistaken, in which case i apologize
    in advance.

    Rgds,
    Alef


  4. Re: parsing a to big string into 20char each

    Alef.Veld@gmail.com wrote:
    > Well, apart from your certainly not untrue points, my problem was
    > stated quite clearly, at least, i thought.


    Actually, you didn't. I also read your post had no idea what you
    meant to do, especially since your "code" was so horribly broken
    that it made things just even more unclear. Please don't forget
    that other people can't read your mind and what you meant to say
    after having spend quite some time trying to come up with a so-
    lution can be hard to figure out when you only present some re-
    sults. Moreover, expressing a problem clearly is often much more
    than 50% of the way to a solution - I often find that when I am
    stuck I actually didn't understand the problem I thought I was
    trying to solve.

    > I simply do not care out to write
    > the specifics of a simple concept. That is just silly. I just need a
    > good algorithm or way to do it. It seems wrong
    > practice to give to much detail when only a type of implementation is
    > required, or a algorithm.


    Well, you need at least to specify clearly what you want to do.
    And I would disagree that details are unimportant, some pesky
    details often dictate which method must be used.

    > I want to divide a string into strings of 20 characters each.


    That's clearer but has nothing to do with e.g. "parsing" (which
    is what the subject of your post says) or "events" (whatever that
    is). If that's all then perhaps something like the following will
    do:

    #define SHORT_LENGTH 20

    char long_string[ 500 ] = "foobaradjakdjakljd......adkajdlasdjal";
    char *in_long_string = long_string;
    char short_string[ SHORT_LENGTH + 1 ];
    size_t len = strlen( long_string );

    while ( 1 ) {
    strncpy( short_string, in_long_string, SHORT_LENGTH );
    short_string[ SHORT_LENGTH ] = '\0';
    printf( "%s\n", short_string );
    if ( len <= SHORT_LENGTH )
    break;
    len -= SHORT_LENGTH;
    in_long_string += SHORT_LENGTH;
    }

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  5. Re: parsing a to big string into 20char each

    Alef.Veld@gmail.com wrote:
    > I want to divide a string into strings of 20 characters each.


    As Eric already told you, more information is really required.

    > I simply do not care out to write the specifics of a simple concept.


    If someone asks for specifics, you really should just reply. There is a reason
    that they are asking. They don't know what you are trying to do, and after all,
    they are taking the time to help you.

    I can give you an algorithm, but I don't know whether it suits your purpose.

    read up to 20 characters
    has end of string been reached?
    - Yes - have you read any characters?
    - yes - process what is left in the buffer
    - no - do nothing end of string was reached before anything was read
    process the characters that you have
    start again

    Regards,

    Mark.

    --
    Mark Hobley
    393 Quinton Road West
    QUINTON
    Birmingham
    B32 1QE

    Telephone: (0121) 247 1596
    International: 0044 121 247 1596

    Email: markhobley at hotpop dot donottypethisbit com

    http://markhobley.yi.org/


  6. Re: parsing a to big string into 20char each

    On Apr 14, 9:08 pm, markhob...@hotpop.deletethisbit.com (Mark Hobley)
    wrote:
    > Alef.V...@gmail.com wrote:
    > > I want to divide a string into strings of 20 characters each.

    >
    > As Eric already told you, more information is really required.
    >
    > > I simply do not care out to write the specifics of a simple concept.

    >
    > If someone asks for specifics, you really should just reply. There is a reason
    > that they are asking. They don't know what you are trying to do, and after all,
    > they are taking the time to help you.
    >
    > I can give you an algorithm, but I don't know whether it suits your purpose.
    >
    > read up to 20 characters
    > has end of string been reached?
    > - Yes - have you read any characters?
    > - yes - process what is left in the buffer
    > - no - do nothing end of string was reached before anything was read
    > process the characters that you have
    > start again
    >
    > Regards,
    >
    > Mark.
    >
    > --
    > Mark Hobley
    > 393 Quinton Road West
    > QUINTON
    > Birmingham
    > B32 1QE
    >
    > Telephone: (0121) 247 1596
    > International: 0044 121 247 1596
    >
    > Email: markhobley at hotpop dot donottypethisbit com
    >
    > http://markhobley.yi.org/


    Alright, seeing 3 people already told me i'm wrong i apologize for not
    being more clear.
    Thank you for the code and suggestions. I'll try to be more detailed
    in my question in
    the future.

    Regards,
    Alef


  7. Re: parsing a to big string into 20char each

    Ok, i now have this, but the first event it displays is always the
    second 20char string, instead of the first if you know what i mean:

    EVENT_LENGTH=85 chars
    event_string[EVENT_LENGTH]
    event_ptr is a pointer to the original pointer i do not wish to modify
    renderbitmapfont is a function that renders the text onto screen
    with event i mean a message i want to put on screen
    the tmp pointer i'm using is a linked list in my program to obtain the
    messages (events)

    event_ptr=tmp->event;
    len=strlen(event_ptr);
    while(1) {
    strncpy(event_string,event_ptr,EVENT_LENGTH);
    event_string[EVENT_LENGTH]='\0';
    if(strlen(event_ptr)==strlen(tmp->event))
    renderbitmapfont(x_event,y_event+=x_event,0,"%s:%s:%s",tmp-
    >timestamp,tmp->hostname,event_string);

    else renderbitmapfont(x_event,y_event+=x_event,0,"%s",event_string);
    if(len<=EVENT_LENGTH)
    break;
    len-=EVENT_LENGTH;
    event_ptr+=EVENT_LENGTH;
    }


  8. Re: parsing a to big string into 20char each

    Alef.Veld@gmail.com wrote:
    > Ok, i now have this, but the first event it displays is always the
    > second 20char string, instead of the first if you know what i mean:


    No, I don't know what you mean. What is '20char'? Nothing in thr rest
    of your post gives any clue what that means.

    > EVENT_LENGTH=85 chars
    > event_string[EVENT_LENGTH]
    > event_ptr is a pointer to the original pointer i do not wish to modify


    'event_ptr' is a copy of 'tmp->pointer' (obviously pointer to a string)
    and not a pointer to a pointer.

    > renderbitmapfont is a function that renders the text onto screen
    > with event i mean a message i want to put on screen
    > the tmp pointer i'm using is a linked list in my program to obtain the
    > messages (events)


    - Alef, there's no shortage of spaces at the moment, so use them while
    they're still cheap. Put them at least around operators, between func-
    tion arguments etc.>

    > event_ptr = tmp->event;
    > len = strlen(event_ptr);
    > while (1) {
    > strncpy(event_string, event_ptr, EVENT_LENGTH);
    > event_string[EVENT_LENGTH] = '\0';
    > if (strlen(event_ptr) == strlen(tmp->event))


    Ok, this will only be true for the very first time you're through the
    loop (I think it would make more sense to keep a counter and compare
    to that instead of calling strlen() twice each time round - not only
    for reasons of efficiency but also because it would make your inten-
    tions clearer).

    > renderbitmapfont(x_event, y_event += x_event, 0, "%s:%s:%s",
    > tmp->timestamp, tmp->hostname, event_string);


    Since you seem to be missing the output from this function call my
    first guess is that you start with a value of 'y_event' that's not
    correct and the function renders your "event" somewhere off-screen.
    Or perhaps 'tmp->timestamp' or 'tmp->hostname' are not valid string
    pointers and your function detects this and doesn't output anything
    in that case. But that's hard to say since I haave no idea that this
    renderbitmapfont() function is supposed to do.

    > else
    > renderbitmapfont(x_event, y_event += x_event, 0,

    "%s", event_string);
    > if (len <= EVENT_LENGTH)
    > break;
    > len -= EVENT_LENGTH;
    > event_ptr += EVENT_LENGTH;
    > }

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

+ Reply to Thread