bin2h.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * bin to header - Part of The peTI-NESulator Project
  3. * bin2h.c: Convert a binary file to a table of byte in a C header file.
  4. *
  5. * Created by Manoel Trapier.
  6. * Copyright 2003-2008 986 Corp. All rights reserved.
  7. *
  8. * $LastChangedDate$
  9. * $Author$
  10. * $HeadURL$
  11. * $Revision$
  12. *
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. int main(int argc, char *argv[])
  17. {
  18. int i;
  19. char *infile;
  20. FILE *fpin = stdin;
  21. FILE *fpout = stdout;
  22. short c;
  23. infile = "stdin";
  24. if (argc > 1)
  25. {
  26. for(i = 1; argv[i] && argv[i][0] == '-'; i++)
  27. {
  28. if (i < argc)
  29. {
  30. switch(argv[i][1])
  31. {
  32. case 'i':
  33. fpin = fopen(argv[i+1], "rb");
  34. infile = argv[i+1];
  35. if (fpin == NULL)
  36. {
  37. fprintf (stderr, "Error: cannot open in file '%s'\n", argv[i+1]);
  38. exit(-1);
  39. }
  40. i++;
  41. break;
  42. case 'o':
  43. fpout = fopen(argv[i+1], "wb");
  44. if (fpout == NULL)
  45. {
  46. fprintf (stderr, "Error: cannot open out file '%s'\n", argv[i+1]);
  47. exit(-1);
  48. }
  49. i++;
  50. break;
  51. default:
  52. fprintf (stderr, "Error: unknown argument: %s\n", argv[i]);
  53. exit(-1);
  54. }
  55. }
  56. }
  57. }
  58. fprintf(fpout, "/* Generated data file from file '%s' */\n\n\n", infile);
  59. fprintf(fpout, "unsigned char data[] = {\n");
  60. i = 0;
  61. while((c = fgetc(fpin)) >= 0)
  62. {
  63. if (i == 0)
  64. fprintf(fpout, "\t\t0x%02X", (unsigned char)c);
  65. else
  66. fprintf(fpout, ", 0x%02X", (unsigned char)c);
  67. i++;
  68. if (i > 10)
  69. {
  70. fprintf(fpout,", \\\n");
  71. i = 0;
  72. }
  73. }
  74. fprintf(fpout, "\n\t\t};\n");
  75. return 0;
  76. }