cmdutils.c 4.2 KB

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