• improve that function (bytes_dig_int)

    From fir@3:633/280.2 to All on Thu Apr 4 07:04:37 2024
    it seems that this group likes such kind of topics, so
    here you got

    improve that function - i wrote it in quick and feel tired so i dont
    want to improve it as for now

    i got "list" of bytes (by list i mean pointer to ram area
    of elements - loke reallocked by reallock, by aray i would rather
    aybe name fully static array and heap seeds are more like lists
    especially if you use them as lists - and got methods like
    "add" (remove etc)

    unsigned char* bytes = NULL;
    int bytes_size = 0;
    int bytes_allocked = 0;
    int bytes_cursor = 0;

    i wint to write a function DigInt that "digs" one int form that list
    and returns its, then the "cursor" is set after that and you can call it
    again untill all ints are "digged"


    void SkipBytesTillNumberBegin()
    {
    for(;;)
    {
    if(bytes[bytes_cursor]==0) break;
    if(IsDigit(bytes[bytes_cursor])) break;
    if(bytes[bytes_cursor]=='-' & bytes_cursor+1<bytes_size & IsDigit(bytes[bytes_cursor+1])) break;
    bytes_cursor++;
    if(bytes_cursor==bytes_size) break;
    }
    }

    //it sets bytes_cursor on bytes_size+1 if not found
    int bytes_dig_int_from_cursor()
    {
    if(bytes_cursor>=bytes_size)
    { bytes_cursor = bytes_size +1; return 'none'; }

    for(;;)
    {
    SkipBytesTillNumberBegin();
    if(bytes_cursor>=bytes_size)
    { bytes_cursor = bytes_size +1; return 'none'; }

    unsigned char* h = &bytes[bytes_cursor];
    int value = strtol(h, &h, 10);

    if(h>&bytes[bytes_size]) ERROR_EXIT("\n*** ERROR of conversion in
    DigInt routine ");
    if(h==&bytes[bytes_cursor]) ERROR_EXIT("\n*** ERROR of conversion
    in DigInt routine ");
    if (errno) ERROR_EXIT("\n*** ERROR of conversion in DigInt
    routine ");

    bytes_cursor = (int)(h-bytes);

    return value;
    }

    return 0;
    }

    void BytesTest()
    {
    bytes_load("some.txt");

    while(bytes_cursor<bytes_size)
    {
    int value = bytes_dig_int_from_cursor();
    if(bytes_cursor>bytes_size) break;
    printf("\n %d", value);
    }
    }


    thic function isnt optimal by many means but im to tired to
    improve it by now but some discussion on this may be eventually fruitfull

    some.txt

    i - 3 04 -9 am 0001fir and -02fir -99and --3firfir4

    result

    3
    4
    -9
    1
    -2
    -99
    -3
    4


    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: i2pn2 (i2pn.org) (3:633/280.2@fidonet)
  • From fir@3:633/280.2 to All on Thu Apr 4 07:47:45 2024
    fir wrote:
    void BytesTest()
    {
    bytes_load("some.txt");

    while(bytes_cursor<bytes_size)
    {
    int value = bytes_dig_int_from_cursor();
    if(bytes_cursor>bytes_size) break;
    printf("\n %d", value);
    }
    }


    as to this part i was thinking that using the bytes_cursor
    (or how to call it better) would be good idea and would simplify
    the usage of it but this loop is also overcomp0-lex so im not sure
    if to using sorta global variable (like "none") wouldnt be better here

    thsi is odlschool idea and i dont seen its used today but here it
    is just simple

    for(;;)
    {
    int value = dig_int();
    if(none) break;

    }




    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: i2pn2 (i2pn.org) (3:633/280.2@fidonet)
  • From fir@3:633/280.2 to All on Thu Apr 4 08:50:37 2024
    fir wrote:
    fir wrote:
    void BytesTest()
    {
    bytes_load("some.txt");

    while(bytes_cursor<bytes_size)
    {
    int value = bytes_dig_int_from_cursor();
    if(bytes_cursor>bytes_size) break;
    printf("\n %d", value);
    }
    }


    as to this part i was thinking that using the bytes_cursor
    (or how to call it better) would be good idea and would simplify
    the usage of it but this loop is also overcomp0-lex so im not sure
    if to using sorta global variable (like "none") wouldnt be better here

    thsi is odlschool idea and i dont seen its used today but here it
    is just simple

    for(;;)
    {
    int value = dig_int();
    if(none) break;

    }


    so i revrited no wto use global variable (module variable - it should be rather named module variable
    than global variable as i once noted variables in modern c like on
    windows are not global but module scope - as programs are modules)


    int bytes_dig_int()
    {
    if(bytes_cursor>=bytes_size){ found =0; return 'none'; }

    for(;;)
    {
    SkipBytesTillNumberBegin();
    if(bytes_cursor>=bytes_size) { found =0; return 'none'; }

    unsigned char* h = &bytes[bytes_cursor];
    int value = strtol(h, &h, 10);

    if(h>&bytes[bytes_size]) ERROR_EXIT("\n*** ERROR in DigInt -
    strtol reached after bytes ");
    if(h==&bytes[bytes_cursor]) ERROR_EXIT("\n*** ERROR in DigInt -
    not a number ");
    if (errno) ERROR_EXIT("\n*** ERROR in DigInt - out of range ");

    bytes_cursor = (int)(h-bytes);

    found = 1;
    return value;
    }

    return 0;
    }

    it is not tested for potential errors though

    imo it is also cool fo write a procedure that finds goven
    name and then digs int right after that - such simple function
    then can be used to read settings from a text file and this is
    kinda fun as it assumes nothing about the file format
    do no ini no xml etc - you just find a string and take an int after that string and this is funny way (i get a little merry after
    noticing that) its also very easy to write

    int compare_bytes_at_to_str(int at, unsigned char* str, int len )
    {
    for(int i=0; i<len;i++)
    {
    if(bytes[at+i]!=str[i]) return 0;
    }
    return 1;
    }
    int bytes_find(char* str)
    {
    int len = StrLen(str);

    for(int i=bytes_cursor; i<bytes_size-len+1; i++)
    {
    if(compare_bytes_at_to_str(i, str, len))
    {
    bytes_cursor = i + len;
    found = 1;
    return i;
    }
    }
    found = 0;
    return -1;

    }


    int bytes_take_named_int_value(char* str)
    {
    bytes_find(str);
    if(!found) return 'none';
    int value = bytes_dig_int();
    if(found) return value;
    else return 'none';


    }






    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: i2pn2 (i2pn.org) (3:633/280.2@fidonet)
  • From Tim Rentsch@3:633/280.2 to All on Tue Apr 9 15:32:28 2024
    fir <fir@grunge.pl> writes:

    it seems that this group likes such kind of topics, so
    here you got

    improve that function - i wrote it in quick and feel tired so i
    dont want to improve it as for now

    i got "list" of bytes (by list i mean pointer to ram area of
    elements - loke reallocked by reallock, by aray i would rather
    aybe name fully static array and heap seeds are more like lists
    especially if you use them as lists - and got methods like "add"
    (remove etc)

    unsigned char* bytes = NULL;
    int bytes_size = 0;
    int bytes_allocked = 0;
    int bytes_cursor = 0;

    i wint to write a function DigInt that "digs" one int form that
    list and returns its, then the "cursor" is set after that and you
    can call it again untill all ints are "digged"

    [code]

    I usually prefer (and recommend) writing code that doesn't make
    use of global state, as for example:

    long
    next_value( char *s0, char **next_s ){
    char *s = s0 + strcspn( s0, "0123456789" );
    return
    !*s
    ? *next_s = s0, 0
    : strtol( s - (s!=s0 && s[-1]=='-'), next_s, 10 );
    }

    Retrieving and processing successive values can be done using
    this function by means of a simple for() loop, as illustrated by
    the code below:

    void
    print_longs( char *s ){
    long v;

    printf( " input: %s\n", s );

    for( char *next; v = next_value( s, &next ), s != next; s = next ){
    printf( " value: %12ld\n", v );
    }

    printf( "\n" );
    }

    Note that the local variable 'next' here takes the place of a
    cursor for the next input.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From fir@3:633/280.2 to All on Sat Apr 13 00:22:41 2024
    Tim Rentsch wrote:
    fir <fir@grunge.pl> writes:

    it seems that this group likes such kind of topics, so
    here you got

    improve that function - i wrote it in quick and feel tired so i
    dont want to improve it as for now

    i got "list" of bytes (by list i mean pointer to ram area of
    elements - loke reallocked by reallock, by aray i would rather
    aybe name fully static array and heap seeds are more like lists
    especially if you use them as lists - and got methods like "add"
    (remove etc)

    unsigned char* bytes = NULL;
    int bytes_size = 0;
    int bytes_allocked = 0;
    int bytes_cursor = 0;

    i wint to write a function DigInt that "digs" one int form that
    list and returns its, then the "cursor" is set after that and you
    can call it again untill all ints are "digged"

    [code]

    I usually prefer (and recommend) writing code that doesn't make
    use of global state, as for example:

    long
    next_value( char *s0, char **next_s ){
    char *s = s0 + strcspn( s0, "0123456789" );
    return
    !*s
    ? *next_s = s0, 0
    : strtol( s - (s!=s0 && s[-1]=='-'), next_s, 10 );
    }

    Retrieving and processing successive values can be done using
    this function by means of a simple for() loop, as illustrated by
    the code below:

    void
    print_longs( char *s ){
    long v;

    printf( " input: %s\n", s );

    for( char *next; v = next_value( s, &next ), s != next; s = next ){
    printf( " value: %12ld\n", v );
    }

    printf( "\n" );
    }

    Note that the local variable 'next' here takes the place of a
    cursor for the next input.


    well ok..this is kinda tricky style but thats a matter of personal style
    i prefer more "strightforward" long descriptive names etc

    two remarks here
    1) people shoudl avoid imo talking a word "global" becouse in
    normal desctop computing at least those variables are not global but
    typically belong to library (dll) so they are library scope/module
    scope not global (this is my own remark as i noticed this) (thay may
    also be exe scope but thsi is also module scope

    so global dont much exist, though there is this problem that when same
    symbol is uset in various libraries it may cause serious conflict

    2) as to avaidingf global state i also thing would not agree

    if the satae is "meaningfull" - like if variable name is really
    meanigfull for given module - it could be used (i mean it should
    be as meaningful as functions provided, than can be "global" (module
    scoe) too imo by analogy

    various "global" (id est module scope) arrays for exampel are quite meaningfull so no problem them being module scope (and dont listen
    to c++ ponys what they say imo, fuck them - in short - i would say ;c)

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: i2pn2 (i2pn.org) (3:633/280.2@fidonet)
  • From Tim Rentsch@3:633/280.2 to All on Sun Apr 14 00:44:12 2024
    fir <fir@grunge.pl> writes:

    Tim Rentsch wrote:

    fir <fir@grunge.pl> writes:

    it seems that this group likes such kind of topics, so
    here you got

    improve that function - i wrote it in quick and feel tired so i
    dont want to improve it as for now

    i got "list" of bytes (by list i mean pointer to ram area of
    elements - loke reallocked by reallock, by aray i would rather
    aybe name fully static array and heap seeds are more like lists
    especially if you use them as lists - and got methods like "add"
    (remove etc)

    unsigned char* bytes = NULL;
    int bytes_size = 0;
    int bytes_allocked = 0;
    int bytes_cursor = 0;

    i wint to write a function DigInt that "digs" one int form that
    list and returns its, then the "cursor" is set after that and you
    can call it again untill all ints are "digged"

    [code]

    I usually prefer (and recommend) writing code that doesn't make
    use of global state, as for example:

    long
    next_value( char *s0, char **next_s ){
    char *s = s0 + strcspn( s0, "0123456789" );
    return
    !*s
    ? *next_s = s0, 0
    : strtol( s - (s!=s0 && s[-1]=='-'), next_s, 10 );
    }

    Retrieving and processing successive values can be done using
    this function by means of a simple for() loop, as illustrated by
    the code below:

    void
    print_longs( char *s ){
    long v;

    printf( " input: %s\n", s );

    for( char *next; v = next_value( s, &next ), s != next; s = next ){ >> printf( " value: %12ld\n", v );
    }

    printf( "\n" );
    }

    Note that the local variable 'next' here takes the place of a
    cursor for the next input.

    well ok..this is kinda tricky style but thats a matter of personal
    style i prefer more "strightforward" long descriptive names etc

    The two aspects are related but my point was about the overall
    structure of the algorithm more than about matters of syntactic
    and lexical style.

    two remarks here
    1) people shoudl avoid imo talking a word "global" becouse in
    normal desctop computing at least those variables are not global
    but typically belong to library (dll) so they are library
    scope/module scope not global (this is my own remark as i noticed
    this) (thay may also be exe scope but thsi is also module scope
    [...]

    Yes the word global may have been misleading. What I meant
    was any object whose lifetime outlives the duration of the
    relevant function call(s). For example, in this function

    int
    whatever( char *s ){
    static int something;
    ...
    }

    the variable 'something' continues past the point where the
    function returns, even though its scope is confined to that of
    the function (and so from that point of view it is not "global").
    Unfortunately there isn't a common and convenient term that
    conveys this meaning. Whatever it is we call it, this "duration
    past the point of function return" is the property I was meaning
    to identify and that in most cases should be avoided.

    --- MBSE BBS v1.0.8.4 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)