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.

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