output-options.hh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright © 2011 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef OUTPUT_OPTIONS_HH
  27. #define OUTPUT_OPTIONS_HH
  28. #include "options.hh"
  29. template <bool default_stdout = true>
  30. struct output_options_t
  31. {
  32. ~output_options_t ()
  33. {
  34. g_free (output_file);
  35. g_free (output_format);
  36. if (out_fp && out_fp != stdout)
  37. fclose (out_fp);
  38. }
  39. void add_options (option_parser_t *parser,
  40. const char **supported_formats = nullptr)
  41. {
  42. const char *text = nullptr;
  43. if (supported_formats)
  44. {
  45. char *items = g_strjoinv ("/", const_cast<char **> (supported_formats));
  46. text = g_strdup_printf ("Set output format\n\n Supported output formats are: %s", items);
  47. g_free (items);
  48. parser->free_later ((char *) text);
  49. }
  50. GOptionEntry entries[] =
  51. {
  52. {"output-file", 'o', 0, G_OPTION_ARG_STRING, &this->output_file, "Set output file-name (default: stdout)","filename"},
  53. {"output-format", 'O', supported_formats ? 0 : G_OPTION_FLAG_HIDDEN,
  54. G_OPTION_ARG_STRING, &this->output_format, text, "format"},
  55. {nullptr}
  56. };
  57. parser->add_group (entries,
  58. "output",
  59. "Output destination & format options:",
  60. "Options for the destination & form of the output",
  61. this);
  62. }
  63. void post_parse (GError **error)
  64. {
  65. if (output_format)
  66. explicit_output_format = true;
  67. if (output_file && !output_format)
  68. {
  69. output_format = strrchr (output_file, '.');
  70. if (output_format)
  71. {
  72. output_format++; /* skip the dot */
  73. output_format = g_strdup (output_format);
  74. }
  75. }
  76. if (output_file && 0 != strcmp (output_file, "-"))
  77. out_fp = fopen (output_file, "wb");
  78. else
  79. {
  80. if (!default_stdout && !output_file)
  81. {
  82. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
  83. "No output file was specified");
  84. return;
  85. }
  86. #if defined(_WIN32) || defined(__CYGWIN__)
  87. setmode (fileno (stdout), O_BINARY);
  88. #endif
  89. out_fp = stdout;
  90. }
  91. if (!out_fp)
  92. {
  93. g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
  94. "Cannot open output file `%s': %s",
  95. g_filename_display_name (output_file), strerror (errno));
  96. return;
  97. }
  98. }
  99. char *output_file = nullptr;
  100. char *output_format = nullptr;
  101. bool explicit_output_format = false;
  102. FILE *out_fp = nullptr;
  103. };
  104. #endif