main.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "stdlib.h"
  2. #include "stdarg.h"
  3. #include "arg.h"
  4. #include "methods.h"
  5. char *argv0;
  6. static char *opt_in = NULL;
  7. static char *opt_out = NULL;
  8. static char *opt_method = NULL;
  9. //extern struct methods t[METHODS];
  10. /* kills program while printing terminating message */
  11. void die(const char *errstr, ...) {
  12. va_list ap;
  13. va_start(ap, errstr);
  14. vfprintf(stderr, errstr, ap);
  15. va_end(ap);
  16. exit(1);
  17. }
  18. /* prints usage string before exiting */
  19. void usage(void) {
  20. die("usage: spngfx [-vl] [-f ifile] [-o ofile] [-m method]\n"
  21. " -f: reads from stdin if not specified\n"
  22. " -o: outputs to stdout if not specified\n"
  23. " -l: list methods\n"
  24. " -h: prints this usage\n"
  25. " -v: print version\n");
  26. }
  27. int main(int argc, char **argv) {
  28. int seq[METHODS];
  29. size_t seq_n = 0;
  30. /* arg parsing */
  31. ARGBEGIN {
  32. case 'f':
  33. opt_in = EARGF(usage());
  34. break;
  35. case 'o':
  36. opt_out = EARGF(usage());
  37. break;
  38. case 'm':
  39. opt_method = EARGF(usage());
  40. int found = 0;
  41. for (int i = 0; i < METHODS; i++) {
  42. if (strcmp(opt_method, "none") == 0)
  43. continue;
  44. if (strcmp(opt_method, t[i].name) == 0) {
  45. if (t[i].f == NULL)
  46. break;
  47. //(*t[i].f)(png);
  48. seq[seq_n++] = i;
  49. found = 1;
  50. }
  51. }
  52. if (!found)
  53. fprintf(stderr, "spngfx: method %s not implemented\n", opt_method);
  54. break;
  55. case 'l':
  56. printf("methods: ");
  57. for (int i = 0; i < METHODS - 1; i++)
  58. printf("%s ", t[i].name);
  59. printf("%s\n", t[METHODS - 1].name);
  60. return 0;
  61. case 'h':
  62. usage();
  63. case 'v':
  64. die("spngfx 2nd edition v2.31 (c) 2019-2022 strlst\n");
  65. } ARGEND;
  66. if (!seq_n)
  67. die("spngfx: must specify valid method to apply to image (-h for help)\n");
  68. if (!opt_in)
  69. opt_in = "/dev/stdin";
  70. if (!opt_out)
  71. //die("spngfx: must specify output file (-h for help)\n");
  72. opt_out = "/dev/stdout";
  73. if (!opt_method)
  74. opt_method = "none";
  75. spng *png = readpng(opt_in);
  76. closepng(png);
  77. if (strcmp(opt_out, "/dev/stdout") != 0)
  78. printpng_meta(png);
  79. //printpng_content(png);
  80. /* exec all seq */
  81. for (int i = 0; i < seq_n; i++)
  82. (*t[seq[i]].f)(png);
  83. writepng(png, opt_out);
  84. freepng(png);
  85. return 0;
  86. }