Discussion:
bytes.c
(too old to reply)
fir
2024-03-30 20:37:15 UTC
Permalink
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();

}
fir
2024-03-30 20:43:41 UTC
Permalink
Post by fir
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
you may say what methods may be handy as i may overlook some

- and note i wouldnt say people know that approach (method) in c,
if they would there would be such knotainers famous (well known) imo
as their are handy tools for c coding imo 0 increasing speed of coding
and readibility of codes also

Loading...