How to interleave 2 binary files into one using the Interleave tool ?

Started by LuizMelos, May 01, 2018, 04:52:07 AM

Previous topic - Next topic

LuizMelos

How to interleave 2 rom files into one using the Interleave tool ?

This is the Interleave.cpp file.

/*
   interleave.cpp
   
   Interleave two or more files

   Build:
      g++ -mno-cygwin -o interleave interleave.cpp

*/

/*
* Includes
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>

/*
*
*/
int main(int argc, char *argv[])
{
   if (argc < 3)
   {
      fprintf(stderr, "interleave\n");
      fprintf(stderr, "Syntax: interleave file-out size file-in file-in...\n");
      return -1;
   }
   
   FILE *fo = fopen(argv[1], "wb");
   if (!fo) { fprintf(stderr, "Error opening output file!\n"); return 1; }
   
   int bufsize = strtoul(argv[2],0,0);
   unsigned char *buffer = new unsigned char[bufsize];

   FILE *fi[32];
   int files = 0;
   for (int a=3; argv[a]; a++)
   {
      fi[files] = fopen(argv[a], "rb");
      if (!fi[files]) { fprintf(stderr, "Error opening input file!\n"); return 1; }
      files++;
   }

   int files_active = files;
   for (int f=0; files_active; f++)
   {
      if (f == files) f = 0;
      int read = 0;
      if (fi[f])
      {
         read = fread(buffer, 1, bufsize, fi[f]);
         if (read <= 0) { fclose(fi[f]); fi[f] = 0; files_active--; continue; }
         fwrite(buffer, 1, read, fo);
      }
      //if (bufsize > read)
      {
         memset(buffer, 0, bufsize - read);
         fwrite(buffer, 1, bufsize - read, fo);
      }
   }
   
   fclose(fo);
   
   delete [] buffer;
   
   return 0;
}

KingMike

I'm not sure how you would benefit from interleaving two ROMs together but okay...
"My watch says 30 chickens" Google, 2018

LuizMelos

How to interleave 2 binary files into one using the Interleave tool ?

Code (interleave.cpp) Select


// This is the Interleave.cpp file.

/*
    interleave.cpp

    Interleave two or more files

   Build:
      g++ -mno-cygwin -o interleave interleave.cpp

*/


/*
* Includes
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>

/*
*
*/
int main(int argc, char *argv[])
{
   if (argc < 3)
   {
      fprintf(stderr, "interleave\n");
      fprintf(stderr, "Syntax: interleave file-out size file-in file-in...\n");
      return -1;
   }
   
   FILE *fo = fopen(argv[1], "wb");
   if (!fo) { fprintf(stderr, "Error opening output file!\n"); return 1; }
   
   int bufsize = strtoul(argv[2],0,0);
   unsigned char *buffer = new unsigned char[bufsize];

   FILE *fi[32];
   int files = 0;
   for (int a=3; argv[a]; a++)
   {
      fi[files] = fopen(argv[a], "rb");
      if (!fi[files]) { fprintf(stderr, "Error opening input file!\n"); return 1; }
      files++;
   }

   int files_active = files;
   for (int f=0; files_active; f++)
   {
      if (f == files) f = 0;
      int read = 0;
      if (fi[f])
      {
         read = fread(buffer, 1, bufsize, fi[f]);
         if (read <= 0) { fclose(fi[f]); fi[f] = 0; files_active--; continue; }
         fwrite(buffer, 1, read, fo);
      }
      //if (bufsize > read)
      {
         memset(buffer, 0, bufsize - read);
         fwrite(buffer, 1, bufsize - read, fo);
      }
   }
   
   fclose(fo);
   
   delete [] buffer;
   
   return 0;
}

MathOnNapkins


KingMike

"My watch says 30 chickens" Google, 2018