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.

118 lines
3.6KB

  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. void parse_options(int argc, char **argv, const OptionDef *options)
  45. {
  46. const char *opt, *arg;
  47. int optindex;
  48. const OptionDef *po;
  49. /* parse options */
  50. optindex = 1;
  51. while (optindex < argc) {
  52. opt = argv[optindex++];
  53. if (opt[0] == '-' && opt[1] != '\0') {
  54. po = options;
  55. while (po->name != NULL) {
  56. if (!strcmp(opt + 1, po->name))
  57. break;
  58. po++;
  59. }
  60. if (!po->name) {
  61. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  62. exit(1);
  63. }
  64. arg = NULL;
  65. if (po->flags & HAS_ARG) {
  66. arg = argv[optindex++];
  67. if (!arg) {
  68. fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
  69. exit(1);
  70. }
  71. }
  72. if (po->flags & OPT_STRING) {
  73. char *str;
  74. str = av_strdup(arg);
  75. *po->u.str_arg = str;
  76. } else if (po->flags & OPT_BOOL) {
  77. *po->u.int_arg = 1;
  78. } else if (po->flags & OPT_INT) {
  79. *po->u.int_arg = atoi(arg);
  80. } else if (po->flags & OPT_FLOAT) {
  81. *po->u.float_arg = atof(arg);
  82. } else {
  83. po->u.func_arg(arg);
  84. }
  85. } else {
  86. parse_arg_file(opt);
  87. }
  88. }
  89. }
  90. void print_error(const char *filename, int err)
  91. {
  92. switch(err) {
  93. case AVERROR_NUMEXPECTED:
  94. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  95. "Use '%%d' to specify the image number:\n"
  96. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  97. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  98. filename);
  99. break;
  100. case AVERROR_INVALIDDATA:
  101. fprintf(stderr, "%s: Error while parsing header\n", filename);
  102. break;
  103. case AVERROR_NOFMT:
  104. fprintf(stderr, "%s: Unknown format\n", filename);
  105. break;
  106. default:
  107. fprintf(stderr, "%s: Error while opening file\n", filename);
  108. break;
  109. }
  110. }