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.

142 lines
4.4KB

  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. #define HAVE_AV_CONFIG_H
  22. #include "avformat.h"
  23. #include "common.h"
  24. #include "cmdutils.h"
  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. pstrcpy(buf, sizeof(buf), po->name);
  38. if (po->flags & HAS_ARG) {
  39. pstrcat(buf, sizeof(buf), " ");
  40. pstrcat(buf, sizeof(buf), po->argname);
  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_FLOAT) {
  93. *po->u.float_arg = atof(arg);
  94. } else if (po->flags & OPT_FUNC2) {
  95. if(po->u.func2_arg(opt+1, arg)<0)
  96. goto unknown_opt;
  97. } else {
  98. po->u.func_arg(arg);
  99. }
  100. } else {
  101. parse_arg_file(opt);
  102. }
  103. }
  104. }
  105. void print_error(const char *filename, int err)
  106. {
  107. switch(err) {
  108. case AVERROR_NUMEXPECTED:
  109. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  110. "Use '%%d' to specify the image number:\n"
  111. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  112. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  113. filename);
  114. break;
  115. case AVERROR_INVALIDDATA:
  116. fprintf(stderr, "%s: Error while parsing header\n", filename);
  117. break;
  118. case AVERROR_NOFMT:
  119. fprintf(stderr, "%s: Unknown format\n", filename);
  120. break;
  121. case AVERROR_IO:
  122. fprintf(stderr, "%s: I/O error occured\n"
  123. "Usually that means that input file is truncated and/or corrupted.\n",
  124. filename);
  125. break;
  126. case AVERROR_NOMEM:
  127. fprintf(stderr, "%s: memory allocation error occured\n", filename);
  128. break;
  129. default:
  130. fprintf(stderr, "%s: Error while opening file\n", filename);
  131. break;
  132. }
  133. }