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.

147 lines
4.6KB

  1. /*
  2. * Various utilities for command line tools
  3. * Copyright (c) 2000-2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "cmdutils.h"
  23. #undef exit
  24. void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
  25. {
  26. const OptionDef *po;
  27. int first;
  28. first = 1;
  29. for(po = options; po->name != NULL; po++) {
  30. char buf[64];
  31. if ((po->flags & mask) == value) {
  32. if (first) {
  33. printf("%s", msg);
  34. first = 0;
  35. }
  36. pstrcpy(buf, sizeof(buf), po->name);
  37. if (po->flags & HAS_ARG) {
  38. pstrcat(buf, sizeof(buf), " ");
  39. pstrcat(buf, sizeof(buf), po->argname);
  40. }
  41. printf("-%-17s %s\n", buf, po->help);
  42. }
  43. }
  44. }
  45. static const OptionDef* find_option(const OptionDef *po, const char *name){
  46. while (po->name != NULL) {
  47. if (!strcmp(name, po->name))
  48. break;
  49. po++;
  50. }
  51. return po;
  52. }
  53. void parse_options(int argc, char **argv, const OptionDef *options)
  54. {
  55. const char *opt, *arg;
  56. int optindex, handleoptions=1;
  57. const OptionDef *po;
  58. /* parse options */
  59. optindex = 1;
  60. while (optindex < argc) {
  61. opt = argv[optindex++];
  62. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  63. if (opt[1] == '-' && opt[2] == '\0') {
  64. handleoptions = 0;
  65. continue;
  66. }
  67. po= find_option(options, opt + 1);
  68. if (!po->name)
  69. po= find_option(options, "default");
  70. if (!po->name) {
  71. unknown_opt:
  72. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  73. exit(1);
  74. }
  75. arg = NULL;
  76. if (po->flags & HAS_ARG) {
  77. arg = argv[optindex++];
  78. if (!arg) {
  79. fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
  80. exit(1);
  81. }
  82. }
  83. if (po->flags & OPT_STRING) {
  84. char *str;
  85. str = av_strdup(arg);
  86. *po->u.str_arg = str;
  87. } else if (po->flags & OPT_BOOL) {
  88. *po->u.int_arg = 1;
  89. } else if (po->flags & OPT_INT) {
  90. *po->u.int_arg = atoi(arg);
  91. } else if (po->flags & OPT_INT64) {
  92. *po->u.int64_arg = strtoll(arg, (char **)NULL, 10);
  93. } else if (po->flags & OPT_FLOAT) {
  94. *po->u.float_arg = atof(arg);
  95. } else if (po->flags & OPT_FUNC2) {
  96. if(po->u.func2_arg(opt+1, arg)<0)
  97. goto unknown_opt;
  98. } else {
  99. po->u.func_arg(arg);
  100. }
  101. } else {
  102. parse_arg_file(opt);
  103. }
  104. }
  105. }
  106. void print_error(const char *filename, int err)
  107. {
  108. switch(err) {
  109. case AVERROR_NUMEXPECTED:
  110. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  111. "Use '%%d' to specify the image number:\n"
  112. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  113. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  114. filename);
  115. break;
  116. case AVERROR_INVALIDDATA:
  117. fprintf(stderr, "%s: Error while parsing header\n", filename);
  118. break;
  119. case AVERROR_NOFMT:
  120. fprintf(stderr, "%s: Unknown format\n", filename);
  121. break;
  122. case AVERROR_IO:
  123. fprintf(stderr, "%s: I/O error occured\n"
  124. "Usually that means that input file is truncated and/or corrupted.\n",
  125. filename);
  126. break;
  127. case AVERROR_NOMEM:
  128. fprintf(stderr, "%s: memory allocation error occured\n", filename);
  129. break;
  130. case AVERROR_NOENT:
  131. fprintf(stderr, "%s: no such file or directory\n", filename);
  132. break;
  133. default:
  134. fprintf(stderr, "%s: Error while opening file\n", filename);
  135. break;
  136. }
  137. }