You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.6KB

  1. /*
  2. * Various utilities for command line tools
  3. * Copyright (c) 2000-2003 Fabrice Bellard
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "common.h"
  23. #include "avformat.h"
  24. #include "cmdutils.h"
  25. void show_help_options(const OptionDef *options)
  26. {
  27. const OptionDef *po;
  28. int i, expert, first;
  29. printf("Main options are:\n");
  30. for(i=0;i<2;i++) {
  31. first = 1;
  32. for(po = options; po->name != NULL; po++) {
  33. char buf[64];
  34. expert = (po->flags & OPT_EXPERT) != 0;
  35. if (expert == i) {
  36. if (expert && first) {
  37. printf("\nAdvanced options are:\n");
  38. first = 0;
  39. }
  40. strcpy(buf, po->name);
  41. if (po->flags & HAS_ARG) {
  42. strcat(buf, " ");
  43. strcat(buf, po->argname);
  44. }
  45. printf("-%-17s %s\n", buf, po->help);
  46. }
  47. }
  48. }
  49. }
  50. void parse_options(int argc, char **argv, const OptionDef *options)
  51. {
  52. const char *opt, *arg;
  53. int optindex;
  54. const OptionDef *po;
  55. /* parse options */
  56. optindex = 1;
  57. while (optindex < argc) {
  58. opt = argv[optindex++];
  59. if (opt[0] == '-' && opt[1] != '\0') {
  60. po = options;
  61. while (po->name != NULL) {
  62. if (!strcmp(opt + 1, po->name))
  63. break;
  64. po++;
  65. }
  66. if (!po->name) {
  67. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  68. exit(1);
  69. }
  70. arg = NULL;
  71. if (po->flags & HAS_ARG) {
  72. arg = argv[optindex++];
  73. if (!arg) {
  74. fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
  75. exit(1);
  76. }
  77. }
  78. if (po->flags & OPT_STRING) {
  79. char *str;
  80. str = strdup(arg);
  81. *po->u.str_arg = str;
  82. } else if (po->flags & OPT_BOOL) {
  83. *po->u.int_arg = 1;
  84. } else {
  85. po->u.func_arg(arg);
  86. }
  87. } else {
  88. parse_arg_file(opt);
  89. }
  90. }
  91. }
  92. void print_error(const char *filename, int err)
  93. {
  94. switch(err) {
  95. case AVERROR_NUMEXPECTED:
  96. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  97. "Use '%%d' to specify the image number:\n"
  98. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  99. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  100. filename);
  101. break;
  102. case AVERROR_INVALIDDATA:
  103. fprintf(stderr, "%s: Error while parsing header\n", filename);
  104. break;
  105. case AVERROR_NOFMT:
  106. fprintf(stderr, "%s: Unknown format\n", filename);
  107. break;
  108. default:
  109. fprintf(stderr, "%s: Error while opening file\n", filename);
  110. break;
  111. }
  112. }