Sujet : improve that function (bytes_dig_int) De : fir (at) *nospam* grunge.pl (fir) Groupes :comp.lang.c Date : 03. Apr 2024, 21:04:37 Autres entêtes Organisation : i2pn2 (i2pn.org) Message-ID :<uukcop$3vhcj$1@i2pn2.org> User-Agent : Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
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