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.

154 lines
4.8KB

  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 <string.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include "avformat.h"
  25. #include "cmdutils.h"
  26. #include "avstring.h"
  27. #undef exit
  28. void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
  29. {
  30. const OptionDef *po;
  31. int first;
  32. first = 1;
  33. for(po = options; po->name != NULL; po++) {
  34. char buf[64];
  35. if ((po->flags & mask) == value) {
  36. if (first) {
  37. printf("%s", msg);
  38. first = 0;
  39. }
  40. av_strlcpy(buf, po->name, sizeof(buf));
  41. if (po->flags & HAS_ARG) {
  42. av_strlcat(buf, " ", sizeof(buf));
  43. av_strlcat(buf, po->argname, sizeof(buf));
  44. }
  45. printf("-%-17s %s\n", buf, po->help);
  46. }
  47. }
  48. }
  49. static const OptionDef* find_option(const OptionDef *po, const char *name){
  50. while (po->name != NULL) {
  51. if (!strcmp(name, po->name))
  52. break;
  53. po++;
  54. }
  55. return po;
  56. }
  57. void parse_options(int argc, char **argv, const OptionDef *options,
  58. void (* parse_arg_function)(const char*))
  59. {
  60. const char *opt, *arg;
  61. int optindex, handleoptions=1;
  62. const OptionDef *po;
  63. /* parse options */
  64. optindex = 1;
  65. while (optindex < argc) {
  66. opt = argv[optindex++];
  67. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  68. if (opt[1] == '-' && opt[2] == '\0') {
  69. handleoptions = 0;
  70. continue;
  71. }
  72. po= find_option(options, opt + 1);
  73. if (!po->name)
  74. po= find_option(options, "default");
  75. if (!po->name) {
  76. unknown_opt:
  77. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  78. exit(1);
  79. }
  80. arg = NULL;
  81. if (po->flags & HAS_ARG) {
  82. arg = argv[optindex++];
  83. if (!arg) {
  84. fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
  85. exit(1);
  86. }
  87. }
  88. if (po->flags & OPT_STRING) {
  89. char *str;
  90. str = av_strdup(arg);
  91. *po->u.str_arg = str;
  92. } else if (po->flags & OPT_BOOL) {
  93. *po->u.int_arg = 1;
  94. } else if (po->flags & OPT_INT) {
  95. *po->u.int_arg = atoi(arg);
  96. } else if (po->flags & OPT_INT64) {
  97. *po->u.int64_arg = strtoll(arg, (char **)NULL, 10);
  98. } else if (po->flags & OPT_FLOAT) {
  99. *po->u.float_arg = atof(arg);
  100. } else if (po->flags & OPT_FUNC2) {
  101. if(po->u.func2_arg(opt+1, arg)<0)
  102. goto unknown_opt;
  103. } else {
  104. po->u.func_arg(arg);
  105. }
  106. } else {
  107. if (parse_arg_function)
  108. parse_arg_function(opt);
  109. }
  110. }
  111. }
  112. void print_error(const char *filename, int err)
  113. {
  114. switch(err) {
  115. case AVERROR_NUMEXPECTED:
  116. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  117. "Use '%%d' to specify the image number:\n"
  118. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  119. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  120. filename);
  121. break;
  122. case AVERROR_INVALIDDATA:
  123. fprintf(stderr, "%s: Error while parsing header\n", filename);
  124. break;
  125. case AVERROR_NOFMT:
  126. fprintf(stderr, "%s: Unknown format\n", filename);
  127. break;
  128. case AVERROR(EIO):
  129. fprintf(stderr, "%s: I/O error occured\n"
  130. "Usually that means that input file is truncated and/or corrupted.\n",
  131. filename);
  132. break;
  133. case AVERROR(ENOMEM):
  134. fprintf(stderr, "%s: memory allocation error occured\n", filename);
  135. break;
  136. case AVERROR(ENOENT):
  137. fprintf(stderr, "%s: no such file or directory\n", filename);
  138. break;
  139. default:
  140. fprintf(stderr, "%s: Error while opening file\n", filename);
  141. break;
  142. }
  143. }