cmdutils.c 4.5 KB

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