Sona 0.50 Source

Sona 0.50/tools/eif2spat/eif2spat.c

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define ERRORMSG_USAGE     "Usage: %s input.eif output.spat\n"
#define ERRORMSG_INOPEN    "Error: can't open Echo FM instrument\"%s\"\n"
#define ERRORMSG_INREAD    "Error: can't read Echo FM instrument \"%s\"\n"
#define ERRORMSG_OUTOPEN   "Error: can't create Sona FM instrument\"%s\"\n"
#define ERRORMSG_OUTWRITE  "Error: can't write Sona FM instrument \"%s\"\n"

int main(int argc, char **argv)
{
   // Check arguments
   if (argc != 3) {
      fprintf(stderr, ERRORMSG_USAGE, argv[0]);
      return EXIT_FAILURE;
   }
   const char *in_filename = argv[1];
   const char *out_filename = argv[2];
   
   // Open files
   FILE *in_file = fopen(in_filename, "rb");
   if (in_file == NULL) {
      fprintf(stderr, ERRORMSG_INOPEN, in_filename);
      return EXIT_FAILURE;
   }
   FILE *out_file = fopen(out_filename, "wb");
   if (out_file == NULL) {
      fprintf(stderr, ERRORMSG_OUTOPEN, out_filename);
      fclose(in_file);
      return EXIT_FAILURE;
   }
   
   // SonaPatch files are pretty much EIF files padded with zeroes to 32
   // bytes (at least until extensions are made that use them), so let's
   // do that
   uint8_t buffer[32] = { 0 };
   
   if (fread(buffer, 1, 29, in_file) != 29) {
      fprintf(stderr, ERRORMSG_INREAD, in_filename);
      fclose(in_file);
      fclose(out_file);
      return EXIT_FAILURE;
   }
   
   if (fwrite(buffer, 1, 32, out_file) != 32) {
      fprintf(stderr, ERRORMSG_OUTWRITE, out_filename);
      fclose(in_file);
      fclose(out_file);
      return EXIT_FAILURE;
   }
   
   // We're done
   fclose(in_file);
   fclose(out_file);
   return EXIT_SUCCESS;
}