Deutsch   English   Français   Italiano  
<uu9t5m$3ii8t$1@i2pn2.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder6.news.weretis.net!i2pn.org!i2pn2.org!.POSTED!not-for-mail
From: fir <fir@grunge.pl>
Newsgroups: comp.lang.c
Subject: bytes.c
Date: Sat, 30 Mar 2024 21:37:15 +0100
Organization: i2pn2 (i2pn.org)
Message-ID: <uu9t5m$3ii8t$1@i2pn2.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 30 Mar 2024 20:37:11 -0000 (UTC)
Injection-Info: i2pn2.org;
	logging-data="3754269"; mail-complaints-to="usenet@i2pn2.org";
	posting-account="+ydHcGjgSeBt3Wz3WTfKefUptpAWaXduqfw5xdfsuS0";
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:27.0) Gecko/20100101 Firefox/27.0 SeaMonkey/2.24
X-Spam-Checker-Version: SpamAssassin 4.0.0
Bytes: 3540
Lines: 108

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();

   }