Sujet : bytes.c De : fir (at) *nospam* grunge.pl (fir) Groupes :comp.lang.c Date : 30. Mar 2024, 21:37:15 Autres entêtes Organisation : i2pn2 (i2pn.org) Message-ID :<uu9t5m$3ii8t$1@i2pn2.org> User-Agent : Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
it is not bad idea to write bytes.c file having methods for this microcontainer (same as ints.c floats.c literals.c etc) (all as a part of sickle.c c library) the idea seems good becouse back then i was writing methods for "chunk", and "chybaks" and it was not celar what it should contain - now as bytes.c are simpler the answer what to put there is simpler and thats good (sadly im feeling old and yet few years ago if i get that idea i found it very interesting to work on thit right now i know its interesting but not find it so much interesting on 'physical level' as i get tired and bored of everything (more tired and rotten than bored in fact) few initial methods on this how it would look like #include <sys/stat.h> int GetFileSize2(char *filename) { struct stat st; if (stat(filename, &st)==0) return (int) st.st_size; ERROR_EXIT("error obtaining file size for %s", filename); return -1; } ///////////////bytes container unsigned char* bytes = NULL; int bytes_size = 0; int bytes_allocked = 0; void bytes_add_(unsigned char val) { (bytes=(unsigned char*)realloc(bytes,++bytes_size*sizeof(unsigned char)))[bytes_size-1]=val; } char* bytes_resize(int size) { bytes_size=size; if((bytes_size+100)*2<bytes_allocked | bytes_size>bytes_allocked) return bytes=(unsigned char*)realloc(bytes, (bytes_allocked=(bytes_size+100)*2)*sizeof(unsigned char)); } void bytes_add(unsigned char val) { if(++bytes_size>bytes_allocked) bytes=(unsigned char*)realloc(bytes, (bytes_allocked=(bytes_size+100)*2)*sizeof(unsigned char)); bytes[bytes_size-1]=val; return; } ////////////////// void bytes_load(char* name) { int flen = GetFileSize2(name); FILE *f = fopen(name, "rb"); if(!f) ERROR_EXIT( "errot: cannot open file %s ", name); int loaded = fread(bytes_resize(flen), 1, flen, f); fclose(f); } void bytes_save(char* name) { FILE* f =fopen(name, "wb"); int saved = fwrite (bytes , 1, bytes_size, f); fclose (f); } //////////////// void bytes_dump_in_hex() { for(int i=0; i<bytes_size; i++) { if(!(i%16)) printf("\n"); printf("%02x ", bytes[i]); } } void bytes_init(unsigned char (*init_predicate)(int ) ) { for(int i=0; i<bytes_size;i++) bytes[i] = init_predicate(i); } //////////////////// unsigned char ini2(int i) { return i&0xff; } void BytesTest() { bytes_resize(1000); bytes_init(ini2); bytes_dump_in_hex(); // bytes_resize(100); // bytes_dump_in_hex(); }